Compare commits
No commits in common. "f2e56844774c43fa02ae0801e7e60dd435a64acb" and "ba5e74d3c6d2eace271ff1fdeb4c0bce75f2320e" have entirely different histories.
f2e5684477
...
ba5e74d3c6
214
filters.go
214
filters.go
|
|
@ -7,48 +7,84 @@ import (
|
||||||
"git.sr.ht/~michalr/go-satel"
|
"git.sr.ht/~michalr/go-satel"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SyncFilter[MsgType any] interface {
|
func isBasicEventElementOkay(basicEventElement satel.BasicEventElement, allowedTypes []SatelChangeType, allowedIndexes []int) bool {
|
||||||
Then(what SyncFilter[MsgType]) SyncFilter[MsgType]
|
for _, allowedType := range allowedTypes {
|
||||||
Call(msg MsgType)
|
if allowedType.GetChangeType() == basicEventElement.Type {
|
||||||
}
|
return true
|
||||||
|
}
|
||||||
type SyncFilterImpl[MsgType any] struct {
|
|
||||||
next SyncFilter[MsgType]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *SyncFilterImpl[MsgType]) Then(what SyncFilter[MsgType]) SyncFilter[MsgType] {
|
|
||||||
impl.next = what
|
|
||||||
return what
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *SyncFilterImpl[MsgType]) CallNext(msg MsgType) {
|
|
||||||
if impl.next != nil {
|
|
||||||
impl.next.Call(msg)
|
|
||||||
}
|
}
|
||||||
|
for _, allowedIndex := range allowedIndexes {
|
||||||
|
if allowedIndex == basicEventElement.Index {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollectFromChannel[MsgType any] struct{ SyncFilterImpl[MsgType] }
|
func FilterByTypeOrIndex(ev <-chan satel.Event, wg *sync.WaitGroup, allowedTypes []SatelChangeType, allowedIndexes []int) <-chan satel.Event {
|
||||||
|
returnChan := make(chan satel.Event)
|
||||||
|
|
||||||
func (collect *CollectFromChannel[MsgType]) Call(msg MsgType) {
|
if (len(allowedTypes) == 0) && (len(allowedIndexes) == 0) {
|
||||||
collect.CallNext(msg)
|
// no allowed types == all types are allowed
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer close(returnChan)
|
||||||
|
|
||||||
|
for e := range ev {
|
||||||
|
returnChan <- e
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
} else {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer close(returnChan)
|
||||||
|
|
||||||
|
for e := range ev {
|
||||||
|
retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)}
|
||||||
|
for _, basicEventElement := range e.BasicEvents {
|
||||||
|
if isBasicEventElementOkay(basicEventElement, allowedTypes, allowedIndexes) {
|
||||||
|
retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(retEv.BasicEvents) != 0 {
|
||||||
|
returnChan <- retEv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (collect CollectFromChannel[MsgType]) Collect(events <-chan MsgType, wg *sync.WaitGroup, onClose func()) {
|
func FilterByLastSeen(ev <-chan satel.Event, wg *sync.WaitGroup, dataStore *DataStore, logger *log.Logger) <-chan satel.Event {
|
||||||
|
returnChan := make(chan satel.Event)
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer onClose()
|
defer close(returnChan)
|
||||||
|
|
||||||
for e := range events {
|
for e := range ev {
|
||||||
collect.Call(e)
|
retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)}
|
||||||
|
for _, basicEventElement := range e.BasicEvents {
|
||||||
|
lastSeen := dataStore.GetSystemState()
|
||||||
|
val, ok := lastSeen[EventKey{basicEventElement.Type, basicEventElement.Index}]
|
||||||
|
if !ok || val.Value != basicEventElement.Value {
|
||||||
|
retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement)
|
||||||
|
// TODO: flush to disk only after the loop finishes
|
||||||
|
dataStore.SetSystemState(EventKey{basicEventElement.Type, basicEventElement.Index}, EventValue{basicEventElement.Value})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(retEv.BasicEvents) != 0 {
|
||||||
|
returnChan <- retEv
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
logger.Print("Satel disconnected.")
|
||||||
}()
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
type ThrottleSync struct {
|
return returnChan
|
||||||
SyncFilterImpl[GenericMessage]
|
|
||||||
|
|
||||||
events chan GenericMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage {
|
func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage {
|
||||||
|
|
@ -71,19 +107,20 @@ throughNewMessages:
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeThrottleSync(sleeper Sleeper, logger *log.Logger, wg *sync.WaitGroup) *ThrottleSync {
|
func Throttle(inputEvents <-chan GenericMessage, wg *sync.WaitGroup, sleeper Sleeper, logger *log.Logger) <-chan GenericMessage {
|
||||||
events := make(chan GenericMessage)
|
returnChan := make(chan GenericMessage)
|
||||||
|
timeoutEvents := make(chan interface{})
|
||||||
throttle := ThrottleSync{SyncFilterImpl[GenericMessage]{}, events}
|
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
timeoutEvents := make(chan interface{})
|
defer wg.Done()
|
||||||
|
defer close(returnChan)
|
||||||
|
|
||||||
var currentEvent *GenericMessage = nil
|
var currentEvent *GenericMessage = nil
|
||||||
loop:
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case ev, ok := <-events:
|
case ev, ok := <-inputEvents:
|
||||||
if !ok {
|
if !ok {
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
|
|
@ -94,117 +131,16 @@ func MakeThrottleSync(sleeper Sleeper, logger *log.Logger, wg *sync.WaitGroup) *
|
||||||
currentEvent = appendToGenericMessage(currentEvent, &ev)
|
currentEvent = appendToGenericMessage(currentEvent, &ev)
|
||||||
case <-timeoutEvents:
|
case <-timeoutEvents:
|
||||||
logger.Print("Time's up, sending all messages we've got for now.")
|
logger.Print("Time's up, sending all messages we've got for now.")
|
||||||
throttle.CallNext(*currentEvent)
|
returnChan <- *currentEvent
|
||||||
currentEvent = nil
|
currentEvent = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If anything is left to be sent, send it now
|
// If anything is left to be sent, send it now
|
||||||
if currentEvent != nil {
|
if currentEvent != nil {
|
||||||
throttle.CallNext(*currentEvent)
|
returnChan <- *currentEvent
|
||||||
}
|
}
|
||||||
wg.Done()
|
|
||||||
logger.Print("Throttling goroutine finishing")
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return &throttle
|
return returnChan
|
||||||
}
|
|
||||||
|
|
||||||
func (throttle *ThrottleSync) Close() { close(throttle.events) }
|
|
||||||
|
|
||||||
func (throttle *ThrottleSync) Call(msg GenericMessage) {
|
|
||||||
throttle.events <- msg
|
|
||||||
}
|
|
||||||
|
|
||||||
type Convert[InMsgType any] struct {
|
|
||||||
out SyncFilter[GenericMessage]
|
|
||||||
convert func(InMsgType) GenericMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeConvert[InMsgType any](convertFunc func(InMsgType) GenericMessage) *Convert[InMsgType] {
|
|
||||||
return &Convert[InMsgType]{nil, convertFunc}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (convert *Convert[InMsgType]) Call(msg InMsgType) {
|
|
||||||
if convert.out == nil {
|
|
||||||
panic("Use ConvertTo() to set next element in the chain.")
|
|
||||||
}
|
|
||||||
convert.out.Call(convert.convert(msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (convert *Convert[InMsgType]) ConvertTo(out SyncFilter[GenericMessage]) SyncFilter[GenericMessage] {
|
|
||||||
convert.out = out
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func (convert *Convert[InMsgType]) Then(_ SyncFilter[InMsgType]) SyncFilter[InMsgType] {
|
|
||||||
panic("Use ConvertTo() with Convert object instead of Then().")
|
|
||||||
}
|
|
||||||
|
|
||||||
type FilterByLastSeen struct {
|
|
||||||
SyncFilterImpl[satel.Event]
|
|
||||||
dataStore *DataStore
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeFilterByLastSeen(dataStore *DataStore) *FilterByLastSeen {
|
|
||||||
return &FilterByLastSeen{SyncFilterImpl[satel.Event]{}, dataStore}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (filter *FilterByLastSeen) Call(ev satel.Event) {
|
|
||||||
retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)}
|
|
||||||
for _, basicEventElement := range ev.BasicEvents {
|
|
||||||
lastSeen := filter.dataStore.GetSystemState()
|
|
||||||
val, ok := lastSeen[EventKey{basicEventElement.Type, basicEventElement.Index}]
|
|
||||||
if !ok || val.Value != basicEventElement.Value {
|
|
||||||
retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement)
|
|
||||||
// TODO: flush to disk only after the loop finishes
|
|
||||||
filter.dataStore.SetSystemState(EventKey{basicEventElement.Type, basicEventElement.Index},
|
|
||||||
EventValue{basicEventElement.Value})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(retEv.BasicEvents) != 0 {
|
|
||||||
filter.CallNext(retEv)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
type FilterByTypeOrIndex struct {
|
|
||||||
SyncFilterImpl[satel.Event]
|
|
||||||
allowedTypes []SatelChangeType
|
|
||||||
allowedIndexes []int
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeFilterByTypeOrIndex(allowedTypes []SatelChangeType, allowedIndexes []int) *FilterByTypeOrIndex {
|
|
||||||
return &FilterByTypeOrIndex{SyncFilterImpl[satel.Event]{}, allowedTypes, allowedIndexes}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isBasicEventElementOkay(basicEventElement satel.BasicEventElement, allowedTypes []SatelChangeType, allowedIndexes []int) bool {
|
|
||||||
for _, allowedType := range allowedTypes {
|
|
||||||
if allowedType.GetChangeType() == basicEventElement.Type {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, allowedIndex := range allowedIndexes {
|
|
||||||
if allowedIndex == basicEventElement.Index {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (filter *FilterByTypeOrIndex) Call(ev satel.Event) {
|
|
||||||
if (len(filter.allowedTypes) == 0) && (len(filter.allowedIndexes) == 0) {
|
|
||||||
// no allowed types == all types are allowed
|
|
||||||
filter.CallNext(ev)
|
|
||||||
} else {
|
|
||||||
retEv := satel.Event{BasicEvents: make([]satel.BasicEventElement, 0)}
|
|
||||||
for _, basicEventElement := range ev.BasicEvents {
|
|
||||||
if isBasicEventElementOkay(basicEventElement, filter.allowedTypes, filter.allowedIndexes) {
|
|
||||||
retEv.BasicEvents = append(retEv.BasicEvents, basicEventElement)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(retEv.BasicEvents) != 0 {
|
|
||||||
filter.CallNext(retEv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
380
filters_test.go
380
filters_test.go
|
|
@ -12,74 +12,115 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSatelEventTypeFiltering(t *testing.T) {
|
func TestSatelEventTypeFiltering(t *testing.T) {
|
||||||
tested := MakeFilterByTypeOrIndex([]SatelChangeType{{satel.ArmedPartition}, {satel.PartitionFireAlarm}},
|
testEvents := make(chan satel.Event)
|
||||||
[]int{})
|
receivedEvents := make([]satel.Event, 0)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
wg := sync.WaitGroup{}
|
||||||
|
|
||||||
tested.Then(mock)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{{satel.ArmedPartition}, {satel.PartitionFireAlarm}}, []int{}) {
|
||||||
tested.Call(makeTestSatelEvent(satel.DoorOpened, 2, true))
|
receivedEvents = append(receivedEvents, e)
|
||||||
tested.Call(makeTestSatelEvent(satel.PartitionAlarm, 3, true))
|
}
|
||||||
tested.Call(makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
|
}()
|
||||||
tested.Call(makeTestSatelEvent(satel.TroublePart1, 5, true))
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ZoneTamper, 6, true))
|
|
||||||
|
|
||||||
assert.Len(t, mock.collected, 2)
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
|
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
|
||||||
|
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Len(t, receivedEvents, 2)
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSatelEventTypeFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
|
func TestSatelEventTypeFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
|
||||||
tested := MakeFilterByTypeOrIndex([]SatelChangeType{},
|
testEvents := make(chan satel.Event)
|
||||||
[]int{})
|
receivedEvents := make([]satel.Event, 0)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
wg := sync.WaitGroup{}
|
||||||
|
|
||||||
tested.Then(mock)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{}) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for index, ct := range SUPPORTED_CHANGE_TYPES {
|
for index, ct := range SUPPORTED_CHANGE_TYPES {
|
||||||
tested.Call(makeTestSatelEvent(ct, index, true))
|
testEvents <- makeTestSatelEvent(ct, index, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(t, mock.collected, len(SUPPORTED_CHANGE_TYPES))
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Len(t, receivedEvents, len(SUPPORTED_CHANGE_TYPES))
|
||||||
for index, ct := range SUPPORTED_CHANGE_TYPES {
|
for index, ct := range SUPPORTED_CHANGE_TYPES {
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(ct, index, true))
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(ct, index, true))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSatelIndexFiltering(t *testing.T) {
|
func TestSatelIndexFiltering(t *testing.T) {
|
||||||
tested := MakeFilterByTypeOrIndex([]SatelChangeType{}, []int{1, 3})
|
testEvents := make(chan satel.Event)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
receivedEvents := make([]satel.Event, 0)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
|
||||||
tested.Then(mock)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{1, 3}) {
|
||||||
tested.Call(makeTestSatelEvent(satel.DoorOpened, 2, true))
|
receivedEvents = append(receivedEvents, e)
|
||||||
tested.Call(makeTestSatelEvent(satel.PartitionAlarm, 3, true))
|
}
|
||||||
tested.Call(makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
|
}()
|
||||||
tested.Call(makeTestSatelEvent(satel.TroublePart1, 5, true))
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ZoneTamper, 6, true))
|
|
||||||
|
|
||||||
assert.Len(t, mock.collected, 2)
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.PartitionAlarm, 3, true))
|
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
|
||||||
|
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
|
||||||
|
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Len(t, receivedEvents, 2)
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionAlarm, 3, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSatelIndexFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
|
func TestSatelIndexFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
|
||||||
tested := MakeFilterByTypeOrIndex([]SatelChangeType{{satel.ArmedPartition}, {satel.PartitionFireAlarm}},
|
testEvents := make(chan satel.Event)
|
||||||
[]int{})
|
receivedEvents := make([]satel.Event, 0)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
wg := sync.WaitGroup{}
|
||||||
myReasonableMaxIndex := 100 // I wanted to use math.MaxInt at first, but it's kind of a waste of time here
|
myReasonableMaxIndex := 100 // I wanted to use math.MaxInt at first, but it's kind of a waste of time here
|
||||||
|
|
||||||
tested.Then(mock)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
for e := range FilterByTypeOrIndex(testEvents, &wg, []SatelChangeType{}, []int{}) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for i := 0; i < myReasonableMaxIndex; i++ {
|
for i := 0; i < myReasonableMaxIndex; i++ {
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, i, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, i, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(t, mock.collected, myReasonableMaxIndex)
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Len(t, receivedEvents, myReasonableMaxIndex)
|
||||||
for i := 0; i < myReasonableMaxIndex; i++ {
|
for i := 0; i < myReasonableMaxIndex; i++ {
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, i, true))
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, i, true))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,26 +130,35 @@ func TestSatelLastSeenFiltering(t *testing.T) {
|
||||||
tempFileName := f.Name()
|
tempFileName := f.Name()
|
||||||
assert.NoError(t, f.Close())
|
assert.NoError(t, f.Close())
|
||||||
defer os.Remove(f.Name())
|
defer os.Remove(f.Name())
|
||||||
|
testEvents := make(chan satel.Event)
|
||||||
|
receivedEvents := make([]satel.Event, 0)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
fakeLog := log.New(io.Discard, "", log.Ltime)
|
fakeLog := log.New(io.Discard, "", log.Ltime)
|
||||||
ds := MakeDataStore(fakeLog, tempFileName)
|
ds := MakeDataStore(fakeLog, tempFileName)
|
||||||
|
|
||||||
tested := MakeFilterByLastSeen(&ds)
|
wg.Add(1)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested.Then(mock)
|
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
|
|
||||||
assert.Len(t, mock.collected, 3)
|
close(testEvents)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
wg.Wait()
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
assert.Len(t, receivedEvents, 3)
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
|
func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
|
||||||
|
|
@ -117,45 +167,61 @@ func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
|
||||||
tempFileName := f.Name()
|
tempFileName := f.Name()
|
||||||
assert.NoError(t, f.Close())
|
assert.NoError(t, f.Close())
|
||||||
defer os.Remove(f.Name())
|
defer os.Remove(f.Name())
|
||||||
|
testEvents := make(chan satel.Event)
|
||||||
|
receivedEvents := make([]satel.Event, 0)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
fakeLog := log.New(io.Discard, "", log.Ltime)
|
fakeLog := log.New(io.Discard, "", log.Ltime)
|
||||||
ds := MakeDataStore(fakeLog, tempFileName)
|
ds := MakeDataStore(fakeLog, tempFileName)
|
||||||
|
|
||||||
tested := MakeFilterByLastSeen(&ds)
|
wg.Add(1)
|
||||||
mock := &GenericSyncMockFilter[satel.Event]{}
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested.Then(mock)
|
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
tested.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
|
|
||||||
assert.Len(t, mock.collected, 3)
|
close(testEvents)
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
wg.Wait()
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
|
||||||
assert.Contains(t, mock.collected, makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
|
||||||
|
|
||||||
tested = nil
|
assert.Len(t, receivedEvents, 3)
|
||||||
mock = nil
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
||||||
|
|
||||||
ds2 := MakeDataStore(fakeLog, tempFileName)
|
testEvents = make(chan satel.Event)
|
||||||
|
receivedEvents = make([]satel.Event, 0)
|
||||||
|
ds = MakeDataStore(fakeLog, tempFileName)
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested2 := MakeFilterByLastSeen(&ds2)
|
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
|
||||||
mock2 := &GenericSyncMockFilter[satel.Event]{}
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
tested2.Then(mock2)
|
receivedEvents = make([]satel.Event, 0)
|
||||||
|
|
||||||
tested2.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
tested2.Call(makeTestSatelEvent(satel.ArmedPartition, 1, false))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
|
||||||
tested2.Call(makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
|
||||||
tested2.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
tested2.Call(makeTestSatelEvent(satel.ArmedPartition, 2, true))
|
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
|
||||||
|
|
||||||
assert.Len(t, mock2.collected, 1)
|
close(testEvents)
|
||||||
assert.Contains(t, mock2.collected, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Len(t, receivedEvents, 1)
|
||||||
|
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockSleeper struct {
|
type MockSleeper struct {
|
||||||
|
|
@ -170,59 +236,9 @@ func (self *MockSleeper) Sleep(ch chan<- interface{}) {
|
||||||
self.callCount += 1
|
self.callCount += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
type GenericSyncMockFilter[T any] struct {
|
func TestThrottle(t *testing.T) {
|
||||||
SyncFilterImpl[T]
|
|
||||||
collected []T
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *GenericSyncMockFilter[T]) Call(msg T) {
|
|
||||||
self.collected = append(self.collected, msg)
|
|
||||||
self.CallNext(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
type SyncMockFilter = GenericSyncMockFilter[GenericMessage]
|
|
||||||
|
|
||||||
func TestSyncCollect(t *testing.T) {
|
|
||||||
testEvents := make(chan GenericMessage)
|
testEvents := make(chan GenericMessage)
|
||||||
wg := sync.WaitGroup{}
|
receivedEvents := make([]GenericMessage, 0)
|
||||||
|
|
||||||
tested := CollectFromChannel[GenericMessage]{}
|
|
||||||
mock := &SyncMockFilter{}
|
|
||||||
mock2 := &SyncMockFilter{}
|
|
||||||
|
|
||||||
tested.Then(mock).Then(mock2)
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
tested.Collect(testEvents, &wg, func() { wg.Done() })
|
|
||||||
|
|
||||||
testEvents <- makeGenericMessage(satel.ArmedPartition, 1, true)
|
|
||||||
testEvents <- makeGenericMessage(satel.DoorOpened, 2, true)
|
|
||||||
testEvents <- makeGenericMessage(satel.PartitionAlarm, 3, true)
|
|
||||||
testEvents <- makeGenericMessage(satel.PartitionFireAlarm, 4, true)
|
|
||||||
testEvents <- makeGenericMessage(satel.TroublePart1, 5, true)
|
|
||||||
testEvents <- makeGenericMessage(satel.ZoneTamper, 6, true)
|
|
||||||
|
|
||||||
close(testEvents)
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
assert.Len(t, mock.collected, 6)
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.ArmedPartition, 1, true))
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.DoorOpened, 2, true))
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.PartitionAlarm, 3, true))
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.PartitionFireAlarm, 4, true))
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.TroublePart1, 5, true))
|
|
||||||
assert.Contains(t, mock.collected, makeGenericMessage(satel.ZoneTamper, 6, true))
|
|
||||||
|
|
||||||
assert.Len(t, mock2.collected, 6)
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.ArmedPartition, 1, true))
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.DoorOpened, 2, true))
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.PartitionAlarm, 3, true))
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.PartitionFireAlarm, 4, true))
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.TroublePart1, 5, true))
|
|
||||||
assert.Contains(t, mock2.collected, makeGenericMessage(satel.ZoneTamper, 6, true))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestThrottleSync(t *testing.T) {
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
fakeLog := log.New(io.Discard, "", log.Ltime)
|
fakeLog := log.New(io.Discard, "", log.Ltime)
|
||||||
mockSleeper := MockSleeper{nil, 0}
|
mockSleeper := MockSleeper{nil, 0}
|
||||||
|
|
@ -234,57 +250,83 @@ func TestThrottleSync(t *testing.T) {
|
||||||
tplMessageTest4 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: false}
|
tplMessageTest4 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: false}
|
||||||
)
|
)
|
||||||
|
|
||||||
tested := MakeThrottleSync(&mockSleeper, fakeLog, &wg)
|
wg.Add(1)
|
||||||
mock := &SyncMockFilter{}
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tested.Then(mock)
|
for e := range Throttle(testEvents, &wg, &mockSleeper, fakeLog) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest1}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1}}
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest2}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest2}}
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest3}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest3}}
|
||||||
*mockSleeper.ch <- nil
|
*mockSleeper.ch <- nil
|
||||||
|
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest4}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest4}}
|
||||||
|
|
||||||
tested.Close()
|
close(testEvents)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
assert.Equal(t, 2, mockSleeper.callCount)
|
assert.Equal(t, 2, mockSleeper.callCount)
|
||||||
assert.Len(t, mock.collected, 2)
|
assert.Len(t, receivedEvents, 2)
|
||||||
assert.Contains(t, mock.collected[0].Messages, tplMessageTest2)
|
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest2)
|
||||||
assert.Contains(t, mock.collected[0].Messages, tplMessageTest3)
|
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest3)
|
||||||
assert.Len(t, mock.collected[0].Messages, 2)
|
assert.Len(t, receivedEvents[0].Messages, 2)
|
||||||
|
|
||||||
assert.Contains(t, mock.collected[1].Messages, tplMessageTest4)
|
assert.Contains(t, receivedEvents[1].Messages, tplMessageTest4)
|
||||||
assert.Len(t, mock.collected[1].Messages, 1)
|
assert.Len(t, receivedEvents[1].Messages, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvert_failsWhenNotConverting(t *testing.T) {
|
func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage {
|
||||||
a := assert.New(t)
|
retval := GenericMessage{make([]satel.BasicEventElement, 0)}
|
||||||
tested := MakeConvert(func(in int) GenericMessage {
|
|
||||||
a.Equal(in, 1)
|
|
||||||
return GenericMessage{}
|
|
||||||
})
|
|
||||||
mock := &GenericSyncMockFilter[int]{}
|
|
||||||
|
|
||||||
a.Panics(func() {
|
for i := 0; i < numElements; i++ {
|
||||||
tested.Then(mock)
|
retval.Messages = append(retval.Messages, element)
|
||||||
tested.Call(1)
|
}
|
||||||
})
|
|
||||||
|
return retval
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvert(t *testing.T) {
|
func TestThrottle_ManyMessagesInOneEvent(t *testing.T) {
|
||||||
a := assert.New(t)
|
testEvents := make(chan GenericMessage)
|
||||||
numCalled := 0
|
receivedEvents := make([]GenericMessage, 0)
|
||||||
tested := MakeConvert(func(in int) GenericMessage {
|
wg := sync.WaitGroup{}
|
||||||
a.Equal(in, 1)
|
fakeLog := log.New(io.Discard, "", log.Ltime)
|
||||||
numCalled += 1
|
mockSleeper := MockSleeper{nil, 0}
|
||||||
return GenericMessage{}
|
|
||||||
})
|
|
||||||
mock := &SyncMockFilter{}
|
|
||||||
|
|
||||||
tested.ConvertTo(mock)
|
var (
|
||||||
tested.Call(1)
|
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
||||||
|
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
|
||||||
|
tplMessageTest3 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: false}
|
||||||
|
tplMessageTest4 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: false}
|
||||||
|
)
|
||||||
|
|
||||||
a.Equal(numCalled, 1)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for e := range Throttle(testEvents, &wg, &mockSleeper, fakeLog) {
|
||||||
|
receivedEvents = append(receivedEvents, e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
testEvents <- makeMassiveEvent(tplMessageTest1, 100)
|
||||||
|
testEvents <- makeMassiveEvent(tplMessageTest2, 100)
|
||||||
|
testEvents <- makeMassiveEvent(tplMessageTest3, 100)
|
||||||
|
*mockSleeper.ch <- nil
|
||||||
|
|
||||||
|
testEvents <- makeMassiveEvent(tplMessageTest4, 100)
|
||||||
|
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Equal(t, 2, mockSleeper.callCount)
|
||||||
|
assert.Len(t, receivedEvents, 2)
|
||||||
|
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest2)
|
||||||
|
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest3)
|
||||||
|
assert.Len(t, receivedEvents[0].Messages, 2)
|
||||||
|
|
||||||
|
assert.Contains(t, receivedEvents[1].Messages, tplMessageTest4)
|
||||||
|
assert.Len(t, receivedEvents[1].Messages, 1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
main.go
37
main.go
|
|
@ -80,32 +80,14 @@ func main() {
|
||||||
|
|
||||||
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath())
|
||||||
|
|
||||||
collect := CollectFromChannel[satel.Event]{}
|
Consume(
|
||||||
|
SendToMatterbridge(
|
||||||
filterByLastSeen := MakeFilterByLastSeen(&dataStore)
|
SendToTg(Throttle(NotifyViaHTTP(tgEvents, config, &wg, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)),
|
||||||
filterByTypeOrIndex := MakeFilterByTypeOrIndex(config.AllowedTypes, config.AllowedIndexes)
|
&wg, sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds)),
|
||||||
convert := MakeConvert(
|
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tgTpl),
|
||||||
func(ev satel.Event) GenericMessage { return GenericMessage{ev.BasicEvents} },
|
s, config, &wg, log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds), ircTpl),
|
||||||
)
|
)
|
||||||
|
|
||||||
notifyViaHttp := MakeNofityViaHTTPSync(config, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds))
|
|
||||||
throttle := MakeThrottleSync(sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds), &wg)
|
|
||||||
sendToTg := MakeSendToTelegramSync(tgSender, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tgTpl)
|
|
||||||
sendToMatterbridge := MakeSendToMatterbridgeSync(s, config,
|
|
||||||
log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds),
|
|
||||||
ircTpl)
|
|
||||||
|
|
||||||
collect.Then(filterByLastSeen).
|
|
||||||
Then(filterByTypeOrIndex).
|
|
||||||
Then(convert)
|
|
||||||
|
|
||||||
convert.ConvertTo(notifyViaHttp).
|
|
||||||
Then(throttle).
|
|
||||||
Then(sendToTg).
|
|
||||||
Then(sendToMatterbridge)
|
|
||||||
|
|
||||||
collect.Collect(s.Events, &wg, func() {})
|
|
||||||
|
|
||||||
go CloseSatelOnCtrlC(s, &cleanShutdown)
|
go CloseSatelOnCtrlC(s, &cleanShutdown)
|
||||||
|
|
||||||
closeDebugTools := make(chan interface{})
|
closeDebugTools := make(chan interface{})
|
||||||
|
|
@ -113,6 +95,13 @@ func main() {
|
||||||
WriteMemoryProfilePeriodically(&wg, log.New(os.Stderr, "DebugTools", log.Lmicroseconds), closeDebugTools)
|
WriteMemoryProfilePeriodically(&wg, log.New(os.Stderr, "DebugTools", log.Lmicroseconds), closeDebugTools)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for e := range FilterByTypeOrIndex(
|
||||||
|
FilterByLastSeen(s.Events, &wg, &dataStore, log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)),
|
||||||
|
&wg, config.AllowedTypes, config.AllowedIndexes) {
|
||||||
|
logger.Print("Received change from SATEL: ", e)
|
||||||
|
tgEvents <- GenericMessage{e.BasicEvents}
|
||||||
|
}
|
||||||
|
|
||||||
logger.Print("Closing...")
|
logger.Print("Closing...")
|
||||||
close(closeDebugTools)
|
close(closeDebugTools)
|
||||||
close(tgEvents)
|
close(tgEvents)
|
||||||
|
|
|
||||||
137
sender_sync.go
137
sender_sync.go
|
|
@ -1,137 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SendToTelegramSync struct {
|
|
||||||
SyncFilterImpl[GenericMessage]
|
|
||||||
|
|
||||||
sender Sender
|
|
||||||
logger *log.Logger
|
|
||||||
tpl *template.Template
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeSendToTelegramSync(sender Sender,
|
|
||||||
logger *log.Logger,
|
|
||||||
tpl *template.Template) *SendToTelegramSync {
|
|
||||||
return &SendToTelegramSync{SyncFilterImpl[GenericMessage]{}, sender, logger, tpl}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
|
|
||||||
err := sendToTg.sender.Send(msg, sendToTg.tpl)
|
|
||||||
if err != nil {
|
|
||||||
// TODO: handle it better
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sendToTg.CallNext(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
type SendToMatterbridgeSync struct {
|
|
||||||
SyncFilterImpl[GenericMessage]
|
|
||||||
|
|
||||||
s SatelNameGetter
|
|
||||||
config AppConfig
|
|
||||||
logger *log.Logger
|
|
||||||
tpl *template.Template
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeSendToMatterbridgeSync(s SatelNameGetter,
|
|
||||||
config AppConfig,
|
|
||||||
logger *log.Logger,
|
|
||||||
tpl *template.Template) *SendToMatterbridgeSync {
|
|
||||||
return &SendToMatterbridgeSync{SyncFilterImpl[GenericMessage]{}, s, config, logger, tpl}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mbridge *SendToMatterbridgeSync) Call(msg GenericMessage) {
|
|
||||||
for _, matterbridgeConfig := range mbridge.config.Matterbridge {
|
|
||||||
body, err := json.Marshal(MatterbridgeMessage{
|
|
||||||
Text: msg.Format(mbridge.tpl, mbridge.s, mbridge.logger),
|
|
||||||
Username: matterbridgeConfig.Username,
|
|
||||||
Gateway: matterbridgeConfig.Gateway,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
mbridge.logger.Fatal("Could not marshal a JSON message: ", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body))
|
|
||||||
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)}
|
|
||||||
client := http.Client{
|
|
||||||
Timeout: httpTimeout,
|
|
||||||
}
|
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
mbridge.logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mbridge.logger.Print("Notified via Matterbridge with result ", res.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
mbridge.CallNext(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
type NotifyViaHTTPSync struct {
|
|
||||||
SyncFilterImpl[GenericMessage]
|
|
||||||
|
|
||||||
config AppConfig
|
|
||||||
logger *log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeNofityViaHTTPSync(config AppConfig, logger *log.Logger) *NotifyViaHTTPSync {
|
|
||||||
return &NotifyViaHTTPSync{SyncFilterImpl[GenericMessage]{}, config, logger}
|
|
||||||
}
|
|
||||||
|
|
||||||
func doHttpNotification(url string, logger *log.Logger) {
|
|
||||||
|
|
||||||
if len(url) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, nil)
|
|
||||||
|
|
||||||
client := http.Client{
|
|
||||||
Timeout: httpTimeout,
|
|
||||||
}
|
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
logger.Print("Could not POST ", url, ": ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
func notifyAllHttp(urls []string, logger *log.Logger) {
|
|
||||||
for _, uri := range urls {
|
|
||||||
doHttpNotification(uri, logger)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (notifyViaHttp *NotifyViaHTTPSync) Call(msg GenericMessage) {
|
|
||||||
inner_arm:
|
|
||||||
for _, basicElement := range msg.Messages {
|
|
||||||
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
|
||||||
if basicElement.Value == ArmedPartition_Armed {
|
|
||||||
notifyAllHttp(notifyViaHttp.config.ArmCallbackUrls, notifyViaHttp.logger)
|
|
||||||
} else {
|
|
||||||
notifyAllHttp(notifyViaHttp.config.DisarmCallbackUrls, notifyViaHttp.logger)
|
|
||||||
}
|
|
||||||
break inner_arm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
inner_alarm:
|
|
||||||
for _, basicElement := range msg.Messages {
|
|
||||||
if basicElement.Type == satel.PartitionAlarm {
|
|
||||||
if basicElement.Value == PartitionAlarm_Alarm {
|
|
||||||
notifyAllHttp(notifyViaHttp.config.AlarmCallbackUrls, notifyViaHttp.logger)
|
|
||||||
break inner_alarm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
tgSyncSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSyncTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
|
|
||||||
a := assert.New(t)
|
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(""))
|
|
||||||
mockBot := MockTgBotAPI{}
|
|
||||||
s := MockSatelNameGetter{"test_name"}
|
|
||||||
sender := TgSender{&mockBot, s, log.New(io.Discard, "", 0), []int64{}}
|
|
||||||
|
|
||||||
tested := MakeSendToTelegramSync(&sender, log.New(io.Discard, "", 0), tpl)
|
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
|
|
||||||
a.Equal(0, len(mockBot.messages))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSyncTelegramSender_SendsMessageToAllChatIds(t *testing.T) {
|
|
||||||
a := assert.New(t)
|
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(""))
|
|
||||||
mockBot := MockTgBotAPI{}
|
|
||||||
s := MockSatelNameGetter{"test_name"}
|
|
||||||
sender := TgSender{&mockBot, s, log.New(os.Stderr, "", 0), []int64{1, 2, 3}}
|
|
||||||
|
|
||||||
tested := MakeSendToTelegramSync(&sender, log.New(os.Stderr, "", 0), tpl)
|
|
||||||
tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
|
|
||||||
a.Equal(3, len(mockBot.messages))
|
|
||||||
}
|
|
||||||
136
sender_worker.go
136
sender_worker.go
|
|
@ -1,8 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.sr.ht/~michalr/go-satel"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -18,8 +26,136 @@ type Sleeper interface {
|
||||||
Sleep(ch chan<- interface{})
|
Sleep(ch chan<- interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Consume(events <-chan GenericMessage) {
|
||||||
|
go func() {
|
||||||
|
for range events {
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage {
|
||||||
|
returnEvents := make(chan GenericMessage)
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer close(returnEvents)
|
||||||
|
|
||||||
|
for e := range events {
|
||||||
|
returnEvents <- e
|
||||||
|
err := s.Send(e, tpl)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: handle it better
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return returnEvents
|
||||||
|
}
|
||||||
|
|
||||||
|
func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
||||||
|
wg.Add(1)
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
if len(url) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, url, nil)
|
||||||
|
|
||||||
|
client := http.Client{
|
||||||
|
Timeout: httpTimeout,
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
logger.Print("Could not POST ", url, ": ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Print("Notified via HTTP with result ", res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifyAllHttp(urls []string, logger *log.Logger, wg *sync.WaitGroup) {
|
||||||
|
for _, uri := range urls {
|
||||||
|
go doHttpNotification(uri, logger, wg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NotifyViaHTTP(events <-chan GenericMessage, config AppConfig, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage {
|
||||||
|
returnEvents := make(chan GenericMessage)
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer close(returnEvents)
|
||||||
|
|
||||||
|
for e := range events {
|
||||||
|
returnEvents <- e
|
||||||
|
inner_arm:
|
||||||
|
for _, basicElement := range e.Messages {
|
||||||
|
if (basicElement.Index == NotificationPartitionIndex) && (basicElement.Type == satel.ArmedPartition) {
|
||||||
|
if basicElement.Value == ArmedPartition_Armed {
|
||||||
|
notifyAllHttp(config.ArmCallbackUrls, logger, wg)
|
||||||
|
} else {
|
||||||
|
notifyAllHttp(config.DisarmCallbackUrls, logger, wg)
|
||||||
|
}
|
||||||
|
break inner_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inner_alarm:
|
||||||
|
for _, basicElement := range e.Messages {
|
||||||
|
if basicElement.Type == satel.PartitionAlarm {
|
||||||
|
if basicElement.Value == PartitionAlarm_Alarm {
|
||||||
|
notifyAllHttp(config.AlarmCallbackUrls, logger, wg)
|
||||||
|
break inner_alarm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return returnEvents
|
||||||
|
}
|
||||||
|
|
||||||
type MatterbridgeMessage struct {
|
type MatterbridgeMessage struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Gateway string `json:"gateway"`
|
Gateway string `json:"gateway"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config AppConfig, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage {
|
||||||
|
returnEvents := make(chan GenericMessage)
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
defer close(returnEvents)
|
||||||
|
|
||||||
|
for e := range events {
|
||||||
|
returnEvents <- e
|
||||||
|
for _, matterbridgeConfig := range config.Matterbridge {
|
||||||
|
body, err := json.Marshal(MatterbridgeMessage{
|
||||||
|
Text: e.Format(tpl, s, logger),
|
||||||
|
Username: matterbridgeConfig.Username,
|
||||||
|
Gateway: matterbridgeConfig.Gateway,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal("Could not marshal a JSON message: ", err)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body))
|
||||||
|
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)}
|
||||||
|
client := http.Client{
|
||||||
|
Timeout: httpTimeout,
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Print("Notified via Matterbridge with result ", res.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return returnEvents
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,19 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
"git.sr.ht/~michalr/go-satel"
|
||||||
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MockTgBotAPI struct {
|
||||||
|
callCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
|
||||||
|
self.callCount += 1
|
||||||
|
return tgbotapi.Message{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tgSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
tgSenderMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
||||||
)
|
)
|
||||||
|
|
@ -17,9 +27,9 @@ var (
|
||||||
func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
|
func TestTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
tpl := template.Must(template.New("TelegramMessage").Parse(""))
|
tpl := template.Must(template.New("TelegramMessage").Parse(""))
|
||||||
mockBot := MockTgBotAPI{}
|
mockBot := MockTgBotAPI{0}
|
||||||
|
|
||||||
tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}}
|
tested := TgSender{&mockBot, MockSatelNameGetter{"mockPart"}, log.New(io.Discard, "", 0), []int64{}}
|
||||||
tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl)
|
tested.Send(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}, tpl)
|
||||||
a.Equal(0, len(mockBot.messages))
|
a.Equal(0, mockBot.callCount)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.sr.ht/~michalr/go-satel"
|
"git.sr.ht/~michalr/go-satel"
|
||||||
|
|
@ -20,27 +21,49 @@ func (self *MockTemplateSender) Send(msg GenericMessage, tpl *template.Template)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MockSatelNameGetter struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) {
|
||||||
|
retval := satel.NameEvent{
|
||||||
|
DevType: devType,
|
||||||
|
DevNumber: index,
|
||||||
|
DevTypeFunction: 0,
|
||||||
|
DevName: self.name,
|
||||||
|
}
|
||||||
|
return &retval, nil
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
|
||||||
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
|
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTelegramTemplate(t *testing.T) {
|
func TestTelegramTemplate(t *testing.T) {
|
||||||
|
testEvents := make(chan GenericMessage)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
|
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
|
||||||
tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate)
|
tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
tgSender := MakeSendToTelegramSync(&mockSender, log.New(io.Discard, "", log.Ltime), tpl)
|
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))
|
||||||
tgSender.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
// assert.Equal(t, "siemka", mockSender.message)
|
// assert.Equal(t, "siemka", mockSender.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIRCTemplate(t *testing.T) {
|
func TestIRCTemplate(t *testing.T) {
|
||||||
|
testEvents := make(chan GenericMessage)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
|
mockSender := MockTemplateSender{s: MockSatelNameGetter{"mockPart"}}
|
||||||
tpl, err := template.New("TestTemplate").Parse(IRCMessageTemplate)
|
tpl, err := template.New("TestIRCTemplate").Parse(IRCMessageTemplate)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
tgSender := MakeSendToTelegramSync(&mockSender, log.New(io.Discard, "", log.Ltime), tpl)
|
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))
|
||||||
tgSender.Call(GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}})
|
testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
|
||||||
|
close(testEvents)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
// assert.Equal(t, "siemka", mockSender.message)
|
// assert.Equal(t, "siemka", mockSender.message)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "git.sr.ht/~michalr/go-satel"
|
||||||
"git.sr.ht/~michalr/go-satel"
|
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
func makeTestSatelEvent(changeType satel.ChangeType, index int, val bool) satel.Event {
|
func makeTestSatelEvent(changeType satel.ChangeType, index int, val bool) satel.Event {
|
||||||
return satel.Event{
|
return satel.Event{
|
||||||
BasicEvents: []satel.BasicEventElement{{Type: changeType, Index: index, Value: val}},
|
BasicEvents: []satel.BasicEventElement{{Type: changeType, Index: index, Value: val}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeGenericMessage(changeType satel.ChangeType, index int, val bool) GenericMessage {
|
|
||||||
return GenericMessage{[]satel.BasicEventElement{{Type: changeType, Index: index, Value: val}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
type MockTgBotAPI struct {
|
|
||||||
messages []tgbotapi.Chattable
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *MockTgBotAPI) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
|
|
||||||
self.messages = append(self.messages, c)
|
|
||||||
return tgbotapi.Message{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type MockSatelNameGetter struct {
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self MockSatelNameGetter) GetName(devType satel.DeviceType, index byte) (*satel.NameEvent, error) {
|
|
||||||
retval := satel.NameEvent{
|
|
||||||
DevType: devType,
|
|
||||||
DevNumber: index,
|
|
||||||
DevTypeFunction: 0,
|
|
||||||
DevName: self.name,
|
|
||||||
}
|
|
||||||
return &retval, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue