100 lines
3.6 KiB
Go
100 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.sr.ht/~michalr/go-satel"
|
|
)
|
|
|
|
type AppConfig struct {
|
|
SatelAddr string
|
|
ChatIds []int64
|
|
AllowedTypes []satel.ChangeType
|
|
AllowedIndexes []int
|
|
PoolInterval time.Duration
|
|
ArmCallbackUrls []string
|
|
DisarmCallbackUrls []string
|
|
AlarmCallbackUrls []string
|
|
}
|
|
|
|
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 []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)
|
|
|
|
config.SatelAddr = satelAddr
|
|
config.ChatIds = chatIds
|
|
config.AllowedTypes = allowedTypes
|
|
config.AllowedIndexes = allowedIndexes
|
|
config.PoolInterval = *satelPoolInterval
|
|
}
|
|
|
|
func MakeConfig(logger *log.Logger) AppConfig {
|
|
config := AppConfig{}
|
|
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
|
|
}
|