1
0
Fork 0

Attempt to handle ctrl+c

This commit is contained in:
Michał Rudowicz 2024-02-18 19:15:56 +01:00
parent 557f5481fd
commit 23004084b4
2 changed files with 25 additions and 1 deletions

View File

@ -146,7 +146,7 @@ func main() {
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
for e := range FilterByIndex(FilterByType(
FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
FilterByLastSeen(CloseChanOnCtrlC(s.Events), "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
allowedTypes),
allowedIndexes) {
logger.Print("Received change from SATEL: ", e)

View File

@ -3,6 +3,8 @@ package main
import (
"errors"
"fmt"
"os"
"os/signal"
"github.com/probakowski/go-satel"
)
@ -91,6 +93,28 @@ var SATEL_STRING_TO_CHANGE_TYPE = map[string]satel.ChangeType{
"zone-isolate": satel.ZoneIsolate,
}
func CloseChanOnCtrlC(ev <-chan satel.Event) <-chan satel.Event {
returnChan := make(chan satel.Event)
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
go func() {
defer close(returnChan)
loop:
for {
select {
case <-exitSignal:
return
case e, ok := <-ev:
if !ok {
break loop
}
returnChan <- e
}
}
}()
return returnChan
}
func StringToSatelChangeType(s string) (satel.ChangeType, error) {
ct, ok := SATEL_STRING_TO_CHANGE_TYPE[s]
if !ok {