Guest User

Untitled

a guest
Sep 26th, 2020
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 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=false; // work around in case ssl certificate is invalid, disable once it is fixed!
  29. $timezone="Europe/Amsterdam"; // specify in which timezone you are... default it will take UTC for database storing...
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. // below code does not need modifications.
  38. date_default_timezone_set($timezone);
  39. $vars = "{\"account\":\"$email\",\"pwd\":\"$password\", \"agreement_agreement\":\"0\"}";
  40.  
  41. //logic retrieved from original script by antonboostra
  42. echo "<pre>\n";
  43. // determine if it is night or day, to prevent retrieval of data (reduce API calls)
  44. $day = date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, 52, 5.5);
  45. $night = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, 52, 5.5);
  46. $now = time();
  47. if($now < ($day-600) || $now > ($night+600)) // 600 seconds (=10 minutes) margin
  48. {
  49. echo "It is before dusk (".date("H:i",$day)." hour) or after sunset (".date("H:i",$night)." hour) \n";
  50. if(!$test) exit(); // exit so we don't run the rest of the code
  51. }
  52.  
  53. //basic setup for logging into SEMS portal
  54. $ch = curl_init();
  55. curl_setopt($ch, CURLOPT_URL,"https://semsportal.com/api/v2/Common/CrossLogin");
  56. curl_setopt($ch, CURLOPT_POST, 1);
  57. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  59. if($SSLExpired==true){
  60. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // disables SSL
  61. }
  62. $headers = [
  63. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  64. 'Accept-Encoding: gzip, deflate',
  65. 'Accept: */*',
  66. 'Connect: Keep-alive',
  67. 'Content-Type: application/json',
  68. 'Host: globalapi.sems.com.cn',
  69. 'Token: {"version":"v2.1.0","client":"ios","language":"en"}',
  70. 'User-Agent: SEMS Portal/3.1.1 (iPhone; iOS 13.7; Scale/2.00)',
  71. ];
  72.  
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  74.  
  75. $server_output = curl_exec ($ch);
  76. if($server_output==false){
  77. echo curl_error($ch);
  78. exit();
  79. }
  80.  
  81. //retrieved basic login details
  82. $server_output = json_decode($server_output, true);
  83. if($debug) echo var_dump($server_output);
  84.  
  85. $uid="";
  86. $timestamp="";
  87. $token="";
  88. $retrieval=false;
  89.  
  90. if(strtoupper(trim($server_output["msg"]))=="SUCCESS"){
  91. echo "<br/>retrieval ok <br/>";
  92. $uid=$server_output["data"]["uid"];
  93. $timestamp=$server_output["data"]["timestamp"];
  94. $token=$server_output["data"]["token"];
  95. $retrieval=true;
  96.  
  97.  
  98. }else{
  99. echo "<br/>Basic SEMS portal login failed. Enable debug mode <br/>";
  100. curl_close ($ch);
  101. exit();
  102. }
  103.  
  104.  
  105.  
  106. if($retrieval && trim($powerstation_id)==""){
  107.  
  108. $vars="{\"page_size\":\"5\",\"orderby\":\"\",\"powerstation_status\":\"\",\"key\":\"\",\"page_index\":\"1\",\"powerstation_id\":\"\",\"powerstation_type\":\"\"}";
  109.  
  110. $headers = [
  111. 'Host: eu.semsportal.com',
  112. 'Content-Type: application/json' ,
  113. 'Accept: */*',
  114. 'User-Agent: SEMS Portal/3.1.1 (iPhone; iOS 13.7; Scale/2.00)' ,
  115. 'Accept-Language: nl-BE;q=1' ,
  116. 'Token: {"language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","client":"ios","token":"'.$token.'","version":"v3.1.1"}'
  117. ];
  118.  
  119.  
  120. curl_setopt($ch, CURLOPT_URL,"https://eu.semsportal.com/api/PowerStationMonitor/QueryPowerStationMonitorForApp");
  121. curl_setopt($ch, CURLOPT_POST, 1);
  122. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  123. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  124. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  125.  
  126.  
  127. $server_output2 = curl_exec ($ch);
  128.  
  129. $server_output2 = json_decode($server_output2, true);
  130. if($debug) var_dump($server_output2);
  131. if(strtoupper(trim($server_output2["msg"]))=="SUCCESS"){
  132. if($debug) var_dump($server_output2);
  133. echo "<br/>retrieval powerstation ok <br/>";
  134. $powerstation_id=$server_output2["data"][0]["powerstation_id"];
  135. echo "retrieved ".$powerstation_id."<br/>\r\n";
  136. }else{
  137. echo "failed to retrieve powerstation id";
  138. }
  139. }else{
  140. echo "Powerstation id is provided: ".$powerstation_id."<br/>";
  141. }
  142.  
  143. $retrieval3=false;
  144. if($retrieval&& !trim($powerstation_id)==""){
  145. echo "retrieval 3 ok <br/>";
  146.  
  147. $vars="{\"powerStationId\":\"$powerstation_id\"}";
  148. $headers = [
  149. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  150. 'Accept-Encoding: gzip, deflate',
  151. 'Accept: */*',
  152. 'Connect: Keep-alive',
  153. 'Content-Type: application/json',
  154. 'Host: globalapi.sems.com.cn',
  155. 'Token: {"version":"v2.1.0","client":"ios","language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","token":"'.$token.'"}',
  156. 'User-Agent: PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  157. ];
  158. curl_setopt($ch, CURLOPT_ENCODING, "");
  159. curl_setopt($ch, CURLOPT_URL,"https://euapi.sems.com.cn/api/v1/PowerStation/GetMonitorDetailByPowerstationId");
  160. curl_setopt($ch, CURLOPT_POST, 1);
  161. curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  163. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  164.  
  165. $server_output3 = curl_exec ($ch);
  166. $server_output3 = json_decode($server_output3, true);
  167.  
  168. if($debug) var_dump($server_output3);
  169.  
  170. $retrieval3=true;
  171. $pac=$server_output3["data"]["kpi"]["pac"];
  172. $powerpki=$server_output3["data"]["kpi"]["power"];
  173. $temp=$server_output3["data"]["inverter"][0]["tempperature"];
  174. $vpv1=$server_output3["data"]["inverter"][0]["d"]["vpv1"];
  175. $vpv2=$server_output3["data"]["inverter"][0]["d"]["vpv2"];
  176.  
  177. $power = intval($pac);
  178. $energy_cum = round(floatval($powerpki)*1000);
  179. $vdc = round(floatval($vpv1),1); // v6 = Voltage DC van MPP1
  180. $temperature = round(floatval($temp),1);
  181. echo "<h1>Energy = $energy_cum Wh - Power = $power W - DC voltage 1 = $vdc, DC voltage 2 = $vpv2 - Temperature = $temperature degr.C\n </h1>";
  182.  
  183. }else{
  184. echo "<br/>Advanced SEMS portal data retrieval failed. Enable debug mode <br/>";
  185. curl_close ($ch);
  186. exit();
  187. }
  188.  
  189. curl_close ($ch);
  190.  
  191.  
  192. if($retrieval && $retrieval3){
  193.  
  194.  
  195. // Combine the RUL to call PVoutpout and submit data Stel
  196. $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';
  197. // if $test is true, only show url otherwise submit
  198. if($test){
  199. echo "URL = $url \n";
  200. }else{
  201. $responds = file_get_contents($url);
  202. echo "Responds PVOutput was: $responds \n";
  203. if($responds == "OK 200: Added Status")
  204. {
  205. echo "The values were succesfully retrieved and submitted to PVOutput \n";
  206. }
  207. else
  208. {
  209. echo "Issue submitting to PVOutput, let's try again...<br>\n";
  210. sleep(4); // wacht 4 seconden
  211. $responds = file_get_contents($url);
  212. if($responds == "OK 200: Added Status")
  213. {
  214. echo "The values were succesfully retrieved and submitted to PVOutput \n";
  215. }
  216. else
  217. {
  218. echo "PVOutput submit failed for a second time, please verify if settings are correct.\n";
  219. echo "Responds PVOutput was: $responds \n";
  220. }
  221. }
  222. }
  223. }
  224.  
  225.  
  226.  
  227.  
  228. echo '</pre>';
  229. ?>
  230.  
  231. </body>
  232. </html>
Add Comment
Please, Sign In to add comment