Skip to content

Commit

Permalink
feat(plc4go/cbus): handle context in browse field
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 30, 2022
1 parent 5aaf204 commit a589161
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions plc4go/internal/cbus/Browser.go
Expand Up @@ -49,7 +49,6 @@ func NewBrowser(connection *Connection, messageCodec spi.MessageCodec) *Browser
}

func (m Browser) BrowseField(ctx context.Context, browseRequest apiModel.PlcBrowseRequest, interceptor func(result apiModel.PlcBrowseEvent) bool, fieldName string, field apiModel.PlcField) (apiModel.PlcResponseCode, []apiModel.PlcBrowseFoundField) {
// TODO: handle ctx
var queryResults []apiModel.PlcBrowseFoundField
switch field := field.(type) {
case *unitInfoField:
Expand Down Expand Up @@ -79,6 +78,10 @@ func (m Browser) BrowseField(ctx context.Context, browseRequest apiModel.PlcBrow
}
unitLoop:
for _, unit := range units {
if err := ctx.Err(); err != nil {
log.Info().Err(err).Msgf("Aborting scan at unit %s", unit)
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
unitAddress := unit.GetAddress()
if !allUnits && allAttributes {
log.Info().Msgf("Querying all attributes of unit %d", unitAddress)
Expand All @@ -89,6 +92,10 @@ func (m Browser) BrowseField(ctx context.Context, browseRequest apiModel.PlcBrow
}
event.Msgf("Query unit %d", unitAddress)
for _, attribute := range attributes {
if err := ctx.Err(); err != nil {
log.Info().Err(err).Msgf("Aborting scan at unit %s", unit)
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
if !allUnits && !allAttributes {
log.Info().Msgf("Querying attribute %s of unit %d", attribute, unitAddress)
} else {
Expand Down Expand Up @@ -132,7 +139,7 @@ func (m Browser) BrowseField(ctx context.Context, browseRequest apiModel.PlcBrow
}
}
default:
return apiModel.PlcResponseCode_INTERNAL_ERROR, nil
return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
}
return apiModel.PlcResponseCode_OK, queryResults
}

0 comments on commit a589161

Please sign in to comment.