HTTP Callback for activated alarm
This commit is contained in:
parent
71d05dade9
commit
ed9981afbe
|
@ -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
|
||||||
|
|
||||||
|
|
4
main.go
4
main.go
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -77,29 +77,40 @@ 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 {
|
||||||
for _, basicElement := range e.Messages {
|
inner_arm:
|
||||||
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
for _, basicElement := range e.Messages {
|
||||||
if basicElement.Value == ArmedPartition_Armed {
|
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
||||||
go doHttpNotification(armCallbackUrl, logger, wg)
|
if basicElement.Value == ArmedPartition_Armed {
|
||||||
} else {
|
go doHttpNotification(armCallbackUrl, logger, wg)
|
||||||
go doHttpNotification(disarmCallbackUrl, 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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue