Insert new Frame in sitemap:
Frame label=“PV” {
Text item=PV1_Power label=“Leistung [%.1f W]”
Text item=Total_Power label=“Last [%.1f W]”
Text item=House_Comsumption label=“Netz [%.1f W]”
Text item=Inverter_Temperature label=“Invertertemperatur [%.1f °C]”
Text item=Battery_Temperature label=“Batterietemperatur [%.1f °C]”
Text item=Battery_Power label=“Batterieleistung [%.1f W]”
Text item=Battery_State_of_Charge label=“Batterieladung [%.1f]”
}
#!/usr/bin/php
<?php
$socket=new tcp_mitel_smdr('bat');
$resp=str_split($socket->hex,6);
$Bat=array("Battery_Temperature"=>$resp[3],"Battery Charge Limit"=>$resp[4],"Battery Discharge Limit"=>$resp[5],"Battery Status"=>$resp[6],"Battery_State_of_Charge"=>$resp[7],"Battery_State_of_Health"=>$resp[8]);
foreach ($Bat as $index => $value) {
$value=hexdec($value);
switch($index) {
case 'Battery_State_of_Charge':
setOHItemState($index,$value);
break;
case 'Battery_Temperature':
$value=$value/10;
setOHItemState($index,$value);
break;
}
echo $index." = ".$value."\n";
}
$socket=new tcp_mitel_smdr('dat');
$resp=str_split($socket->hex,3);
$Dat=array("PV1_Voltage"=>$resp[6].$resp[7],"PV1_Current"=>$resp[8].$resp[9],"PV1_Power"=>$resp[10].$resp[11].$resp[12].$resp[13],"Total_WR_Power"=>$resp[74].$resp[75].$resp[76].$resp[77],"Active_Power"=>$resp[78].$resp[79].$resp[80].$resp[81],"Load_L1"=>$resp[126].$resp[127].$resp[128].$resp[129],"Load_L2"=>$resp[130].$resp[131].$resp[132].$resp[133],"Load_L3"=>$resp[134].$resp[135].$resp[136].$resp[137],"Inverter_Temperature"=>$resp[152].$resp[153],"Battery_Voltage"=>$resp[160].$resp[161],"Battery_Current"=>$resp[162].$resp[163],"Battery_Mode"=>$resp[168].$resp[169],"Battery_Power"=>"0","Total_Power"=>"0","House_Comsumption"=>"0");
// 1: "Spare",2: "Discharge",3: "Charge"
$watt=array();
foreach ($Dat as $index => $value) {
$value=hexdec($value);
switch($index) {
/*case 'PV1_Power':
$value=$value/100;
break;*/
case 'Inverter_Temperature':
$value=$value/10;
break;
case 'Active_Power':
if ($value > 32768){$value=$value- 65535;}
// $value=$value/100;
break;
case 'Battery_Current':
//$value=hexdec($value);
if ($value > 32768){$value=$value- 65535;}
$value=$value/10;
break;
case 'Battery_Power':
$value=($watt[9]*$watt[10])/10;
break;
case 'Total_Power':
$value=$watt[5]+$watt[6]+$watt[7];
break;
case 'House_Comsumption':
$value=(($watt[3]+$watt[12]+$watt[4])-$watt[13]);///100;
break;
default:
break;
}
array_push($watt,$value);
echo $index." = ".$value."\n";
setOHItemState($index,$value);
}
function setOHItemState($item, $state)
{ $oh_url = "192.168.211.56";
$oh_port = "8080";
$return = false;
$headers = array();
$headers[] = 'Content-Type: text/plain';
$headers[] = 'Accept: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://$oh_url:$oh_port/rest/items/$item");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $state);
curl_exec($ch);
if (!curl_errno($ch)) { $return = true; }
curl_close($ch);
return $return;
}
function getOHItemState($oh_url, $oh_port, $item)
{ $state = 'OFF'; $headers = array(); $headers[] = 'Content-Type: text/plain'; $headers[] = 'Accept: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://$oh_url:$oh_port/rest/items/$item"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch);
if (!curl_errno($ch)) { $json = json_decode($response, true); if(isset($json['state'])) { $state = $json['state']; } } curl_close($ch);
return $state; }
class tcp_mitel_smdr {
static $pbx_ip = '192.168.211.104';
static $pbx_port = '8899';
static $pbx_timeout = 30;
static $pbx_flags;
public $hex = '';
private $pbx;
function __construct($run='') {
$this->pbx=false;
switch($run) {
case 'bat':
$this->loop_socket("\xF7\x03\x90\x88\x00\x0B\xBD\xB1");
break;
case 'dat':
$this->loop_socket("\xF7\x03\x89\x1C\x00\x7D\x7A\xE7");
break;
}
}
function pbx_connect($msg){
//Connect to Server
$errno = '';
$errstr = '';
$this->pbx = stream_socket_client("udp://".tcp_mitel_smdr::$pbx_ip.":".tcp_mitel_smdr::$pbx_port."", $errno, $errstr, tcp_mitel_smdr::$pbx_timeout);
fwrite($this->pbx, $msg);
stream_set_timeout($this->pbx, 1);
}
function loop_socket($msg){
$disconnected=0;
while($disconnected<1){
$disconnected +=$this->process_socket($msg);
// echo('pbx disconnected, reconnecting');
}
fclose($this->pbx);
}
function process_socket($msg){
$this->pbx_connect($msg);
//Receive response from server. Loop until the response is finished
//while ($this->pbx && !feof($this->pbx)) {
// read from pbx
$callstring = fread($this->pbx, 257);
// the string may just be a PING.
if($callstring){
$this->hex=$this->echoit($callstring);
// write to mysql
#$smdr = new db_class();
#$smdr->save_data($callstring);
}
//}
return 1;
}
function echoit($resp) {
$hex = '';
$ascii=substr($resp,5,strlen($resp)-2);
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
//var_dump($hex);
return $hex;
}
}
?>