Fehlerbehebung Server senden

This commit is contained in:
belevo\mh
2024-10-16 07:48:48 +02:00
parent 085c8a8567
commit df18b2d7c6
6 changed files with 80 additions and 26 deletions

View File

@@ -11,7 +11,7 @@ class Belevo_Server_Kommunikation extends IPSModule
parent::Create();
//Netzbezug
$this->RegisterPropertyFloat("E_PNB_5M_0", 0);
$this->RegisterVariableFloat("Netzbezug", "Netzbezug", "",0);
$this->RegisterPropertyFloat("Netzbezug", "", "",0);
/*//Boilerstatus
$this->RegisterPropertyInteger("G_BS_5M_0", 0);
$this->RegisterVariableInteger("Boilerstatus", 0);
@@ -41,6 +41,7 @@ class Belevo_Server_Kommunikation extends IPSModule
$this->RegisterPropertyString("BaseURL","https://brain.belevo.ch/status?nr=");
$this->RegisterPropertyString("Anlagenummer",0);
$this->RegisterPropertyBoolean("InfluxJaNein", false);
$this->RegisterTimer("Timer_Influx", 0, 'IPS_RequestAction(' . $this->InstanceID . ', "GetAction", "");');// 5min = 1000 * 60 * 5 = 300'000ms
@@ -52,10 +53,12 @@ class Belevo_Server_Kommunikation extends IPSModule
$InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein");
if ($InfluxJaNein) {
// Timer auf 5 Minuten setzen
$this->SetTimerInterval("Timer_Influx", 300000);
$this->SetTimerInterval("Timer_Influx", 10000);//Alle 10 Sekunden
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Ja");
} else {
// Timer stoppen
$this->SetTimerInterval("Timer_Influx", 0);
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Nein");
}
}
@@ -72,22 +75,19 @@ class Belevo_Server_Kommunikation extends IPSModule
}
}
public function GetAction() {
$jaNeinAuswahl = $this->ReadPropertyBoolean("JaNeinAuswahl");
if ($jaNeinAuswahl) {
public function GetAction() {
$json = $this->MakeJson();
$baseURL = $this->ReadPropertyString("BaseURL");
$anlagenummer = $this->ReadPropertyString("Anlagenummer");
// Kombiniere die URL mit der Anlagenummer
$fullURL = $baseURL . $anlagenummer;
$this->SendJsonToInfluxDB($fullURL, $json);
} else {
IPS_LogMessage("Belevo_Server_Kommunikation", "Keine Aufzeichnung im Influx");
if (!empty($anlagenummer) && !empty($json)) {
// Kombiniere die URL mit der Anlagenummer
$fullURL = $baseURL . $anlagenummer;
$this->SendJsonToInfluxDB($fullURL, $json);
} else {
IPS_LogMessage("Belevo_Server_Kommunikation", "Keine Aufzeichnung im Influx: Anlagenummer oder JSON-Daten fehlen->GetAction()");
}
}
}
public function MakeJson() {
@@ -106,12 +106,12 @@ $this->SetValue("E_PNB_5M_0", $netzbezug);
// Werte in ein Array packen
$data = [
"_measurement" => $this->ReadPropertyString("Anlagenummer"),
"_field" =>[
"measurement" => $this->ReadPropertyString("Anlagenummer"),
"field" =>[
//"G_BT_5M_0" => $this->GetValue("G_BT_5M_0"),
//"G_SK_5M_3" => $this->GetValue("G_SK_5M_3"),
//"G_SK_5M_4" => $this->GetValue("G_SK_5M_4"),
"E_PNB_5M_0" => $this->GetValue("E_PNB_5M_0"),
"E_PNB_5M_0" => $netzbezug,
]
];
@@ -126,13 +126,19 @@ return $json;
private function SendJsonToInfluxDB($url, $json)
{
$token = "iFNTONC5UvST8GT5itWqxiO6m4O1yHt5hus54J8eqotk5IZiq0Wjd0_Xi6dozmo32QR3esJqk6hJAvJ8X2SxtQ==";
// $token = "Token iFNTONC5UvST8GT5itWqxiO6m4O1yHt5hus54J8eqotk5IZiq0Wjd0_Xi6dozmo32QR3esJqk6hJAvJ8X2SxtQ==";
/*
if (empty($token)) {
IPS_LogMessage("Belevo_Server_Kommunikation", "InfluxDB-Token fehlt->SendJsonToInfluxDB");
return;
}*/
$options = [
'http' => [
'header' => "Content-Type: application/json\r\n".
"Authorization: Bearer $token\r\n",
"daten: $json".
"id: test\r\n",
'method' => 'POST',
'content' => $json,
],
];
$context = stream_context_create($options);
@@ -140,9 +146,9 @@ return $json;
if ($result === FALSE) {
IPS_LogMessage("Belevo_Server_Kommunikation", "Fehler beim senden an Influx");
IPS_LogMessage("Belevo_Server_Kommunikation", "Fehler beim senden an Influx->SendJsonToInfluxDB()");
} else {
IPS_LogMessage("Belevo_Server_Kommunikation", "Daten erfolgreich an Influx gesendet");
IPS_LogMessage("Belevo_Server_Kommunikation", "Daten erfolgreich an Influx gesendet->SendJsonToInfluxDB()");
}
}