EnsureLimitEnProfile(); $this->RegisterProperties(); $this->RegisterRecipientVariables(); $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'); 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'); $this->RegisterTimer('Update', 0, 'VTMA_checkValues(' . $this->InstanceID . ');'); } public function ApplyChanges() { $this->EnsureLimitEnProfile(); // Ensure that existing Multialarmer instances also receive the // recipient variables after the module update. $this->RegisterRecipientVariables(); parent::ApplyChanges(); $this->UpdateRecipientVisibility(); $this->SetTimerInterval('Update', 5 * 1000); } 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')); $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 ($NotifyEn02 === true && $IdVariable02 > 0) { $this->notify($IdVariable02, $webId, $IdVariable02, $NotifyTitel02, IPS_GetName($IdVariable02), 'Flame'); } if ($NotifyEn03 === true && $IdVariable03 > 0) { $this->notify($IdVariable03, $webId, $IdVariable03, $NotifyTitel03, IPS_GetName($IdVariable03), 'Flame'); } 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!'); } // Kept for existing scripts and the former configuration button. public function sendTestmail() { $this->sendTestNotification(); } 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); } 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 EnsureLimitEnProfile() { if (!IPS_VariableProfileExists('LimitEn')) { IPS_CreateVariableProfile('LimitEn', 0); IPS_SetVariableProfileAssociation('LimitEn', true, 'Ja', '', 0x00FF00); IPS_SetVariableProfileAssociation('LimitEn', false, 'Nein', '', 0xFFFF00); } } 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); } }