Compare commits
No commits in common. "ed9981afbeda107cebda7adc7636c24539bcf549" and "b12d7168c9bd34701f51725a00f2124dd7e47f70" have entirely different histories.
ed9981afbe
...
b12d7168c9
|
@ -17,9 +17,8 @@ $ 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
2
go.mod
|
@ -3,7 +3,7 @@ module alarm_bot
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733
|
git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8
|
||||||
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
2
go.sum
|
@ -8,8 +8,6 @@ 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=
|
||||||
|
|
7
main.go
7
main.go
|
@ -18,6 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
MessageNotMoreOftenThanSeconds = 15
|
||||||
PersistenceFilename = "hs_wro_last_seen.bin"
|
PersistenceFilename = "hs_wro_last_seen.bin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -149,11 +150,11 @@ func main() {
|
||||||
|
|
||||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||||
|
|
||||||
NotifyViaHTTP(
|
Consume(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),
|
logger,
|
||||||
)
|
))
|
||||||
|
|
||||||
go CloseSatelOnCtrlC(s)
|
go CloseSatelOnCtrlC(s)
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,6 @@ 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 {
|
||||||
|
@ -50,7 +47,7 @@ func getSatelMessageContent(messages []satel.BasicEventElement, s SatelNameGette
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self SatelMsgContent) GetName() string {
|
func (self SatelMsgContent) GetName() string {
|
||||||
if self.SatelEvent.Type == satel.ArmedPartition || self.SatelEvent.Type == satel.PartitionAlarm {
|
if self.SatelEvent.Type == satel.ArmedPartition {
|
||||||
// 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 {
|
||||||
|
@ -96,14 +93,6 @@ 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:
|
||||||
|
@ -145,7 +134,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 getPartitionAlarmStatus(self.SatelEvent.Value)
|
return fmt.Sprintf("%s: %s", self.SatelEvent.Type.String(), getEmojiWhenTrueIsBad(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:
|
||||||
|
|
|
@ -77,18 +77,16 @@ 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) {
|
func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage {
|
||||||
|
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 {
|
||||||
if armDisarmCallbackEnabled {
|
inner:
|
||||||
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 {
|
||||||
|
@ -96,21 +94,12 @@ func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log
|
||||||
} else {
|
} else {
|
||||||
go doHttpNotification(disarmCallbackUrl, logger, wg)
|
go doHttpNotification(disarmCallbackUrl, logger, wg)
|
||||||
}
|
}
|
||||||
break inner_arm
|
break inner
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue