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

104 lines
2.5 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"
"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 13:56:03 +00:00
func getCmdLineParams() (string, []int64) {
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-08 21:09:31 +00:00
fmt.Println("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
os.Exit(1)
}
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 13:56:03 +00:00
fmt.Printf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
os.Exit(1)
}
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}
)
satelAddr, chatIds := getCmdLineParams()
s := makeSatel(satelAddr)
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-10 23:13:31 +00:00
tgSender := TgSender{bot}
2024-02-11 13:56:03 +00:00
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper)
for e := range s.Events {
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
}