1
0
Fork 0
hswro-alarm-bot/config.go

182 lines
6.2 KiB
Go
Raw Permalink Normal View History

2024-03-23 09:40:20 +00:00
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
2024-03-23 09:40:20 +00:00
"time"
"git.sr.ht/~michalr/go-satel"
2024-03-24 17:39:12 +00:00
"gopkg.in/yaml.v3"
2024-03-23 09:40:20 +00:00
)
2024-03-24 17:39:12 +00:00
const (
ConfigFilePath = "hswro-alarm-bot.yml"
)
type OwnDuration struct {
duration time.Duration
}
type SatelChangeType struct {
changeType satel.ChangeType
}
2024-12-27 11:28:51 +00:00
type MatterbridgeConfig struct {
URI string `yaml:"uri"`
Token string `yaml:"token"`
Gateway string `yaml:"gateway"`
/// Username from which the messages will appear
Username string `yaml:"username"`
}
2024-03-23 09:40:20 +00:00
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"`
WriteMemoryProfile bool `yaml:"write-memory-profile"`
Matterbridge []MatterbridgeConfig `yaml:"matterbridge"`
TelegramApiKey string `yaml:"telegram-api-key"`
2024-03-24 17:39:12 +00:00
}
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.")
writeMemoryProfile := flag.Bool("write-memory-profile", false, "Whether application should dump its memory profile every 24 hours. Default: false")
flag.Parse()
2025-01-02 18:14:21 +00:00
if (len(*satelApiAddr) == 0 || len(*satelApiPort) == 0) && (len(config.SatelAddr) == 0) {
logger.Fatal("Satel address not provided. Use --satel-addr=ADDR and --satel-port=PORT or satel-addr field in the configuration file to solve this.")
}
if len(*satelApiAddr) != 0 && len(*satelApiPort) != 0 {
if len(config.SatelAddr) != 0 {
logger.Print("Overriding satel-addr from config with values from command line. ", config.SatelAddr, " will be replaced with ", *satelApiAddr, ":", *satelApiPort)
}
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
config.SatelAddr = satelAddr
}
var chatIds []int64
2025-01-02 18:14:21 +00:00
chatIdsStrings := strings.Split(*chatIdRaw, ",")
for _, chatIdStr := range chatIdsStrings {
2025-01-02 18:14:21 +00:00
if len(chatIdStr) == 0 {
continue
}
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, ",")
2024-03-24 17:39:12 +00:00
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)
}
2024-03-24 17:39:12 +00:00
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))
}
if writeMemoryProfile != nil {
config.WriteMemoryProfile = *writeMemoryProfile
}
2025-01-02 18:14:21 +00:00
config.ChatIds = append(config.ChatIds, chatIds...)
config.AllowedTypes = append(config.AllowedTypes, allowedTypes...)
config.AllowedIndexes = append(config.AllowedIndexes, allowedIndexes...)
}
func MakeConfig(logger *log.Logger) AppConfig {
2024-03-24 17:39:12 +00:00
config := loadConfigFromFile(ConfigFilePath, logger)
config.WriteMemoryProfile = false
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {
2024-03-24 15:54:33 +00:00
config.ArmCallbackUrls = append(config.ArmCallbackUrls, os.Getenv("NOTIFY_URL_ARM"))
}
if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 {
2024-03-24 15:54:33 +00:00
config.DisarmCallbackUrls = append(config.DisarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM"))
}
if len(os.Getenv("ALARM_URL_ARM")) != 0 {
2024-03-24 15:54:33 +00:00
config.AlarmCallbackUrls = append(config.AlarmCallbackUrls, os.Getenv("ALARM_URL_ARM"))
}
getCmdLineParams(&config, logger)
return config
2024-03-23 09:40:20 +00:00
}