From 7f3b5a4abe6051b381922e809b8acd6f55fa4fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sat, 10 Feb 2024 07:51:36 +0100 Subject: [PATCH] Little reorganizing --- main.go | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 34313c2..2fcf0de 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,28 @@ type TgEvent struct { msg tgbotapi.MessageConfig } +type SecurityEvent interface { + Execute(chat_ids []int64, tg_events chan TgEvent) +} + +type SatelEvent struct { + ev satel.Event +} + +func (s SatelEvent) Execute(chat_ids []int64, tg_events chan TgEvent) { + fmt.Println("Change from SATEL: ", "type", s.ev.Type, "index", s.ev.Index, "value", s.ev.Value) + for _, chat_id := range chat_ids { + msg := tgbotapi.NewMessage(chat_id, fmt.Sprintf("Change from SATEL: Zone: %d Type: %s Value: %t", + s.ev.Index, s.ev.Type, s.ev.Value)) + + send_tg_message(tg_events, msg) + } +} + +type EmptyEvent struct{} + +func (s EmptyEvent) Execute(chat_ids []int64, tg_events chan TgEvent) {} + func tg_sender_worker(tg_events <-chan TgEvent, bot *tgbotapi.BotAPI, wg *sync.WaitGroup) { wg.Add(1) defer wg.Done() @@ -52,8 +74,11 @@ func send_tg_message(tg_events chan TgEvent, msg tgbotapi.MessageConfig) { } func main() { - var wg sync.WaitGroup - tg_events := make(chan TgEvent) + var ( + wg sync.WaitGroup + tg_events = make(chan TgEvent) + sec_events = make(chan SecurityEvent) + ) satel_api_addr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device") satel_api_port := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device") chat_id_raw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.") @@ -86,15 +111,12 @@ func main() { } go tg_sender_worker(tg_events, bot, &wg) - for e, ok := <-s.Events; ok; e, ok = <-s.Events { - fmt.Println("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value) - for _, chat_id := range chat_ids { - msg := tgbotapi.NewMessage(chat_id, fmt.Sprintf("Change from SATEL: Zone: %d Type: %s Value: %t", - e.Index, e.Type, e.Value)) + sec_events <- SatelEvent{e} + } - send_tg_message(tg_events, msg) - } + for ev := range sec_events { + ev.Execute(chat_ids, tg_events) } close(tg_events)