EnsureLimitEnProfile(); $this->RegisterProperties(); $this->RegisterRecipientVariables(); $this->RegisterAlarmVariables(); $this->RegisterPropertyInteger('ValueId01', 0); $this->RegisterPropertyInteger('ValueId02', 0); $this->RegisterPropertyInteger('ValueId03', 0); $this->RegisterPropertyInteger('ValueId04', 0); $this->RegisterTimer('Update', 0, 'VTMA_checkValues(' . $this->InstanceID . ');'); } public function ApplyChanges() { $this->EnsureLimitEnProfile(); // Ensure that existing Multialarmer instances also receive newly // added status variables without changing manually assigned names. $this->RegisterRecipientVariables(); $this->RegisterAlarmVariables(); parent::ApplyChanges(); $this->UpdateRecipientVisibility(); $this->UpdateAlarmVariableVisibility(); $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')); $NotifyText01 = $this->GetNotificationText('NotifyText01', $IdVariable01); $NotifyTitel02 = GetValue($this->GetIDForIdent('NotifyTitel02')); $NotifyText02 = $this->GetNotificationText('NotifyText02', $IdVariable02); $NotifyTitel03 = GetValue($this->GetIDForIdent('NotifyTitel03')); $NotifyText03 = $this->GetNotificationText('NotifyText03', $IdVariable03); $NotifyTitel04 = GetValue($this->GetIDForIdent('NotifyTitel04')); $NotifyText04 = $this->GetNotificationText('NotifyText04', $IdVariable04); if ($NotifyEn01 === true && $this->IsConfiguredAlarmVariable($IdVariable01)) { $this->notify($IdVariable01, $webId, $IdVariable01, $NotifyTitel01, $NotifyText01, 'Flame'); } if ($NotifyEn02 === true && $this->IsConfiguredAlarmVariable($IdVariable02)) { $this->notify($IdVariable02, $webId, $IdVariable02, $NotifyTitel02, $NotifyText02, 'Flame'); } if ($NotifyEn03 === true && $this->IsConfiguredAlarmVariable($IdVariable03)) { $this->notify($IdVariable03, $webId, $IdVariable03, $NotifyTitel03, $NotifyText03, 'Flame'); } if ($NotifyEn04 === true && $this->IsConfiguredAlarmVariable($IdVariable04)) { $this->notify($IdVariable04, $webId, $IdVariable04, $NotifyTitel04, $NotifyText04, '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); $this->RegisterPropertyBoolean('AppendVariableName', true); // 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->RegisterEditableStringVariable('MailRecipients', 'E-Mail Empfänger', '~TextBox', 180, ''); // 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->RegisterEditableStringVariable('WhatsAppRecipients', 'WhatsApp Empfänger', '~TextBox', 190, ''); } private function RegisterAlarmVariables() { $this->RegisterEditableBooleanVariable('NotifyEn01', 'Variable 01 Melden?', 'LimitEn', 95); $this->RegisterEditableStringVariable('NotifyTitel01', 'Titel der Benachrichtigung Var 01', '~TextBox', 100, 'Störung!'); $this->RegisterEditableStringVariable('NotifyText01', 'Meldungstext Var 01', '~TextBox', 110, ''); $this->RegisterEditableBooleanVariable('NotifyEn02', 'Variable 02 Melden?', 'LimitEn', 115); $this->RegisterEditableStringVariable('NotifyTitel02', 'Titel der Benachrichtigung Var 02', '~TextBox', 120, 'Störung!'); $this->RegisterEditableStringVariable('NotifyText02', 'Meldungstext Var 02', '~TextBox', 130, ''); $this->RegisterEditableBooleanVariable('NotifyEn03', 'Variable 03 Melden?', 'LimitEn', 135); $this->RegisterEditableStringVariable('NotifyTitel03', 'Titel der Benachrichtigung Var 03', '~TextBox', 140, 'Störung!'); $this->RegisterEditableStringVariable('NotifyText03', 'Meldungstext Var 03', '~TextBox', 150, ''); $this->RegisterEditableBooleanVariable('NotifyEn04', 'Variable 04 Melden?', 'LimitEn', 155); $this->RegisterEditableStringVariable('NotifyTitel04', 'Titel der Benachrichtigung Var 04', '~TextBox', 160, 'Störung!'); $this->RegisterEditableStringVariable('NotifyText04', 'Meldungstext Var 04', '~TextBox', 170, ''); } private function RegisterEditableBooleanVariable($ident, $name, $profile, $position) { $variableId = @$this->GetIDForIdent($ident); if (!$variableId) { $variableId = $this->RegisterVariableBoolean($ident, $name, $profile, $position); } $this->EnableAction($ident); return $variableId; } private function RegisterEditableStringVariable($ident, $name, $profile, $position, $initialValue) { $variableId = @$this->GetIDForIdent($ident); if (!$variableId) { $variableId = $this->RegisterVariableString($ident, $name, $profile, $position); SetValue($variableId, $initialValue); } $this->EnableAction($ident); return $variableId; } 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 UpdateAlarmVariableVisibility() { $visibility = array( 1 => $this->IsConfiguredAlarmVariable($this->ReadPropertyInteger('ValueId01')), 2 => $this->IsConfiguredAlarmVariable($this->ReadPropertyInteger('ValueId02')), 3 => $this->IsConfiguredAlarmVariable($this->ReadPropertyInteger('ValueId03')), 4 => $this->IsConfiguredAlarmVariable($this->ReadPropertyInteger('ValueId04')) ); foreach ($visibility as $number => $visible) { foreach (array('NotifyEn', 'NotifyTitel', 'NotifyText') as $prefix) { $variableId = @$this->GetIDForIdent($prefix . sprintf('%02d', $number)); if ($variableId > 0) { IPS_SetHidden($variableId, !$visible); } } } } private function GetNotificationText($textIdent, $sourceVariableId) { $textVariableId = @$this->GetIDForIdent($textIdent); $message = $textVariableId > 0 ? GetValueString($textVariableId) : ''; if (!$this->ReadPropertyBoolean('AppendVariableName') || !$this->IsConfiguredAlarmVariable($sourceVariableId)) { return $message; } $variableName = IPS_GetName($sourceVariableId); if ($message === '') { return $variableName; } return $message . "\n" . $variableName; } private function IsConfiguredAlarmVariable($variableId) { return $variableId > 0 && IPS_VariableExists($variableId); } 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) { $bufferIdent = 'Alarm_' . $trigid; if (GetValueBoolean($trigid) === true && $this->GetBuffer($bufferIdent) === 'true') { $this->SetBuffer($bufferIdent, '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($bufferIdent, 'true'); } return GetValueBoolean($trigid); } private function Log($message) { IPS_LogMessage('Multialarmer ' . $this->InstanceID, $message); } }