// Interface for testing, provides testing data, and dynamic updates, etc. // WebSocket emulation class for testing function WebSocketTest(actions) { this.actions = actions.slice(); // copy this.actions.reverse() this.processNextItem() } Array.prototype.bisect = function (val, lo, hi) { var mid; if (lo == null) lo = 0; if (hi == null) hi = this.length; while (lo < hi) { mid = Math.floor((lo + hi) / 2); if (this[mid] < val) lo = mid + 1; else hi = mid; } return lo; }; // Reduce array ar down to cnt number of points if necesssary function reducepoints(ar, cnt) { // If we're already less than cnt, don't do anything. if (ar.length < cnt) return ar; // Just skip points for now var ret = []; var x = 0; for (idx = 0; idx < cnt; idx++) { ret[idx] = ar[Math.floor(x / cnt)]; x += ar.length; } // always include the last point ret[idx] = ar[ar.length - 1]; return ret; } WebSocketTest.prototype.send = function (s) { console.log("ws send: " + s); var msg = s.split(" "); if (msg[0] == 'win') { if (msg[1] == 'NaN') msg[1] = -Infinity; if (msg[2] == 'NaN') msg[2] = Infinity; var loidx, hiidx; // production loidx = fakedata.production.bisect([msg[1], 0]); if (loidx > 0) loidx -= 1; hiidx = fakedata.production.bisect([msg[2], 0]) + 1; var prodar = fakedata.production.slice(loidx, hiidx); prodar = reducepoints(prodar, 800); // consumption loidx = fakedata.consumption.bisect([msg[1], 0]); if (loidx > 0) loidx -= 1; hiidx = fakedata.consumption.bisect([msg[2], 0]) + 1; var consar = fakedata.consumption.slice(loidx, hiidx); consar = reducepoints(consar, 800); var data = { "production": prodar, "consumption": consar, "grid": prodar, } this.makercv('windata ' + JSON.stringify(data)); } } // WebSocketTest.prototype.close = function () // Internal WebSocketTest.prototype.processNextItem = function () { var item = this.actions.pop() if (item === undefined) { console.log("WebSocketTest finished running"); return; } var wst = this; setTimeout(function() { item[1](wst); wst.processNextItem(); }, item[0]); return this; } WebSocketTest.prototype.makercv = function (m) { this.onmessage(new MessageEvent('websockettest', { data: m })); } function getoverviewdata() { return { grid: fakedata.prodindex, consumption: fakedata.consindex, production: fakedata.prodindex, } } // Setup the socket that will be used var socket = new WebSocketTest([ [ 10, function(a) { a.onopen(new Object()) } ], [ 10, function(a) { a.makercv('o ' + JSON.stringify(getoverviewdata())) } ], [ 10, function(a) { a.makercv('p .123') } ], [ 10, function(a) { a.makercv('c .302') } ], [ 10, function(a) { a.makercv('ng .758') } ], [ 2000, function(a) { a.makercv('p .234') } ], [ 10, function(a) { a.makercv('ng -.584') } ], ]);