2024-02-13 22:20:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-15 19:06:06 +00:00
|
|
|
"log"
|
|
|
|
|
2024-03-04 20:11:34 +00:00
|
|
|
"git.sr.ht/~michalr/go-satel"
|
2024-02-13 22:20:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func FilterByType(ev <-chan satel.Event, allowedTypes []satel.ChangeType) <-chan satel.Event {
|
|
|
|
returnChan := make(chan satel.Event)
|
|
|
|
|
2024-02-18 17:44:08 +00:00
|
|
|
if len(allowedTypes) == 0 {
|
|
|
|
// no allowed types == all types are allowed
|
|
|
|
go func() {
|
|
|
|
for e := range ev {
|
|
|
|
returnChan <- e
|
|
|
|
}
|
|
|
|
close(returnChan)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
for e := range ev {
|
|
|
|
for _, allowedType := range allowedTypes {
|
|
|
|
if allowedType == e.Type {
|
|
|
|
returnChan <- e
|
|
|
|
continue
|
|
|
|
}
|
2024-02-13 22:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-18 17:44:08 +00:00
|
|
|
close(returnChan)
|
|
|
|
}()
|
|
|
|
}
|
2024-02-13 22:20:15 +00:00
|
|
|
|
|
|
|
return returnChan
|
|
|
|
}
|
2024-02-14 20:34:16 +00:00
|
|
|
|
2024-02-18 18:00:49 +00:00
|
|
|
func FilterByIndex(ev <-chan satel.Event, allowedIndexes []int) <-chan satel.Event {
|
|
|
|
returnChan := make(chan satel.Event)
|
|
|
|
|
|
|
|
if len(allowedIndexes) == 0 {
|
|
|
|
// no allowed indexes == all indexes are allowed
|
|
|
|
go func() {
|
|
|
|
for e := range ev {
|
|
|
|
returnChan <- e
|
|
|
|
}
|
|
|
|
close(returnChan)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
for e := range ev {
|
|
|
|
for _, allowedIndex := range allowedIndexes {
|
|
|
|
if allowedIndex == e.Index {
|
|
|
|
returnChan <- e
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(returnChan)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnChan
|
|
|
|
}
|
|
|
|
|
2024-02-28 20:26:20 +00:00
|
|
|
func FilterByLastSeen(ev <-chan satel.Event, dataStore *DataStore, logger *log.Logger) <-chan satel.Event {
|
2024-02-14 20:34:16 +00:00
|
|
|
returnChan := make(chan satel.Event)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for e := range ev {
|
2024-02-28 20:26:20 +00:00
|
|
|
lastSeen := dataStore.GetSystemState()
|
2024-02-14 20:34:16 +00:00
|
|
|
val, ok := lastSeen[EventKey{e.Type, e.Index}]
|
2024-02-28 20:26:20 +00:00
|
|
|
if !ok || val.Value != e.Value {
|
2024-02-14 20:34:16 +00:00
|
|
|
returnChan <- e
|
2024-02-28 20:26:20 +00:00
|
|
|
dataStore.SetSystemState(EventKey{e.Type, e.Index}, EventValue{e.Value})
|
2024-02-14 20:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(returnChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return returnChan
|
|
|
|
}
|
2024-03-03 11:59:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|