1
0
Fork 0
hswro-alarm-bot/filters.go

47 lines
899 B
Go

package main
import (
"github.com/probakowski/go-satel"
)
func FilterByType(ev <-chan satel.Event, allowedTypes []satel.ChangeType) <-chan satel.Event {
returnChan := make(chan satel.Event)
go func() {
for e := range ev {
for _, allowedType := range allowedTypes {
if allowedType == e.Type {
returnChan <- e
continue
}
}
}
close(returnChan)
}()
return returnChan
}
type EventKey struct {
ChangeType satel.ChangeType
Index int
}
func FilterByLastSeen(ev <-chan satel.Event) <-chan satel.Event {
returnChan := make(chan satel.Event)
go func() {
lastSeen := make(map[EventKey]bool) // key: ChangeType/Index; value: Value
for e := range ev {
val, ok := lastSeen[EventKey{e.Type, e.Index}]
if !ok || val != e.Value {
lastSeen[EventKey{e.Type, e.Index}] = e.Value
returnChan <- e
}
}
close(returnChan)
}()
return returnChan
}