Attempt to handle ctrl+c
This commit is contained in:
parent
557f5481fd
commit
23004084b4
2
main.go
2
main.go
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue