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