diff --git a/sender_sync.go b/sender_sync.go index a61d5eb..e65af69 100644 --- a/sender_sync.go +++ b/sender_sync.go @@ -7,6 +7,8 @@ import ( "html/template" "log" "net/http" + + "git.sr.ht/~michalr/go-satel" ) type SendToTelegramSync struct { @@ -42,6 +44,13 @@ type SendToMatterbridgeSync struct { tpl *template.Template } +func MakeSendToMatterbridgeSync(s SatelNameGetter, + config AppConfig, + logger *log.Logger, + tpl *template.Template) SendToMatterbridgeSync { + return SendToMatterbridgeSync{SyncFilterImpl[GenericMessage]{}, s, config, logger, tpl} +} + func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) { for _, matterbridgeConfig := range mbridge.config.Matterbridge { body, err := json.Marshal(MatterbridgeMessage{ @@ -67,3 +76,38 @@ func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) { mbridge.CallNext(msg) } + +type NotifyViaHTTPSync struct { + SyncFilterImpl[GenericMessage] + + config AppConfig + logger *log.Logger +} + +func MakeNofityViaHTTPSync(config AppConfig, logger *log.Logger) NotifyViaHTTPSync { + return NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger} +} + +func (notifyViaHttp *NotifyViaHTTPSync) Call(msg GenericMessage) { +inner_arm: + for _, basicElement := range msg.Messages { + if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { + if basicElement.Value == ArmedPartition_Armed { + notifyAllHttp(notifyViaHttp.config.ArmCallbackUrls, notifyViaHttp.logger) + } else { + notifyAllHttp(notifyViaHttp.config.DisarmCallbackUrls, notifyViaHttp.logger) + } + break inner_arm + } + } +inner_alarm: + for _, basicElement := range msg.Messages { + if basicElement.Type == satel.PartitionAlarm { + if basicElement.Value == PartitionAlarm_Alarm { + notifyAllHttp(notifyViaHttp.config.AlarmCallbackUrls, notifyViaHttp.logger) + break inner_alarm + } + } + } + +} diff --git a/sender_worker.go b/sender_worker.go index 5a705fa..36e9ee2 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -54,9 +54,7 @@ func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger return returnEvents } -func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) { - wg.Add(1) - defer wg.Done() +func doHttpNotification(url string, logger *log.Logger) { if len(url) == 0 { return @@ -74,9 +72,9 @@ func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) { logger.Print("Notified via HTTP with result ", res.StatusCode) } -func notifyAllHttp(urls []string, logger *log.Logger, wg *sync.WaitGroup) { +func notifyAllHttp(urls []string, logger *log.Logger) { for _, uri := range urls { - go doHttpNotification(uri, logger, wg) + go doHttpNotification(uri, logger) } } @@ -94,9 +92,9 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait for _, basicElement := range e.Messages { if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) { if basicElement.Value == ArmedPartition_Armed { - notifyAllHttp(config.ArmCallbackUrls, logger, wg) + notifyAllHttp(config.ArmCallbackUrls, logger) } else { - notifyAllHttp(config.DisarmCallbackUrls, logger, wg) + notifyAllHttp(config.DisarmCallbackUrls, logger) } break inner_arm } @@ -105,7 +103,7 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait for _, basicElement := range e.Messages { if basicElement.Type == satel.PartitionAlarm { if basicElement.Value == PartitionAlarm_Alarm { - notifyAllHttp(config.AlarmCallbackUrls, logger, wg) + notifyAllHttp(config.AlarmCallbackUrls, logger) break inner_alarm } }