From 66290c7cf7ceb2eaa4744b890bf3367718be2ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Tue, 5 Aug 2025 15:00:53 +0200 Subject: [PATCH] =?UTF-8?q?Easee-Station=20f=C3=BCr=20normalbetrieb=20inte?= =?UTF-8?q?griert.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ladestation_v2/form.json | 4 ++ Ladestation_v2/module.php | 133 +++++++++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/Ladestation_v2/form.json b/Ladestation_v2/form.json index 03f8d9f..b1060f6 100644 --- a/Ladestation_v2/form.json +++ b/Ladestation_v2/form.json @@ -24,6 +24,10 @@ { "caption": "Easee - Nur Solarladen", "value": 5 + }, + { + "caption": "Easee", + "value": 6 } ] }, diff --git a/Ladestation_v2/module.php b/Ladestation_v2/module.php index 5e152f5..873d3cb 100644 --- a/Ladestation_v2/module.php +++ b/Ladestation_v2/module.php @@ -87,6 +87,7 @@ class Ladestation_v2 extends IPSModule $this->SetValue("Idle", true); $this->RegisterTimer("Timer_Do_UserCalc_EVC",$this->ReadPropertyInteger("Interval")*1000,"IPS_RequestAction(" .$this->InstanceID .', "Do_UserCalc", "");'); + $this->RegisterTimer("Timer_Refresh_Token",0,"IPS_RequestAction(" .$this->InstanceID .', "Refresh_Token", "");'); } @@ -95,6 +96,14 @@ class Ladestation_v2 extends IPSModule parent::ApplyChanges(); $this->SetTimerInterval("Timer_Do_UserCalc_EVC",$this->ReadPropertyInteger("Interval")*1000); + // erstelle einen Timer der das Request token aktualisiert wenn die station dies braucht. + if($carType==6){ + + $this->Refresh_Token(); + $this->SetTimerInterval("Timer_Refresh_Token",1800); + + } + // Zusätzliche Anpassungen nach Bedarf } @@ -146,6 +155,10 @@ class Ladestation_v2 extends IPSModule $this->ResetTimer(); break; + case "Refresh_Token": + $this->Refresh_Token(); + break; + default: throw new Exception("Invalid Ident"); } @@ -399,7 +412,49 @@ class Ladestation_v2 extends IPSModule } break; - + + case 6: + + + + //Aktueller Zustand Ladestation abfragen (Leistung, auto eingesteckt?) + $ch2 = curl_init(); + + $url2 = "https://api.easee.com/api/chargers/".$this->ReadPropertyString("Seriennummer")."/state"; + curl_setopt_array($ch2, [ + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", + CURLOPT_HTTPHEADER => [ + "Authorization: Bearer ".$this->GetBuffer("Easee_Token")."", + "content-type: application/*+json" + ], + ]); + + curl_setopt($ch2, CURLOPT_URL, $url2); + curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); + $response_easee = curl_exec($ch2); + curl_close($ch2); + + $easee_data = json_decode($response_easee, true); + + + if (json_last_error() === JSON_ERROR_NONE && isset($easee_data["chargerOpMode"])) { + + if ($easee_data["chargerOpMode"] != 1 && (($easee_data["chargerOpMode"] != 6)||($this->GetValue("Car_detected")==true)) && $easee_data["chargerOpMode"] != 7) { + $car_on_station = true; + } + else{ + $car_on_station = false; + } + $this->SetValue("Ladeleistung_Effektiv", round($easee_data["totalPower"]*1000)); + $this->SetValue("Fahrzeugstatus", $easee_data["chargerOpMode"]); + } + + break; + default: $this->SetValue("Car_detected", false); $this->SetValue("Fahrzeugstatus", -1); @@ -598,6 +653,9 @@ class Ladestation_v2 extends IPSModule case 5: // Keine base Url nötig break; + case 6: + // Keine base Url nötig + break; } $ch = curl_init(); @@ -639,6 +697,24 @@ class Ladestation_v2 extends IPSModule ], ]); break; + + case 6: + + $url = "https://api.easee.com/api/chargers/".$this->ReadPropertyString("Seriennummer")."/commands/set_dynamic_charger_current"; + curl_setopt_array($ch, [ + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => "{\"amps\":". $value .",\"minutes\":0}", + CURLOPT_HTTPHEADER => [ + "Authorization: Bearer ".$this->GetBuffer("Easee_Token")."", + "content-type: application/*+json" + ], + ]); + break; + default: return "Invalid station type."; } @@ -670,6 +746,23 @@ class Ladestation_v2 extends IPSModule if($this->ReadPropertyString("Username") != $this->GetValue("Letzer_User")){ return; } + $url2 = "https://api.easee.com/api/chargers/".$this->ReadPropertyString("Seriennummer")."/commands/set_dynamic_charger_current"; + curl_setopt_array($ch, [ + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => "{\"amps\":". $value .",\"minutes\":0}", + CURLOPT_HTTPHEADER => [ + "Authorization: Bearer ".$this->GetBuffer("Easee_Token")."", + "content-type: application/*+json" + ], + ]); + break; + + case 6: + $url2 = "https://api.easee.com/api/chargers/".$this->ReadPropertyString("Seriennummer")."/commands/set_dynamic_charger_current"; curl_setopt_array($ch, [ CURLOPT_ENCODING => "", @@ -684,6 +777,7 @@ class Ladestation_v2 extends IPSModule ], ]); break; + default: return "Invalid station type."; } @@ -711,6 +805,43 @@ class Ladestation_v2 extends IPSModule } } + public function Refresh_Token(){ + + $payload = json_encode([ + 'userName' => $this->ReadPropertyString("Username"), + 'password' => $this->ReadPropertyString("Password"), + ]); + + // cURL-Handle initialisieren + $ch = curl_init('https://api.easee.com/api/accounts/login'); + + // cURL-Optionen setzen + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, // Antwort als String zurückgeben + CURLOPT_POST => true, // POST-Methode verwenden + CURLOPT_HTTPHEADER => [ + 'Accept: application/json', + 'Content-Type: application/json', + ], + CURLOPT_POSTFIELDS => $payload, // JSON-Body übergeben + ]); + + // Anfrage ausführen und Antwort speichern + $response = curl_exec($ch); + + // Auf Fehler prüfen + if ($response === false) { + echo 'cURL-Fehler: ' . curl_error($ch); + curl_close($ch); + exit; + } + + // cURL-Handle schließen + curl_close($ch); + $this->SetBuffer("Easee_Token",json_decode($response, true)['accessToken']); + + } + public function CheckIdle($power) { $lastpower = GetValue($this->GetIDForIdent("Aktuelle_Leistung"));