Refactor Multialarmer module: enhance email and WhatsApp notification features, add configuration options for API keys and recipient management

This commit is contained in:
jhartworks committed 2026-10-29 12:24:24 +01:00
1 parent e100b8425e
commit 8c2f8244e6
2 files changed
+302 -183

No files matched your search

+28 -21
View File
@@ -1,25 +1,32 @@
{
"elements":
[
"elements": [
{ "type": "SelectInstance", "name": "VisuId", "caption": "Kachelvisu ID" },
{ "type": "SelectInstance", "name": "MailerId", "caption": "Email SMTP ID" },
{ "type": "ValidationTextBox", "name": "MailAddresses", "caption": "E-Mail Adresses (Commaseperated)" , "width": "40%"},
{ "type": "SelectVariable", "name": "ValueId01", "validVariableTypes": [0],"caption": "Überwachter Wert 1" },
{ "type": "SelectVariable", "name": "ValueId02", "validVariableTypes": [0],"caption": "Überwachter Wert 2" },
{ "type": "SelectVariable", "name": "ValueId03", "validVariableTypes": [0],"caption": "Überwachter Wert 3" },
{ "type": "SelectVariable", "name": "ValueId04", "validVariableTypes": [0],"caption": "Überwachter Wert 4" }
],
"actions":
[
{ "type": "Button", "label": "Set Init Values", "onClick": "VTMA_setInitValues($id);" },
{ "type": "Button", "label": "Send a Mail", "onClick": "VTMA_sendTestmail($id);" }
],
"status":
[
{
"type": "ExpansionPanel",
"caption": "E-Mail",
"items": [
{ "type": "CheckBox", "name": "SendInternalMail", "caption": "Internen SMTP-Mailversand aktivieren" },
{ "type": "SelectInstance", "name": "MailerId", "caption": "E-Mail SMTP ID" },
{ "type": "CheckBox", "name": "SendMailApi", "caption": "Mail-API aktivieren" },
{ "type": "PasswordTextBox", "name": "MailApiKey", "caption": "Mail-API-Key" }
]
},
{
"type": "ExpansionPanel",
"caption": "WhatsApp",
"items": [
{ "type": "CheckBox", "name": "SendWhatsApp", "caption": "WhatsApp-API aktivieren" },
{ "type": "PasswordTextBox", "name": "WhatsAppApiKey", "caption": "WhatsApp-API-Key" }
]
},
{ "type": "SelectVariable", "name": "ValueId01", "validVariableTypes": [0], "caption": "Überwachter Wert 1" },
{ "type": "SelectVariable", "name": "ValueId02", "validVariableTypes": [0], "caption": "Überwachter Wert 2" },
{ "type": "SelectVariable", "name": "ValueId03", "validVariableTypes": [0], "caption": "Überwachter Wert 3" },
{ "type": "SelectVariable", "name": "ValueId04", "validVariableTypes": [0], "caption": "Überwachter Wert 4" }
],
"actions": [
{ "type": "Button", "label": "Initialwerte setzen", "onClick": "VTMA_setInitValues($id);" },
{ "type": "Button", "label": "Testbenachrichtigung senden", "onClick": "VTMA_sendTestNotification($id);" }
],
"status": []
}
+274 -162
View File
@@ -1,203 +1,315 @@
<?
// Klassendefinition
class Multialarmer extends IPSModule {
// Überschreibt die interne IPS_Create($id) Funktion
public function Create() {
// Diese Zeile nicht löschen.
<?php
class Multialarmer extends IPSModule
{
private const MAIL_API_URL = 'https://mailapi.se-inno.de';
private const WHATSAPP_API_URL = 'https://whatsappapi.se-inno.de';
private const WHATSAPP_TEMPLATE = 'se_portal';
public function Create()
{
parent::Create();
$this->RegisterProperties();
$this->RegisterRecipientVariables();
$this->RegisterPropertyInteger("VisuId",0);
$this->RegisterPropertyInteger("MailerId",0);
$this->RegisterPropertyString("MailAddresses","jh@se-inno.de, info@se-inno.de");
$this->RegisterPropertyInteger('ValueId01', 0);
$this->RegisterPropertyInteger('ValueId02', 0);
$this->RegisterPropertyInteger('ValueId03', 0);
$this->RegisterPropertyInteger('ValueId04', 0);
$this->RegisterPropertyInteger("ValueId01",0);
$this->RegisterPropertyInteger("ValueId02",0);
$this->RegisterPropertyInteger("ValueId03",0);
$this->RegisterPropertyInteger("ValueId04",0);
$this->RegisterVariableBoolean('NotifyEn01', 'Variable 01 Melden?', 'LimitEn', 95);
$this->EnableAction('NotifyEn01');
$this->RegisterVariableBoolean('NotifyEn02', 'Variable 02 Melden?', 'LimitEn', 115);
$this->EnableAction('NotifyEn02');
$this->RegisterVariableBoolean('NotifyEn03', 'Variable 03 Melden?', 'LimitEn', 135);
$this->EnableAction('NotifyEn03');
$this->RegisterVariableBoolean('NotifyEn04', 'Variable 04 Melden?', 'LimitEn', 155);
$this->EnableAction('NotifyEn04');
$this->RegisterVariableBoolean("NotifyEn01", "Variable 01 Melden?","LimitEn",95);
$this->EnableAction("NotifyEn01");
$this->RegisterVariableBoolean("NotifyEn02", "Variable 02 Melden?","LimitEn",115);
$this->EnableAction("NotifyEn02");
$this->RegisterVariableBoolean("NotifyEn03", "Variable 03 Melden?","LimitEn",135);
$this->EnableAction("NotifyEn03");
$this->RegisterVariableBoolean("NotifyEn04", "Variable 04 Melden?","LimitEn",155);
$this->EnableAction("NotifyEn04");
SetValue($this->RegisterVariableString('NotifyTitel01', 'Titel der Benachrichtigung Var 01', '~TextBox', 100), 'Störung!');
$this->EnableAction('NotifyTitel01');
SetValue($this->RegisterVariableString('NotifyTitel02', 'Titel der Benachrichtigung Var 02', '~TextBox', 120), 'Störung!');
$this->EnableAction('NotifyTitel02');
SetValue($this->RegisterVariableString('NotifyTitel03', 'Titel der Benachrichtigung Var 03', '~TextBox', 140), 'Störung!');
$this->EnableAction('NotifyTitel03');
SetValue($this->RegisterVariableString('NotifyTitel04', 'Titel der Benachrichtigung Var 04', '~TextBox', 160), 'Störung!');
$this->EnableAction('NotifyTitel04');
SetValue($this->RegisterVariableString("NotifyTitel01", "Titel der Benachrichtigung Var 01","~TextBox",100),"Störung!");
$this->EnableAction("NotifyTitel01");
SetValue($this->RegisterVariableString("NotifyTitel02", "Titel der Benachrichtigung Var 02","~TextBox",120),"Störung!");
$this->EnableAction("NotifyTitel02");
SetValue($this->RegisterVariableString("NotifyTitel03", "Titel der Benachrichtigung Var 03","~TextBox",140),"Störung!");
$this->EnableAction("NotifyTitel03");
SetValue($this->RegisterVariableString("NotifyTitel04", "Titel der Benachrichtigung Var 04","~TextBox",160),"Störung!");
$this->EnableAction("NotifyTitel04");
/*
SetValue($this->RegisterVariableString("NotifyText01", "Störungstext Var 01","~TextBox",110),"Störung 1");
$this->EnableAction("NotifyText01");
SetValue($this->RegisterVariableString("NotifyText02", "Störungstext Var 02","~TextBox",130),"Störung 2");
$this->EnableAction("NotifyText02");
SetValue($this->RegisterVariableString("NotifyText03", "Störungstext Var 03","~TextBox",150),"Störung 3");
$this->EnableAction("NotifyText03");
SetValue($this->RegisterVariableString("NotifyText04", "Störungstext Var 04","~TextBox",170),"Störung 4");
$this->EnableAction("NotifyText04");
*/
$this->RegisterTimer("Update", 0, 'VTMA_checkValues('.$this->InstanceID.');');
$this->RegisterTimer('Update', 0, 'VTMA_checkValues(' . $this->InstanceID . ');');
}
// Überschreibt die intere IPS_ApplyChanges($id) Funktion
public function ApplyChanges() {
// Diese Zeile nicht löschen
public function ApplyChanges()
{
// Ensure that existing Multialarmer instances also receive the
// recipient variables after the module update.
$this->RegisterRecipientVariables();
parent::ApplyChanges();
$this->SetTimerInterval("Update", 5 * 1000);
$this->UpdateRecipientVisibility();
$this->SetTimerInterval('Update', 5 * 1000);
}
public function RequestAction($Ident, $Value) {
public function RequestAction($Ident, $Value)
{
$varid = $this->GetIDForIdent($Ident);
SetValue($varid, $Value);
// Changing recipients is a configuration task and must not trigger
// a new alarm evaluation.
if ($Ident !== 'MailRecipients' && $Ident !== 'WhatsAppRecipients') {
$this->checkValues();
}
public function checkValues(){
$webId = $this->ReadPropertyInteger("VisuId");
$IdVariable01 = $this->ReadPropertyInteger("ValueId01");
$IdVariable02 = $this->ReadPropertyInteger("ValueId02");
$IdVariable03 = $this->ReadPropertyInteger("ValueId03");
$IdVariable04 = $this->ReadPropertyInteger("ValueId04");
$NotifyEn01 = GetValue($this->GetIDForIdent("NotifyEn01"));
$NotifyEn02 = GetValue($this->GetIDForIdent("NotifyEn02"));
$NotifyEn03 = GetValue($this->GetIDForIdent("NotifyEn03"));
$NotifyEn04 = GetValue($this->GetIDForIdent("NotifyEn04"));
$NotifyTitel01 = GetValue($this->GetIDForIdent("NotifyTitel01"));
$NotifyText01 = IPS_GetName($IdVariable01);//GetValue($this->GetIDForIdent("NotifyText01"));
$NotifyTitel02 = GetValue($this->GetIDForIdent("NotifyTitel02"));
$NotifyText02 = IPS_GetName($IdVariable02);//GetValue($this->GetIDForIdent("NotifyText02"));
$NotifyTitel03 = GetValue($this->GetIDForIdent("NotifyTitel03"));
$NotifyText03 = IPS_GetName($IdVariable03);//GetValue($this->GetIDForIdent("NotifyText03"));
$NotifyTitel04 = GetValue($this->GetIDForIdent("NotifyTitel04"));
$NotifyText04 = IPS_GetName($IdVariable04);//GetValue($this->GetIDForIdent("NotifyText04"));
if($NotifyEn01 == true && $IdVariable01 > 0){
$value01 = GetValue($IdVariable01);
$this->notify($IdVariable01, $webId, $IdVariable01, $NotifyTitel01, $NotifyText01, "Flame");
}
if($NotifyEn02 == true && $IdVariable02 > 0){
$value02 = GetValue($IdVariable02);
public function checkValues()
{
$webId = $this->ReadPropertyInteger('VisuId');
$this->notify($IdVariable02, $webId, $IdVariable02, $NotifyTitel02, $NotifyText02, "Flame");
$IdVariable01 = $this->ReadPropertyInteger('ValueId01');
$IdVariable02 = $this->ReadPropertyInteger('ValueId02');
$IdVariable03 = $this->ReadPropertyInteger('ValueId03');
$IdVariable04 = $this->ReadPropertyInteger('ValueId04');
$NotifyEn01 = GetValue($this->GetIDForIdent('NotifyEn01'));
$NotifyEn02 = GetValue($this->GetIDForIdent('NotifyEn02'));
$NotifyEn03 = GetValue($this->GetIDForIdent('NotifyEn03'));
$NotifyEn04 = GetValue($this->GetIDForIdent('NotifyEn04'));
$NotifyTitel01 = GetValue($this->GetIDForIdent('NotifyTitel01'));
$NotifyTitel02 = GetValue($this->GetIDForIdent('NotifyTitel02'));
$NotifyTitel03 = GetValue($this->GetIDForIdent('NotifyTitel03'));
$NotifyTitel04 = GetValue($this->GetIDForIdent('NotifyTitel04'));
if ($NotifyEn01 === true && $IdVariable01 > 0) {
$this->notify($IdVariable01, $webId, $IdVariable01, $NotifyTitel01, IPS_GetName($IdVariable01), 'Flame');
}
if($NotifyEn03 == true && $IdVariable03 > 0){
$value03 = GetValue($IdVariable03);
$this->notify($IdVariable03, $webId, $IdVariable03, $NotifyTitel03, $NotifyText03, "Flame");
if ($NotifyEn02 === true && $IdVariable02 > 0) {
$this->notify($IdVariable02, $webId, $IdVariable02, $NotifyTitel02, IPS_GetName($IdVariable02), 'Flame');
}
if($NotifyEn04 == true && $IdVariable04 > 0){
$value04 = GetValue($IdVariable04);
$this->notify($IdVariable04, $webId, $IdVariable04, $NotifyTitel04, $NotifyText04, "Flame");
if ($NotifyEn03 === true && $IdVariable03 > 0) {
$this->notify($IdVariable03, $webId, $IdVariable03, $NotifyTitel03, IPS_GetName($IdVariable03), 'Flame');
}
}
public function setInitValues(){
SetValue($this->GetIDForIdent("NotifyTitel01"),"Störung!");
SetValue($this->GetIDForIdent("NotifyTitel02"),"Störung!");
SetValue($this->GetIDForIdent("NotifyTitel03"),"Störung!");
SetValue($this->GetIDForIdent("NotifyTitel04"),"Störung!");
}
public function sendTestmail(){
$art= "";
$smname = "";
$this->sendMail($art, $smname, true);
}
private function sendMail($art, $smname, $testmail){
$MailerId = $this->ReadPropertyInteger("MailerId");
$MailAddresses = $this->ReadPropertyString("MailAddresses");
$MailAddresses = str_replace(" ", "", $MailAddresses);
$MailAddresses = str_replace(";", ",", $MailAddresses);
$MailAddresses = explode(",",$MailAddresses);
//print_r($MailAddresses);
if ($testmail == true){
$art= "MAILSYSTEMTEST";
$smname = "It was just a Test to be ready for a real emergency ;-)";
}
if ($MailerId > 0){
foreach ($MailAddresses as $MailAddress){
SMTP_SendMailEx($MailerId, $MailAddress, "SE Inno Mailservice ".$art, $smname);
if ($NotifyEn04 === true && $IdVariable04 > 0) {
$this->notify($IdVariable04, $webId, $IdVariable04, $NotifyTitel04, IPS_GetName($IdVariable04), 'Flame');
}
}
public function setInitValues()
{
SetValue($this->GetIDForIdent('NotifyTitel01'), 'Störung!');
SetValue($this->GetIDForIdent('NotifyTitel02'), 'Störung!');
SetValue($this->GetIDForIdent('NotifyTitel03'), 'Störung!');
SetValue($this->GetIDForIdent('NotifyTitel04'), 'Störung!');
}
private function notify($trigid, $webfrontid, $targetid, $art, $smname, $ico){
$MailerId = $this->ReadPropertyInteger("MailerId");
$MailAddresses = array($this->ReadPropertyString("MailAddresses"));
if (GetValueBoolean($trigid) == true && ($this->GetBuffer($smname) == "true")) {
$this->SetBuffer($smname, "false");
VISU_PostNotification($webfrontid, $art, $smname, $ico, $targetid);
$this->sendMail($art, $smname, false);
// Kept for existing scripts and the former configuration button.
public function sendTestmail()
{
$this->sendTestNotification();
}
elseif (GetValueBoolean($trigid) == false) {
$this->SetBuffer($smname, "true");
public function sendTestNotification()
{
$art = 'MAILSYSTEMTEST';
$smname = 'Dies ist nur ein Test, damit der Multialarmer im Notfall bereit ist.';
$this->sendMail($art, $smname);
$this->sendWhatsApp($art, $smname);
}
if (GetValueBoolean($trigid) == true){
return true;
}else{
private function RegisterProperties()
{
$this->RegisterPropertyInteger('VisuId', 0);
$this->RegisterPropertyInteger('MailerId', 0);
// MailAddresses remains as the migration source for existing instances.
$this->RegisterPropertyString('MailAddresses', 'jh@se-inno.de, info@se-inno.de');
$this->RegisterPropertyBoolean('SendInternalMail', true);
$this->RegisterPropertyBoolean('SendMailApi', false);
$this->RegisterPropertyString('MailApiKey', '');
$this->RegisterPropertyBoolean('SendWhatsApp', false);
$this->RegisterPropertyString('WhatsAppApiKey', '');
}
private function RegisterRecipientVariables()
{
$mailRecipientsWereMissing = !@$this->GetIDForIdent('MailRecipients');
$mailRecipientsId = $this->RegisterVariableString('MailRecipients', 'E-Mail Empfänger', '~TextBox', 180);
$this->EnableAction('MailRecipients');
// Existing MailAddresses configurations are transferred on the first
// registration. Both e-mail methods intentionally use this one list.
if ($mailRecipientsWereMissing) {
SetValue($mailRecipientsId, $this->ReadPropertyString('MailAddresses'));
}
$this->RegisterVariableString('WhatsAppRecipients', 'WhatsApp Empfänger', '~TextBox', 190);
$this->EnableAction('WhatsAppRecipients');
}
private function UpdateRecipientVisibility()
{
$mailRecipientsId = $this->GetIDForIdent('MailRecipients');
if ($mailRecipientsId > 0) {
IPS_SetHidden($mailRecipientsId, !($this->ReadPropertyBoolean('SendInternalMail') || $this->ReadPropertyBoolean('SendMailApi')));
}
$whatsAppRecipientsId = $this->GetIDForIdent('WhatsAppRecipients');
if ($whatsAppRecipientsId > 0) {
IPS_SetHidden($whatsAppRecipientsId, !$this->ReadPropertyBoolean('SendWhatsApp'));
}
}
private function sendMail($art, $smname)
{
if (!$this->ReadPropertyBoolean('SendInternalMail') && !$this->ReadPropertyBoolean('SendMailApi')) {
return;
}
$mailAddresses = $this->GetRecipients('MailRecipients');
if (count($mailAddresses) === 0) {
$this->Log('Keine E-Mail-Empfänger konfiguriert.');
return;
}
$subject = 'SE Inno Mailservice ' . $art;
if ($this->ReadPropertyBoolean('SendInternalMail')) {
$mailerId = $this->ReadPropertyInteger('MailerId');
if ($mailerId > 0) {
foreach ($mailAddresses as $mailAddress) {
SMTP_SendMailEx($mailerId, $mailAddress, $subject, $smname);
}
} else {
$this->Log('Interner Mailversand ist aktiviert, aber keine SMTP-Instanz ist ausgewählt.');
}
}
if ($this->ReadPropertyBoolean('SendMailApi')) {
$apiKey = trim($this->ReadPropertyString('MailApiKey'));
if ($apiKey === '') {
$this->Log('Mail-API ist aktiviert, aber kein API-Key ist hinterlegt.');
return;
}
foreach ($mailAddresses as $mailAddress) {
$this->PostApi(self::MAIL_API_URL, array(
'api_key' => $apiKey,
'address' => $mailAddress,
'subject' => $subject,
'message' => $smname
), 'Mail-API');
}
}
}
private function sendWhatsApp($art, $smname)
{
if (!$this->ReadPropertyBoolean('SendWhatsApp')) {
return;
}
$apiKey = trim($this->ReadPropertyString('WhatsAppApiKey'));
if ($apiKey === '') {
$this->Log('WhatsApp ist aktiviert, aber kein API-Key ist hinterlegt.');
return;
}
$numbers = $this->GetRecipients('WhatsAppRecipients');
if (count($numbers) === 0) {
$this->Log('WhatsApp ist aktiviert, aber keine Empfängernummer ist konfiguriert.');
return;
}
foreach ($numbers as $number) {
$this->PostApi(self::WHATSAPP_API_URL, array(
'api_key' => $apiKey,
'number' => $number,
'message' => $smname,
'template' => self::WHATSAPP_TEMPLATE,
'pn' => $art
), 'WhatsApp-API');
}
}
private function GetRecipients($ident)
{
$recipients = GetValue($this->GetIDForIdent($ident));
$recipients = preg_split('/[;,]/', $recipients);
$result = array();
foreach ($recipients as $recipient) {
$recipient = trim($recipient);
if ($recipient !== '') {
$result[] = $recipient;
}
}
return $result;
}
private function PostApi($url, $data, $channel)
{
if (!function_exists('curl_init')) {
$this->Log($channel . ' konnte nicht gesendet werden: cURL ist nicht verfügbar.');
return false;
}
$ch = curl_init($url);
if ($ch === false) {
$this->Log($channel . ' konnte nicht initialisiert werden.');
return false;
}
}
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10
));
?>
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($response === false || $httpCode < 200 || $httpCode >= 300) {
$message = $channel . ' konnte nicht gesendet werden (HTTP ' . $httpCode . ').';
if ($error !== '') {
$message .= ' cURL: ' . $error;
}
$this->Log($message);
return false;
}
return true;
}
private function notify($trigid, $webfrontid, $targetid, $art, $smname, $ico)
{
if (GetValueBoolean($trigid) === true && $this->GetBuffer($smname) === 'true') {
$this->SetBuffer($smname, 'false');
if ($webfrontid > 0) {
VISU_PostNotification($webfrontid, $art, $smname, $ico, $targetid);
}
$this->sendMail($art, $smname);
$this->sendWhatsApp($art, $smname);
} elseif (GetValueBoolean($trigid) === false) {
$this->SetBuffer($smname, 'true');
}
return GetValueBoolean($trigid);
}
private function Log($message)
{
IPS_LogMessage('Multialarmer ' . $this->InstanceID, $message);
}
}