1
0
Fork 0
hswro-alarm-bot/main.go

107 lines
2.8 KiB
Go
Raw Normal View History

2024-02-08 18:23:46 +00:00
package main
import (
2024-02-09 22:32:32 +00:00
"flag"
2024-02-08 18:23:46 +00:00
"fmt"
2024-02-11 21:48:05 +00:00
"log"
2024-02-08 18:23:46 +00:00
"net"
2024-02-09 22:32:32 +00:00
"os"
"strconv"
2024-02-09 22:32:32 +00:00
"strings"
"sync"
"time"
2024-02-08 18:23:46 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
2024-02-09 22:32:32 +00:00
"github.com/probakowski/go-satel"
2024-02-08 18:23:46 +00:00
)
2024-02-09 22:32:32 +00:00
const (
2024-02-10 23:13:31 +00:00
MessageNotMoreOftenThanSeconds = 15
2024-02-09 22:32:32 +00:00
)
2024-02-10 23:13:31 +00:00
type TgSender struct {
bot *tgbotapi.BotAPI
2024-02-09 22:32:32 +00:00
}
2024-02-10 23:13:31 +00:00
func (self TgSender) Send(msg GenericMessage) error {
2024-02-11 13:56:03 +00:00
toSend := tgbotapi.NewMessage(msg.chatId, msg.msg)
_, err := self.bot.Send(toSend)
2024-02-10 23:13:31 +00:00
return err
2024-02-10 06:51:36 +00:00
}
2024-02-11 13:56:03 +00:00
func sendTgMessage(tgEvents chan GenericMessage, msg string, chatIds []int64) {
for _, chatId := range chatIds {
tgEvents <- GenericMessage{chatId, msg}
2024-02-10 06:51:36 +00:00
}
}
2024-02-11 10:51:41 +00:00
type RealSleeper struct {
duration time.Duration
}
func (self RealSleeper) Sleep(ch chan<- interface{}) {
time.Sleep(self.duration)
ch <- nil
}
2024-02-11 21:48:05 +00:00
func getCmdLineParams(logger *log.Logger) (string, []int64) {
2024-02-11 13:56:03 +00:00
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")
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
2024-02-08 18:23:46 +00:00
flag.Parse()
2024-02-11 13:56:03 +00:00
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 {
2024-02-11 21:48:05 +00:00
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
2024-02-08 18:23:46 +00:00
}
2024-02-11 13:56:03 +00:00
chatIdsStrings := strings.Split(*chatIdRaw, ",")
var chatIds []int64
for _, chatIdStr := range chatIdsStrings {
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
if err != nil {
2024-02-11 21:48:05 +00:00
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
}
2024-02-11 13:56:03 +00:00
chatIds = append(chatIds, chatId)
}
2024-02-08 18:23:46 +00:00
2024-02-11 13:56:03 +00:00
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
return satelAddr, chatIds
}
func makeSatel(satelAddr string) *satel.Satel {
satelConn, err := net.Dial("tcp", satelAddr)
2024-02-08 18:23:46 +00:00
if err != nil {
panic(err)
}
2024-02-11 13:56:03 +00:00
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10})
}
func main() {
var (
wg sync.WaitGroup
tgEvents = make(chan GenericMessage, 100)
sleeper = RealSleeper{time.Second * MessageNotMoreOftenThanSeconds}
2024-02-11 21:48:05 +00:00
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
2024-02-11 13:56:03 +00:00
)
2024-02-11 21:48:05 +00:00
satelAddr, chatIds := getCmdLineParams(logger)
2024-02-11 13:56:03 +00:00
s := makeSatel(satelAddr)
2024-02-11 21:48:05 +00:00
logger.Printf("Connected to Satel: %s", satelAddr)
2024-02-11 13:56:03 +00:00
2024-02-08 18:23:46 +00:00
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
2024-02-09 22:32:32 +00:00
if err != nil {
panic(err)
}
2024-02-11 21:48:05 +00:00
logger.Print("Created Telegram Bot API client")
2024-02-09 22:32:32 +00:00
2024-02-10 23:13:31 +00:00
tgSender := TgSender{bot}
2024-02-11 21:48:05 +00:00
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds))
2024-02-11 13:56:03 +00:00
for e := range s.Events {
2024-02-11 21:48:05 +00:00
logger.Println("Received change from SATEL: ", e)
2024-02-11 13:56:03 +00:00
sendTgMessage(tgEvents, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chatIds)
2024-02-08 18:23:46 +00:00
}
2024-02-09 22:32:32 +00:00
2024-02-11 13:56:03 +00:00
close(tgEvents)
2024-02-09 22:32:32 +00:00
wg.Wait()
2024-02-08 18:23:46 +00:00
}