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

123 lines
3.3 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 {
chatIds := msg.chatIds.GetTgIds()
if chatIds == nil {
return nil
}
b := strings.Builder{}
for _, msg := range msg.msgs {
b.WriteString(msg.String())
b.WriteRune('\n')
}
message := b.String()
for _, chatId := range *chatIds {
toSend := tgbotapi.NewMessage(chatId, message)
_, err := self.bot.Send(toSend)
if err != nil {
return err
}
}
return nil
2024-02-10 06:51:36 +00:00
}
func sendTgMessage(tgEvents chan GenericMessage, msg string, chatId int64) {
tgEvents <- GenericMessage{TgChatId{chatId}, []MsgContent{MsgContent{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{}) {
2024-02-11 21:51:33 +00:00
go func() {
time.Sleep(self.duration)
ch <- nil
}()
2024-02-11 10:51:41 +00:00
}
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-18 00:44:10 +00:00
Consume(SendToTg(tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)),
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
2024-02-15 19:20:56 +00:00
for e := range FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)) {
2024-02-11 21:56:58 +00:00
logger.Print("Received change from SATEL: ", e)
sendTgMessage(tgEvents, fmt.Sprint("Change from SATEL: type:", e.Type, ", index:", e.Index, ", value:", e.Value), chatIds[0])
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
}