General cleanup, testing improvements
This commit is contained in:
parent
efbd8e882c
commit
1f03609182
68
main.go
68
main.go
|
@ -23,14 +23,14 @@ type TgSender struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self TgSender) Send(msg GenericMessage) error {
|
func (self TgSender) Send(msg GenericMessage) error {
|
||||||
to_send := tgbotapi.NewMessage(msg.chat_id, msg.msg)
|
toSend := tgbotapi.NewMessage(msg.chatId, msg.msg)
|
||||||
_, err := self.bot.Send(to_send)
|
_, err := self.bot.Send(toSend)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func send_tg_message(tg_events chan GenericMessage, msg string, chat_ids []int64) {
|
func sendTgMessage(tgEvents chan GenericMessage, msg string, chatIds []int64) {
|
||||||
for _, chat_id := range chat_ids {
|
for _, chatId := range chatIds {
|
||||||
tg_events <- GenericMessage{chat_id, msg}
|
tgEvents <- GenericMessage{chatId, msg}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,49 +43,61 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
|
||||||
ch <- nil
|
ch <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func getCmdLineParams() (string, []int64) {
|
||||||
var (
|
satelApiAddr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device")
|
||||||
wg sync.WaitGroup
|
satelApiPort := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device")
|
||||||
tg_events = make(chan GenericMessage)
|
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
||||||
sleeper = RealSleeper{time.Second * 15}
|
|
||||||
)
|
|
||||||
satel_api_addr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device")
|
|
||||||
satel_api_port := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device")
|
|
||||||
chat_id_raw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if len(*satel_api_addr) == 0 || len(*satel_api_port) == 0 || len(*chat_id_raw) == 0 {
|
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.")
|
fmt.Println("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
chat_ids_strings := strings.Split(*chat_id_raw, ",")
|
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
||||||
var chat_ids []int64
|
var chatIds []int64
|
||||||
for _, chat_id_str := range chat_ids_strings {
|
for _, chatIdStr := range chatIdsStrings {
|
||||||
chat_id, err := strconv.ParseInt(chat_id_str, 10, 64)
|
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Tried to use a non-int value for one of tg_chat_ids: %s. That's bad.", chat_id_str)
|
fmt.Printf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
chat_ids = append(chat_ids, chat_id)
|
chatIds = append(chatIds, chatId)
|
||||||
}
|
}
|
||||||
|
|
||||||
satel_addr := fmt.Sprintf("%s:%s", *satel_api_addr, *satel_api_port)
|
satelAddr := fmt.Sprintf("%s:%s", *satelApiAddr, *satelApiPort)
|
||||||
satel_conn, err := net.Dial("tcp", satel_addr)
|
return satelAddr, chatIds
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeSatel(satelAddr string) *satel.Satel {
|
||||||
|
satelConn, err := net.Dial("tcp", satelAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
s := satel.NewConfig(satel_conn, satel.Config{EventsQueueSize: 1000})
|
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)
|
||||||
|
|
||||||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tgSender := TgSender{bot}
|
tgSender := TgSender{bot}
|
||||||
go tg_sender_worker(tg_events, tgSender, &wg, sleeper)
|
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper)
|
||||||
for e, ok := <-s.Events; ok; e, ok = <-s.Events {
|
for e := range s.Events {
|
||||||
send_tg_message(tg_events, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chat_ids)
|
sendTgMessage(tgEvents, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chatIds)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(tg_events)
|
close(tgEvents)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenericMessage struct {
|
type GenericMessage struct {
|
||||||
chat_id int64
|
chatId int64
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sender interface {
|
type Sender interface {
|
||||||
|
@ -18,7 +18,7 @@ type Sleeper interface {
|
||||||
Sleep(ch chan<- interface{})
|
Sleep(ch chan<- interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tg_sender_worker(tg_events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper) {
|
func tgSenderWorker(tgEvents <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
messagesToSend := make(map[int64]*strings.Builder)
|
messagesToSend := make(map[int64]*strings.Builder)
|
||||||
|
@ -28,37 +28,37 @@ func tg_sender_worker(tg_events <-chan GenericMessage, s Sender, wg *sync.WaitGr
|
||||||
loop:
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case ev, ok := <-tg_events:
|
case ev, ok := <-tgEvents:
|
||||||
if !ok {
|
if !ok {
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
// Collect all messages to send them at once
|
// Collect all messages to send them at once
|
||||||
_, messageBuilderExists := messagesToSend[ev.chat_id]
|
_, messageBuilderExists := messagesToSend[ev.chatId]
|
||||||
if !messageBuilderExists {
|
if !messageBuilderExists {
|
||||||
messagesToSend[ev.chat_id] = &strings.Builder{}
|
messagesToSend[ev.chatId] = &strings.Builder{}
|
||||||
}
|
}
|
||||||
messagesToSend[ev.chat_id].WriteString(ev.msg)
|
messagesToSend[ev.chatId].WriteString(ev.msg)
|
||||||
messagesToSend[ev.chat_id].WriteRune('\n')
|
messagesToSend[ev.chatId].WriteRune('\n')
|
||||||
if !waitingStarted {
|
if !waitingStarted {
|
||||||
waitingStarted = true
|
waitingStarted = true
|
||||||
sleeper.Sleep(timeoutEvents)
|
sleeper.Sleep(timeoutEvents)
|
||||||
}
|
}
|
||||||
case <-timeoutEvents:
|
case <-timeoutEvents:
|
||||||
waitingStarted = false
|
waitingStarted = false
|
||||||
for chat_id, msgBuilder := range messagesToSend {
|
for chatId, msgBuilder := range messagesToSend {
|
||||||
err := s.Send(GenericMessage{chat_id, msgBuilder.String()})
|
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: handle it better
|
// TODO: handle it better
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
delete(messagesToSend, chat_id)
|
delete(messagesToSend, chatId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If anything is left to be sent, send it now
|
// If anything is left to be sent, send it now
|
||||||
for chat_id, msgBuilder := range messagesToSend {
|
for chatId, msgBuilder := range messagesToSend {
|
||||||
err := s.Send(GenericMessage{chat_id, msgBuilder.String()})
|
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: handle it better
|
// TODO: handle it better
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -16,29 +16,34 @@ func (self *MockSender) Send(msg GenericMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockSleeper struct {
|
type MockSleeper struct {
|
||||||
ch *chan<- interface{}
|
ch *chan<- interface{}
|
||||||
|
callCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MockSleeper) Sleep(ch chan<- interface{}) {
|
func (self *MockSleeper) Sleep(ch chan<- interface{}) {
|
||||||
if self.ch == nil {
|
if self.ch == nil {
|
||||||
self.ch = &ch
|
self.ch = &ch
|
||||||
}
|
}
|
||||||
|
self.callCount += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageThrottling(t *testing.T) {
|
func TestMessageThrottling(t *testing.T) {
|
||||||
testEvents := make(chan GenericMessage)
|
testEvents := make(chan GenericMessage)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
mockSender := MockSender{make([]GenericMessage, 0)}
|
mockSender := MockSender{make([]GenericMessage, 0)}
|
||||||
mockSleeper := MockSleeper{}
|
mockSleeper := MockSleeper{nil, 0}
|
||||||
go tg_sender_worker(testEvents, &mockSender, &wg, &mockSleeper)
|
go tgSenderWorker(testEvents, &mockSender, &wg, &mockSleeper)
|
||||||
testEvents <- GenericMessage{123, "test1"}
|
testEvents <- GenericMessage{123, "test1"}
|
||||||
testEvents <- GenericMessage{124, "test3"}
|
testEvents <- GenericMessage{124, "test3"}
|
||||||
testEvents <- GenericMessage{123, "test2"}
|
testEvents <- GenericMessage{123, "test2"}
|
||||||
testEvents <- GenericMessage{124, "test4"}
|
testEvents <- GenericMessage{124, "test4"}
|
||||||
|
assert.Equal(t, 1, mockSleeper.callCount)
|
||||||
*mockSleeper.ch <- nil
|
*mockSleeper.ch <- nil
|
||||||
|
assert.Equal(t, 1, mockSleeper.callCount)
|
||||||
testEvents <- GenericMessage{123, "test5"}
|
testEvents <- GenericMessage{123, "test5"}
|
||||||
close(testEvents)
|
close(testEvents)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
assert.Equal(t, 2, mockSleeper.callCount)
|
||||||
|
|
||||||
assert.Len(t, mockSender.messages, 3)
|
assert.Len(t, mockSender.messages, 3)
|
||||||
assert.Contains(t, mockSender.messages, GenericMessage{123, "test1\ntest2\n"})
|
assert.Contains(t, mockSender.messages, GenericMessage{123, "test1\ntest2\n"})
|
||||||
|
|
Loading…
Reference in New Issue