138 lines
3.5 KiB
Go
138 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/probakowski/go-satel"
|
|
)
|
|
|
|
const (
|
|
MessageNotMoreOftenThanSeconds = 15
|
|
)
|
|
|
|
type TgSender struct {
|
|
bot *tgbotapi.BotAPI
|
|
}
|
|
|
|
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.TgString())
|
|
b.WriteRune('\n')
|
|
}
|
|
message := b.String()
|
|
for _, chatId := range *chatIds {
|
|
toSend := tgbotapi.NewMessage(chatId, message)
|
|
toSend.ParseMode = "HTML"
|
|
_, err := self.bot.Send(toSend)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func sendTgMessage(tgEvents chan GenericMessage, msg MsgContent, chatId int64) {
|
|
tgEvents <- GenericMessage{TgChatId{chatId}, []MsgContent{msg}}
|
|
}
|
|
|
|
type RealSleeper struct {
|
|
duration time.Duration
|
|
}
|
|
|
|
type SatelMsgContent struct {
|
|
ev satel.Event
|
|
}
|
|
|
|
func (self SatelMsgContent) TgString() string {
|
|
return fmt.Sprint("<b>", self.ev.Type, "</b>, <i>index</i>:<b>", self.ev.Index, "</b>, value:<b>", self.ev.Value, "</b>")
|
|
}
|
|
|
|
func (self RealSleeper) Sleep(ch chan<- interface{}) {
|
|
go func() {
|
|
time.Sleep(self.duration)
|
|
ch <- nil
|
|
}()
|
|
}
|
|
|
|
func getCmdLineParams(logger *log.Logger) (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.")
|
|
flag.Parse()
|
|
|
|
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 0 {
|
|
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
|
}
|
|
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
|
var chatIds []int64
|
|
for _, chatIdStr := range chatIdsStrings {
|
|
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
|
|
if err != nil {
|
|
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
|
|
}
|
|
chatIds = append(chatIds, chatId)
|
|
}
|
|
|
|
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
|
|
return satelAddr, chatIds
|
|
}
|
|
|
|
func makeSatel(satelAddr string) *satel.Satel {
|
|
satelConn, err := net.Dial("tcp", satelAddr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10})
|
|
}
|
|
|
|
func main() {
|
|
var (
|
|
wg sync.WaitGroup
|
|
tgEvents = make(chan GenericMessage, 100)
|
|
sleeper = RealSleeper{time.Second * MessageNotMoreOftenThanSeconds}
|
|
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
|
|
)
|
|
|
|
satelAddr, chatIds := getCmdLineParams(logger)
|
|
|
|
s := makeSatel(satelAddr)
|
|
logger.Printf("Connected to Satel: %s", satelAddr)
|
|
|
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
logger.Print("Created Telegram Bot API client")
|
|
|
|
tgSender := TgSender{bot}
|
|
|
|
Consume(
|
|
SendToTg(
|
|
tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)),
|
|
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
|
|
|
|
for e := range FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)) {
|
|
logger.Print("Received change from SATEL: ", e)
|
|
for _, chatId := range chatIds {
|
|
sendTgMessage(tgEvents, SatelMsgContent{e}, chatId)
|
|
}
|
|
}
|
|
|
|
close(tgEvents)
|
|
wg.Wait()
|
|
}
|