// 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; loidx = fakedata.production.bisect([msg[1], 0]); if (loidx > 0) loidx -= 1; hiidx = fakedata.production.bisect([msg[2], 0]) + 1; var subar = fakedata.production.slice(loidx, hiidx); subar = reducepoints(subar, 800); var data = { "production": subar, "consumption": subar, "grid": subar, } 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 })); } fakeData = [ [ 1578544199000, 0.3934 ], [ 1578544209000, 0.4934 ], [ 1578544219000, 0.5234 ], [ 1578544229000, 0.4734 ], [ 1578544239000, 0.4334 ], [ 1578544249000, 0.2334 ], [ 1578544259000, 0.1034 ], [ 1578544269000, -0.4334 ], [ 1578544279000, -0.6334 ], [ 1578544289000, -0.2334 ], ] fakeConData = [ [ 1578544199000, 0.3934 ], [ 1578544209000, 0.4934 ], [ 1578544219000, 0.5234 ], [ 1578544229000, 0.4734 ], [ 1578544239000, 0.4334 ], [ 1578544249000, 0.2334 ], [ 1578544259000, 0.1034 ], [ 1578544269000, 0.4334 ], [ 1578544279000, 0.6334 ], [ 1578544289000, 0.2334 ], ] fakeOverviewData = { grid: fakeData, consumption: fakeConData, production: fakeConData, } function getoverviewdata() { return { grid: fakedata.index, consumption: fakedata.index, production: fakedata.index, } } // 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') } ], ]);