Advertisement
seanw1

Enphase Enlighten Banner PHP Script

Apr 25th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.67 KB | None | 0 0
  1. <?php
  2. $key = ""; // enter API key here
  3. $publicurl = "https://enlighten.enphaseenergy.com/public/systems/"; // enter public view URL here
  4.  
  5. $width = 320;
  6. $height = 240;
  7. $graphw = $width - 40;
  8. $graphh = $height / 2 - 60;
  9.  
  10. function w_get($wurl) {
  11.     $wfile = fopen($wurl, 'r');
  12.     while ($wfile && !feof($wfile)) $wstring .= fread($wfile, 8192);
  13.     fclose($wfile);
  14.     return ($wstring);
  15. }
  16.  
  17. // only gets stats for first system
  18. $ar = json_decode(w_get("https://api.enphaseenergy.com/api/systems?key={$key}"), true);
  19. $id = $ar['systems'][0]['system_id'];
  20. $name = $ar['systems'][0]['system_public_name'];
  21. $sum = json_decode(w_get("https://api.enphaseenergy.com/api/systems/{$id}/summary?key={$key}"), true);
  22. $alert = json_decode(w_get("https://api.enphaseenergy.com/api/systems/{$id}/alerts?key={$key}"), true);
  23. $pow = json_decode(w_get("https://api.enphaseenergy.com/api/systems/{$id}/power_today?key={$key}"), true);
  24. $life = json_decode(w_get("https://api.enphaseenergy.com/api/systems/{$id}/energy_lifetime?key={$key}"), true);
  25.  
  26. $alevel = 0;
  27. foreach($alert['alerts'] as $val) {
  28.     if ($val['level'] == "medium" && $alevel < 1) $alevel = 1;
  29.     if ($val['level'] == "high" && $alevel < 2) $alevel = 2;   
  30. }
  31. if ($alevel == 0) $alertpng = "normal_medium.png";
  32.     else if ($alevel == 1) $alertpng = "warning_medium.png";
  33.     else if ($alevel == 2) $alertpng = "error_medium.png";
  34.  
  35. $peak = 0;
  36. foreach ($pow['production'] as $val) if ($val > $peak) $peak = $val;
  37. $dayarray = implode(',',$pow['production']);
  38.  
  39. $lpeak = 0;
  40. foreach ($life['production'] as $val) if ($val > $lpeak) $lpeak = $val;
  41. $lifearray = implode(',',$life['production']);
  42.  
  43. $content1 = "Power Production<br><br><span style='color:#F85C16;font-size:36px;'>{$sum['current_power']}W</span><br><br>Today's Peak: ".sprintf("%0.2f kW", $peak/1000);
  44. $content2 = "Today's Energy<br><span style='color:#F85C16;font-size:36px;'>".sprintf("%0.1f", $sum['energy_today']/1000)."kWh</span><br><canvas id='daygraph' height={$graphh} width={$graphw}></canvas>";
  45. $content3 = "Lifetime Energy<br><span style='color:#F85C16;font-size:36px;'>".sprintf("%0.1f", $sum['energy_lifetime']/1000000)."MWh</span><br><canvas id='lifegraph' height={$graphh} width={$graphw}></canvas>";
  46.  
  47. echo "
  48. <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
  49. <head><style type=\"text/css\">
  50. table {border-radius:10px;font: 18px \"Lucida Grande\",Lucida,Arial,Verdana;}
  51. td {color:#333333;border:none;}
  52. th {color:#333333;border:none;}
  53. a {color:#333333;text-decoration:none;}
  54. .tab {font-size:14px; background:#A0A0A0; padding:5px; border-radius: 10px 10px 0px 0px; cursor:pointer;}
  55. .seltab {font-size:14px; background:#E0E0E0; padding:5px; border-radius: 10px 10px 0px 0px; cursor:default;}
  56. .content {background:#E0E0E0; padding:5px; border-radius: 0px 0px 10px 10px;}
  57. .center {vertical-align: middle;text-align: center;}
  58. .grad {
  59.     background-image: linear-gradient(bottom, #CCCCCC 75%, #AAAAAA 100%);
  60.     background-image: -o-linear-gradient(bottom, #CCCCCC 75%, #AAAAAA 100%);
  61.     background-image: -moz-linear-gradient(bottom, #CCCCCC 75%, #AAAAAA 100%);
  62.     background-image: -webkit-linear-gradient(bottom, #CCCCCC 75%, #AAAAAA 100%);
  63.     background-image: -ms-linear-gradient(bottom, #CCCCCC 75%, #AAAAAA 100%);
  64.     background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.75, #CCCCCC), color-stop(1, #AAAAAA));
  65. }
  66. </style></head><html><body>
  67.  
  68. <table width={$width} height={$height} border=0 class='grad'>
  69. <tr><td style='padding:5px 5px 0px 5px;font-size:24px;'><a href='{$publicurl}'><img style='border:none;' src='http://assets1.enphaseenergy.com/images/icons/site_medium.png'> <b>{$name}</b> <img style='vertical-align:bottom;float:right;border:none;' src='http://assets1.enphaseenergy.com/images/icons/{$alertpng}'></a></td></tr>
  70.  
  71. <script language='javascript'>
  72. var didday = 0, didlife = 0;
  73.  
  74. function drawarray(canvas, data, max)
  75. {
  76.     if (data.length == 0) return;
  77.     if (max == 0) max = 1;
  78.     var cv=document.getElementById(canvas);
  79.     var ctx=cv.getContext('2d');
  80.     ctx.strokeStyle='#0063FF';
  81.     for (x = 0 ; x < data.length ; x++) {
  82.         ctx.moveTo(x * {$graphw} / data.length,{$graphh} );
  83.         ctx.lineTo(x * {$graphw} / data.length,{$graphh} - {$graphh} * data[x] / max - 1);
  84.     }
  85.     ctx.stroke();
  86. }
  87.  
  88. function drawday() {
  89.     if (didday) return;
  90.     var data=[{$dayarray}];
  91.     drawarray('daygraph', data, {$peak});
  92.     didday = 1;
  93. }
  94.  
  95. function drawlife() {
  96.     if (didlife) return;
  97.     var data=[{$lifearray}];
  98.     drawarray('lifegraph', data, {$lpeak});
  99.     didlife = 1;
  100. }
  101.  
  102. function seltab(t) {
  103.     for (x = 1 ; x <= 3 ; x++) {
  104.         document.getElementById('tab'+x).className = (x==t)?'seltab':'tab';
  105.         document.getElementById('content'+x).style.display = (x==t)?'block':'none';
  106.     }
  107. }
  108. </script>
  109.  
  110. <tr>
  111. <td style='padding:5px 5px 5px 5px; height:100%;'>
  112.  
  113. <table border=0 style='border-spacing: 2px 0px;' width='100%' height='100%'>
  114. <tr>
  115. <th onClick='seltab(1);' id='tab1' class='seltab'>Current</th>
  116. <th onClick='drawday();seltab(2);' id='tab2' class='tab'>Daily</th>
  117. <th onClick='drawlife();seltab(3);' id='tab3' class='tab'>Lifetime</th>
  118. </tr>
  119.  
  120. <tr><td class='content' colspan=3 style='height:100%'>
  121. <span id='content1' class='center' style='display:block;'>{$content1}</span>
  122. <span id='content2' class='center' style='display:none;'>{$content2}</span>
  123. <span id='content3' class='center' style='display:none;'>{$content3}</span>
  124. </td></tr>
  125. </table>
  126.  
  127. </td>
  128. </tr>
  129.  
  130. <tr><td align='right' valign='center'><span style='font-size:10px'>POWERED BY &nbsp;</span><a href='http://enphase.com' target='_blank'><img src='http://enphase.com/wp-uploads/enphase.com/2011/06/EnphaseAPIMark.png' style='border:none;vertical-align:middle;'></a></td></tr>
  131. </table>
  132. </body>
  133. </html>
  134. ";
  135.  
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement