Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modbus_gateway input plugin #8013

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d4f41af
Create modbusgw input plugin
wz2b Aug 19, 2020
e7531b5
Create modbusgw input plugin
wz2b Aug 19, 2020
b278450
Merge branch 'master' of https://github.com/influxdata/telegraf into …
wz2b Aug 20, 2020
cb616e7
Remove toml "default" and add default to Init
wz2b Aug 20, 2020
504bb0a
rename module from modbusgw to modbus_gateway. Removed plugin name f…
wz2b Aug 20, 2020
7d4b5c3
Fix name of plugin (was modbusgw, now modbus_gateway) in README.md
wz2b Aug 20, 2020
0773598
Fixed some typos in the README.md and made the intent of default regi…
wz2b Aug 21, 2020
03f5b6d
fix missing close quote in comment
wz2b Aug 21, 2020
fe7df2d
Add OutputFormat parameter to field definitions to allow fields to be…
wz2b Aug 22, 2020
42012c5
Fixed error in unit test that made it not compile. Added unit tests …
wz2b Aug 23, 2020
419aff2
Merge branch 'master' of https://github.com/influxdata/telegraf into …
wz2b Aug 23, 2020
9e26adb
An entirely new way to convert values, using the built-in go binary s…
wz2b Aug 24, 2020
65635ac
Improved one of the for loops to not have 'i' hanging around past the…
wz2b Aug 24, 2020
1700001
Not sure why, but licenses were missing, probably do to some dependen…
wz2b Aug 25, 2020
87d8b24
Merge branch 'master' of https://github.com/influxdata/telegraf into …
wz2b Aug 25, 2020
8c36cdf
license declaration in wrong alphabetical order
wz2b Aug 25, 2020
5a45c6a
Updated CustomByteOrder to work using a pointer receiver rather than …
wz2b Aug 25, 2020
b3c79bb
oops, missed one change due to conversion update
wz2b Aug 25, 2020
5896856
fix tests
wz2b Aug 25, 2020
de37d8f
some dependency failed, ran go mod tidy
wz2b Aug 25, 2020
5aa4654
Add unit tests for converting (using the modbus byte order specifier)…
wz2b Aug 26, 2020
09711a0
Add floating point conversion tests; find some bugs, fix them up.
wz2b Aug 26, 2020
8d38560
Add a read flush before launching the next poll. This is to address …
wz2b Aug 26, 2020
09dd9c7
The defautl modbus implementation used by the original driver has a b…
wz2b Aug 26, 2020
ac4e062
use wz2b/modbus v0.1.1
wz2b Aug 26, 2020
5597346
Updated plugin documentation
wz2b Aug 28, 2020
ff5fee9
Add a little debug output
wz2b Aug 28, 2020
6614e88
Merge branch 'master' into modbusgw-input-plugin
ssoroka May 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
wz2b committed Aug 25, 2020
commit 589685695f833758409c9e4c95b2fba7c3256da4
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ require (
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8
github.com/stretchr/testify v1.5.1
github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62
github.com/tedsuo/ifrit v0.0.0-20191009134036-9a97d0632f00 // indirect
github.com/tedsuo/ifrit v0.0.0-20191009134036-9a97d0632f00
github.com/tidwall/gjson v1.6.0
github.com/vishvananda/netlink v0.0.0-20171020171820-b2de5d10e38e // indirect
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc // indirect
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/modbus_gateway/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func _assert(t *testing.T, order *CustomByteOrder, err error, success bool, expe
t.Errorf("Test %s (%s) expected 0x%08X, got 0x%08X", order.order, reflect.TypeOf(expected), expected, got)
t.Logf(" order should have been %v", order.positions)
} else {
t.Logf("Test %s (%s) PASSED", order.order, reflect.TypeOf(expected))
//t.Logf("Test %s (%s) PASSED", order.order, reflect.TypeOf(expected))
}

}
7 changes: 4 additions & 3 deletions plugins/inputs/modbus_gateway/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/influxdata/telegraf/metric"
"github.com/prometheus/common/log"
"math"
"reflect"
"time"
)

Expand Down Expand Up @@ -135,7 +136,7 @@ func scale(f *FieldDef, value interface{}) interface{} {
return nil
}

case "INT", "INT64":
case "INT", "INT32", "INT64":
switch v := value.(type) {
case int:
return int64(math.Round((float64(v) * f.Scale) + f.Offset))
Expand All @@ -151,7 +152,7 @@ func scale(f *FieldDef, value interface{}) interface{} {
return nil
}

case "UINT", "UINT64":
case "UINT", "UINT32", "UINT64":
switch v := value.(type) {
case int:
return uint64(math.Round((float64(v) * f.Scale) + f.Offset))
Expand All @@ -168,7 +169,7 @@ func scale(f *FieldDef, value interface{}) interface{} {
}

default:
log.Warn("Invalid output format")
log.Warnf("Invalid output format %s", reflect.TypeOf(value))
return nil
}
}
23 changes: 14 additions & 9 deletions plugins/inputs/modbus_gateway/modbus_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@ func TestModbusGroupings(t *testing.T) {
g := metric.NewSeriesGrouper()

tm := time.Now()
r := Request{}

f1 := FieldDef{
Name: "f1",
Scale: 1.0,
Offset: 0.0,
OutputFormat: "INT64",
OutputFormat: "UINT32",
Omit: false,
}
f2 := FieldDef{
Name: "f2",
Scale: 1.0,
Offset: 0.0,
OutputFormat: "FLOAT",
OutputFormat: "UINT32",
Omit: false,
}

outputToGroup(g, &r, &f1, 1, tm)
outputToGroup(g, &r, &f2, 1, tm)
e := g.Add("m", nil, tm, f1.Name, scale(&f1, uint32(1)))
if e != nil {
t.Errorf("Could not add field %s", f1.Name)
}

e = g.Add("m", nil, tm, f2.Name, scale(&f2, uint32(1)))
if e != nil {
t.Errorf("Could not add field %s", f2.Name)
}

if len(g.Metrics()) != 1 {
t.Errorf("Grouping failed - should have generated 1 metric, but generated %d\n", len(g.Metrics()))
}

t.Logf("Metrics: %++v", g.Metrics())

firstMetric := g.Metrics()[0]

rf1, ok := firstMetric.GetField("f1")
Expand All @@ -49,7 +54,7 @@ func TestModbusGroupings(t *testing.T) {
return
}

_, ok = rf1.(int64)
_, ok = rf1.(uint64)
if !ok {
t.Errorf("Metric did not match field type specificier, type was %s", reflect.TypeOf(rf1))
}
Expand All @@ -60,7 +65,7 @@ func TestModbusGroupings(t *testing.T) {
return
}

_, ok = rf2.(float64)
_, ok = rf2.(uint64)
if !ok {
t.Errorf("Metric did not match field type specificier, type was %s", reflect.TypeOf(rf1))
}
Expand Down