diff --git a/Monitoring Client/form.json b/Monitoring Client/form.json new file mode 100644 index 0000000..dbc06e1 --- /dev/null +++ b/Monitoring Client/form.json @@ -0,0 +1,20 @@ +{ + "elements": + [ + { "type": "SelectCategory", "name": "ParseCategoryID", "caption": "Target" }, + { "type": "SelectObject", "name": "MqttCLientID", "caption": "MQTT-Client" }, + { "type": "ValidationTextBox", "name": "Projectnumber", "caption": "Projectnumber" }, + { "type": "ValidationTextBox", "name": "Projectname", "caption": "Projectname" }, + { "type": "NumberSpinner", "name": "ISP", "caption": "ISP-Number" }, + { "type": "NumberSpinner", "name": "Updatetime", "caption": "Updatetime","suffix": "seconds","minimum ": "10" } + + ], + "actions": + [ + + ], + "status": + [ + + ] +} \ No newline at end of file diff --git a/Monitoring Client/module.json b/Monitoring Client/module.json new file mode 100644 index 0000000..bb61556 --- /dev/null +++ b/Monitoring Client/module.json @@ -0,0 +1,14 @@ +{ + "id": "{12369413-F10C-57D1-FB84-5BA243EFEE5C}", + "name": "Monitoring Client", + "type": 3, + "vendor": "J.H. Artworks", + "aliases": + [ + ], + "url": "https://jh-artworks.com", + "parentRequirements": [], + "childRequirements": [], + "implemented": [], + "prefix": "SEMC" +} \ No newline at end of file diff --git a/Monitoring Client/module.php b/Monitoring Client/module.php new file mode 100644 index 0000000..eadb29d --- /dev/null +++ b/Monitoring Client/module.php @@ -0,0 +1,154 @@ +RegisterPropertyInteger("ParseCategoryID","0"); + $this->RegisterPropertyInteger("MqttCLientID","0"); + $this->RegisterPropertyString("Projectnumber",""); + $this->RegisterPropertyString("Projectname",""); + $this->RegisterPropertyInteger("ISP","1"); + $this->RegisterPropertyInteger("Updatetime","20"); + $this->RegisterTimer("Update", 0, 'SEMC_SendTopic('.$this->InstanceID.');'); + + } + + // Überschreibt die intere IPS_ApplyChanges($id) Funktion + public function ApplyChanges() { + // Diese Zeile nicht löschen + parent::ApplyChanges(); + + $this->SetTimerInterval("Update", $this->ReadPropertyInteger("Updatetime") * 1000); + + } + /** + * 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: + * + * DWM_SendMessage($id); + * + */ + /* Publish a given payload over a MQTT server under + a given topic without having to manually create a + MQTT server device */ + public function MqttPublish($server_id, $topic, $payload, $retain) { + // ensure server instance exists + if(!IPS_InstanceExists($server_id)) { + return false; + } + + // convert array structure to json string + if(is_array($payload)) $payload = json_encode($payload); + + // determine data type + if(is_string($payload)) { + $ips_var_type = 3; + } else if(is_float($payload)) { + $ips_var_type = 2; + } else if(is_int($payload)) { + $ips_var_type = 1; + } else if(is_bool($payload)) { + $ips_var_type = 0; + } else { // unsupported + return false; + } + + $module_id = "{01C00ADD-D04E-452E-B66A-D253278743FE}" /* Module ID of MQTT Server Device */; + $ident = "TempMQTTDevice"; + + // enter semaphore to ensure the temporary device gets used by one thread at a time + if(IPS_SemaphoreEnter($ident, 100)) { + // get temporary MQTT Server Device or create if needed + $id = @IPS_GetObjectIDByIdent($ident, $_IPS['SELF']); + if($id === false) { + $id = @IPS_CreateInstance($module_id); + if($id === false) { + return false; + } + IPS_SetParent($id, $_IPS['SELF']); + IPS_SetIdent($id, $ident); + } + + // ensure the specified server instance is actually compatible + if(!IPS_IsInstanceCompatible($id, $server_id)) { + return false; + } + + // ensure that the temporary device is actually connected to the correct server instance + $inst_config = IPS_GetInstance($id); + if($inst_config["ConnectionID"] != $server_id) { + IPS_DisconnectInstance($id); + if(!@IPS_ConnectInstance($id, $server_id)) { + return false; + } + } + + // name object to help with debugging + IPS_SetName($id, "Temporary MQTT Server Device for topic " . $topic); + + // configure temporary device + $config_arr = array( + "Retain" => $retain, + "Topic" => $topic, + "Type" => $ips_var_type + ); + $config_str = json_encode($config_arr); + IPS_SetConfiguration($id, $config_str); + IPS_ApplyChanges($id); + + // get Value variable and use it to publish the payload + $var_id = @IPS_GetObjectIDByIdent("Value", $id); + RequestAction($var_id, $payload); + + IPS_SemaphoreLeave($ident); + } else { // semaphore timeout + return false; + } + + return true; + } // MQTT_Publish + + public function SendTopic() { + + // Discord webhook URL + $catId = $this->ReadPropertyInteger("ParseCategoryID"); + $mqttId = $this->ReadPropertyInteger("MqttCLientID"); + $projectnumber = $this->ReadPropertyString("Projectnumber"); + $projectname = $this->ReadPropertyString("Projectname"); + $ispnumber = $this->ReadPropertyInteger("ISP"); + $updatetime = $this->ReadPropertyInteger("Updatetime"); + + $catChilds = IPS_GetChildrenIDs($catId); + + 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 = $projectnumber. "/ISP" .$ispnumber. "/". $parname."_".$varname; + $time = time(); + $payload = getvalue($childid); + if($changedtime > time() - $updatetime){ + SEMC_MqttPublish($this->InstanceID,$mqttId, $topic, $payload, false); + } + + + } + + } + + } + + + +} +?> \ No newline at end of file diff --git a/Monitoring Server/form.json b/Monitoring Server/form.json new file mode 100644 index 0000000..2f0a48c --- /dev/null +++ b/Monitoring Server/form.json @@ -0,0 +1,17 @@ +{ + "elements": + [ + { "type": "ValidationTextBox", "name": "DiscordWsUrl", "caption": "Discord Websocket URL" } + + ], + "actions": + [ + { "type": "ValidationTextBox", "name": "Title", "caption": "Title / User" }, + { "type": "ValidationTextBox", "name": "Message", "caption": "Message" }, + { "type": "Button", "label": "Send Message", "onClick": "DWM_SendMessage($id, $Title, $Message);" } + ], + "status": + [ + + ] +} \ No newline at end of file diff --git a/Monitoring Server/module.json b/Monitoring Server/module.json new file mode 100644 index 0000000..38d5b8c --- /dev/null +++ b/Monitoring Server/module.json @@ -0,0 +1,14 @@ +{ + "id": "{C07DA6C0-41B2-2651-08E8-D8F3CFEC9546}", + "name": "Monitoring Server", + "type": 3, + "vendor": "J.H. Artworks", + "aliases": + [ + ], + "url": "https://jh-artworks.com", + "parentRequirements": [], + "childRequirements": [], + "implemented": [], + "prefix": "SEMS" +} \ No newline at end of file diff --git a/Monitoring Server/module.php b/Monitoring Server/module.php new file mode 100644 index 0000000..0746ef6 --- /dev/null +++ b/Monitoring Server/module.php @@ -0,0 +1,67 @@ +RegisterPropertyString("DiscordWsUrl",""); + } + + // Überschreibt die intere IPS_ApplyChanges($id) Funktion + public function ApplyChanges() { + // Diese Zeile nicht löschen + parent::ApplyChanges(); + } + /** + * 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: + * + * DWM_SendMessage($id); + * + */ + public function SendMessage($topic,$message) { + + // Discord webhook URL + $webhookUrl = $this->ReadPropertyString("DiscordWsUrl"); + + // Data to send as JSON + $data = array( + "username" => $topic, + "content" => $message + ); + + // Encode the data as JSON + $jsonData = json_encode($data); + + // Initialize cURL session + $ch = curl_init($webhookUrl); + + // Set cURL options + 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 + $response = curl_exec($ch); + + // Check for cURL errors + if (curl_errno($ch)) { + echo "cURL Error: " . curl_error($ch); + } + + // Close cURL session + curl_close($ch); + + // Display the response from the webhook + ///echo "Webhook Response: " . $response; + + + } +} +?> \ No newline at end of file diff --git a/library.json b/library.json new file mode 100644 index 0000000..feb5e4b --- /dev/null +++ b/library.json @@ -0,0 +1,13 @@ +{ + "id": "{B8E6BCF6-0F82-E783-5B09-40CB53F88354}", + "author": "J.H. Artworks", + "name": "SE Monitoring", + "url": "https://jh-artworks.com", + "compatibility": { + "version": "6.0", + "date": 1491343200 + }, + "version": "1.0", + "build": 0, + "date": 0 +} \ No newline at end of file