Solar Array and home energy dashboard.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

123 lines
2.8 KiB

  1. // Interface for testing, provides testing data, and dynamic updates, etc.
  2. // WebSocket emulation class for testing
  3. function WebSocketTest(actions) {
  4. this.actions = actions.slice(); // copy
  5. this.actions.reverse()
  6. this.processNextItem()
  7. }
  8. Array.prototype.bisect = function (val, lo, hi) {
  9. var mid;
  10. if (lo == null)
  11. lo = 0;
  12. if (hi == null)
  13. hi = this.length;
  14. while (lo < hi) {
  15. mid = Math.floor((lo + hi) / 2);
  16. if (this[mid] < val)
  17. lo = mid + 1;
  18. else
  19. hi = mid;
  20. }
  21. return lo;
  22. };
  23. // Reduce array ar down to cnt number of points if necesssary
  24. function reducepoints(ar, cnt) {
  25. // If we're already less than cnt, don't do anything.
  26. if (ar.length < cnt)
  27. return ar;
  28. // Just skip points for now
  29. var ret = [];
  30. var x = 0;
  31. for (idx = 0; idx < cnt; idx++) {
  32. ret[idx] = ar[Math.floor(x / cnt)];
  33. x += ar.length;
  34. }
  35. // always include the last point
  36. ret[idx] = ar[ar.length - 1];
  37. return ret;
  38. }
  39. WebSocketTest.prototype.send = function (s) {
  40. console.log("ws send: " + s);
  41. var msg = s.split(" ");
  42. if (msg[0] == 'win') {
  43. if (msg[1] == 'NaN')
  44. msg[1] = -Infinity;
  45. if (msg[2] == 'NaN')
  46. msg[2] = Infinity;
  47. var loidx, hiidx;
  48. // production
  49. loidx = fakedata.production.bisect([msg[1], 0]);
  50. if (loidx > 0)
  51. loidx -= 1;
  52. hiidx = fakedata.production.bisect([msg[2], 0]) + 1;
  53. var prodar = fakedata.production.slice(loidx, hiidx);
  54. prodar = reducepoints(prodar, 800);
  55. // consumption
  56. loidx = fakedata.consumption.bisect([msg[1], 0]);
  57. if (loidx > 0)
  58. loidx -= 1;
  59. hiidx = fakedata.consumption.bisect([msg[2], 0]) + 1;
  60. var consar = fakedata.consumption.slice(loidx, hiidx);
  61. consar = reducepoints(consar, 800);
  62. var data = {
  63. "production": prodar,
  64. "consumption": consar,
  65. "grid": prodar,
  66. }
  67. this.makercv('windata ' + JSON.stringify(data));
  68. }
  69. }
  70. // WebSocketTest.prototype.close = function ()
  71. // Internal
  72. WebSocketTest.prototype.processNextItem = function () {
  73. var item = this.actions.pop()
  74. if (item === undefined) {
  75. console.log("WebSocketTest finished running");
  76. return;
  77. }
  78. var wst = this;
  79. setTimeout(function() {
  80. item[1](wst);
  81. wst.processNextItem();
  82. }, item[0]);
  83. return this;
  84. }
  85. WebSocketTest.prototype.makercv = function (m) {
  86. this.onmessage(new MessageEvent('websockettest', { data: m }));
  87. }
  88. function getoverviewdata() {
  89. return {
  90. grid: fakedata.prodindex,
  91. consumption: fakedata.consindex,
  92. production: fakedata.prodindex,
  93. }
  94. }
  95. // Setup the socket that will be used
  96. var socket = new WebSocketTest([
  97. [ 10, function(a) { a.onopen(new Object()) } ],
  98. [ 10, function(a) { a.makercv('o ' + JSON.stringify(getoverviewdata())) } ],
  99. [ 10, function(a) { a.makercv('p .123') } ],
  100. [ 10, function(a) { a.makercv('c .302') } ],
  101. [ 10, function(a) { a.makercv('ng .758') } ],
  102. [ 2000, function(a) { a.makercv('p .234') } ],
  103. [ 10, function(a) { a.makercv('ng -.584') } ],
  104. ]);