Advertisement
Guest User

Untitled

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