1
0
Fork 0

Compare commits

..

4 Commits

6 changed files with 48 additions and 24 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

2
go.mod
View File

@ -3,7 +3,7 @@ module alarm_bot
go 1.19 go 1.19
require ( require (
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8 git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
) )

2
go.sum
View File

@ -8,6 +8,8 @@ git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0 h1:E5gMgMCgaZltNC
git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8 h1:edwd27GRcof9fC93rBv0yTolaEcs5AlFmWyVquYKRK0= git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8 h1:edwd27GRcof9fC93rBv0yTolaEcs5AlFmWyVquYKRK0=
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733 h1:pvnT5ZT9LlY2/s8PBPA0PJz3Sw63nGLU18o6MQMzJUM=
git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -18,7 +18,6 @@ import (
) )
const ( const (
MessageNotMoreOftenThanSeconds = 15
PersistenceFilename = "hs_wro_last_seen.bin" PersistenceFilename = "hs_wro_last_seen.bin"
) )
@ -150,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,
logger, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds),
)) )
go CloseSatelOnCtrlC(s) go CloseSatelOnCtrlC(s)

View File

@ -12,6 +12,9 @@ import (
const ( const (
ArmedPartition_Armed = true ArmedPartition_Armed = true
ArmedPartition_Unrmed = false ArmedPartition_Unrmed = false
PartitionAlarm_Alarm = true
PartitionAlarm_AlarmCancelled = false
) )
type SatelNameGetter interface { type SatelNameGetter interface {
@ -47,7 +50,7 @@ func getSatelMessageContent(messages []satel.BasicEventElement, s SatelNameGette
} }
func (self SatelMsgContent) GetName() string { func (self SatelMsgContent) GetName() string {
if self.SatelEvent.Type == satel.ArmedPartition { if self.SatelEvent.Type == satel.ArmedPartition || self.SatelEvent.Type == satel.PartitionAlarm {
// Satel has 1-based indexes of partitions, go-satel has 0-based // Satel has 1-based indexes of partitions, go-satel has 0-based
name, err := self.s.GetName(satel.DeviceType_Partition, byte(self.SatelEvent.Index+1)) name, err := self.s.GetName(satel.DeviceType_Partition, byte(self.SatelEvent.Index+1))
if err != nil { if err != nil {
@ -93,6 +96,14 @@ func getArmedPartitionStatus(v bool) string {
} }
} }
func getPartitionAlarmStatus(v bool) string {
if v == PartitionAlarm_Alarm {
return "⚠️⚠️ ALARM! ⚠️⚠️"
} else {
return "Alarm cancelled"
}
}
func (self SatelMsgContent) FormatEvent() string { func (self SatelMsgContent) FormatEvent() string {
switch self.SatelEvent.Type { switch self.SatelEvent.Type {
case satel.ZoneViolation: case satel.ZoneViolation:
@ -134,7 +145,7 @@ func (self SatelMsgContent) FormatEvent() string {
case satel.PartitionBlockedForGuardRound: case satel.PartitionBlockedForGuardRound:
return fmt.Sprintf("%s: %t", self.SatelEvent.Type.String(), self.SatelEvent.Value) return fmt.Sprintf("%s: %t", self.SatelEvent.Type.String(), self.SatelEvent.Value)
case satel.PartitionAlarm: case satel.PartitionAlarm:
return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(self.SatelEvent.Value)) return getPartitionAlarmStatus(self.SatelEvent.Value)
case satel.PartitionFireAlarm: case satel.PartitionFireAlarm:
return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(self.SatelEvent.Value)) return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(self.SatelEvent.Value))
case satel.PartitionAlarmMemory: case satel.PartitionAlarmMemory:

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
} }