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
+347 -228

No files matched your search

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