1
0
Fork 0
This commit is contained in:
Michał Rudowicz 2024-02-11 22:48:05 +01:00
parent 1f03609182
commit bec0241d9c
3 changed files with 21 additions and 10 deletions

17
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
@ -43,23 +44,21 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
ch <- nil
}
func getCmdLineParams() (string, []int64) {
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 {
fmt.Println("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
os.Exit(1)
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 {
fmt.Printf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
os.Exit(1)
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
}
chatIds = append(chatIds, chatId)
}
@ -81,20 +80,24 @@ func main() {
wg sync.WaitGroup
tgEvents = make(chan GenericMessage, 100)
sleeper = RealSleeper{time.Second * MessageNotMoreOftenThanSeconds}
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
)
satelAddr, chatIds := getCmdLineParams()
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}
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper)
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds))
for e := range s.Events {
logger.Println("Received change from SATEL: ", e)
sendTgMessage(tgEvents, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chatIds)
}

View File

@ -1,6 +1,7 @@
package main
import (
"log"
"strings"
"sync"
)
@ -18,7 +19,8 @@ type Sleeper interface {
Sleep(ch chan<- interface{})
}
func tgSenderWorker(tgEvents <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper) {
func tgSenderWorker(tgEvents <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper, logger *log.Logger) {
logger.Print("Starting")
wg.Add(1)
defer wg.Done()
messagesToSend := make(map[int64]*strings.Builder)
@ -40,10 +42,12 @@ loop:
messagesToSend[ev.chatId].WriteString(ev.msg)
messagesToSend[ev.chatId].WriteRune('\n')
if !waitingStarted {
logger.Print("Waiting for more messages to arrive before sending...")
waitingStarted = true
sleeper.Sleep(timeoutEvents)
}
case <-timeoutEvents:
logger.Print("Time's up, sending all messages we've got for now.")
waitingStarted = false
for chatId, msgBuilder := range messagesToSend {
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
@ -56,6 +60,7 @@ loop:
}
}
logger.Print("Exiting")
// If anything is left to be sent, send it now
for chatId, msgBuilder := range messagesToSend {
err := s.Send(GenericMessage{chatId, msgBuilder.String()})

View File

@ -1,9 +1,12 @@
package main
import (
"github.com/stretchr/testify/assert"
"io"
"log"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
type MockSender struct {
@ -32,7 +35,7 @@ func TestMessageThrottling(t *testing.T) {
wg := sync.WaitGroup{}
mockSender := MockSender{make([]GenericMessage, 0)}
mockSleeper := MockSleeper{nil, 0}
go tgSenderWorker(testEvents, &mockSender, &wg, &mockSleeper)
go tgSenderWorker(testEvents, &mockSender, &wg, &mockSleeper, log.New(io.Discard, "", log.Ltime))
testEvents <- GenericMessage{123, "test1"}
testEvents <- GenericMessage{124, "test3"}
testEvents <- GenericMessage{123, "test2"}