1
0
Fork 0

Try again to connect after Satel disconnects

This commit is contained in:
Michał Rudowicz 2024-03-23 10:26:36 +01:00
parent df2fdc970b
commit c026863342
2 changed files with 23 additions and 11 deletions

30
main.go
View File

@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"git.sr.ht/~michalr/go-satel" "git.sr.ht/~michalr/go-satel"
@ -19,6 +20,7 @@ import (
const ( const (
PersistenceFilename = "hs_wro_last_seen.bin" PersistenceFilename = "hs_wro_last_seen.bin"
retryDelaySec = 25
) )
type RealSleeper struct { type RealSleeper struct {
@ -100,12 +102,14 @@ func getPersistenceFilePath() string {
func main() { func main() {
var ( var (
wg sync.WaitGroup wg sync.WaitGroup
tgEvents = make(chan GenericMessage, 5) tgEvents = make(chan GenericMessage, 5)
logger = log.New(os.Stderr, "Main", log.Lmicroseconds) logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
sleeper = RealSleeper{time.Second * 60} sleeper = RealSleeper{time.Second * 60}
stopRequested = atomic.Bool{}
) )
stopRequested.Store(false)
satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger) satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger)
s := makeSatel(satelAddr, poolInterval) s := makeSatel(satelAddr, poolInterval)
@ -129,13 +133,19 @@ func main() {
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl), tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
) )
go CloseSatelOnCtrlC(s) go CloseSatelOnCtrlC(s, &stopRequested)
for e := range FilterByTypeOrIndex( for stopRequested.Load() == false {
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), for e := range FilterByTypeOrIndex(
&wg, allowedTypes, allowedIndexes) { FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
logger.Print("Received change from SATEL: ", e) &wg, allowedTypes, allowedIndexes) {
tgEvents <- GenericMessage{e.BasicEvents} logger.Print("Received change from SATEL: ", e)
tgEvents <- GenericMessage{e.BasicEvents}
}
if stopRequested.Load() == false {
logger.Printf("Waiting %d seconds before trying again", retryDelaySec)
time.Sleep(retryDelaySec * time.Second)
}
} }
close(tgEvents) close(tgEvents)

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"sync/atomic"
"git.sr.ht/~michalr/go-satel" "git.sr.ht/~michalr/go-satel"
) )
@ -93,10 +94,11 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{
"zone-isolate": satel.ZoneIsolate, "zone-isolate": satel.ZoneIsolate,
} }
func CloseSatelOnCtrlC(s *satel.Satel) { func CloseSatelOnCtrlC(s *satel.Satel, stopRequested *atomic.Bool) {
exitSignal := make(chan os.Signal, 1) exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt) signal.Notify(exitSignal, os.Interrupt)
<-exitSignal <-exitSignal
stopRequested.Store(true)
s.Close() s.Close()
} }