Attempt to handle ctrl+c

command_handler
Michał Rudowicz 2024-02-18 19:15:56 +01:00
rodzic 557f5481fd
commit 23004084b4
2 zmienionych plików z 25 dodań i 1 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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 {