1
0
Fork 0

Make config fields public

This commit is contained in:
Michał Rudowicz 2024-03-24 16:54:33 +01:00
parent 12fc45ec38
commit 39ec8f2764
3 changed files with 26 additions and 26 deletions

View File

@ -13,14 +13,14 @@ import (
) )
type AppConfig struct { type AppConfig struct {
satelAddr string SatelAddr string
chatIds []int64 ChatIds []int64
allowedTypes []satel.ChangeType AllowedTypes []satel.ChangeType
allowedIndexes []int AllowedIndexes []int
poolInterval time.Duration PoolInterval time.Duration
armCallbackUrls []string ArmCallbackUrls []string
disarmCallbackUrls []string DisarmCallbackUrls []string
alarmCallbackUrls []string AlarmCallbackUrls []string
} }
func getCmdLineParams(config *AppConfig, logger *log.Logger) { func getCmdLineParams(config *AppConfig, logger *log.Logger) {
@ -71,27 +71,27 @@ func getCmdLineParams(config *AppConfig, logger *log.Logger) {
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
config.satelAddr = satelAddr config.SatelAddr = satelAddr
config.chatIds = chatIds config.ChatIds = chatIds
config.allowedTypes = allowedTypes config.AllowedTypes = allowedTypes
config.allowedIndexes = allowedIndexes config.AllowedIndexes = allowedIndexes
config.poolInterval = *satelPoolInterval config.PoolInterval = *satelPoolInterval
} }
func MakeConfig(logger *log.Logger) AppConfig { func MakeConfig(logger *log.Logger) AppConfig {
config := AppConfig{} config := AppConfig{}
config.armCallbackUrls = []string{} config.ArmCallbackUrls = []string{}
config.disarmCallbackUrls = []string{} config.DisarmCallbackUrls = []string{}
config.alarmCallbackUrls = []string{} config.AlarmCallbackUrls = []string{}
if len(os.Getenv("NOTIFY_URL_ARM")) != 0 { if len(os.Getenv("NOTIFY_URL_ARM")) != 0 {
config.armCallbackUrls = append(config.armCallbackUrls, os.Getenv("NOTIFY_URL_ARM")) config.ArmCallbackUrls = append(config.ArmCallbackUrls, os.Getenv("NOTIFY_URL_ARM"))
} }
if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 { if len(os.Getenv("NOTIFY_URL_DISARM")) != 0 {
config.disarmCallbackUrls = append(config.disarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM")) config.DisarmCallbackUrls = append(config.DisarmCallbackUrls, os.Getenv("NOTIFY_URL_DISARM"))
} }
if len(os.Getenv("ALARM_URL_ARM")) != 0 { if len(os.Getenv("ALARM_URL_ARM")) != 0 {
config.alarmCallbackUrls = append(config.alarmCallbackUrls, os.Getenv("ALARM_URL_ARM")) config.AlarmCallbackUrls = append(config.AlarmCallbackUrls, os.Getenv("ALARM_URL_ARM"))
} }
getCmdLineParams(&config, logger) getCmdLineParams(&config, logger)

View File

@ -58,8 +58,8 @@ func main() {
stopRequested.Store(false) stopRequested.Store(false)
config := MakeConfig(logger) config := MakeConfig(logger)
s := makeSatel(config.satelAddr, config.poolInterval) s := makeSatel(config.SatelAddr, config.PoolInterval)
logger.Printf("Connected to Satel: %s", config.satelAddr) logger.Printf("Connected to Satel: %s", config.SatelAddr)
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil { if err != nil {
@ -67,7 +67,7 @@ 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), config.ChatIds}
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
@ -84,7 +84,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, config.AllowedTypes, config.AllowedIndexes) {
logger.Print("Received change from SATEL: ", e) logger.Print("Received change from SATEL: ", e)
tgEvents <- GenericMessage{e.BasicEvents} tgEvents <- GenericMessage{e.BasicEvents}
} }

View File

@ -83,9 +83,9 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait
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 {
notifyAllHttp(config.armCallbackUrls, logger, wg) notifyAllHttp(config.ArmCallbackUrls, logger, wg)
} else { } else {
notifyAllHttp(config.disarmCallbackUrls, logger, wg) notifyAllHttp(config.DisarmCallbackUrls, logger, wg)
} }
break inner_arm break inner_arm
} }
@ -94,7 +94,7 @@ func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.Wait
for _, basicElement := range e.Messages { for _, basicElement := range e.Messages {
if basicElement.Type == satel.PartitionAlarm { if basicElement.Type == satel.PartitionAlarm {
if basicElement.Value == PartitionAlarm_Alarm { if basicElement.Value == PartitionAlarm_Alarm {
notifyAllHttp(config.alarmCallbackUrls, logger, wg) notifyAllHttp(config.AlarmCallbackUrls, logger, wg)
break inner_alarm break inner_alarm
} }
} }