FilterByType()
This commit is contained in:
parent
ef746d73ec
commit
0d263179b4
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/probakowski/go-satel"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSatelEventTypeFiltering(t *testing.T) {
|
||||
testEvents := make(chan satel.Event)
|
||||
receivedEvents := make([]satel.Event, 0)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
for e := range FilterByType(testEvents, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}) {
|
||||
receivedEvents = append(receivedEvents, e)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
|
||||
testEvents <- satel.Event{Type: satel.DoorOpened, Index: 2, Value: true}
|
||||
testEvents <- satel.Event{Type: satel.PartitionAlarm, Index: 3, Value: true}
|
||||
testEvents <- satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true}
|
||||
testEvents <- satel.Event{Type: satel.TroublePart1, Index: 5, Value: true}
|
||||
testEvents <- satel.Event{Type: satel.ZoneTamper, Index: 6, Value: true}
|
||||
|
||||
close(testEvents)
|
||||
wg.Wait()
|
||||
|
||||
assert.Len(t, receivedEvents, 2)
|
||||
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
|
||||
assert.Contains(t, receivedEvents, satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true})
|
||||
}
|
Loading…
Reference in New Issue