Guest User

Untitled

a guest
Jun 28th, 2020
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. <html>
  2. <header>
  3. </header>
  4. <body>
  5. <?php
  6.  
  7. // Code written in a night by Roeland Kindt for SEMS portal retrieval
  8. // PVoutput portion copied from older Goodwe2PVoutput script, thanks Antonboonstra!
  9.  
  10. // Below portion is for PVOUTput site
  11. // Retrieve API key from PVOutput, you can create it on http://pvoutput.org/account.jsp under API settings
  12. $keyPVO = "";
  13. // Fill in your System ID, you can retrieve it from http://pvoutput.org/account.jsp (at the bottom)
  14. $sidPVO = "";
  15.  
  16. // // SEMS portal portion
  17. // You can retriev powerstation id from URL if you visit website, or run the script once, it will provide the ID to you.
  18. $powerstation_id=""; // not mandatory first run
  19.  
  20. // Login SEMS portal website
  21. $email="";
  22. // Password SEMS portal website.
  23. $password="";
  24.  
  25. //enable+/disable debug/test mode
  26. $test=true; // if true shows pvoutput url but does not submit it, set to false if working.
  27. $debug=false; // prints var_dump of vars, set to true to var_dump
  28. $SSLExpired=true; // work around in case ssl certificate is invalid, disable once it is fixed!
  29.  
  30.  
  31. // below code does not need modifications.
  32.  
  33. $vars = "{\"account\":\"$email\",\"pwd\":\"$password\"}";
  34.  
  35. //logic retrieved from original script by antonboostra
  36. echo "<pre>\n";
  37. // determine if it is night or day, to prevent retrieval of data (reduce API calls)
  38. $day = date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, 52, 5.5);
  39. $night = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, 52, 5.5);
  40. $now = time();
  41. if($now < ($day-600) || $now > ($night+600)) // 600 seconds (=10 minutes) margin
  42. {
  43. echo "It is before dusk (".date("H:i",$day)." hour) or after sunset (".date("H:i",$night)." hour) \n";
  44. if(!$test) exit(); // exit so we don't run the rest of the code
  45. }
  46.  
  47. //basic setup for logging into SEMS portal
  48. $ch = curl_init();
  49. curl_setopt($ch, CURLOPT_URL,"https://globalapi.sems.com.cn/api/v1/Common/CrossLogin");
  50. curl_setopt($ch, CURLOPT_POST, 1);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  53. if($SSLExpired==true){
  54. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // disables SSL
  55. }
  56. $headers = [
  57. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  58. 'Accept-Encoding: gzip, deflate',
  59. 'Accept: */*',
  60. 'Connect: Keep-alive',
  61. 'Content-Type: application/json',
  62. 'Host: globalapi.sems.com.cn',
  63. 'Token: {"version":"v2.1.0","client":"ios","language":"en"}',
  64. 'User-Agent: PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  65. ];
  66.  
  67. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  68.  
  69. $server_output = curl_exec ($ch);
  70. if($server_output==false){
  71. echo curl_error($ch);
  72. exit();
  73. }
  74.  
  75. //retrieved basic login details
  76. $server_output = json_decode($server_output, true);
  77. if($debug) echo var_dump($server_output);
  78.  
  79. $uid="";
  80. $timestamp="";
  81. $token="";
  82. $retrieval=false;
  83.  
  84. if(strtoupper(trim($server_output["msg"]))=="SUCCESS"){
  85. echo "<br/>retrieval ok <br/>";
  86. $uid=$server_output["data"]["uid"];
  87. $timestamp=$server_output["data"]["timestamp"];
  88. $token=$server_output["data"]["token"];
  89. $retrieval=true;
  90. }else{
  91. echo "<br/>Basic SEMS portal login failed. Enable debug mode <br/>";
  92. curl_close ($ch);
  93. exit();
  94. }
  95.  
  96.  
  97. if($retrieval && trim($powerstation_id)==""){
  98. $vars="{\"page_size\":\"5\",\"orderby\":\"\",\"powerstation_status\":\"\",\"key\":\"\",\"page_index\":\"1\",\"powerstation_id\":\"\",\"powerstation_type\":\"\"}";
  99. $headers = [
  100. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  101. 'Accept-Encoding: gzip, deflate',
  102. 'Accept: */*',
  103. 'Connect: Keep-alive',
  104. 'Content-Type: application/json',
  105. 'Host: globalapi.sems.com.cn',
  106. 'Token: {"version":"v2.1.0","client":"ios","language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","token":"'.$token.'"}',
  107. 'User-Agent: PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  108. ];
  109. curl_setopt($ch, CURLOPT_URL,"https://euapi.sems.com.cn/api/PowerStationMonitor/QueryPowerStationMonitorForApp");
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  112. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  113. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  114.  
  115. $server_output2 = curl_exec ($ch);
  116. $server_output2 = json_decode($server_output2, true);
  117. if(strtoupper(trim($server_output2["msg"]))=="SUCCESS"){
  118. if($debug) var_dump($server_output2);
  119. echo "<br/>retrieval powerstation ok <br/>";
  120. $powerstation_id=$server_output2["data"][0]["powerstation_id"];
  121. echo "retrieved ".$powerstation_id."<br/>";
  122. }
  123. }else{
  124. echo "Powerstation id is provided: ".$powerstation_id."<br/>";
  125. }
  126.  
  127. $retrieval3=false;
  128. if($retrieval&& !trim($powerstation_id)==""){
  129. echo "retrieval 3 ok <br/>";
  130.  
  131. $vars="{\"powerStationId\":\"$powerstation_id\"}";
  132. $headers = [
  133. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  134. 'Accept-Encoding: gzip, deflate',
  135. 'Accept: */*',
  136. 'Connect: Keep-alive',
  137. 'Content-Type: application/json',
  138. 'Host: globalapi.sems.com.cn',
  139. 'Token: {"version":"v2.1.0","client":"ios","language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","token":"'.$token.'"}',
  140. 'User-Agent: PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  141. ];
  142. curl_setopt($ch, CURLOPT_URL,"https://euapi.sems.com.cn/api/v1/PowerStation/GetMonitorDetailByPowerstationId");
  143. curl_setopt($ch, CURLOPT_POST, 1);
  144. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  145. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  146. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  147.  
  148. $server_output3 = curl_exec ($ch);
  149. $server_output3 = json_decode($server_output3, true);
  150.  
  151. if($debug) var_dump($server_output3);
  152.  
  153. $retrieval3=true;
  154. $pac=$server_output3["data"]["kpi"]["pac"];
  155. $powerpki=$server_output3["data"]["kpi"]["power"];
  156. $temp=$server_output3["data"]["inverter"][0]["tempperature"];
  157. $vpv1=$server_output3["data"]["inverter"][0]["d"]["vpv1"];
  158. $vpv2=$server_output3["data"]["inverter"][0]["d"]["vpv2"];
  159.  
  160. $power = intval($pac);
  161. $energy_cum = round(floatval($powerpki)*1000);
  162. $vdc = round(floatval($vpv1),1); // v6 = Voltage DC van MPP1
  163. $temperature = round(floatval($temp),1);
  164. echo "<h1>Energy = $energy_cum Wh - Power = $power W - DC voltage 1 = $vdc, DC voltage 2 = $vpv2 - Temperature = $temperature degr.C\n </h1>";
  165.  
  166. }else{
  167. echo "<br/>Advanced SEMS portal data retrieval failed. Enable debug mode <br/>";
  168. curl_close ($ch);
  169. exit();
  170. }
  171.  
  172. curl_close ($ch);
  173.  
  174.  
  175. if($retrieval && $retrieval3){
  176.  
  177. // Combine the RUL to call PVoutpout and submit data Stel
  178. $url = 'http://pvoutput.org/service/r2/addstatus.jsp?key='.$keyPVO.'&sid='.$sidPVO.'&d='.date("Ymd").'&t='.date("H:i").'&v1='.$energy_cum.'&v2='.$power.'&v5='.$temperature.'&v6='.$vdc.'&c1=0';
  179. // if $test is true, only show url otherwise submit
  180. if($test){
  181. echo "URL = $url \n";
  182. }else{
  183. $responds = file_get_contents($url);
  184. echo "Responds PVOutput was: $responds \n";
  185. if($responds == "OK 200: Added Status")
  186. {
  187. echo "The values were succesfully retrieved and submitted to PVOutput \n";
  188. }
  189. else
  190. {
  191. echo "Issue submitting to PVOutput, let's try again...<br>\n";
  192. sleep(4); // wacht 4 seconden
  193. $responds = file_get_contents($url);
  194. if($responds == "OK 200: Added Status")
  195. {
  196. echo "The values were succesfully retrieved and submitted to PVOutput \n";
  197. }
  198. else
  199. {
  200. echo "PVOutput submit failed for a second time, please verify if settings are correct.\n";
  201. echo "Responds PVOutput was: $responds \n";
  202. }
  203. }
  204. }
  205. }
  206.  
  207.  
  208.  
  209.  
  210. echo '</pre>';
  211. ?>
  212.  
  213. </body>
  214. </html>
Add Comment
Please, Sign In to add comment