Skip to content

Commit c21fb12

Browse files
authored
Add option to preread device tree (#349)
1 parent 3191c0d commit c21fb12

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ require (
6161
gopkg.in/yaml.v3 v3.0.1 // indirect
6262
)
6363

64-
replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0
64+
replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373

go.sum

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ github.com/dmarkham/enumer v1.5.9 h1:NM/1ma/AUNieHZg74w67GkHFBNB15muOt3sj486QVZk
1515
github.com/dmarkham/enumer v1.5.9/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E=
1616
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
1717
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
18-
github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0 h1:TRyQYvEfiVsQup88bfboOmFy8bPYltAFJfzeS6JcSP4=
19-
github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0/go.mod h1:ei49YhPP0v5cVJZAX9k/VdqQakUYNKOEvKB/6vnkZaU=
18+
github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373 h1:+LFx0Rik2XlqQM8sq3VW3c8DbxN2hKBkjjusniBSsAs=
19+
github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373/go.mod h1:WpbUAyptAAi0VAriSRopZa6uhiJOJCTz7KFvgGtNRXc=
2020
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
2121
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
2222
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
@@ -25,9 +25,8 @@ github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7z
2525
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
2626
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
2727
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
28-
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
29-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
30-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
28+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
29+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3130
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
3231
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
3332
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
@@ -40,7 +39,6 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
4039
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
4140
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
4241
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
43-
github.com/grid-x/serial v0.0.0-20191104121038-e24bc9bf6f08/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk=
4442
github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa h1:Rsn6ARgNkXrsXJIzhkE4vQr5Gbx2LvtEMv4BJOK4LyU=
4543
github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk=
4644
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
@@ -135,5 +133,5 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
135133
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
136134
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
137135
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
138-
pgregory.net/rapid v0.4.7 h1:MTNRktPuv5FNqOO151TM9mDTa+XHcX6ypYeISDVD14g=
139-
pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
136+
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
137+
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=

meters/sunspec/sunspec.go

+31-21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ func FixKostal(p sunspec.Point) {
3939
}
4040
}
4141

42+
// Initialize reads the device tree
43+
func DeviceTree(client modbus.Client) ([]sunspec.Device, error) {
44+
in, err := sunspecbus.Open(client)
45+
if err != nil {
46+
if in == nil {
47+
return nil, err
48+
}
49+
50+
err = meters.ErrPartiallyOpened
51+
}
52+
53+
devices := in.Collect(sunspec.AllDevices)
54+
if len(devices) == 0 {
55+
return nil, errors.New("sunspec: device not found")
56+
}
57+
58+
return devices, err
59+
}
60+
4261
// NewDevice creates a Sunspec device
4362
func NewDevice(meterType string, subdevice ...int) *SunSpec {
4463
var dev int
@@ -58,20 +77,20 @@ func NewDevice(meterType string, subdevice ...int) *SunSpec {
5877

5978
// Initialize implements the Device interface
6079
func (d *SunSpec) Initialize(client modbus.Client) error {
61-
var partiallyOpen bool
62-
in, err := sunspecbus.Open(client)
63-
if err != nil {
64-
if in == nil {
65-
return err
66-
}
67-
68-
partiallyOpen = true
80+
devices, err := DeviceTree(client)
81+
if err != nil && !errors.Is(err, meters.ErrPartiallyOpened) {
82+
return err
6983
}
7084

71-
devices := in.Collect(sunspec.AllDevices)
72-
if len(devices) == 0 {
73-
return errors.New("sunspec: device not found")
85+
if err := d.InitializeWithTree(devices); err != nil {
86+
return err
7487
}
88+
89+
// this may be ErrPartiallyOpened
90+
return err
91+
}
92+
93+
func (d *SunSpec) InitializeWithTree(devices []sunspec.Device) error {
7594
if len(devices) <= d.subdevice {
7695
return fmt.Errorf("sunspec: subdevice %d not found", d.subdevice)
7796
}
@@ -84,16 +103,7 @@ func (d *SunSpec) Initialize(client modbus.Client) error {
84103
}
85104

86105
// collect relevant models
87-
if err := d.collectModels(device); err != nil {
88-
return err
89-
}
90-
91-
// return partial open error if everything else went fine
92-
if partiallyOpen {
93-
err = fmt.Errorf("%w", meters.ErrPartiallyOpened)
94-
}
95-
96-
return err
106+
return d.collectModels(device)
97107
}
98108

99109
func stringVal(b sunspec.Block, point string) string {

0 commit comments

Comments
 (0)