Skip to content

Commit

Permalink
Sunspec.Query: simplify code
Browse files Browse the repository at this point in the history
use direct map access instead of iterating over it.
  • Loading branch information
SchumacherFM committed Apr 10, 2024
1 parent c46b9da commit a434fe4
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions meters/sunspec/sunspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (d *SunSpec) collectModels(device sunspec.Device) error {
func (d *SunSpec) relevantModelIds() []sunspec.ModelId {
modelIds := make([]sunspec.ModelId, 0, len(modelMap))
for k := range modelMap {
modelIds = append(modelIds, sunspec.ModelId(k))
modelIds = append(modelIds, k)
}

return modelIds
Expand Down Expand Up @@ -297,27 +297,27 @@ func (d *SunSpec) QueryOp(client modbus.Client, measurement meters.Measurement)
}

for _, model := range d.models {
for modelID, blockMap := range modelMap {
if modelID != model.Id() {
modelID := model.Id()
blockMap, ok := modelMap[modelID]
if !ok {
continue
}

for blockID, pointMap := range blockMap {
if blockID >= model.Blocks() {
continue
}

for blockID, pointMap := range blockMap {
if blockID >= model.Blocks() {
continue
}

for pointID, m := range pointMap {
if m == measurement {
v, err := d.QueryPoint(client, int(modelID), blockID, pointID)

var mr meters.MeasurementResult
if err == nil {
mr = makeResult(v, measurement)
}
for pointID, m := range pointMap {
if m == measurement {
v, err := d.QueryPoint(client, int(modelID), blockID, pointID)

return mr, err
var mr meters.MeasurementResult
if err == nil {
mr = makeResult(v, measurement)
}

return mr, err
}
}
}
Expand All @@ -334,44 +334,44 @@ func (d *SunSpec) Query(client modbus.Client) (res []meters.MeasurementResult, e
}

for _, model := range d.models {
for modelID, blockMap := range modelMap {
if modelID != model.Id() {
continue
}
blockMap, ok := modelMap[model.Id()]
if !ok {
continue
}

// sort blocks so block 0 is always read first
sortedBlocks := make([]int, 0, len(blockMap))
for k := range blockMap {
sortedBlocks = append(sortedBlocks, k)
}
sort.Ints(sortedBlocks)
// sort blocks so block 0 is always read first
sortedBlocks := make([]int, 0, len(blockMap))
for k := range blockMap {
sortedBlocks = append(sortedBlocks, k)
}
sort.Ints(sortedBlocks)

// always add zero block
if sortedBlocks[0] != 0 {
sortedBlocks = append([]int{0}, sortedBlocks...)
}
// always add zero block
if sortedBlocks[0] != 0 {
sortedBlocks = append([]int{0}, sortedBlocks...)
}

for blockID := range sortedBlocks {
if blockID >= model.Blocks() {
continue
}
for blockID := range sortedBlocks {
if blockID >= model.Blocks() {
continue
}

pointMap := blockMap[blockID]
block := model.MustBlock(blockID)
pointMap := blockMap[blockID]
block := model.MustBlock(blockID)

if err := block.Read(); err != nil {
return res, err
}
if err := block.Read(); err != nil {
return res, err
}

for pointID, m := range pointMap {
point := block.MustPoint(pointID)
for pointID, m := range pointMap {
point := block.MustPoint(pointID)

if v, err := d.convertPoint(block, point); err == nil {
res = append(res, makeResult(v, m))
}
if v, err := d.convertPoint(block, point); err == nil {
res = append(res, makeResult(v, m))
}
}
}

}

return res, nil
Expand Down

0 comments on commit a434fe4

Please sign in to comment.