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.
 
 
 
 
 

121 lines
2.7 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. WebSocketTest.prototype.send = function (s) {
  24. console.log("ws send: " + s);
  25. var msg = s.split(" ");
  26. if (msg[0] == 'win') {
  27. if (msg[1] == 'NaN')
  28. msg[1] = -Infinity;
  29. if (msg[2] == 'NaN')
  30. msg[2] = Infinity;
  31. var loidx, hiidx;
  32. loidx = fakedata.production.bisect([msg[1], 0]);
  33. hiidx = fakedata.production.bisect([msg[2], 0]);
  34. var subar = fakedata.production.slice(loidx, hiidx + 1);
  35. var data = {
  36. "production": subar,
  37. "consumption": subar,
  38. "grid": subar,
  39. }
  40. this.makercv('windata ' + JSON.stringify(data));
  41. }
  42. }
  43. // WebSocketTest.prototype.close = function ()
  44. // Internal
  45. WebSocketTest.prototype.processNextItem = function () {
  46. var item = this.actions.pop()
  47. if (item === undefined) {
  48. console.log("WebSocketTest finished running");
  49. return;
  50. }
  51. var wst = this;
  52. setTimeout(function() {
  53. item[1](wst);
  54. wst.processNextItem();
  55. }, item[0]);
  56. return this;
  57. }
  58. WebSocketTest.prototype.makercv = function (m) {
  59. this.onmessage(new MessageEvent('websockettest', { data: m }));
  60. }
  61. fakeData = [
  62. [ 1578544199000, 0.3934 ],
  63. [ 1578544209000, 0.4934 ],
  64. [ 1578544219000, 0.5234 ],
  65. [ 1578544229000, 0.4734 ],
  66. [ 1578544239000, 0.4334 ],
  67. [ 1578544249000, 0.2334 ],
  68. [ 1578544259000, 0.1034 ],
  69. [ 1578544269000, -0.4334 ],
  70. [ 1578544279000, -0.6334 ],
  71. [ 1578544289000, -0.2334 ],
  72. ]
  73. fakeConData = [
  74. [ 1578544199000, 0.3934 ],
  75. [ 1578544209000, 0.4934 ],
  76. [ 1578544219000, 0.5234 ],
  77. [ 1578544229000, 0.4734 ],
  78. [ 1578544239000, 0.4334 ],
  79. [ 1578544249000, 0.2334 ],
  80. [ 1578544259000, 0.1034 ],
  81. [ 1578544269000, 0.4334 ],
  82. [ 1578544279000, 0.6334 ],
  83. [ 1578544289000, 0.2334 ],
  84. ]
  85. fakeOverviewData = {
  86. grid: fakeData,
  87. consumption: fakeConData,
  88. production: fakeConData,
  89. }
  90. function getoverviewdata() {
  91. return {
  92. grid: fakedata.index,
  93. consumption: fakedata.index,
  94. production: fakedata.index,
  95. }
  96. }
  97. // Setup the socket that will be used
  98. var socket = new WebSocketTest([
  99. [ 10, function(a) { a.onopen(new Object()) } ],
  100. [ 10, function(a) { a.makercv('o ' + JSON.stringify(getoverviewdata())) } ],
  101. [ 10, function(a) { a.makercv('p .123') } ],
  102. [ 10, function(a) { a.makercv('c .302') } ],
  103. [ 10, function(a) { a.makercv('ng .758') } ],
  104. [ 2000, function(a) { a.makercv('p .234') } ],
  105. [ 10, function(a) { a.makercv('ng -.584') } ],
  106. ]);