diff --git a/README.md b/README.md index 147057e..46192f7 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ $ TELEGRAM_APITOKEN=YOUR_API_TOKEN ./alarm_bot --satel_addr=127.0.0.1 --satel_po Set the following environment variables: -- `NOTIFY_URL_ARM` - for an URL that will be POST when partition *0* is armed -- `NOTIFY_URL_DISARM` - for an URL that will be POST when partition *0* is unarmed +- `NOTIFY_URL_ARM` - for an URL that will be POST when partition **0** is armed +- `NOTIFY_URL_DISARM` - for an URL that will be POST when partition **0** is unarmed +- `ALARM_URL_ARM` - for an URL that will be POST when **any** partition alarm is activated ### Filtering events by change type diff --git a/main.go b/main.go index 8aa5481..b5adef4 100644 --- a/main.go +++ b/main.go @@ -149,11 +149,11 @@ func main() { dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) - Consume(NotifyViaHTTP( + NotifyViaHTTP( SendToTg(tgEvents, tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds), - )) + ) go CloseSatelOnCtrlC(s) diff --git a/sender_worker.go b/sender_worker.go index ceeb643..b6b9c7d 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -77,29 +77,40 @@ func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) { logger.Print("Notified via HTTP with result ", res.StatusCode) } -func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage { - returnEvents := make(chan GenericMessage) - +func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) { armCallbackUrl := os.Getenv("NOTIFY_URL_ARM") disarmCallbackUrl := os.Getenv("NOTIFY_URL_DISARM") + alarmCallbackUrl := os.Getenv("ALARM_URL_ARM") + armDisarmCallbackEnabled := (len(armCallbackUrl) != 0) && (len(disarmCallbackUrl) != 0) + alarmCallbackEnabled := (len(alarmCallbackUrl) != 0) go func() { wg.Add(1) defer wg.Done() for e := range events { - inner: - for _, basicElement := range e.Messages { - if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { - if basicElement.Value == ArmedPartition_Armed { - go doHttpNotification(armCallbackUrl, logger, wg) - } else { - go doHttpNotification(disarmCallbackUrl, logger, wg) + if armDisarmCallbackEnabled { + inner_arm: + for _, basicElement := range e.Messages { + if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { + if basicElement.Value == ArmedPartition_Armed { + go doHttpNotification(armCallbackUrl, logger, wg) + } else { + go doHttpNotification(disarmCallbackUrl, logger, wg) + } + break inner_arm + } + } + } + if alarmCallbackEnabled { + inner_alarm: + for _, basicElement := range e.Messages { + if basicElement.Type == satel.PartitionAlarm { + if basicElement.Value == PartitionAlarm_Alarm { + go doHttpNotification(alarmCallbackUrl, logger, wg) + break inner_alarm + } } - break inner } } } - close(returnEvents) }() - - return returnEvents }