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.
 
 
 
 
 

144 lines
3.2 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. loidx = fakedata.production.bisect([msg[1], 0]);
  49. if (loidx > 0)
  50. loidx -= 1;
  51. hiidx = fakedata.production.bisect([msg[2], 0]) + 1;
  52. var subar = fakedata.production.slice(loidx, hiidx);
  53. subar = reducepoints(subar, 800);
  54. var data = {
  55. "production": subar,
  56. "consumption": subar,
  57. "grid": subar,
  58. }
  59. this.makercv('windata ' + JSON.stringify(data));
  60. }
  61. }
  62. // WebSocketTest.prototype.close = function ()
  63. // Internal
  64. WebSocketTest.prototype.processNextItem = function () {
  65. var item = this.actions.pop()
  66. if (item === undefined) {
  67. console.log("WebSocketTest finished running");
  68. return;
  69. }
  70. var wst = this;
  71. setTimeout(function() {
  72. item[1](wst);
  73. wst.processNextItem();
  74. }, item[0]);
  75. return this;
  76. }
  77. WebSocketTest.prototype.makercv = function (m) {
  78. this.onmessage(new MessageEvent('websockettest', { data: m }));
  79. }
  80. fakeData = [
  81. [ 1578544199000, 0.3934 ],
  82. [ 1578544209000, 0.4934 ],
  83. [ 1578544219000, 0.5234 ],
  84. [ 1578544229000, 0.4734 ],
  85. [ 1578544239000, 0.4334 ],
  86. [ 1578544249000, 0.2334 ],
  87. [ 1578544259000, 0.1034 ],
  88. [ 1578544269000, -0.4334 ],
  89. [ 1578544279000, -0.6334 ],
  90. [ 1578544289000, -0.2334 ],
  91. ]
  92. fakeConData = [
  93. [ 1578544199000, 0.3934 ],
  94. [ 1578544209000, 0.4934 ],
  95. [ 1578544219000, 0.5234 ],
  96. [ 1578544229000, 0.4734 ],
  97. [ 1578544239000, 0.4334 ],
  98. [ 1578544249000, 0.2334 ],
  99. [ 1578544259000, 0.1034 ],
  100. [ 1578544269000, 0.4334 ],
  101. [ 1578544279000, 0.6334 ],
  102. [ 1578544289000, 0.2334 ],
  103. ]
  104. fakeOverviewData = {
  105. grid: fakeData,
  106. consumption: fakeConData,
  107. production: fakeConData,
  108. }
  109. function getoverviewdata() {
  110. return {
  111. grid: fakedata.index,
  112. consumption: fakedata.index,
  113. production: fakedata.index,
  114. }
  115. }
  116. // Setup the socket that will be used
  117. var socket = new WebSocketTest([
  118. [ 10, function(a) { a.onopen(new Object()) } ],
  119. [ 10, function(a) { a.makercv('o ' + JSON.stringify(getoverviewdata())) } ],
  120. [ 10, function(a) { a.makercv('p .123') } ],
  121. [ 10, function(a) { a.makercv('c .302') } ],
  122. [ 10, function(a) { a.makercv('ng .758') } ],
  123. [ 2000, function(a) { a.makercv('p .234') } ],
  124. [ 10, function(a) { a.makercv('ng -.584') } ],
  125. ]);