Little reorganizing
This commit is contained in:
parent
f574369d0d
commit
7f3b5a4abe
40
main.go
40
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))
|
||||
|
||||
send_tg_message(tg_events, msg)
|
||||
sec_events <- SatelEvent{e}
|
||||
}
|
||||
|
||||
for ev := range sec_events {
|
||||
ev.Execute(chat_ids, tg_events)
|
||||
}
|
||||
|
||||
close(tg_events)
|
||||
|
|
Loading…
Reference in New Issue