diff --git a/go.mod b/go.mod index 3db899eb..885f7f7d 100644 --- a/go.mod +++ b/go.mod @@ -61,4 +61,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0 +replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373 diff --git a/go.sum b/go.sum index a4a9b3cb..9c75f731 100644 --- a/go.sum +++ b/go.sum @@ -15,8 +15,8 @@ github.com/dmarkham/enumer v1.5.9 h1:NM/1ma/AUNieHZg74w67GkHFBNB15muOt3sj486QVZk github.com/dmarkham/enumer v1.5.9/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E= github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik= github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE= -github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0 h1:TRyQYvEfiVsQup88bfboOmFy8bPYltAFJfzeS6JcSP4= -github.com/evcc-io/modbus v0.0.0-20240419133105-829633e7c6b0/go.mod h1:ei49YhPP0v5cVJZAX9k/VdqQakUYNKOEvKB/6vnkZaU= +github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373 h1:+LFx0Rik2XlqQM8sq3VW3c8DbxN2hKBkjjusniBSsAs= +github.com/evcc-io/modbus v0.0.0-20240915144537-980a0405c373/go.mod h1:WpbUAyptAAi0VAriSRopZa6uhiJOJCTz7KFvgGtNRXc= github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= 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 github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= 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= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -github.com/grid-x/serial v0.0.0-20191104121038-e24bc9bf6f08/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk= github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa h1:Rsn6ARgNkXrsXJIzhkE4vQr5Gbx2LvtEMv4BJOK4LyU= github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk= 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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -pgregory.net/rapid v0.4.7 h1:MTNRktPuv5FNqOO151TM9mDTa+XHcX6ypYeISDVD14g= -pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= +pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw= +pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= diff --git a/meters/sunspec/sunspec.go b/meters/sunspec/sunspec.go index 123cf9da..e2071569 100644 --- a/meters/sunspec/sunspec.go +++ b/meters/sunspec/sunspec.go @@ -39,6 +39,25 @@ func FixKostal(p sunspec.Point) { } } +// Initialize reads the device tree +func DeviceTree(client modbus.Client) ([]sunspec.Device, error) { + in, err := sunspecbus.Open(client) + if err != nil { + if in == nil { + return nil, err + } + + err = meters.ErrPartiallyOpened + } + + devices := in.Collect(sunspec.AllDevices) + if len(devices) == 0 { + return nil, errors.New("sunspec: device not found") + } + + return devices, err +} + // NewDevice creates a Sunspec device func NewDevice(meterType string, subdevice ...int) *SunSpec { var dev int @@ -58,20 +77,20 @@ func NewDevice(meterType string, subdevice ...int) *SunSpec { // Initialize implements the Device interface func (d *SunSpec) Initialize(client modbus.Client) error { - var partiallyOpen bool - in, err := sunspecbus.Open(client) - if err != nil { - if in == nil { - return err - } - - partiallyOpen = true + devices, err := DeviceTree(client) + if err != nil && !errors.Is(err, meters.ErrPartiallyOpened) { + return err } - devices := in.Collect(sunspec.AllDevices) - if len(devices) == 0 { - return errors.New("sunspec: device not found") + if err := d.InitializeWithTree(devices); err != nil { + return err } + + // this may be ErrPartiallyOpened + return err +} + +func (d *SunSpec) InitializeWithTree(devices []sunspec.Device) error { if len(devices) <= d.subdevice { return fmt.Errorf("sunspec: subdevice %d not found", d.subdevice) } @@ -84,16 +103,7 @@ func (d *SunSpec) Initialize(client modbus.Client) error { } // collect relevant models - if err := d.collectModels(device); err != nil { - return err - } - - // return partial open error if everything else went fine - if partiallyOpen { - err = fmt.Errorf("%w", meters.ErrPartiallyOpened) - } - - return err + return d.collectModels(device) } func stringVal(b sunspec.Block, point string) string {