Synchronous HTTP notification
This commit is contained in:
parent
499fa82d96
commit
a5034ca3b5
|
@ -7,6 +7,8 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.sr.ht/~michalr/go-satel"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SendToTelegramSync struct {
|
type SendToTelegramSync struct {
|
||||||
|
@ -42,6 +44,13 @@ type SendToMatterbridgeSync struct {
|
||||||
tpl *template.Template
|
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) {
|
func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) {
|
||||||
for _, matterbridgeConfig := range mbridge.config.Matterbridge {
|
for _, matterbridgeConfig := range mbridge.config.Matterbridge {
|
||||||
body, err := json.Marshal(MatterbridgeMessage{
|
body, err := json.Marshal(MatterbridgeMessage{
|
||||||
|
@ -67,3 +76,38 @@ func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) {
|
||||||
|
|
||||||
mbridge.CallNext(msg)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -54,9 +54,7 @@ func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger
|
||||||
return returnEvents
|
return returnEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
func doHttpNotification(url string, logger *log.Logger) {
|
||||||
wg.Add(1)
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
if len(url) == 0 {
|
if len(url) == 0 {
|
||||||
return
|
return
|
||||||
|
@ -74,9 +72,9 @@ func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
||||||
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
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 {
|
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 {
|
for _, basicElement := range e.Messages {
|
||||||
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
||||||
if basicElement.Value == ArmedPartition_Armed {
|
if basicElement.Value == ArmedPartition_Armed {
|
||||||
notifyAllHttp(config.ArmCallbackUrls, logger, wg)
|
notifyAllHttp(config.ArmCallbackUrls, logger)
|
||||||
} else {
|
} else {
|
||||||
notifyAllHttp(config.DisarmCallbackUrls, logger, wg)
|
notifyAllHttp(config.DisarmCallbackUrls, logger)
|
||||||
}
|
}
|
||||||
break inner_arm
|
break inner_arm
|
||||||
}
|
}
|
||||||
|
@ -105,7 +103,7 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait
|
||||||
for _, basicElement := range e.Messages {
|
for _, basicElement := range e.Messages {
|
||||||
if basicElement.Type == satel.PartitionAlarm {
|
if basicElement.Type == satel.PartitionAlarm {
|
||||||
if basicElement.Value == PartitionAlarm_Alarm {
|
if basicElement.Value == PartitionAlarm_Alarm {
|
||||||
notifyAllHttp(config.AlarmCallbackUrls, logger, wg)
|
notifyAllHttp(config.AlarmCallbackUrls, logger)
|
||||||
break inner_alarm
|
break inner_alarm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue