sensortoy/app/js/standards/capability.js

274 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-05-20 16:10:40 +00:00
/**
*
* User: Martin Donnelly
* Date: 2016-05-20
* Time: 10:32
*
*/
'use strict';
2016-06-01 15:03:59 +00:00
var capabilityManager = {
capabilityList : [],
register : function(details) {
console.log('Registered:', details );
this.capabilityList.push(details);
},
discover : function(id) {
var wanted = this.capabilityList.filter(function(obj) {
return obj.id === id;
});
return wanted.length === 1 ? wanted[0].module : null;
}
};
2016-05-20 16:10:40 +00:00
var CAPABILITY = function(p) {
this.name = '-CAPABILITY-';
this.capabilityID = p.capabilityID || null;
this.state = '';
this.serviceDef = p.service || null;
this.internalID = null;
this.frameID = null;
this.$id = null;
this.deviceID = null;
2016-05-27 08:25:10 +00:00
this.data = [];
this.ctx = null;
this.first = false;
2016-06-01 15:03:59 +00:00
this.target = null;
this.$frame = null;
capabilityManager.register({id:this.capabilityID, module:this});
};
CAPABILITY.prototype.setFrame = function() {
this.$frame = (this.target !== null) ? $('#'+ this.target) : null;
2016-05-20 16:10:40 +00:00
};
CAPABILITY.prototype.setInternalID = function() {
this.internalID = (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1).toString(36);
this.frameID = 'f-' + this.internalID;
};
CAPABILITY.prototype.setDeviceID = function(dID) {
this.deviceID = dID;
};
CAPABILITY.prototype.getDeviceID = function() {
return this.deviceID ;
};
CAPABILITY.prototype.insertFrame = function() {
this.setInternalID();
console.log('FrameID: ' , this.frameID);
var title = [this.name, ' - ', this.deviceID].join(' ');
var frame = $('<div />', {
class: 'mui-panel',
id: this.frameID});
$('<div />', { class: 'mui-row'}).append($('<div />', { class: 'mui-col-xs-12 mui--text-title', text: title})).appendTo(frame);
2016-06-01 15:03:59 +00:00
this.$frame.append(frame);
2016-05-20 16:10:40 +00:00
this.$id = $('#' + this.frameID);
};
CAPABILITY.prototype.inherits = function(a, b) {
var c = function() {};
c.prototype = b.prototype;
a.superClass_ = b.prototype;
a.prototype = new c;
a.prototype.constructor = a;
};
CAPABILITY.prototype.onError = function(e) {
console.error(e);
};
2016-05-27 08:25:10 +00:00
CAPABILITY.prototype.storeData = function(data, alt) {
if (!this.first) {
this.first = true;
return [];
}
var target = alt || this.data;
if (target.length === 99) {
target = target.slice(1);
}
target.push(data);
if (alt) {
return target;
} else {
this.data = target;
}
};
CAPABILITY.prototype.startGraph = function(id) {
var c;
c = id[0].getContext('2d');
this.ctx = c;
this.ctx.fillStyle = '#ffffff';
this.ctx.fillRect(0,0,300,150);
};
CAPABILITY.prototype.generateBlankGraph = function(subID) {
var _subID = subID || '';
var xmlns = 'http://www.w3.org/2000/svg';
var svgID = this.frameID + _subID + '-svg';
var text1ID = this.frameID + _subID + '-txt1';
var lineID = this.frameID + _subID + '-line';
var svg = document.createElementNS(xmlns,'svg');
svg.setAttributeNS(xmlns,'id',svgID);
svg.setAttributeNS(xmlns,'width',300);
svg.setAttributeNS(xmlns,'height',150);
svg.setAttributeNS(xmlns,'fill', 'blue');
var line = document.createElementNS(xmlns,'line');
line.setAttributeNS(null,'x1','46');
line.setAttributeNS(null,'y1','12');
line.setAttributeNS(null,'x2','280');
line.setAttributeNS(null,'y2', '12');
2016-06-01 15:03:59 +00:00
line.setAttributeNS(null,'style','stroke:#004c6d;stroke-width:2;');
2016-05-27 08:25:10 +00:00
svg.appendChild(line);
line = document.createElementNS(xmlns,'line');
line.setAttributeNS(null,'x1','46');
line.setAttributeNS(null,'y1','136');
line.setAttributeNS(null,'x2','280');
line.setAttributeNS(null,'y2', '136');
2016-06-01 15:03:59 +00:00
line.setAttributeNS(null,'style','stroke:#004c6d;stroke-width:2;');
2016-05-27 08:25:10 +00:00
svg.appendChild(line);
var text = document.createElementNS(xmlns,'text');
text.setAttributeNS(null,'id',text1ID);
text.setAttributeNS(null,'x','36');
text.setAttributeNS(null,'y','15');
text.setAttributeNS(null,'text-anchor', 'end');
2016-06-01 15:03:59 +00:00
text.setAttributeNS(null,'style','font-family:"Ubuntu Condensed",sans-serif;font-size:12;fill: #004c6d;text-align:right;');
2016-05-27 08:25:10 +00:00
text.textContent = '000';
svg.appendChild(text);
text = document.createElementNS(xmlns,'text');
text.setAttributeNS(null,'id','text2');
text.setAttributeNS(null,'x','36');
text.setAttributeNS(null,'y','140');
text.setAttributeNS(null,'text-anchor', 'end');
2016-06-01 15:03:59 +00:00
text.setAttributeNS(null,'style','font-family:"Ubuntu Condensed",sans-serif;font-size:12;fill: #004c6d;text-align:right;');
2016-05-27 08:25:10 +00:00
text.textContent = '0';
svg.appendChild(text);
var polyline = document.createElementNS(xmlns,'polyline');
2016-05-20 16:10:40 +00:00
2016-05-27 08:25:10 +00:00
polyline.setAttributeNS(null,'id',lineID);
polyline.setAttributeNS(null,'fill','none');
2016-06-01 15:03:59 +00:00
polyline.setAttributeNS(null,'stroke','#2196F3');
2016-05-27 08:25:10 +00:00
//#e5f7fd
// old #B5C7FF
polyline.setAttributeNS(null,'text-anchor', 'end');
polyline.setAttributeNS(null,'stroke-width','2');
svg.appendChild(polyline);
return svg;
};
CAPABILITY.prototype.animateGraph = function() {
//This.simpleGraph(this.data);
};
CAPABILITY.prototype.simpleGraph = function(data, subID) {
var _subID;
var _data;
var text1ID;
var lineID;
_data = data || this.data;
_subID = subID || '';
lineID = [this.frameID , _subID , '-line'].join('');
text1ID = [this.frameID , _subID , '-txt1'].join('');
if (_data.length > 0) {
var ceiling = _data.reduce(function(p, v) {
return (p > v ? p : v);
});
/* Var floor = _data.reduce(function(p, v) {
return (p < v ? p : v);
});
*/
var calcArray = [];
var ceilingLimit = Math.floor(ceiling / 10) * 10;
if (ceilingLimit < ceiling) {
ceilingLimit = Math.floor((ceiling + (ceiling * 0.25)) / 10) * 10;
}
if (ceilingLimit < 30) {
ceilingLimit = 30;
}
var scale = 124 / ceilingLimit;
// Var xstep = (280 - 46) / 100;
var xstep = 2.34;
var startX = 46 + (100 - _data.length) * xstep;
for (var x = 0;x < _data.length;x++) {
calcArray.push((startX + (x * xstep)).toFixed(2) + ',' + (136 - ((_data[x]) * scale)).toFixed(2));
}
var elm = document.getElementById(lineID);
elm.setAttribute('points',calcArray.join(' '));
elm = document.getElementById(text1ID);
elm.textContent = ceilingLimit;
}
};
2016-05-20 16:10:40 +00:00
var inheritsFrom = function(a, b) {
var c = function() {};
c.prototype = b.prototype;
a.superClass_ = b.prototype;
a.prototype = new c;
a.prototype.constructor = a;
a.prototype.superClass_ = b.prototype;
};