Refactoring
This commit is contained in:
@@ -1,62 +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"
|
||||
}
|
||||
"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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"translations": {
|
||||
"de": {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,128 +2,146 @@
|
||||
|
||||
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(
|
||||
"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");
|
||||
// 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", "");');
|
||||
$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) {
|
||||
|
||||
public function getWetter($bn, $pw, $loc)
|
||||
{
|
||||
// URL mit Parametern zusammenstellen
|
||||
$url = "https://brain.belevo.ch/v2wetter?loc=" . urlencode($loc) . "&nr=" . urlencode($bn);
|
||||
|
||||
$url =
|
||||
"https://brain.belevo.ch/v2wetter?loc=" .
|
||||
urlencode($loc) .
|
||||
"&nr=" .
|
||||
urlencode($bn);
|
||||
|
||||
// HTTP-Anfrage-Header einstellen
|
||||
$headers = [
|
||||
"id: $pw"
|
||||
];
|
||||
|
||||
$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'])) {
|
||||
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']
|
||||
"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
|
||||
|
||||
$zusatzVariablen = json_decode(
|
||||
$this->ReadPropertyString("ZusatzVariablen"),
|
||||
true
|
||||
); // JSON decodieren
|
||||
|
||||
// Array für die Ausgabe erstellen
|
||||
$output = array();
|
||||
|
||||
$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'];
|
||||
if (isset($variable["Variablenname"])) {
|
||||
$variablenname = $variable["Variablenname"];
|
||||
} else {
|
||||
IPS_LogMessage("Belevo_Server_Kommunikation", "Variablenname nicht gesetzt für die Variable: " . json_encode($variable));
|
||||
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'];
|
||||
|
||||
|
||||
$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.");
|
||||
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)
|
||||
{
|
||||
@@ -136,29 +154,40 @@ class Belevo_Server_Kommunikation extends IPSModule
|
||||
}
|
||||
}
|
||||
|
||||
public function GetAction() {
|
||||
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"));
|
||||
|
||||
$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']);
|
||||
if (isset($answer["temp"])) {
|
||||
$this->SetValue("Temperatur", $answer["temp"]);
|
||||
} else {
|
||||
IPS_LogMessage("Belevo_Server_Kommunikation", "Temperatur-Wert ist nicht vorhanden.");
|
||||
IPS_LogMessage(
|
||||
"Belevo_Server_Kommunikation",
|
||||
"Temperatur-Wert ist nicht vorhanden."
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($answer['cloud'])) {
|
||||
$this->SetValue("Wolkenwarscheinlichkeit", $answer['cloud']);
|
||||
|
||||
if (isset($answer["cloud"])) {
|
||||
$this->SetValue("Wolkenwarscheinlichkeit", $answer["cloud"]);
|
||||
} else {
|
||||
IPS_LogMessage("Belevo_Server_Kommunikation", "Wolkenwarscheinlichkeit-Wert ist nicht vorhanden.");
|
||||
IPS_LogMessage(
|
||||
"Belevo_Server_Kommunikation",
|
||||
"Wolkenwarscheinlichkeit-Wert ist nicht vorhanden."
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($json)) {
|
||||
@@ -166,80 +195,77 @@ class Belevo_Server_Kommunikation extends IPSModule
|
||||
$fullURL = $baseURL;
|
||||
$this->SendJsonToInfluxDB($fullURL, $json);
|
||||
} else {
|
||||
IPS_LogMessage("Belevo_Server_Kommunikation", "Keine Aufzeichnung im Influx: Anlagenummer oder JSON-Daten fehlen->GetAction()");
|
||||
IPS_LogMessage(
|
||||
"Belevo_Server_Kommunikation",
|
||||
"Keine Aufzeichnung im Influx: Anlagenummer oder JSON-Daten fehlen"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Werte in ein Array packen
|
||||
public function MakeJson($json)
|
||||
{
|
||||
$an_nummer = $this->ReadPropertyString("Anlagenummer"); // Anlagenummer lesen
|
||||
$InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein");
|
||||
public function MakeJson($json)
|
||||
{
|
||||
$an_nummer = $this->ReadPropertyString("Anlagenummer"); // Anlagenummer lesen
|
||||
$InfluxJaNein = $this->ReadPropertyBoolean("InfluxJaNein");
|
||||
|
||||
// Werte in ein Array packen
|
||||
$influxData = array(
|
||||
"Value" => array(
|
||||
"InfluxAllowed" => $InfluxJaNein // Setze InfluxAllowed
|
||||
),
|
||||
"Parameter" => array(
|
||||
"Influxadr" => $an_nummer
|
||||
),
|
||||
"Tracker" => $json // Initialisiere Tracker als leeres Array
|
||||
);
|
||||
// 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);
|
||||
|
||||
|
||||
// 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;
|
||||
// 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 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);
|
||||
|
||||
// cURL-Request ausführen
|
||||
$result = curl_exec($curl);
|
||||
|
||||
|
||||
$error = curl_error($curl);
|
||||
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
// cURL beenden
|
||||
curl_close($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);
|
||||
}
|
||||
if ($error) {
|
||||
IPS_LogMessage(
|
||||
"Belevo_Server_Kommunikation",
|
||||
"Fehler beim Senden an Influx: " . $error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user