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.
 
 
 
 
 

161 lines
3.1 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.get('consumptionNav').setData(data.consumption, false);
  51. solarchart.get('gridNav').setData(data.grid, false);
  52. solarchart.get('productionNav').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.get('consumption').setData(data.consumption, false);
  59. solarchart.get('grid').setData(data.grid, false);
  60. solarchart.get('production').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. id: 'consumptionNav',
  83. showInNavigator: true,
  84. data: [],
  85. },
  86. {
  87. type: 'bar',
  88. name: 'gridNav',
  89. id: 'gridNav',
  90. showInNavigator: true,
  91. data: [],
  92. },
  93. {
  94. type: 'bar',
  95. name: 'productionNav',
  96. id: 'productionNav',
  97. showInNavigator: true,
  98. data: [],
  99. },
  100. ]
  101. },
  102. xAxis: {
  103. events: {
  104. afterSetExtremes: afterSetExtremes,
  105. },
  106. type: "datetime",
  107. title: {
  108. enabled: false,
  109. },
  110. ordinal: false,
  111. },
  112. yAxis: {
  113. title: {
  114. text: 'W',
  115. }
  116. },
  117. tooltip: {
  118. split: true,
  119. valueSuffix: ' W',
  120. },
  121. plotOptions: {
  122. area: {
  123. //stacking: 'normal',
  124. lineColor: '#666666',
  125. fillOpacity: .25,
  126. lineWidth: 1,
  127. marker: {
  128. lineWidth: 1,
  129. lineColor: '#666666'
  130. }
  131. }
  132. },
  133. series: [
  134. {
  135. name: 'consumption',
  136. id: 'consumption',
  137. type: 'line',
  138. data: [ ],
  139. },
  140. {
  141. name: 'grid',
  142. id: 'grid',
  143. type: 'line',
  144. data: [ ],
  145. },
  146. {
  147. name: 'production',
  148. id: 'production',
  149. type: 'line',
  150. data: [ ],
  151. },
  152. ]
  153. });