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:
|
||||
|
||||
- `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
|
||||
|
||||
|
|
4
main.go
4
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue