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.
 
 
 
 
 

174 lines
3.6 KiB

  1. Highcharts.setOptions({
  2. time: {
  3. timezone: 'America/Los_Angeles',
  4. },
  5. lang: {
  6. thousandsSep: ',',
  7. },
  8. });
  9. socket.onopen = function() {
  10. // connected
  11. }
  12. var msgNumbs = {
  13. p: "#production",
  14. ng: "#netgrid",
  15. c: "#consumption",
  16. };
  17. function netgridcolor(v) {
  18. if (v <= 0)
  19. return "#00ff00";
  20. else
  21. return "#ff0000";
  22. }
  23. function afterSetExtremes(e) {
  24. // hack to deal w/ HighStocks being stupid
  25. if (Date.now() - solarchart.sd_lastwindata < 500) {
  26. // Make sure we trigger again, and that the user doesn't
  27. // have to wait too long
  28. solarchart.sd_lastwindata = Date.now() - 500;
  29. return;
  30. }
  31. solarchart.showLoading('Loading data from server...');
  32. console.log(e)
  33. socket.send('win ' + Math.round(e.min).toString() + ' ' + Math.round(e.max).toString());
  34. }
  35. socket.onmessage = function(m) {
  36. var msg = m.data.split(" ");
  37. if (msg[0] in msgNumbs) {
  38. // simple update values
  39. var num = parseFloat(msg[1]);
  40. var targ = $(msgNumbs[msg[0]]);
  41. targ.text(num.toString());
  42. // update color for ng
  43. if (msg[0] == "ng") {
  44. $(".gridpower").css('background', netgridcolor(num));
  45. }
  46. } else if (msg[0] == 'o') {
  47. // received overview data
  48. var data = JSON.parse(msg[1]);
  49. console.log(solarchart);
  50. solarchart.navigator.series[consumIdx].setData(data.consumption, false);
  51. solarchart.navigator.series[gridIdx].setData(data.grid, false);
  52. solarchart.navigator.series[prodIdx].setData(data.production, false);
  53. solarchart.redraw();
  54. } else if (msg[0] == 'windata') {
  55. var data = JSON.parse(msg[1]);
  56. console.log(data);
  57. solarchart.sd_lastwindata = Date.now();
  58. solarchart.series[consumDataIdx].setData(data.consumption, false);
  59. solarchart.series[gridDataIdx].setData(data.grid, false);
  60. solarchart.series[prodDataIdx].setData(data.production, false);
  61. solarchart.redraw();
  62. solarchart.hideLoading()
  63. }
  64. }
  65. var solarchart = Highcharts.stockChart('solarchart', {
  66. chart: {
  67. zoomType: 'x'
  68. },
  69. title: {
  70. text: 'Panel Production'
  71. },
  72. subtitle: {
  73. text: document.ontouchstart === undefined ?
  74. 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
  75. },
  76. navigator: {
  77. adaptToUpdatedData: false, // XXX - keep?
  78. series: [
  79. {
  80. type: 'bar',
  81. name: 'consumptionNav',
  82. showInNavigator: true,
  83. data: [],
  84. },
  85. {
  86. type: 'bar',
  87. name: 'gridNav',
  88. showInNavigator: true,
  89. data: [],
  90. },
  91. {
  92. type: 'bar',
  93. name: 'productionNav',
  94. showInNavigator: true,
  95. data: [],
  96. },
  97. ]
  98. },
  99. xAxis: {
  100. events: {
  101. afterSetExtremes: afterSetExtremes,
  102. },
  103. type: "datetime",
  104. title: {
  105. enabled: false,
  106. },
  107. ordinal: false,
  108. },
  109. yAxis: {
  110. title: {
  111. text: 'W',
  112. }
  113. },
  114. tooltip: {
  115. split: true,
  116. valueSuffix: ' W',
  117. },
  118. plotOptions: {
  119. area: {
  120. //stacking: 'normal',
  121. lineColor: '#666666',
  122. fillOpacity: .25,
  123. lineWidth: 1,
  124. marker: {
  125. lineWidth: 1,
  126. lineColor: '#666666'
  127. }
  128. }
  129. },
  130. series: [
  131. {
  132. name: 'consumption',
  133. type: 'line',
  134. data: [ ],
  135. },
  136. {
  137. name: 'grid',
  138. type: 'line',
  139. data: [ ],
  140. },
  141. {
  142. name: 'production',
  143. type: 'line',
  144. data: [ ],
  145. },
  146. ]
  147. });
  148. // Indexes for chart series
  149. delete solarchart.navigator.series[0];
  150. for (var idx in solarchart.navigator.series) {
  151. if (solarchart.navigator.series[idx].name == 'consumptionNav')
  152. consumIdx = idx;
  153. else if (solarchart.navigator.series[idx].name == 'gridNav')
  154. gridIdx = idx;
  155. else if (solarchart.navigator.series[idx].name == 'productionNav')
  156. prodIdx = idx;
  157. }
  158. for (var idx in solarchart.series) {
  159. if (solarchart.series[idx].name == 'consumption')
  160. consumDataIdx = idx;
  161. else if (solarchart.series[idx].name == 'grid')
  162. gridDataIdx = idx;
  163. else if (solarchart.series[idx].name == 'production')
  164. prodDataIdx = idx;
  165. }