From 765a41f8a1b4313972d57796f8408a00c680fb4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 3 Mar 2024 12:59:02 +0100 Subject: [PATCH] Log Satel disconnections --- filters.go | 14 ++++++++++++++ main.go | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/filters.go b/filters.go index 041615c..7f6bd6d 100644 --- a/filters.go +++ b/filters.go @@ -79,3 +79,17 @@ func FilterByLastSeen(ev <-chan satel.Event, dataStore *DataStore, logger *log.L return returnChan } + +func CallWhenClosed(ev <-chan satel.Event, cbk func()) <-chan satel.Event { + returnChan := make(chan satel.Event) + + go func() { + for e := range ev { + returnChan <- e + } + cbk() + close(returnChan) + }() + + return returnChan +} diff --git a/main.go b/main.go index b44ac84..0fd1757 100644 --- a/main.go +++ b/main.go @@ -147,7 +147,9 @@ func main() { go CloseSatelOnCtrlC(s) for e := range FilterByIndex(FilterByType( - FilterByLastSeen(s.Events, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), + FilterByLastSeen( + CallWhenClosed(s.Events, func() { logger.Print("Satel disconnected.") }), + &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)), allowedTypes), allowedIndexes) { logger.Print("Received change from SATEL: ", e)