Files
Energiemanager_Symconmodule…/Belevo_Server_Kommunikation/module.php

223 lines
8.2 KiB
PHP

<?php
class Belevo_Server_Kommunikation extends IPSModule
{
public function Create()
{
// Zweite Gleiche Variable wird in der Benutzeroberfläche von Symcon angezeigt
//Variable: dynamische Daten
//Property: statische Konfigurationsdaten
parent::Create();
//Netzbezug
$this->RegisterPropertyFloat("E_PNB_5M_0", 0);
$this->RegisterPropertyInteger("Netzbezug", 0);
/*//Boilerstatus
$this->RegisterPropertyInteger("G_BS_5M_0", 0);
$this->RegisterPropertyInteger("Boilerstatus", 0);
//Boilertemperatur
$this->RegisterPropertyFloat("G_BT_5M_0",0); // Wird an Influx Server gesendet
$this->RegisterPropertyInteger("Boilertemperatur", 0); // Ist, Jetzige Boilertemperatur
//Schaltkontake des Boilers
$this->RegisterPropertyInteger("G_SK_5M_1",0);
$this->RegisterPropertyInteger("Schaltkontakt_1", 0);
$this->RegisterPropertyInteger("G_SK_5M_2",0);
$this->RegisterPropertyInteger("Schaltkontakt_2", 0);
//Schaltkontake des Boilers
$this->RegisterPropertyInteger("G_SK_5M_3",0);
$this->RegisterPropertyInteger("Kontakt_Teillast", 0);
$this->RegisterPropertyInteger("G_SK_5M_4",0);
$this->RegisterPropertyInteger("Kontakt_Volllast", 0);
//Wärmepumpe Status
$this->RegisterPropertyFloat("G_WPS_5M_0",0);
$this->RegisterPropertyInteger("Waermepumpe_Status", 0);
//Wolkenwahrscheinlichkeit
$this->RegisterPropertyFloat("G_WW_5M_0",0);
$this->RegisterPropertyInteger("Wolkenwahrscheinlichkeit", 0);
//Aussentemperatur
$this->RegisterPropertyFloat("G_AT_5M_0",0);
$this->RegisterPropertyInteger("Aussentemperatur", 0);
*/
//$this->RegisterPropertyString("BaseURL","https://brain.belevo.ch/status?nr=");
$this->RegisterPropertyString("BaseURL","http://192.168.20.140:5000/influx");
$this->RegisterPropertyString("Anlagenummer",0);
$this->RegisterPropertyBoolean("InfluxJaNein", false);
$this->RegisterPropertyArray("ZusatzVariablen",[]);
$this->RegisterTimer("Timer_Influx", 0, 'IPS_RequestAction(' . $this->InstanceID . ', "GetAction", "");');// 5min = 1000 * 60 * 5 = 300'000ms
}
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", 5000); // Alle 5 Sekunden
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Ja");
// Verarbeite die ZusatzVariablen
$this->ProcessZusatzVariablen();
} else {
// Timer stoppen
$this->SetTimerInterval("Timer_Influx", 0);
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Nein");
}
}
private function ProcessZusatzVariablen()
{
// Abrufen der ZusatzVariablen-Liste
$zusatzVariablen = $this->ReadPropertyArray("ZusatzVariablen");
// Verarbeitung der Variablen
if (!empty($zusatzVariablen)) {
foreach ($zusatzVariablen as $variable) {
$variablenname = $variable['Variablenname'];
$variableID = $variable['Variable'];
// Überprüfen, ob die Variable existiert
if (IPS_VariableExists($variableID)) {
$wert = GetValue($variableID);
IPS_LogMessage("ZusatzVariable", "Name: $variablenname, ID: $variableID, Wert: $wert");
// Hier können Sie den Wert in die InfluxDB schreiben oder weiterverarbeiten
// Beispiel: $this->WriteToInfluxDB($variablenname, $wert);
} else {
IPS_LogMessage("ZusatzVariable", "Variable mit ID $variableID existiert nicht.");
}
}
}
}
public function RequestAction($Ident, $Value)
{
switch ($Ident) {
case "GetAction":
$this->GetAction();
break;
default:
throw new Exception("Invalid action");
}
}
public function GetAction() {
$json = $this->MakeJson();
$baseURL = $this->ReadPropertyString("BaseURL");
$anlagenummer = $this->ReadPropertyString("Anlagenummer");
if (!empty($json)) {
// Kombiniere die URL mit der Anlagenummer
$fullURL = $baseURL;
IPS_LogMessage("Belevo_Server_Kommunikation", "URL: ".$fullURL);
IPS_LogMessage("Belevo_Server_Kommunikation", "JSON: " . $json);
$this->SendJsonToInfluxDB($fullURL, $json);
} else {
IPS_LogMessage("Belevo_Server_Kommunikation", "Keine Aufzeichnung im Influx: Anlagenummer oder JSON-Daten fehlen->GetAction()");
}
}
public function MakeJson() {
$netzbezug = GetValue($this->ReadPropertyInteger("Netzbezug"));
$an_nummer = $this->ReadPropertyString("Anlagenummer"); // Verwende ReadPropertyString, um die Anlagenummer zu lesen
$InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein");
//$this->SetValue("E_PNB_5M_0", $netzbezug);
// Werte in ein Array packen
public function MakeJson()
{
$an_nummer = $this->ReadPropertyString("Anlagenummer"); // Verwende ReadPropertyString, um die Anlagenummer zu lesen
$InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein");
// Werte in ein Array packen
$influxData = array(
"Value" => array(
"InfluxAllowed" => $InfluxJaNein // Setze InfluxAllowed auf true für den Test
),
"Parameter" => array(
"Influxadr" => $an_nummer
),
"Tracker" => array() // Initialisiere Tracker als leeres Array
);
// Zusatzvariablen aus den Instanzeigenschaften abrufen
$zusatzVariablen = $this->ReadPropertyArray("ZusatzVariablen");
// Protokolliere den Inhalt der Zusatzvariablen
IPS_LogMessage("MakeJson", "Zusatzvariablen Array: " . json_encode($zusatzVariablen));
// Dynamisch Zusatzvariablen zum Tracker hinzufügen
if (!empty($zusatzVariablen)) {
foreach ($zusatzVariablen as $variable) {
$variablenname = $variable['Variablenname'];
$variableID = $variable['Variable'];
// Überprüfen, ob die Variable existiert
if (IPS_VariableExists($variableID)) {
$wert = GetValue($variableID); // Den aktuellen Wert der Zusatzvariable abrufen
$influxData['Tracker'][$variablenname] = $wert; // Wert zum Tracker hinzufügen
} else {
IPS_LogMessage("MakeJson", "Variable mit ID $variableID existiert nicht.");
}
}
}
// Protokolliere den Inhalt des influxData-Arrays vor der JSON-Konvertierung
IPS_LogMessage("MakeJson", "InfluxData Array vor JSON-Konvertierung: " . json_encode($influxData));
// Array in JSON konvertieren
$json = json_encode($influxData);
IPS_LogMessage("Belevo_Server_Kommunikation", "Erstelltes JSON: " . $json);
// JSON zurückgeben oder weiterverarbeiten
return $json;
}
private function SendJsonToInfluxDB($url, $jsonData) {
// 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: test'
],
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);
} else {
IPS_LogMessage("Belevo_Server_Kommunikation", "HTTP-Status: " . $httpCode);
IPS_LogMessage("Belevo_Server_Kommunikation", "Antwort von Influx: " . $result);
}
}
}
?>