From ada4bbc76cd98219953b0db588e1242544eca497 Mon Sep 17 00:00:00 2001 From: TrystanLea Date: Mon, 20 May 2024 23:32:29 +0100 Subject: [PATCH] fix for latest openevse wifi v4 --- devices/openevse.php | 167 ++++++++++++++++++++++++++++++++----------- lib/misc.php | 5 +- 2 files changed, 129 insertions(+), 43 deletions(-) diff --git a/devices/openevse.php b/devices/openevse.php index 88f8414..5c2ca82 100644 --- a/devices/openevse.php +++ b/devices/openevse.php @@ -6,10 +6,12 @@ class openevse private $last_ctrlmode = array(); private $last_timer = array(); private $last_soc_update = 0; + private $host = "openevse.local"; public function __construct($mqtt_client,$basetopic) { $this->mqtt_client = $mqtt_client; $this->basetopic = $basetopic; + $this->host = "192.168.1.118"; } public function default_settings() { @@ -47,9 +49,10 @@ public function on($device) { if ($this->last_ctrlmode[$device]!="on") { $this->last_ctrlmode[$device] = "on"; - $this->mqtt_client->publish("$device/rapi/in/\$ST","00 00 00 00",0); - $this->mqtt_client->publish("$device/rapi/in/\$FE","",0); - schedule_log("$device switch on"); + $result = $this->http_post('/override',array( + 'state' => 'active' + )); + schedule_log("$device switch on $result"); } } @@ -61,9 +64,10 @@ public function off($device) { if ($this->last_ctrlmode[$device]!="off") { $this->last_ctrlmode[$device] = "off"; - $this->mqtt_client->publish("$device/rapi/in/\$ST","00 00 00 00",0); - $this->mqtt_client->publish("$device/rapi/in/\$FS","",0); - schedule_log("$device switch off"); + $result = $this->http_post('/override',array( + 'state' => 'disabled' + )); + schedule_log("$device switch off $result"); } } @@ -76,8 +80,40 @@ public function timer($device,$s1,$e1,$s2,$e2) { if ($timer_str!=$this->last_timer[$device]) { $this->last_timer[$device] = $timer_str; - $this->mqtt_client->publish("$device/rapi/in/\$ST",$timer_str,0); - schedule_log("$device set timer $timer_str"); + + // clear override + $this->http_delete('/override'); + + // get list of events + $result = $this->http_get('/schedule'); + $events = json_decode($result); + + // if there are more than 2 events, delete the rest + if (count($events) > 2) { + foreach ($events as $event) { + if ($event->id > 2) { + $result = $this->http_delete('/schedule/'.$event->id); + } + } + } + + // set event 1 + $active_time_str = time_conv_dec_str($s1,":").":00"; + $result = $this->http_post('/schedule/1',array( + 'state' => 'active', + 'time' => $active_time_str, + 'days' => ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'] + )); + + // set event 2 + $disabled_time_str = time_conv_dec_str($e1,":").":00"; + $result = $this->http_post('/schedule/2',array( + 'state' => 'disabled', + 'time' => $disabled_time_str, + 'days' => ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'] + )); + + schedule_log("$device set timer active ".$active_time_str." disabled ".$disabled_time_str); } } @@ -91,13 +127,13 @@ public function set_divert_mode($device,$mode) { if ($this->last_divert_mode[$device]!=$mode) { $this->last_divert_mode[$device] = $mode; - $this->mqtt_client->publish("$device/divertmode/set",$mode,0); - schedule_log("$device divert mode $mode"); + // $this->mqtt_client->publish("$device/divertmode/set",$mode,0); + schedule_log("$device divert mode not implemented"); } } - + public function send_state_request($device) { - $this->mqtt_client->publish($this->basetopic."/$device/in/state","",0); + return false; } public function handle_state_response($schedule,$message,$timezone) { @@ -107,41 +143,37 @@ public function handle_state_response($schedule,$message,$timezone) { public function get_state($mqtt_request,$device,$timezone) { $valid = true; $state = new stdClass; - - // Get OpenEVSE timer state - if ($result = $mqtt_request->request($this->basetopic."/$device/rapi/in/\$GD","",$this->basetopic."/$device/rapi/out")) { - $ret = explode(" ",substr($result,4,11)); - if (count($ret)==4) { - $state->timer_start1 = ((int)$ret[0])+((int)$ret[1]/60); - $state->timer_stop1 = ((int)$ret[2])+((int)$ret[3]/60); - $state->timer_start2 = 0; - $state->timer_stop2 = 0; - } else { - $valid = false; - } + + // Get OpenEVSE timer state using curl + $result = $this->http_get('/schedule'); + $events = json_decode($result); + + // there should be 2 events + if (count($events) == 2) { + // split by : + $parts = explode(":",$events[0]->time); + $state->timer_start1 = ((int)$parts[0])+((int)$parts[1]/60); + $parts = explode(":",$events[1]->time); + $state->timer_stop1 = ((int)$parts[0])+((int)$parts[1]/60); + $state->timer_start2 = 0; + $state->timer_stop2 = 0; } else { $valid = false; } - // Get OpenEVSE state - if ($result = $mqtt_request->request($this->basetopic."/$device/rapi/in/\$GS","",$this->basetopic."/$device/rapi/out")) { - $ret = explode(" ",$result); - if ($ret[1]==254) { - if ($state->timer_start1==0 && $state->timer_stop1==0) { - $state->ctrl_mode = "off"; - } else { - $state->ctrl_mode = "timer"; - } - } - else if ($ret[1]==1 || $ret[1]==3) { - if ($state->timer_start1==0 && $state->timer_stop1==0) { - $state->ctrl_mode = "on"; - } else { - $state->ctrl_mode = "timer"; - } + // Get OpenEVSE state + $result = $this->http_get('/override'); + $override = json_decode($result); + if (isset($override->state)) { + if ($override->state == "active") { + $state->ctrl_mode = "on"; + } else if ($override->state == "disabled") { + $state->ctrl_mode = "off"; + } else { + $state->ctrl_mode = "timer"; } } else { - $valid = false; + $state->ctrl_mode = "timer"; } if ($valid) return $state; else return false; @@ -184,4 +216,57 @@ public function auto_update_timeleft($schedule) { } return $schedule; } + + // curl post + public function http_post($url,$data) { + $url = "http://".$this->host.$url; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + $headers = array( + 'Accept: application/json', + 'Content-Type: application/json', + ); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $result = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + curl_close ($ch); + return $result; + } + + // curl get + public function http_get($url) { + $url = "http://".$this->host.$url; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + curl_close ($ch); + return $result; + } + + // curl delete + public function http_delete($url) { + $url = "http://".$this->host.$url; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + $result = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + curl_close ($ch); + return $result; + } } diff --git a/lib/misc.php b/lib/misc.php index 4fdc151..c4e6dfc 100644 --- a/lib/misc.php +++ b/lib/misc.php @@ -43,8 +43,9 @@ function exceptions_error_handler($severity, $message, $filename, $lineno) { function schedule_log($message){ if ($fh = @fopen("/var/log/emoncms/demandshaper.log","a")) { - $now = microtime(true); - $micro = sprintf("%03d",($now - ($now >> 0)) * 1000); + $now = microtime(true); + $micro = sprintf("%03d", (int)(($now - (int)$now) * 1000)); + $now = DateTime::createFromFormat('U', (int)$now); // Only use UTC for logs $now = $now->format("Y-m-d H:i:s").".$micro"; @fwrite($fh,$now." | ".$message."\n");