1
0
Fork 0

HTTP Callback for activated alarm

This commit is contained in:
Michał Rudowicz 2024-03-06 22:31:08 +01:00
parent 71d05dade9
commit ed9981afbe
3 changed files with 30 additions and 18 deletions

View File

@ -17,8 +17,9 @@ $ TELEGRAM_APITOKEN=YOUR_API_TOKEN ./alarm_bot --satel_addr=127.0.0.1 --satel_po
Set the following environment variables: Set the following environment variables:
- `NOTIFY_URL_ARM` - for an URL that will be POST when partition *0* is armed - `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_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 ### Filtering events by change type

View File

@ -149,11 +149,11 @@ func main() {
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) 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), SendToTg(tgEvents, tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
&wg, &wg,
log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds), log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds),
)) )
go CloseSatelOnCtrlC(s) go CloseSatelOnCtrlC(s)

View File

@ -77,16 +77,18 @@ 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 NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage { func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) {
returnEvents := make(chan GenericMessage)
armCallbackUrl := os.Getenv("NOTIFY_URL_ARM") armCallbackUrl := os.Getenv("NOTIFY_URL_ARM")
disarmCallbackUrl := os.Getenv("NOTIFY_URL_DISARM") 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() { go func() {
wg.Add(1) wg.Add(1)
defer wg.Done() defer wg.Done()
for e := range events { for e := range events {
inner: if armDisarmCallbackEnabled {
inner_arm:
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 {
@ -94,12 +96,21 @@ func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log
} else { } else {
go doHttpNotification(disarmCallbackUrl, logger, wg) go doHttpNotification(disarmCallbackUrl, logger, wg)
} }
break inner 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
}
}
} }
} }
} }
close(returnEvents)
}() }()
return returnEvents
} }