272 lines
8.7 KiB
PHP
272 lines
8.7 KiB
PHP
<?php
|
|
|
|
class Belevo_Server_Kommunikation extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
// Die Standard-Create() Methode aufrufen
|
|
parent::Create();
|
|
|
|
// Registrierung der Eigenschaften
|
|
$this->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
|
|
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Ja");
|
|
} else {
|
|
// Timer stoppen
|
|
$this->SetTimerInterval("Timer_Influx", 0);
|
|
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Nein");
|
|
}
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|