Fix nil pointer error in knx_listener (#9444)
This commit is contained in:
parent
138c204388
commit
9b22161d92
|
|
@ -148,9 +148,11 @@ func (kl *KNXListener) listen() {
|
||||||
// Match GA to DataPointType and measurement name
|
// Match GA to DataPointType and measurement name
|
||||||
ga := msg.Destination.String()
|
ga := msg.Destination.String()
|
||||||
target, ok := kl.gaTargetMap[ga]
|
target, ok := kl.gaTargetMap[ga]
|
||||||
if !ok && !kl.gaLogbook[ga] {
|
if !ok {
|
||||||
kl.Log.Infof("Ignoring message %+v for unknown GA %q", msg, ga)
|
if !kl.gaLogbook[ga] {
|
||||||
kl.gaLogbook[ga] = true
|
kl.Log.Infof("Ignoring message %+v for unknown GA %q", msg, ga)
|
||||||
|
kl.gaLogbook[ga] = true
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,31 @@ func setValue(data dpt.DatapointValue, value interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestMessage struct {
|
||||||
|
address string
|
||||||
|
dpt string
|
||||||
|
value interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProduceKnxEvent(t *testing.T, address string, datapoint string, value interface{}) *knx.GroupEvent {
|
||||||
|
addr, err := cemi.NewGroupAddrString(address)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
data, ok := dpt.Produce(datapoint)
|
||||||
|
require.True(t, ok)
|
||||||
|
err = setValue(data, value)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return &knx.GroupEvent{
|
||||||
|
Command: knx.GroupWrite,
|
||||||
|
Destination: addr,
|
||||||
|
Data: data.Pack(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRegularReceives_DPT(t *testing.T) {
|
func TestRegularReceives_DPT(t *testing.T) {
|
||||||
// Define the test-cases
|
// Define the test-cases
|
||||||
var testcases = []struct {
|
var testcases = []TestMessage{
|
||||||
address string
|
|
||||||
dpt string
|
|
||||||
value interface{}
|
|
||||||
}{
|
|
||||||
{"1/0/1", "1.001", true},
|
{"1/0/1", "1.001", true},
|
||||||
{"1/0/2", "1.002", false},
|
{"1/0/2", "1.002", false},
|
||||||
{"1/0/3", "1.003", true},
|
{"1/0/3", "1.003", true},
|
||||||
|
|
@ -95,19 +113,8 @@ func TestRegularReceives_DPT(t *testing.T) {
|
||||||
|
|
||||||
// Send the defined test data
|
// Send the defined test data
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
addr, err := cemi.NewGroupAddrString(testcase.address)
|
event := ProduceKnxEvent(t, testcase.address, testcase.dpt, testcase.value)
|
||||||
require.NoError(t, err)
|
client.Send(*event)
|
||||||
|
|
||||||
data, ok := dpt.Produce(testcase.dpt)
|
|
||||||
require.True(t, ok)
|
|
||||||
err = setValue(data, testcase.value)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
client.Send(knx.GroupEvent{
|
|
||||||
Command: knx.GroupWrite,
|
|
||||||
Destination: addr,
|
|
||||||
Data: data.Pack(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give the accumulator some time to collect the data
|
// Give the accumulator some time to collect the data
|
||||||
|
|
@ -133,3 +140,51 @@ func TestRegularReceives_DPT(t *testing.T) {
|
||||||
assert.True(t, !tstart.After(m.Time))
|
assert.True(t, !tstart.After(m.Time))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegularReceives_MultipleMessages(t *testing.T) {
|
||||||
|
listener := KNXListener{
|
||||||
|
ServiceType: "dummy",
|
||||||
|
Measurements: []Measurement{
|
||||||
|
{"temperature", "1.001", []string{"1/1/1"}},
|
||||||
|
},
|
||||||
|
Log: testutil.Logger{Name: "knx_listener"},
|
||||||
|
}
|
||||||
|
|
||||||
|
acc := &testutil.Accumulator{}
|
||||||
|
|
||||||
|
// Setup the listener to test
|
||||||
|
err := listener.Start(acc)
|
||||||
|
require.NoError(t, err)
|
||||||
|
client := listener.client.(*KNXDummyInterface)
|
||||||
|
|
||||||
|
testMessages := []TestMessage{
|
||||||
|
{"1/1/1", "1.001", true},
|
||||||
|
{"1/1/1", "1.001", false},
|
||||||
|
{"1/1/2", "1.001", false},
|
||||||
|
{"1/1/2", "1.001", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testcase := range testMessages {
|
||||||
|
event := ProduceKnxEvent(t, testcase.address, testcase.dpt, testcase.value)
|
||||||
|
client.Send(*event)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Give the accumulator some time to collect the data
|
||||||
|
acc.Wait(2)
|
||||||
|
|
||||||
|
// Stop the listener
|
||||||
|
listener.Stop()
|
||||||
|
|
||||||
|
// Check if we got what we expected
|
||||||
|
require.Len(t, acc.Metrics, 2)
|
||||||
|
|
||||||
|
assert.Equal(t, "temperature", acc.Metrics[0].Measurement)
|
||||||
|
assert.Equal(t, "1/1/1", acc.Metrics[0].Tags["groupaddress"])
|
||||||
|
assert.Len(t, acc.Metrics[0].Fields, 1)
|
||||||
|
assert.Equal(t, true, acc.Metrics[0].Fields["value"])
|
||||||
|
|
||||||
|
assert.Equal(t, "temperature", acc.Metrics[1].Measurement)
|
||||||
|
assert.Equal(t, "1/1/1", acc.Metrics[1].Tags["groupaddress"])
|
||||||
|
assert.Len(t, acc.Metrics[1].Fields, 1)
|
||||||
|
assert.Equal(t, false, acc.Metrics[1].Fields["value"])
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue