SHARE
TWEET

Untitled

a guest Oct 9th, 2018 244 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="3eac9f1a-bf1b-45ab-9e07-404c0cf8c083"; // 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.  
  68.     //retrieved basic login details
  69.     $server_output = json_decode($server_output, true);
  70.         if($debug) echo var_dump($server_output);
  71.        
  72.         $uid="";
  73.         $timestamp="";
  74.         $token="";
  75.         $retrieval=false;
  76.        
  77.     if($server_output["msg"]=="success"){
  78.         echo "<br/>retrieval ok <br/>";
  79.         $uid=$server_output["data"]["uid"];
  80.         $timestamp=$server_output["data"]["timestamp"];
  81.         $token=$server_output["data"]["token"];
  82.         $retrieval=true;
  83.     }else{
  84.         echo "<br/>Basic SEMS portal login failed. Enable debug mode <br/>";
  85.         curl_close ($ch);
  86.         exit();
  87.     }
  88.  
  89.  
  90.     if($retrieval && trim($powerstation_id)==""){
  91.         $vars="{\"page_size\":\"5\",\"orderby\":\"\",\"powerstation_status\":\"\",\"key\":\"\",\"page_index\":\"1\",\"powerstation_id\":\"\",\"powerstation_type\":\"\"}";
  92.         $headers = [
  93.         'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  94.         'Accept-Encoding: gzip, deflate',
  95.         'Accept: */*',
  96.         'Connect: Keep-alive',
  97.         'Content-Type: application/json',
  98.         'Host: globalapi.sems.com.cn',
  99.         'Token: {"version":"v2.1.0","client":"ios","language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","token":"'.$token.'"}',
  100.         'User-Agent:  PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  101.         ];
  102.         curl_setopt($ch, CURLOPT_URL,"https://euapi.sems.com.cn/api/PowerStationMonitor/QueryPowerStationMonitorForApp");
  103.         curl_setopt($ch, CURLOPT_POST, 1);
  104.         curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
  105.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  106.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  107.  
  108.         $server_output2 = curl_exec ($ch);
  109.         $server_output2 = json_decode($server_output2, true);
  110.         if($server_output2["msg"]=="success"){
  111.             if($debug) var_dump($server_output2);
  112.             echo "<br/>retrieval powerstation ok <br/>";
  113.             $powerstation_id=$server_output2["data"][0]["powerstation_id"];
  114.             echo "retrieved ".$powerstation_id."<br/>";
  115.         }
  116.     }else{
  117.         echo "Powerstation id is provided: ".$powerstation_id."<br/>";
  118.     }
  119.  
  120. $retrieval3=false;
  121. if($retrieval&& !trim($powerstation_id)==""){
  122.     echo "retrieval 3 ok <br/>";
  123.  
  124.     $vars="{\"powerStationId\":\"$powerstation_id\"}";
  125.     $headers = [
  126.     'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  127.     'Accept-Encoding: gzip, deflate',
  128.     'Accept: */*',
  129.     'Connect: Keep-alive',
  130.     'Content-Type: application/json',
  131.     'Host: globalapi.sems.com.cn',
  132.     'Token: {"version":"v2.1.0","client":"ios","language":"en","timestamp":'.$timestamp.',"uid":"'.$uid.'","token":"'.$token.'"}',
  133.     'User-Agent:  PVMaster/2.1.0 (iPhone; iOS 12.0; Scale/2.00)',
  134.     ];
  135.     curl_setopt($ch, CURLOPT_URL,"https://euapi.sems.com.cn/api/v1/PowerStation/GetMonitorDetailByPowerstationId");
  136.     curl_setopt($ch, CURLOPT_POST, 1);
  137.     curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
  138.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  139.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  140.  
  141.     $server_output3 = curl_exec ($ch);
  142.     $server_output3 = json_decode($server_output3, true);
  143.    
  144.     if($debug) var_dump($server_output3);
  145.    
  146.     $retrieval3=true;
  147.     $pac=$server_output3["data"]["kpi"]["pac"];
  148.     $powerpki=$server_output3["data"]["kpi"]["power"];
  149.     $temp=$server_output3["data"]["inverter"][0]["tempperature"];
  150.     $vpv1=$server_output3["data"]["inverter"][0]["d"]["vpv1"];
  151.     $vpv2=$server_output3["data"]["inverter"][0]["d"]["vpv2"];
  152.    
  153.     $power = intval($pac);
  154.     $energy_cum = round(floatval($powerpki)*1000);
  155.     $vdc = round(floatval($vpv1),1); // v6 = Voltage DC van MPP1
  156.     $temperature = round(floatval($temp),1);
  157.     echo "<h1>Energy = $energy_cum Wh - Power = $power W - DC voltage 1 = $vdc, DC voltage 2 = $vpv2 - Temperature = $temperature degr.C\n </h1>";
  158.  
  159. }else{
  160.     echo "<br/>Advanced SEMS portal data retrieval failed. Enable debug mode <br/>";
  161.     curl_close ($ch);
  162.     exit();
  163. }
  164.  
  165. curl_close ($ch);
  166.  
  167.  
  168. if($retrieval && $retrieval3){
  169.  
  170.     // Combine the RUL to call PVoutpout and submit data Stel
  171.     $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';
  172.     // if $test is true, only show url otherwise submit
  173.     if($test){
  174.         echo "URL = $url \n";
  175.     }else{
  176.         $responds = file_get_contents($url);
  177.         echo "Responds PVOutput was: $responds \n";
  178.         if($responds == "OK 200: Added Status")
  179.         {
  180.           echo "The values were succesfully retrieved and submitted to PVOutput \n";
  181.         }
  182.         else
  183.         {
  184.           echo "Issue submitting to PVOutput, let's try again...<br>\n";
  185.           sleep(4); // wacht 4 seconden
  186.           $responds = file_get_contents($url);
  187.           if($responds == "OK 200: Added Status")
  188.           {
  189.             echo "The values were succesfully retrieved and submitted to PVOutput \n";
  190.           }
  191.           else
  192.           {
  193.             echo "PVOutput submit failed for a second time, please verify if settings are correct.\n";
  194.             echo "Responds PVOutput was: $responds \n";
  195.           }
  196.         }
  197.       }
  198. }
  199.  
  200.  
  201.  
  202.  
  203. echo '</pre>';
  204. ?>
  205.  
  206. </body>
  207. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top