1
0
Fork 0

Introduction of AppConfig

This commit is contained in:
Michał Rudowicz 2024-03-23 10:40:20 +01:00
parent c026863342
commit 2c3286206e
2 changed files with 22 additions and 7 deletions

15
config.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"time"
"git.sr.ht/~michalr/go-satel"
)
type AppConfig struct {
satelAddr string
chatIds []int64
allowedTypes []satel.ChangeType
allowedIndexes []int
poolInterval time.Duration
}

14
main.go
View File

@ -34,7 +34,7 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
}() }()
} }
func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType, []int, time.Duration) { func getCmdLineParams(logger *log.Logger) AppConfig {
satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device") 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") 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.") chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
@ -81,7 +81,7 @@ func getCmdLineParams(logger *log.Logger) (string, []int64, []satel.ChangeType,
} }
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort) satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
return satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval return AppConfig{satelAddr, chatIds, allowedTypes, allowedIndexes, *satelPoolInterval}
} }
func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel { func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel {
@ -110,10 +110,10 @@ func main() {
) )
stopRequested.Store(false) stopRequested.Store(false)
satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger) cfg := getCmdLineParams(logger)
s := makeSatel(satelAddr, poolInterval) s := makeSatel(cfg.satelAddr, cfg.poolInterval)
logger.Printf("Connected to Satel: %s", satelAddr) logger.Printf("Connected to Satel: %s", cfg.satelAddr)
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil { if err != nil {
@ -121,7 +121,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), chatIds} tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), cfg.chatIds}
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))
@ -138,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, allowedTypes, allowedIndexes) { &wg, cfg.allowedTypes, cfg.allowedIndexes) {
logger.Print("Received change from SATEL: ", e) logger.Print("Received change from SATEL: ", e)
tgEvents <- GenericMessage{e.BasicEvents} tgEvents <- GenericMessage{e.BasicEvents}
} }