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

12
main.go
View File

@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"git.sr.ht/~michalr/go-satel"
@ -19,6 +20,7 @@ import (
const (
PersistenceFilename = "hs_wro_last_seen.bin"
retryDelaySec = 25
)
type RealSleeper struct {
@ -104,8 +106,10 @@ func main() {
tgEvents = make(chan GenericMessage, 5)
logger = log.New(os.Stderr, "Main", log.Lmicroseconds)
sleeper = RealSleeper{time.Second * 60}
stopRequested = atomic.Bool{}
)
stopRequested.Store(false)
satelAddr, chatIds, allowedTypes, allowedIndexes, poolInterval := getCmdLineParams(logger)
s := makeSatel(satelAddr, poolInterval)
@ -129,14 +133,20 @@ func main() {
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tpl),
)
go CloseSatelOnCtrlC(s)
go CloseSatelOnCtrlC(s, &stopRequested)
for stopRequested.Load() == false {
for e := range FilterByTypeOrIndex(
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
&wg, allowedTypes, allowedIndexes) {
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)
wg.Wait()

View File

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