/** * * User: Martin Donnelly * Date: 2016-05-20 * Time: 10:32 * */ 'use strict'; 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; this.data = []; this.ctx = null; this.first = false; }; 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 = $('
', { class: 'mui-panel', id: this.frameID}); $('', { class: 'mui-row'}).append($('', { class: 'mui-col-xs-12 mui--text-title', text: title})).appendTo(frame); $('#frames').append(frame); 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); }; 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'); line.setAttributeNS(null,'style','stroke:#bad649;stroke-width:2;'); 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'); line.setAttributeNS(null,'style','stroke:#bad649;stroke-width:2;'); 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'); text.setAttributeNS(null,'style','font-family:"Ubuntu Condensed",sans-serif;font-size:12;fill: #bad649;text-align:right;'); 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'); text.setAttributeNS(null,'style','font-family:"Ubuntu Condensed",sans-serif;font-size:12;fill: #bad649;text-align:right;'); text.textContent = '0'; svg.appendChild(text); var polyline = document.createElementNS(xmlns,'polyline'); polyline.setAttributeNS(null,'id',lineID); polyline.setAttributeNS(null,'fill','none'); polyline.setAttributeNS(null,'stroke','#e5f7fd'); //#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; } }; 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; };