diff --git a/P/README.md b/Belevo_Server_K/README.md similarity index 100% rename from P/README.md rename to Belevo_Server_K/README.md diff --git a/Belevo_Server_K/form.json b/Belevo_Server_K/form.json new file mode 100644 index 0000000..1f2b53f --- /dev/null +++ b/Belevo_Server_K/form.json @@ -0,0 +1,67 @@ +{ + "elements":[ + { + "type":"Label", + "caption":"Influx Punkte" + }, + { + "type":"Select", + "name":"InfluxJaNein", + "caption":"Influx Aufzeichnen Ja oder Nein", + "options":[ + { + "caption":"Ja", + "value":true + }, + { + "caption":"Nein", + "value":false + } + ] + }, + { + "type":"ValidationTextBox", + "name":"Anlagenummer", + "caption":"Influxadresse", + "suffix":"", + "validate":"^An_\\d{4}$" + }, + { + "type":"ValidationTextBox", + "name":"Gerätenummer", + "caption":"Gerätenummer" + }, + { + "type":"ValidationTextBox", + "name":"Ortschaft", + "caption":"Ortschaft" + }, + { + "type":"List", + "name":"ZusatzVariablen", + "caption":"Zusätzliche Datenpunkte für Influxaufzeichnung", + "add":true, + "delete":true, + "columns":[ + { + "caption":"Influx Name", + "name":"Variablenname", + "width":"200px", + "add":"", + "edit":{ + "type":"ValidationTextBox" + } + }, + { + "caption":"Datenpunkt", + "name":"Variable", + "width":"300px", + "add":0, + "edit":{ + "type":"SelectVariable" + } + } + ] + } + ] + } \ No newline at end of file diff --git a/P/module.json b/Belevo_Server_K/module.json similarity index 68% rename from P/module.json rename to Belevo_Server_K/module.json index 443e2ee..f7e21a2 100644 --- a/P/module.json +++ b/Belevo_Server_K/module.json @@ -1,6 +1,6 @@ { - "id": "{5633D73B-C8F9-1CE4-BC17-47EAA5CE3240}", - "name": "P", + "id": "{C300EE71-B955-5ED4-7FE5-393B7030158B}", + "name": "Belevo_Server_K", "type": 3, "vendor": "Belevo AG", "aliases": [], diff --git a/Belevo_Server_K/module.php b/Belevo_Server_K/module.php new file mode 100644 index 0000000..e9f2fc4 --- /dev/null +++ b/Belevo_Server_K/module.php @@ -0,0 +1,269 @@ +RegisterPropertyString( + "BaseURL", + "https://brain.belevo.ch/storedata" + ); + $this->RegisterPropertyString("Anlagenummer", "0"); + $this->RegisterPropertyBoolean("InfluxJaNein", false); + + // JSON-String für Zusatzvariablen + $this->RegisterPropertyString("ZusatzVariablen", json_encode([])); // Bezeichnung der Liste + $this->RegisterPropertyString("Variable", "0"); // Datenpunkt kann im Syncom ausgewählt werden + $this->RegisterPropertyString("Variablenname", "0"); // Name für Influxaufzeichnung + $this->RegisterPropertyString("Gerätenummer", "0"); + $this->RegisterPropertyString("Ortschaft", "0"); + + $this->RegisterVariableInteger( + "Wolkenwarscheinlichkeit", + "Wolkenwarscheinlichkeit" + ); + $this->RegisterVariableInteger("Temperatur", "Temperatur"); + + // Timer registrieren + $this->RegisterTimer( + "Timer_Influx", + 0, + "IPS_RequestAction(" . $this->InstanceID . ', "GetAction", "");' + ); + } + + public function ApplyChanges() + { + parent::ApplyChanges(); + + // Holen Sie sich die Einstellung, ob Influx aktiviert ist + $InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein"); + + if ($InfluxJaNein) { + // Timer auf 5 Minuten setzen + $this->SetTimerInterval("Timer_Influx", 300000); // Alle 5 Minuten -> 5*60*1000=300000 + } else { + // Timer stoppen + $this->SetTimerInterval("Timer_Influx", 0); + } + } + + public function getWetter($bn, $pw, $loc) + { + // URL mit Parametern zusammenstellen + $url = + "https://brain.belevo.ch/v2wetter?loc=" . + urlencode($loc) . + "&nr=" . + urlencode($bn); + + // HTTP-Anfrage-Header einstellen + $headers = ["id: $pw"]; + + // cURL-Initialisierung + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + // HTTP-Anfrage ausführen und Antwort erhalten + $response = curl_exec($ch); + curl_close($ch); + + // Antwort in ein Array dekodieren + $data = json_decode($response, true); + + // Überprüfen, ob die benötigten Felder vorhanden sind und zurückgeben + if ( + isset($data["forecast"]["forecastday"][0]["hour"][5]["temp_c"]) && + isset($data["forecast"]["forecastday"][0]["hour"][8]["cloud"]) + ) { + return [ + "temp" => + $data["forecast"]["forecastday"][0]["hour"][5]["temp_c"], + "cloud" => + $data["forecast"]["forecastday"][0]["hour"][8]["cloud"], + ]; + } + + // Fehlerbehandlung + return null; + } + + private function ProcessZusatzVariablen() + { + // Abrufen der ZusatzVariablen-Liste + $zusatzVariablen = json_decode( + $this->ReadPropertyString("ZusatzVariablen"), + true + ); // JSON decodieren + + // Array für die Ausgabe erstellen + $output = []; + + // Verarbeitung der Variablen + if (!empty($zusatzVariablen)) { + foreach ($zusatzVariablen as $variable) { + // Überprüfen, ob der Variablenname gesetzt ist + if (isset($variable["Variablenname"])) { + $variablenname = $variable["Variablenname"]; + } else { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Variablenname nicht gesetzt für die Variable: " . + json_encode($variable) + ); + continue; // Mit der nächsten Variable fortfahren + } + + $variableID = $variable["Variable"]; + + // Überprüfen, ob die Variable existiert + if (IPS_VariableExists($variableID)) { + $wert = GetValue($variableID); // Den aktuellen Wert der Zusatzvariable abrufen + + // Wert dem Variablenname zuweisen + $output[$variablenname] = $wert; + } else { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Variable mit ID $variableID existiert nicht." + ); + } + } + } + + // Wenn gewünscht, kannst du das JSON zurückgeben oder speichern + return json_encode($output); + } + + public function RequestAction($Ident, $Value) + { + switch ($Ident) { + case "GetAction": + $this->GetAction(); + break; + default: + throw new Exception("Invalid action"); + } + } + + public function GetAction() + { + $output = $this->ProcessZusatzVariablen(); + $json = $this->MakeJson($output); + // Verarbeite die Zusatzvariablen + + $baseURL = $this->ReadPropertyString("BaseURL"); + + $anlagenummer = $this->ReadPropertyString("Anlagenummer"); + + $answer = $this->getWetter( + $anlagenummer, + $this->ReadPropertyString("Gerätenummer"), + $this->ReadPropertyString("Ortschaft") + ); + + //$this->SetValue("Temperatur", $answer['temp']); + //$this->SetValue("Wolkenwarscheinlichkeit", $answer['cloud']); + if (isset($answer["temp"])) { + $this->SetValue("Temperatur", $answer["temp"]); + } else { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Temperatur-Wert ist nicht vorhanden." + ); + } + + if (isset($answer["cloud"])) { + $this->SetValue("Wolkenwarscheinlichkeit", $answer["cloud"]); + } else { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Wolkenwarscheinlichkeit-Wert ist nicht vorhanden." + ); + } + + if (!empty($json)) { + // Kombiniere die URL mit der Anlagenummer + $fullURL = $baseURL; + $this->SendJsonToInfluxDB($fullURL, $json); + } else { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Keine Aufzeichnung im Influx: Anlagenummer oder JSON-Daten fehlen" + ); + } + } + + public function MakeJson($json) + { + $an_nummer = $this->ReadPropertyString("Anlagenummer"); // Anlagenummer lesen + $InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein"); + + // Werte in ein Array packen + $influxData = [ + "Value" => [ + "InfluxAllowed" => $InfluxJaNein, // Setze InfluxAllowed + ], + "Parameter" => [ + "Influxadr" => $an_nummer, + ], + "Tracker" => $json, // Initialisiere Tracker als leeres Array + ]; + + // Array in JSON konvertieren + $json = json_encode($influxData); + + // JSON zurückgeben oder weiterverarbeiten + return $json; + } + + private function SendJsonToInfluxDB($url, $jsonData) + { + $pw = $this->ReadPropertyString("Gerätenummer"); + if (empty($pw)) { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Fehler: Gerätenummer ist leer." + ); + return; + } + + // cURL Initialisieren + $curl = curl_init(); + // Optionen für cURL-Request definieren + curl_setopt_array($curl, [ + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, // POST-Methode verwenden + CURLOPT_HTTPHEADER => [ + "Content-Type: application/json", // Header für JSON-Daten setzen + "Accept: application/json", + "id:" . $pw, + ], + CURLOPT_POSTFIELDS => $jsonData, // JSON-Daten als POST-Feld senden + ]); + + // cURL-Request ausführen + $result = curl_exec($curl); + + $error = curl_error($curl); + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + // cURL beenden + curl_close($curl); + + if ($error) { + IPS_LogMessage( + "Belevo_Server_Kommunikation", + "Fehler beim Senden an Influx: " . $error + ); + } + } +} + +?> diff --git a/P/form.json b/P/form.json deleted file mode 100644 index 604dc9d..0000000 --- a/P/form.json +++ /dev/null @@ -1,4 +0,0 @@ - -{ - -} diff --git a/P/module.php b/P/module.php deleted file mode 100644 index 05e74e6..0000000 --- a/P/module.php +++ /dev/null @@ -1,20 +0,0 @@ - -