Compare commits
No commits in common. "4f91940047e78d0137237432a9211af4a16ce017" and "c0268633423928a045f5500ac5a337867970104d" have entirely different histories.
4f91940047
...
c026863342
162
config.go
162
config.go
|
@ -1,162 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ConfigFilePath = "hswro-alarm-bot.yml"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OwnDuration struct {
|
|
||||||
duration time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
type SatelChangeType struct {
|
|
||||||
changeType satel.ChangeType
|
|
||||||
}
|
|
||||||
|
|
||||||
type AppConfig struct {
|
|
||||||
SatelAddr string `yaml:"satel-addr"`
|
|
||||||
ChatIds []int64 `yaml:"tg-chat-ids"`
|
|
||||||
AllowedTypes []SatelChangeType `yaml:"allowed-types"`
|
|
||||||
AllowedIndexes []int `yaml:"allowed-indexes"`
|
|
||||||
PoolInterval OwnDuration `yaml:"pool-interval"`
|
|
||||||
ArmCallbackUrls []string `yaml:"arm-callback-urls"`
|
|
||||||
DisarmCallbackUrls []string `yaml:"disarm-callback-urls"`
|
|
||||||
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
||||||
var inputStr string
|
|
||||||
err := unmarshal(&inputStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ct, err := StringToSatelChangeType(inputStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*m = SatelChangeType{ct}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *OwnDuration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
||||||
var inputStr string
|
|
||||||
err := unmarshal(&inputStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
duration, err := time.ParseDuration(inputStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*m = OwnDuration{duration}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self SatelChangeType) GetChangeType() satel.ChangeType { return self.changeType }
|
|
||||||
func (self OwnDuration) GetDuration() time.Duration { return self.duration }
|
|
||||||
|
|
||||||
func loadConfigFromFile(filePath string, logger *log.Logger) AppConfig {
|
|
||||||
f, err := os.ReadFile(filePath)
|
|
||||||
if err != nil {
|
|
||||||
logger.Print("Error opening config file: ", err, ". Trying to continue without it")
|
|
||||||
return AppConfig{}
|
|
||||||
}
|
|
||||||
return parseConfigFromFile(f, logger)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseConfigFromFile(contents []byte, logger *log.Logger) AppConfig {
|
|
||||||
var config AppConfig
|
|
||||||
err := yaml.Unmarshal(contents, &config)
|
|
||||||
if err != nil {
|
|
||||||
logger.Print("Error while parsing config file: ", err, ". Trying to continue without it")
|
|
||||||
return AppConfig{}
|
|
||||||
}
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCmdLineParams(config *AppConfig, logger *log.Logger) {
|
|
||||||
satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device")
|
|
||||||
satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device")
|
|
||||||
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
|
||||||
allowedTypesRaw := flag.String("allowed-types", "", "Satel change types that are allowed. All other types will be discarded. By default all are allowed. Use \",\" to specify multiple types.")
|
|
||||||
allowedIndexesRaw := flag.String("allowed-indexes", "", "Satel indexes (zones?) that are allowed. All other indexes will be discarded. By default all are allowed. Use \",\" to specify multiple indexes.")
|
|
||||||
satelPoolInterval := flag.Duration("pool-interval", 5*time.Second, "How often should the SATEL device be pooled for changes? Default: 5 seconds.")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 {
|
|
||||||
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
|
||||||
}
|
|
||||||
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
|
||||||
var chatIds []int64
|
|
||||||
for _, chatIdStr := range chatIdsStrings {
|
|
||||||
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
|
|
||||||
}
|
|
||||||
chatIds = append(chatIds, chatId)
|
|
||||||
}
|
|
||||||
allowedTypesStrings := strings.Split(*allowedTypesRaw, ",")
|
|
||||||
var allowedTypes []SatelChangeType
|
|
||||||
for _, allowedTypeStr := range allowedTypesStrings {
|
|
||||||
if len(allowedTypeStr) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
allowedType, err := StringToSatelChangeType(allowedTypeStr)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Error trying to understand an allowed type: %s.", err)
|
|
||||||
}
|
|
||||||
allowedTypes = append(allowedTypes, SatelChangeType{allowedType})
|
|
||||||
}
|
|
||||||
allowedIndexesStrings := strings.Split(*allowedIndexesRaw, ",")
|
|
||||||
var allowedIndexes []int
|
|
||||||
for _, allowedIndexStr := range allowedIndexesStrings {
|
|
||||||
if len(allowedIndexStr) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
allowedIndex, err := strconv.ParseInt(allowedIndexStr, 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Tried to use a non-int value for one of allowed indexes: %s. That's bad.", allowedIndexStr)
|
|
||||||
}
|
|
||||||
allowedIndexes = append(allowedIndexes, int(allowedIndex))
|
|
||||||
}
|
|
||||||
|
|
||||||
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
|
|
||||||
|
|
||||||
config.SatelAddr = satelAddr
|
|
||||||
config.ChatIds = chatIds
|
|
||||||
config.AllowedTypes = allowedTypes
|
|
||||||
config.AllowedIndexes = allowedIndexes
|
|
||||||
config.PoolInterval = OwnDuration{*satelPoolInterval}
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeConfig(logger *log.Logger) AppConfig {
|
|
||||||
config := loadConfigFromFile(ConfigFilePath, logger)
|
|
||||||
config.ArmCallbackUrls = []string{}
|
|
||||||
config.DisarmCallbackUrls = []string{}
|
|
||||||
config.AlarmCallbackUrls = []string{}
|
|
||||||
|
|
||||||
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {
|
|
||||||
config.ArmCallbackUrls = append(config.ArmCallbackUrls, os.Getenv("NOTIFY_URL_ARM"))
|
|
||||||
}
|
|
||||||
if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 {
|
|
||||||
config.DisarmCallbackUrls = append(config.DisarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM"))
|
|
||||||
}
|
|
||||||
if len(os.Getenv("ALARM_URL_ARM")) != 0 {
|
|
||||||
config.AlarmCallbackUrls = append(config.AlarmCallbackUrls, os.Getenv("ALARM_URL_ARM"))
|
|
||||||
}
|
|
||||||
|
|
||||||
getCmdLineParams(&config, logger)
|
|
||||||
return config
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
const data = `
|
|
||||||
satel-addr: "test satel address"
|
|
||||||
tg-chat-ids:
|
|
||||||
- 1234
|
|
||||||
- 5678
|
|
||||||
- 9876
|
|
||||||
allowed-types:
|
|
||||||
- "zone-isolate"
|
|
||||||
- "zone-alarm"
|
|
||||||
allowed-indexes:
|
|
||||||
- 5678
|
|
||||||
- 1337
|
|
||||||
pool-interval: 5m
|
|
||||||
arm-callback-urls:
|
|
||||||
- "test arm callback url"
|
|
||||||
- "second test arm callback url"
|
|
||||||
disarm-callback-urls:
|
|
||||||
- "test disarm callback url"
|
|
||||||
- "second test disarm callback url"
|
|
||||||
alarm-callback-urls:
|
|
||||||
- "test alarm callback url"
|
|
||||||
- "second test alarm callback url"
|
|
||||||
`
|
|
||||||
|
|
||||||
func TestParseYamlConfig(t *testing.T) {
|
|
||||||
a := assert.New(t)
|
|
||||||
|
|
||||||
actualConfig := parseConfigFromFile([]byte(data), log.New(os.Stderr, "", log.Ltime))
|
|
||||||
|
|
||||||
a.Equal("test satel address", actualConfig.SatelAddr)
|
|
||||||
a.ElementsMatch([]int64{1234, 5678, 9876}, actualConfig.ChatIds)
|
|
||||||
a.ElementsMatch([]int{5678, 1337}, actualConfig.AllowedIndexes)
|
|
||||||
a.ElementsMatch([]SatelChangeType{{satel.ZoneIsolate}, {satel.ZoneAlarm}}, actualConfig.AllowedTypes)
|
|
||||||
a.Equal(5*time.Minute, actualConfig.PoolInterval.GetDuration())
|
|
||||||
a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls)
|
|
||||||
a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls)
|
|
||||||
a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls)
|
|
||||||
}
|
|
|
@ -7,9 +7,9 @@ import (
|
||||||
"git.sr.ht/~michalr/go-satel"
|
"git.sr.ht/~michalr/go-satel"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isBasicEventElementOkay(basicEventElement satel.BasicEventElement, allowedTypes []SatelChangeType, allowedIndexes []int) bool {
|
func isBasicEventElementOkay(basicEventElement satel.BasicEventElement, allowedTypes []satel.ChangeType, allowedIndexes []int) bool {
|
||||||
for _, allowedType := range allowedTypes {
|
for _, allowedType := range allowedTypes {
|
||||||
if allowedType.GetChangeType() == basicEventElement.Type {
|
if allowedType == basicEventElement.Type {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func isBasicEventElementOkay(basicEventElement satel.BasicEventElement, allowedT
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilterByTypeOrIndex(ev <-chan satel.Event, wg *sync.WaitGroup, allowedTypes []SatelChangeType, allowedIndexes []int) <-chan satel.Event {
|
func FilterByTypeOrIndex(ev <-chan satel.Event, wg *sync.WaitGroup, allowedTypes []satel.ChangeType, allowedIndexes []int) <-chan satel.Event {
|
||||||
returnChan := make(chan satel.Event)
|
returnChan := make(chan satel.Event)
|
||||||
|
|
||||||
if (len(allowedTypes) == 0) && (len(allowedIndexes) == 0) {
|
if (len(allowedTypes) == 0) && (len(allowedIndexes) == 0) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ func TestSatelEventTypeFiltering(t *testing.T) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{{satel.ArmedPartition}, {satel.PartitionFireAlarm}}, []int{}) {
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}, []int{}) {
|
||||||
receivedEvents = append(receivedEvents, e)
|
receivedEvents = append(receivedEvents, e)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
@ -46,7 +46,7 @@ func TestSatelEventTypeFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testin
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{}) {
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{}) {
|
||||||
receivedEvents = append(receivedEvents, e)
|
receivedEvents = append(receivedEvents, e)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
@ -72,7 +72,7 @@ func TestSatelIndexFiltering(t *testing.T) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{1, 3}) {
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{1, 3}) {
|
||||||
receivedEvents = append(receivedEvents, e)
|
receivedEvents = append(receivedEvents, e)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
@ -101,7 +101,7 @@ func TestSatelIndexFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{}) {
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{}) {
|
||||||
receivedEvents = append(receivedEvents, e)
|
receivedEvents = append(receivedEvents, e)
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
66
main.go
66
main.go
|
@ -1,11 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
@ -30,6 +34,56 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, []int, time.Duration) {
|
||||||
|
satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device")
|
||||||
|
satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device")
|
||||||
|
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
||||||
|
allowedTypesRaw := flag.String("allowed-types", "", "Satel change types that are allowed. All other types will be discarded. By default all are allowed. Use \",\" to specify multiple types.")
|
||||||
|
allowedIndexesRaw := flag.String("allowed-indexes", "", "Satel indexes (zones?) that are allowed. All other indexes will be discarded. By default all are allowed. Use \",\" to specify multiple indexes.")
|
||||||
|
satelPoolInterval := flag.Duration("pool-interval", 5*time.Second, "How often should the SATEL device be pooled for changes? Default: 5 seconds.")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 {
|
||||||
|
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
||||||
|
}
|
||||||
|
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
||||||
|
var chatIds []int64
|
||||||
|
for _, chatIdStr := range chatIdsStrings {
|
||||||
|
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
|
||||||
|
}
|
||||||
|
chatIds = append(chatIds, chatId)
|
||||||
|
}
|
||||||
|
allowedTypesStrings := strings.Split(*allowedTypesRaw, ",")
|
||||||
|
var allowedTypes []satel.ChangeType
|
||||||
|
for _, allowedTypeStr := range allowedTypesStrings {
|
||||||
|
if len(allowedTypeStr) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
allowedType, err := StringToSatelChangeType(allowedTypeStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("Error trying to understand an allowed type: %s.", err)
|
||||||
|
}
|
||||||
|
allowedTypes = append(allowedTypes, allowedType)
|
||||||
|
}
|
||||||
|
allowedIndexesStrings := strings.Split(*allowedIndexesRaw, ",")
|
||||||
|
var allowedIndexes []int
|
||||||
|
for _, allowedIndexStr := range allowedIndexesStrings {
|
||||||
|
if len(allowedIndexStr) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
allowedIndex, err := strconv.ParseInt(allowedIndexStr, 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("Tried to use a non-int value for one of allowed indexes: %s. That's bad.", allowedIndexStr)
|
||||||
|
}
|
||||||
|
allowedIndexes = append(allowedIndexes, int(allowedIndex))
|
||||||
|
}
|
||||||
|
|
||||||
|
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
|
||||||
|
return satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval
|
||||||
|
}
|
||||||
|
|
||||||
func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel {
|
func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel {
|
||||||
satelConn, err := net.Dial("tcp", satelAddr)
|
satelConn, err := net.Dial("tcp", satelAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,10 +110,10 @@ func main() {
|
||||||
)
|
)
|
||||||
|
|
||||||
stopRequested.Store(false)
|
stopRequested.Store(false)
|
||||||
config := MakeConfig(logger)
|
satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger)
|
||||||
|
|
||||||
s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration())
|
s := makeSatel(satelAddr, poolInterval)
|
||||||
logger.Printf("Connected to Satel: %s", config.SatelAddr)
|
logger.Printf("Connected to Satel: %s", satelAddr)
|
||||||
|
|
||||||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -67,14 +121,14 @@ func main() {
|
||||||
}
|
}
|
||||||
logger.Print("Created Telegram Bot API client")
|
logger.Print("Created Telegram Bot API client")
|
||||||
|
|
||||||
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
|
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), chatIds}
|
||||||
|
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
|
||||||
|
|
||||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||||
|
|
||||||
Consume(
|
Consume(
|
||||||
SendToTg(Throttle(NotifyViaHTTP(tgEvents, config, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),
|
SendToTg(Throttle(NotifyViaHTTP(tgEvents, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),
|
||||||
&wg, sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds)),
|
&wg, sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds)),
|
||||||
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
|
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
|
||||||
)
|
)
|
||||||
|
@ -84,7 +138,7 @@ func main() {
|
||||||
for stopRequested.Load() == false {
|
for stopRequested.Load() == false {
|
||||||
for e := range FilterByTypeOrIndex(
|
for e := range FilterByTypeOrIndex(
|
||||||
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||||
&wg, config.AllowedTypes, config.AllowedIndexes) {
|
&wg, allowedTypes, allowedIndexes) {
|
||||||
logger.Print("Received change from SATEL: ", e)
|
logger.Print("Received change from SATEL: ", e)
|
||||||
tgEvents <- GenericMessage{e.BasicEvents}
|
tgEvents <- GenericMessage{e.BasicEvents}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
"git.sr.ht/~michalr/go-satel"
|
||||||
|
@ -51,7 +52,6 @@ func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger
|
||||||
func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
if len(url) == 0 {
|
if len(url) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -65,41 +65,43 @@ 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 notifyAllHttp(urls []string, logger *log.Logger, wg *sync.WaitGroup) {
|
func NotifyViaHTTP(events <-chan GenericMessage, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage {
|
||||||
for _, uri := range urls {
|
|
||||||
go doHttpNotification(uri, logger, wg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage {
|
|
||||||
returnEvents := make(chan GenericMessage)
|
returnEvents := make(chan GenericMessage)
|
||||||
|
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() {
|
go func() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for e := range events {
|
for e := range events {
|
||||||
returnEvents <- e
|
returnEvents <- e
|
||||||
inner_arm:
|
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) {
|
||||||
notifyAllHttp(config.ArmCallbackUrls, logger, wg)
|
if basicElement.Value == ArmedPartition_Armed {
|
||||||
} else {
|
go doHttpNotification(armCallbackUrl, logger, wg)
|
||||||
notifyAllHttp(config.DisarmCallbackUrls, logger, wg)
|
} else {
|
||||||
}
|
go doHttpNotification(disarmCallbackUrl, logger, wg)
|
||||||
break inner_arm
|
}
|
||||||
}
|
break inner_arm
|
||||||
}
|
}
|
||||||
inner_alarm:
|
}
|
||||||
for _, basicElement := range e.Messages {
|
}
|
||||||
if basicElement.Type == satel.PartitionAlarm {
|
if alarmCallbackEnabled {
|
||||||
if basicElement.Value == PartitionAlarm_Alarm {
|
inner_alarm:
|
||||||
notifyAllHttp(config.AlarmCallbackUrls, logger, wg)
|
for _, basicElement := range e.Messages {
|
||||||
break inner_alarm
|
if basicElement.Type == satel.PartitionAlarm {
|
||||||
|
if basicElement.Value == PartitionAlarm_Alarm {
|
||||||
|
go doHttpNotification(alarmCallbackUrl, logger, wg)
|
||||||
|
break inner_alarm
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
close(returnEvents)
|
close(returnEvents)
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Reference in New Issue