178 lines
5.7 KiB
PHP
178 lines
5.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", "http://192.168.20.140:5000/influx");
|
|
$this->RegisterPropertyString("Anlagenummer", "0");
|
|
$this->RegisterPropertyBoolean("InfluxJaNein", false);
|
|
|
|
// JSON-String für Zusatzvariablen
|
|
$this->RegisterPropertyString("ZusatzVariablen", json_encode([]));
|
|
$this->RegisterPropertyString("Variable","0");
|
|
$this->RegisterPropertyString("Variablenname","0");
|
|
|
|
// 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", 5000); // Alle 5 Sekunden
|
|
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Ja");
|
|
|
|
|
|
|
|
} else {
|
|
// Timer stoppen
|
|
$this->SetTimerInterval("Timer_Influx", 0);
|
|
IPS_LogMessage("Belevo_Server_Kommunikation", "Influx Nein");
|
|
}
|
|
}
|
|
|
|
private function ProcessZusatzVariablen()
|
|
{
|
|
// Abrufen der ZusatzVariablen-Liste
|
|
$zusatzVariablen = json_decode($this->ReadPropertyString("ZusatzVariablen"), true); // JSON decodieren
|
|
|
|
// Array für die Ausgabe erstellen
|
|
$output = array();
|
|
|
|
// 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); // Den aktuellen Wert der Zusatzvariable abrufen
|
|
IPS_LogMessage("ZusatzVariable", "Name: $variablenname, ID: $variableID, Wert: $wert");
|
|
|
|
// Wert dem Variablenname zuweisen
|
|
$output[$variablenname] = $wert;
|
|
} else {
|
|
IPS_LogMessage("ZusatzVariable", "Variable mit ID $variableID existiert nicht.");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Optional: Ausgabe im JSON-Format protokollieren
|
|
IPS_LogMessage("OutputJSON", json_encode($output));
|
|
|
|
// 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");
|
|
|
|
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()");
|
|
}
|
|
}
|
|
|
|
// Werte in ein Array packen
|
|
public function MakeJson($a)
|
|
{
|
|
$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" => $a // Initialisiere Tracker als leeres Array
|
|
);
|
|
|
|
|
|
|
|
// Array in JSON konvertieren
|
|
$json = json_encode($influxData);
|
|
IPS_LogMessage("MakeJson", "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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|