103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"math"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/probakowski/go-satel"
|
|
)
|
|
|
|
const (
|
|
MaximumMessagesPerMinute = 2
|
|
NanosecondsPerMinute = 6e10
|
|
)
|
|
|
|
type TgEvent struct {
|
|
msg tgbotapi.MessageConfig
|
|
}
|
|
|
|
func tg_sender_worker(tg_events <-chan TgEvent, bot *tgbotapi.BotAPI, wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
defer wg.Done()
|
|
lastExecutionTime := time.Now()
|
|
minimumTimeBetweenEachExecution := time.Duration(math.Ceil(NanosecondsPerMinute / MaximumMessagesPerMinute))
|
|
_, shouldOmitTg := os.LookupEnv("OMIT_TG")
|
|
|
|
for ev := range tg_events {
|
|
defer func() { lastExecutionTime = time.Now() }()
|
|
|
|
time.Sleep(minimumTimeBetweenEachExecution - time.Since(lastExecutionTime))
|
|
fmt.Println("Will send to TG: ", ev.msg.Text)
|
|
if shouldOmitTg {
|
|
continue
|
|
}
|
|
_, err := bot.Send(ev.msg)
|
|
if err != nil {
|
|
// TODO: handle it better
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func send_tg_message(tg_events chan TgEvent, msg tgbotapi.MessageConfig) {
|
|
tg_events <- TgEvent{msg}
|
|
}
|
|
|
|
func main() {
|
|
var wg sync.WaitGroup
|
|
tg_events := make(chan TgEvent)
|
|
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.")
|
|
flag.Parse()
|
|
|
|
if len(*satel_api_addr) == 0 || len(*satel_api_port) == 0 || len(*chat_id_raw) == 0 {
|
|
fmt.Println("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
|
os.Exit(1)
|
|
}
|
|
chat_ids_strings := strings.Split(*chat_id_raw, ",")
|
|
var chat_ids []int64
|
|
for _, chat_id_str := range chat_ids_strings {
|
|
chat_id, err := strconv.ParseInt(chat_id_str, 10, 64)
|
|
if err != nil {
|
|
fmt.Printf("Tried to use a non-int value for one of tg_chat_ids: %s. That's bad.", chat_id_str)
|
|
os.Exit(1)
|
|
}
|
|
chat_ids = append(chat_ids, chat_id)
|
|
}
|
|
|
|
satel_addr := fmt.Sprintf("%s:%s", *satel_api_addr, *satel_api_port)
|
|
satel_conn, err := net.Dial("tcp", satel_addr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
s := satel.NewConfig(satel_conn, satel.Config{EventsQueueSize: 1000})
|
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
close(tg_events)
|
|
wg.Wait()
|
|
}
|