Add files via upload

This commit is contained in:
J.H. Artworks
2024-01-11 13:41:40 +01:00
committed by GitHub
parent 0f2a072c1f
commit c34dc87a52
4 changed files with 349 additions and 69 deletions
+5 -2
View File
@@ -1,7 +1,10 @@
{ {
"elements": "elements":
[ [
{ "type": "SelectCategory", "name": "ParseCategoryID", "caption": "Target" }, { "type": "SelectCategory", "name": "ParseNotifyCategoryID", "caption": "Notify Target ID" },
{ "type": "SelectCategory", "name": "ParseAlarmCategoryID", "caption": "Alarm Target ID" },
{ "type": "SelectCategory", "name": "ParseAnalogCategoryID", "caption": "Analog Target ID" },
{ "type": "SelectObject", "name": "MqttCLientID", "caption": "MQTT-Client" }, { "type": "SelectObject", "name": "MqttCLientID", "caption": "MQTT-Client" },
{ "type": "NumberSpinner", "name": "Projectyear", "caption": "Projectyear" }, { "type": "NumberSpinner", "name": "Projectyear", "caption": "Projectyear" },
{ "type": "ValidationTextBox", "name": "Projectnumber", "caption": "Projectnumber" }, { "type": "ValidationTextBox", "name": "Projectnumber", "caption": "Projectnumber" },
@@ -12,7 +15,7 @@
], ],
"actions": "actions":
[ [
{ "type": "Button", "label": "Force Send", "onClick": "SEMC_ForceSendTopic($id);" }
], ],
"status": "status":
[ [
+83 -24
View File
@@ -6,7 +6,10 @@ class MonitoringClient extends IPSModule {
// Diese Zeile nicht löschen. // Diese Zeile nicht löschen.
parent::Create(); parent::Create();
$this->RegisterPropertyInteger("ParseCategoryID","0"); $this->RegisterPropertyInteger("ParseNotifyCategoryID","0");
$this->RegisterPropertyInteger("ParseAlarmCategoryID","0");
$this->RegisterPropertyInteger("ParseAnalogCategoryID","0");
$this->RegisterPropertyInteger("MqttCLientID","0"); $this->RegisterPropertyInteger("MqttCLientID","0");
$this->RegisterPropertyString("Projectnumber",""); $this->RegisterPropertyString("Projectnumber","");
$this->RegisterPropertyString("Projectname",""); $this->RegisterPropertyString("Projectname","");
@@ -113,44 +116,100 @@ class MonitoringClient extends IPSModule {
} // MQTT_Publish } // MQTT_Publish
public function SendTopic() { public function SendTopic() {
$mqttId = $this->ReadPropertyInteger("MqttCLientID");
$projectyear = $this->ReadPropertyInteger("Projectyear");
$projectnumber = $this->ReadPropertyString("Projectnumber");
$projectname = $this->ReadPropertyString("Projectname");
$ispnumber = $this->ReadPropertyInteger("ISP");
$updatetime = $this->ReadPropertyInteger("Updatetime");
// Discord webhook URL $catIds[0]["id"] = $this->ReadPropertyInteger("ParseNotifyCategoryID");
$catId = $this->ReadPropertyInteger("ParseCategoryID"); $catIds[0]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Notify/";
$mqttId = $this->ReadPropertyInteger("MqttCLientID"); $catIds[1]["id"] = $this->ReadPropertyInteger("ParseAlarmCategoryID");
$projectyear = $this->ReadPropertyInteger("Projectyear"); $catIds[1]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Alarm/";
$projectnumber = $this->ReadPropertyString("Projectnumber"); $catIds[2]["id"] = $this->ReadPropertyInteger("ParseAnalogCategoryID");
$projectname = $this->ReadPropertyString("Projectname"); $catIds[2]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Analog/";
$ispnumber = $this->ReadPropertyInteger("ISP");
$updatetime = $this->ReadPropertyInteger("Updatetime");
$catChilds = IPS_GetChildrenIDs($catId);
foreach ($catChilds as $catChild) {
$childids = IPS_GetChildrenIDs($catChild); foreach ($catIds as $catId){
$parname = IPS_GetName($catChild); $catChilds = IPS_GetChildrenIDs($catId["id"]);
foreach ($childids as $childid){ foreach ($catChilds as $catChild) {
$childids = IPS_GetChildrenIDs($catChild);
$parname = IPS_GetName($catChild);
foreach ($childids as $childid){
$varInfo = IPS_GetVariable($childid);
$changedtime = $varInfo["VariableChanged"];
$varname = IPS_GetName($childid);
$topic = $catId["top"]. $parname."_".$varname;
$time = time();
$payload = round(getvalue($childid), 2);
if($changedtime > time() - $updatetime){
SEMC_MqttPublish($this->InstanceID,$mqttId, $topic, $payload, false);
}
}
$varInfo = IPS_GetVariable($childid);
$changedtime = $varInfo["VariableChanged"];
$varname = IPS_GetName($childid);
$topic = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/". $parname."_".$varname;
$time = time();
$payload = round(getvalue($childid), 2);
if($changedtime > time() - $updatetime){
SEMC_MqttPublish($this->InstanceID,$mqttId, $topic, $payload, false);
} }
}
}
} public function ForceSendTopic() {
$mqttId = $this->ReadPropertyInteger("MqttCLientID");
$projectyear = $this->ReadPropertyInteger("Projectyear");
$projectnumber = $this->ReadPropertyString("Projectnumber");
$projectname = $this->ReadPropertyString("Projectname");
$ispnumber = $this->ReadPropertyInteger("ISP");
$updatetime = $this->ReadPropertyInteger("Updatetime");
$catIds[0]["id"] = $this->ReadPropertyInteger("ParseNotifyCategoryID");
$catIds[0]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Notify/";
$catIds[1]["id"] = $this->ReadPropertyInteger("ParseAlarmCategoryID");
$catIds[1]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Alarm/";
$catIds[2]["id"] = $this->ReadPropertyInteger("ParseAnalogCategoryID");
$catIds[2]["top"] = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Analog/";
foreach ($catIds as $catId){
$catChilds = IPS_GetChildrenIDs($catId["id"]);
foreach ($catChilds as $catChild) {
$childids = IPS_GetChildrenIDs($catChild);
$parname = IPS_GetName($catChild);
foreach ($childids as $childid){
$varname = IPS_GetName($childid);
$topic = $catId["top"]. $parname."_".$varname;
$payload = round(getvalue($childid), 2);
SEMC_MqttPublish($this->InstanceID,$mqttId, $topic, $payload, false);
}
}
} }
$topic = "Projekte". $projectyear."/".$projectnumber. "/ISP" .$ispnumber. "/Name";
SEMC_MqttPublish($this->InstanceID,$mqttId, $topic, $projectname, false);
} }
} }
?> ?>
+27 -4
View File
@@ -1,14 +1,37 @@
{ {
"elements": "elements":
[ [
{ "type": "ValidationTextBox", "name": "DiscordWsUrl", "caption": "Discord Websocket URL" } { "type": "SelectCategory", "name": "ParseNotifyCategoryID", "caption": "Digital-Objects Target ID" },
{ "type": "SelectCategory", "name": "ParseAlarmCategoryID", "caption": "Alarm-Objects Target ID" },
{ "type": "SelectCategory", "name": "ParseAnalogCategoryID", "caption": "Analog-Objects Target ID" },
{ "type": "NumberSpinner", "name": "Projectnumber", "caption": "Projectnumber without leading P Character" },
{ "type": "ValidationTextBox", "name": "Projectname", "caption": "Projectname" },
{ "type": "ValidationTextBox", "name": "Projectlocation", "caption": "Projectlocation" },
{ "type": "ValidationTextBox", "name": "Projecttags", "caption": "Projecttags" },
{ "type": "NumberSpinner", "name": "ISP", "caption": "ISP-Number" },
{ "type": "SelectInstance", "name": "VisuId", "caption": "Web Visu" },
{ "type": "CheckBox", "name": "SendNotification", "caption": "Send Notification" },
{ "type": "CheckBox", "name": "LogValues", "caption": "Log Values" },
{ "type": "Select", "name": "SslHttp", "caption": "SSL ?",
"options": [
{ "caption": "Normal", "value": "http://" },
{ "caption": "SSL", "value": "https://" }
]
},
{ "type": "ValidationTextBox", "name": "InfluxServer", "caption": "Influx Server" },
{ "type": "NumberSpinner", "name": "InfluxPort", "caption": "Influx Port" },
{ "type": "ValidationTextBox", "name": "InfluxToken", "caption": "Influx API Token" },
{ "type": "ValidationTextBox", "name": "InfluxOrg", "caption": "Influx Org." },
{ "type": "NumberSpinner", "name": "UpdateintervallValues", "caption": "Updateintervall Values in Seconds" },
{ "type": "NumberSpinner", "name": "UpdateintervallAlarms", "caption": "Updateintervall Alarms in Seconds" }
], ],
"actions": "actions":
[ [
{ "type": "ValidationTextBox", "name": "Title", "caption": "Title / User" }, { "type": "Button", "label": "Check Connection", "onClick": "SEMS_checkInfluxState($id);" }
{ "type": "ValidationTextBox", "name": "Message", "caption": "Message" },
{ "type": "Button", "label": "Send Message", "onClick": "DWM_SendMessage($id, $Title, $Message);" }
], ],
"status": "status":
[ [
+230 -35
View File
@@ -1,16 +1,66 @@
<? <?
declare(strict_types=1);
include_once __DIR__ . '/../libs/vendor/autoload.php';
use InfluxDB2\Model\WritePrecision;
// Klassendefinition // Klassendefinition
class DiscordMessage extends IPSModule { class MonitoringServer extends IPSModule {
// Überschreibt die interne IPS_Create($id) Funktion // Überschreibt die interne IPS_Create($id) Funktion
public function Create() { public function Create() {
// Diese Zeile nicht löschen. // Diese Zeile nicht löschen.
parent::Create(); parent::Create();
$this->RegisterPropertyString("DiscordWsUrl",""); $this->RegisterPropertyInteger("ParseNotifyCategoryID","0");
$this->RegisterPropertyInteger("ParseAlarmCategoryID","0");
$this->RegisterPropertyInteger("ParseAnalogCategoryID","0");
$this->RegisterPropertyInteger("ISP","1");
$this->RegisterPropertyInteger("VisuId","0");
$this->RegisterPropertyInteger("Projectnumber",123456);
$this->RegisterPropertyString("Projectname","Musterprojekt");
$this->RegisterPropertyString("Projectlocation","Urbach");
$this->RegisterPropertyString("Projecttags","street=Raiffeisenstrasse streetnumber=17");
$this->RegisterPropertyBoolean("SendNotification","false");
$this->RegisterPropertyBoolean("LogValues","true");
$this->RegisterPropertyString("SslHttp","http://");
$this->RegisterPropertyString("InfluxServer","172.16.9.106");
$this->RegisterPropertyInteger("InfluxPort",8086);
$this->RegisterPropertyString("InfluxToken","jTafRGaUcOb8bsNf031VCLwuU_Y00HpXRn1We6ufhl67JRSmFAtQ9GShplR9zFbGx6d2kmednMD7Lp5BpihQLw==");
$this->RegisterPropertyString("InfluxOrg","SE-Inno");
$this->RegisterPropertyInteger("UpdateintervallValues","30");
$this->RegisterPropertyInteger("UpdateintervallAlarms","10");
$this->RegisterTimer("UpdateAlarms", 0, 'SEMS_checkAlarms('.$this->InstanceID.');');
$this->RegisterTimer("UpdateValues", 0, 'SEMS_checkValues('.$this->InstanceID.');');
$this->RegisterVariableInteger("AlarmVarCount", "Number of Alarmvalues", "", 0);
$this->RegisterVariableInteger("AlarmActiveCount", "Number of Values in Alarmstate", "", 1);
$this->RegisterVariableInteger("AnalogVarCount", "Number of Analogvalues", "", 2);
$this->RegisterVariableInteger("DigitalVarCount", "Number of DigitalValues", "", 3);
$this->RegisterVariableString("InfluxState", "Influx-DB State", "", 4);
$this->RegisterVariableString("InfluxMessage", "Influx-DB Message", "", 5);
} }
// Überschreibt die intere IPS_ApplyChanges($id) Funktion // Überschreibt die intere IPS_ApplyChanges($id) Funktion
public function ApplyChanges() { public function ApplyChanges() {
$this->SetTimerInterval("UpdateAlarms", $this->ReadPropertyInteger("UpdateintervallAlarms") * 1000);
$this->SetTimerInterval("UpdateValues", $this->ReadPropertyInteger("UpdateintervallValues") * 1000);
// Diese Zeile nicht löschen // Diese Zeile nicht löschen
parent::ApplyChanges(); parent::ApplyChanges();
} }
@@ -18,50 +68,195 @@ class DiscordMessage extends IPSModule {
* Die folgenden Funktionen stehen automatisch zur Verfügung, wenn das Modul über die "Module Control" eingefügt wurden. * Die folgenden Funktionen stehen automatisch zur Verfügung, wenn das Modul über die "Module Control" eingefügt wurden.
* Die Funktionen werden, mit dem selbst eingerichteten Prefix, in PHP und JSON-RPC wiefolgt zur Verfügung gestellt: * Die Funktionen werden, mit dem selbst eingerichteten Prefix, in PHP und JSON-RPC wiefolgt zur Verfügung gestellt:
* *
* DWM_SendMessage($id);
* *
*/ */
public function SendMessage($topic,$message) {
// Discord webhook URL public $client = "";
$webhookUrl = $this->ReadPropertyString("DiscordWsUrl"); public $writeApi = "";
public $state = "";
public function connectInflux(){
// Data to send as JSON $ssl = $this->ReadPropertyString("SslHttp");
$data = array( $server = $this->ReadPropertyString("InfluxServer");
"username" => $topic, $port = $this->ReadPropertyInteger("InfluxPort");
"content" => $message $token = $this->ReadPropertyString("InfluxToken");
); $org = $this->ReadPropertyString("InfluxOrg");
$projectnumber = $this->ReadPropertyInteger("Projectnumber");
$ispnumber = $this->ReadPropertyInteger("ISP");
// Encode the data as JSON
$jsonData = json_encode($data);
// Initialize cURL session $this->client = new InfluxDB2\Client([
$ch = curl_init($webhookUrl); "url" => $ssl.$server.":".$port,
"token" => $token,
"bucket" => "P".$projectnumber."_ISP".$ispnumber,
"org" => $org,
"precision" => InfluxDB2\Model\WritePrecision::S
]);
$this->writeApi = $this->client->createWriteApi();
// Set cURL options $this->state = $this->client->health();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($jsonData)
));
// Execute the cURL session and capture the response SetValueString($this->GetIDForIdent("InfluxState"),$this->state["status"]);
$response = curl_exec($ch); SetValueString($this->GetIDForIdent("InfluxMessage"),$this->state["message"]);
// Check for cURL errors }
if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch);
public function checkInfluxState(){
$this->connectInflux();
print_r($this->client->health());
}
public function checkAlarms() {
$catAlarmId = $this->ReadPropertyInteger("ParseAlarmCategoryID");
$visuId = $this->ReadPropertyInteger("VisuId");
$projectnumber = $this->ReadPropertyInteger("Projectnumber");
$projectname = $this->ReadPropertyString("Projectname");
$ispnumber = $this->ReadPropertyInteger("ISP");
$sendit = $this->ReadPropertyBoolean("SendNotification");
$inalarmcount = 0;
$alarmvalues = 0;
$catChilds = IPS_GetChildrenIDs($catAlarmId);
if ($catAlarmId > 0){
foreach ($catChilds as $catChild) {
$childids = IPS_GetChildrenIDs($catChild);
$parname = IPS_GetName($catChild);
foreach ($childids as $childid){
$varInfo = IPS_GetVariable($childid);
$changedtime = $varInfo["VariableChanged"];
$varname = IPS_GetName($childid);
$time = time();
$payload = GetValueBoolean($childid);
$alarmvalues++;
if ($sendit == true){
if ($this->notify($childid, $visuId, $catAlarmId, $projectnumber, $projectname, $ispnumber) == true){
$inalarmcount++;
}
}
}
}
}
SetValueInteger($this->GetIDForIdent("AlarmVarCount"),$alarmvalues);
SetValueInteger($this->GetIDForIdent("AlarmActiveCount"),$inalarmcount);
$rootcat = IPS_GetParent($catAlarmId);
if ($inalarmcount > 0 && $rootcat >0){
IPS_SetIcon($rootcat,"Alert");
}else{
IPS_SetIcon($rootcat,"");
}
}
public function checkValues() {
$catNotifyId = $this->ReadPropertyInteger("ParseNotifyCategoryID");
$catAnalogId = $this->ReadPropertyInteger("ParseAnalogCategoryID");
$projectnumber = $this->ReadPropertyInteger("Projectnumber");
$projectname = $this->ReadPropertyString("Projectname");
$projectlocation = $this->ReadPropertyString("Projectlocation");
$projecttags = $this->ReadPropertyString("Projecttags");
$updatetime = $this->ReadPropertyInteger("UpdateintervallValues");
$ispnumber = $this->ReadPropertyInteger("ISP");
$org = $this->ReadPropertyString("InfluxOrg");
$logit = $this->ReadPropertyBoolean("LogValues");
$catChilds = IPS_GetChildrenIDs($catAnalogId);
$numberofvalues = 0;
$this->connectInflux();
if ($catAnalogId > 0){
foreach ($catChilds as $catChild) { //for each object in category
$objchildids = IPS_GetChildrenIDs($catChild); //get variables of objects
$parname = IPS_GetName($catChild);
foreach ($objchildids as $objchildid){
$varInfo = IPS_GetVariable($objchildid);
$changedtime = $varInfo["VariableChanged"];
$varname = IPS_GetName($objchildid);
$time = time();
$payload = GetValue($objchildid);
if(is_float($payload)) {
$payload = round($payload, 2);
}
$numberofvalues++;
if($changedtime > $time - $updatetime){
$parname = str_replace(" ","", $parname);
$varname = str_replace(" ","", $varname);
$dataArray = [
'name' => $parname."_".$varname,
'tags' => ["projectname=".$projectname." location=".$projectlocation." ".$projecttags],
'fields' => ["value" => $payload],
'time' => $time
];
if($this->state["status"] == "pass"){
$this->writeApi->write($dataArray, WritePrecision::S, "P".$projectnumber."_ISP".$ispnumber, $org);
}
}
}
}
}
SetValueInteger($this->GetIDForIdent("AnalogVarCount"),$numberofvalues);
}
private function notify($trigid, $webfrontid, $targetid, $projectnumber, $projectname, $ispnr){
$art = $projectnumber." | ".$projectname. " | ISP ".$ispnr;
$ico = "Flame";
$smname = IPS_GetName(IPS_GetParent($trigid))."_".IPS_GetName($trigid);
if (GetValueBoolean($trigid) == true && ($this->GetBuffer($smname) == "true")) {
$this->SetBuffer($smname, "false");
VISU_PostNotification($webfrontid, $art, $smname, $ico, $targetid);
WFC_PushNotification($webfrontid, $art, $smname, "", $targetid);
}
elseif (GetValueBoolean($trigid) == false) {
$this->SetBuffer($smname, "true");
} }
// Close cURL session if (GetValueBoolean($trigid) == true){
curl_close($ch); return true;
}else{
// Display the response from the webhook return false;
///echo "Webhook Response: " . $response; }
} }
} }
?> ?>