Logging
This commit is contained in:
parent
1f03609182
commit
bec0241d9c
17
main.go
17
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -43,23 +44,21 @@ func (self RealSleeper) Sleep(ch chan<- interface{}) {
|
||||||
ch <- nil
|
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")
|
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")
|
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.")
|
chatIdRaw := flag.String("tg-chat-id", "", "Telegram Chat ID where to send updates. Use \",\" to specify multiple IDs.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if len(*satelApiAddr) == 0 || len(*satelApiPort) == 0 || len(*chatIdRaw) == 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.")
|
logger.Fatal("Use --satel-addr=ADDR, --satel-port=PORT and --tg-chat-id=CHAT_ID command line flags to continue.")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
chatIdsStrings := strings.Split(*chatIdRaw, ",")
|
||||||
var chatIds []int64
|
var chatIds []int64
|
||||||
for _, chatIdStr := range chatIdsStrings {
|
for _, chatIdStr := range chatIdsStrings {
|
||||||
chatId, err := strconv.ParseInt(chatIdStr, 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_chatIds: %s. That's bad.", chatIdStr)
|
logger.Fatalf("Tried to use a non-int value for one of tg_chatIds: %s. That's bad.", chatIdStr)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
chatIds = append(chatIds, chatId)
|
chatIds = append(chatIds, chatId)
|
||||||
}
|
}
|
||||||
|
@ -81,20 +80,24 @@ func main() {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
tgEvents = make(chan GenericMessage, 100)
|
tgEvents = make(chan GenericMessage, 100)
|
||||||
sleeper = RealSleeper{time.Second * MessageNotMoreOftenThanSeconds}
|
sleeper = RealSleeper{time.Second * MessageNotMoreOftenThanSeconds}
|
||||||
|
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
|
||||||
)
|
)
|
||||||
|
|
||||||
satelAddr, chatIds := getCmdLineParams()
|
satelAddr, chatIds := getCmdLineParams(logger)
|
||||||
|
|
||||||
s := makeSatel(satelAddr)
|
s := makeSatel(satelAddr)
|
||||||
|
logger.Printf("Connected to Satel: %s", 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)
|
||||||
}
|
}
|
||||||
|
logger.Print("Created Telegram Bot API client")
|
||||||
|
|
||||||
tgSender := TgSender{bot}
|
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 {
|
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)
|
sendTgMessage(tgEvents, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chatIds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +19,8 @@ type Sleeper interface {
|
||||||
Sleep(ch chan<- 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)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
messagesToSend := make(map[int64]*strings.Builder)
|
messagesToSend := make(map[int64]*strings.Builder)
|
||||||
|
@ -40,10 +42,12 @@ loop:
|
||||||
messagesToSend[ev.chatId].WriteString(ev.msg)
|
messagesToSend[ev.chatId].WriteString(ev.msg)
|
||||||
messagesToSend[ev.chatId].WriteRune('\n')
|
messagesToSend[ev.chatId].WriteRune('\n')
|
||||||
if !waitingStarted {
|
if !waitingStarted {
|
||||||
|
logger.Print("Waiting for more messages to arrive before sending...")
|
||||||
waitingStarted = true
|
waitingStarted = true
|
||||||
sleeper.Sleep(timeoutEvents)
|
sleeper.Sleep(timeoutEvents)
|
||||||
}
|
}
|
||||||
case <-timeoutEvents:
|
case <-timeoutEvents:
|
||||||
|
logger.Print("Time's up, sending all messages we've got for now.")
|
||||||
waitingStarted = false
|
waitingStarted = false
|
||||||
for chatId, msgBuilder := range messagesToSend {
|
for chatId, msgBuilder := range messagesToSend {
|
||||||
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
|
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
|
// If anything is left to be sent, send it now
|
||||||
for chatId, msgBuilder := range messagesToSend {
|
for chatId, msgBuilder := range messagesToSend {
|
||||||
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
|
err := s.Send(GenericMessage{chatId, msgBuilder.String()})
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"io"
|
||||||
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockSender struct {
|
type MockSender struct {
|
||||||
|
@ -32,7 +35,7 @@ func TestMessageThrottling(t *testing.T) {
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
mockSender := MockSender{make([]GenericMessage, 0)}
|
mockSender := MockSender{make([]GenericMessage, 0)}
|
||||||
mockSleeper := MockSleeper{nil, 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{123, "test1"}
|
||||||
testEvents <- GenericMessage{124, "test3"}
|
testEvents <- GenericMessage{124, "test3"}
|
||||||
testEvents <- GenericMessage{123, "test2"}
|
testEvents <- GenericMessage{123, "test2"}
|
||||||
|
|
Loading…
Reference in New Issue