From 00341c4dfeee5035a2ee269bcae35a5ab2304bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Thu, 19 Dec 2024 10:17:31 +0100 Subject: [PATCH] smart me ladestation integriert --- Ladestation_Universal/form.json | 27 +++++---- Ladestation_Universal/module.php | 98 +++++++++++++++++++++++++++++++- library.json | 2 +- 3 files changed, 112 insertions(+), 15 deletions(-) diff --git a/Ladestation_Universal/form.json b/Ladestation_Universal/form.json index d1609a7..a5062ab 100644 --- a/Ladestation_Universal/form.json +++ b/Ladestation_Universal/form.json @@ -62,22 +62,27 @@ "name": "ID", "caption": "ID", "suffix": "", - "visible": false, - "visibilityCondition": { - "field": "Ladestation", - "value": 3 - } + "visible": true + }, { "type": "ValidationTextBox", "name": "Seriennummer", "caption": "Seriennummer", "suffix": "", - "visible": false, - "visibilityCondition": { - "field": "Ladestation", - "value": 3 - } - } + "visible": true + + }, + + { + "type": "ValidationTextBox", + "name": "Username", + "caption": "Username", + "suffix": "", + "visible": true + + }, + { "type": "PasswordTextBox", "name": "Password", "caption": "Passwort" } + ] } \ No newline at end of file diff --git a/Ladestation_Universal/module.php b/Ladestation_Universal/module.php index 73d5f38..caf5d9c 100644 --- a/Ladestation_Universal/module.php +++ b/Ladestation_Universal/module.php @@ -13,6 +13,10 @@ class Ladestation_Universal extends IPSModule $this->RegisterPropertyInteger("MaxLeistung_1ph", 7800); $this->RegisterPropertyString("IP_Adresse", "0.0.0.0"); $this->RegisterPropertyInteger("Ladestation", 2); + $this->RegisterPropertyString("ID", ""); + $this->RegisterPropertyString("Seriennummer", ""); + $this->RegisterPropertyString("Username", ""); + $this->RegisterPropertyString("Password", ""); // Ladestationspezifische Variabeln @@ -171,6 +175,15 @@ class Ladestation_Universal extends IPSModule "http://" . $this->ReadPropertyString("IP_Adresse") . "/mqtt?payload=" ); } + elseif($this->ReadPropertyInteger("Ladestation")==3){ + curl_setopt( + $ch, + CURLOPT_URL, + "https://api.smart-me.com/pico/charging/". $this->ReadPropertyInteger("ID"); + ); + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyInteger("Username") . ":" . $$this->ReadPropertyInteger("Password")); + } // Setze die Option, die Antwort als String zurückzugeben @@ -187,6 +200,7 @@ class Ladestation_Universal extends IPSModule // Dekodiere die JSON-Antwort $data = json_decode($response, true); + if($this->ReadPropertyInteger("Ladestation")==2||$this->ReadPropertyInteger("Ladestation")==1){ // Überprüfe, ob das JSON-Dekodieren erfolgreich war und der Schlüssel "car" existiert if (json_last_error() === JSON_ERROR_NONE && isset($data["car"])) { // Speichere den Wert von "car" in der Variable $status @@ -205,7 +219,28 @@ class Ladestation_Universal extends IPSModule } } } + if($this->ReadPropertyInteger("Ladestation")==3){ + // Überprüfe, ob das JSON-Dekodieren erfolgreich war und der Schlüssel "car" existiert + if (json_last_error() === JSON_ERROR_NONE && isset($data["State"])) { + // Speichere den Wert von "car" in der Variable $status + SetValue( + $this->GetIDForIdent("Ladeleistung_Effektiv"), + round($data["ActiveChargingPower"]*1000) + ); + + SetValue($this->GetIDForIdent("Fahrzeugstatus"), $data["State"]); + + if ($data["ActiveChargingPower"]/$data["MaxAllowedChargingCurrent"] > 0.4 && $data["car"] != 1) { + SetValue($this->GetIDForIdent("Lademodus"), 1); + } elseif ($data["car"] != 1) { + SetValue($this->GetIDForIdent("Lademodus"), 0); + } + } + } + + } + // Peak-Wert speichern $this->SetValue("Peak", $Peak); @@ -251,7 +286,9 @@ class Ladestation_Universal extends IPSModule $baseUrl ="http://" . $this->ReadPropertyString("IP_Adresse") . "/api/set?"; }elseif($this->ReadPropertyInteger("Ladestation")==1){ $baseUrl ="http://" . $this->ReadPropertyString("IP_Adresse") . "/mqtt?payload="; - } + }elseif($this->ReadPropertyInteger("Ladestation")==3){ + $baseUrl ="https://api.smart-me.com/pico/loadmanagementgroup/current/" . $this->ReadPropertyString("Seriennummer") . "?current="; + } // Base URL IPS_LogMessage("Ladestation", "Aufgerufene ip" . $baseUrl); @@ -266,15 +303,33 @@ class Ladestation_Universal extends IPSModule if ($value == 0) { if($this->ReadPropertyInteger("Ladestation")==2){ $url = $baseUrl . "frc=1"; + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + return $response; + }elseif($this->ReadPropertyInteger("Ladestation")==1){ $url = $baseUrl . "alw=0"; + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + return $response; - } + }elseif($this->ReadPropertyInteger("Ladestation")==3){ + $url = $baseUrl . "0"; curl_setopt($ch, CURLOPT_URL, $url); + + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyInteger("Username") . ":" . $$this->ReadPropertyInteger("Password")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; + + } + } // For value between 1 and 32, make two requests @@ -285,7 +340,9 @@ class Ladestation_Universal extends IPSModule }elseif($this->ReadPropertyInteger("Ladestation")==1){ $url = $baseUrl . "alw=1"; - } + }elseif($this->ReadPropertyInteger("Ladestation")==3){ + // Nichts zu tun für Smart-Me station +} curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response1 = curl_exec($ch); @@ -301,11 +358,43 @@ class Ladestation_Universal extends IPSModule if($this->ReadPropertyInteger("Ladestation")==2){ $url2 = $baseUrl . "amp=$value"; + curl_setopt($ch, CURLOPT_URL, $url2); + $response2 = curl_exec($ch); + + // Check for errors + if (curl_errno($ch)) { + curl_close($ch); + return "Error:" . curl_error($ch); + } + + // Close cURL session + curl_close($ch); + + // Return responses + return [$response1, $response2]; }elseif($this->ReadPropertyInteger("Ladestation")==1){ $url2 = $baseUrl . "amp=$value"; + curl_setopt($ch, CURLOPT_URL, $url2); + $response2 = curl_exec($ch); + + // Check for errors + if (curl_errno($ch)) { + curl_close($ch); + return "Error:" . curl_error($ch); + } + + // Close cURL session + curl_close($ch); + + // Return responses + return [$response1, $response2]; } + elseif($this->ReadPropertyInteger("Ladestation")==3){ + $url2 = $baseUrl . $value*1000; curl_setopt($ch, CURLOPT_URL, $url2); + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyInteger("Username") . ":" . $$this->ReadPropertyInteger("Password")); $response2 = curl_exec($ch); // Check for errors @@ -319,6 +408,9 @@ class Ladestation_Universal extends IPSModule // Return responses return [$response1, $response2]; + + } + } // If value is out of range diff --git a/library.json b/library.json index a453d4e..9e47d76 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "compatibility": { "version": "7.1" }, - "version": "0.198", + "version": "0.199", "build": 0, "date": 0 } \ No newline at end of file