Skip to content

Commit

Permalink
fix(inputs/modbus): #11105 fix requests starting with an omitted field (
Browse files Browse the repository at this point in the history
  • Loading branch information
TimurDela authored and MyaLongmire committed Jul 6, 2022
1 parent 92b3d44 commit 117a335
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
67 changes: 67 additions & 0 deletions plugins/inputs/modbus/modbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,3 +1857,70 @@ func TestConfigurationPerRequestFail(t *testing.T) {
})
}
}

func TestRequestsStartingWithOmits(t *testing.T) {
modbus := Modbus{
Name: "Test",
Controller: "tcp://localhost:1502",
ConfigurationType: "request",
Log: testutil.Logger{},
}
modbus.Requests = []requestDefinition{
{SlaveID: 1,
ByteOrder: "ABCD",
RegisterType: "holding",
Fields: []requestFieldDefinition{
{
Name: "holding-0",
Address: uint16(0),
InputType: "INT16",
Omit: true,
},
{
Name: "holding-1",
Address: uint16(1),
InputType: "UINT16",
Omit: true,
},
{
Name: "holding-2",
Address: uint16(2),
InputType: "INT16",
},
},
},
}
require.NoError(t, modbus.Init())
require.NotEmpty(t, modbus.requests)
require.NotNil(t, modbus.requests[1])
require.Equal(t, uint16(0), modbus.requests[1].holding[0].address)

serv := mbserver.NewServer()
require.NoError(t, serv.ListenTCP("localhost:1502"))
defer serv.Close()

handler := mb.NewTCPClientHandler("localhost:1502")
require.NoError(t, handler.Connect())
defer handler.Close()
client := mb.NewClient(handler)
_, err := client.WriteMultipleRegisters(uint16(0), 3, []byte{0x00, 0x01, 0x00, 0x02, 0x00, 0x03})
require.NoError(t, err)

expected := []telegraf.Metric{
testutil.MustMetric(
"modbus",
map[string]string{
"type": cHoldingRegisters,
"slave_id": strconv.Itoa(int(modbus.Requests[0].SlaveID)),
"name": modbus.Name,
},
map[string]interface{}{"holding-2": int16(3)},
time.Unix(0, 0),
),
}

var acc testutil.Accumulator
require.NoError(t, modbus.Gather(&acc))
acc.Wait(len(expected))
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
7 changes: 4 additions & 3 deletions plugins/inputs/modbus/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ func newRequest(f field, tags map[string]string) request {
r := request{
address: f.address,
length: f.length,
fields: []field{f},
fields: []field{},
tags: map[string]string{},
}

if !f.omit {
r.fields = append(r.fields, f)
}
// Copy the tags
for k, v := range tags {
r.tags[k] = v
Expand Down Expand Up @@ -63,6 +65,5 @@ func groupFieldsToRequests(fields []field, tags map[string]string, maxBatchSize
current = newRequest(f, tags)
}
requests = append(requests, current)

return requests
}

0 comments on commit 117a335

Please sign in to comment.