Skip to content

Commit

Permalink
refactor: leverage latest profile cache
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Hung <[email protected]>
  • Loading branch information
Chris Hung committed Apr 6, 2021
1 parent 7dcc612 commit 6bfecdd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
42 changes: 18 additions & 24 deletions internal/application/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,11 @@ func CommandHandler(isRead bool, sendEvent bool, correlationID string, vars map[
}
}()

var method string
if isRead {
method = common.GetCmdMethod
} else {
method = common.SetCmdMethod
}
cmd := vars[v2.Command]
cmdExists, e := cache.Profiles().CommandExists(device.ProfileName, cmd, method)
if e != nil {
cmdExists, err := cache.Profiles().CommandExists(device.ProfileName, cmd)
if err != nil {
errMsg := fmt.Sprintf("failed to identify command %s in cache", cmd)
return res, edgexErr.NewCommonEdgeX(edgexErr.KindServerError, errMsg, e)
return res, edgexErr.NewCommonEdgeX(edgexErr.KindServerError, errMsg, err)
}

helper := NewCommandProcessor(device, deviceResource, correlationID, cmd, body, attributes, dic)
Expand Down Expand Up @@ -177,23 +171,23 @@ func (c *CommandProcessor) ReadCommand() (res *dtos.Event, e edgexErr.EdgeX) {
lc := bootstrapContainer.LoggingClientFrom(c.dic.Get)
lc.Debugf("Application - readCmd: reading cmd: %s; %s: %s", c.cmd, common.CorrelationHeader, c.correlationID)

// check GET ResourceOperation(s) exist for provided command
ros, e := cache.Profiles().ResourceOperations(c.device.ProfileName, c.cmd, common.GetCmdMethod)
if e != nil {
errMsg := fmt.Sprintf("GET ResourceOperation(s) for %s command not found", c.cmd)
return res, edgexErr.NewCommonEdgeX(edgexErr.KindNotAllowed, errMsg, e)
// check deviceCommand is not write-only
dc, _ := cache.Profiles().DeviceCommand(c.device.ProfileName, c.cmd)
if dc.ReadWrite == v2.ReadWrite_W {
errMsg := fmt.Sprintf("deviceCommand %s is marked as write-only", c.cmd)
return res, edgexErr.NewCommonEdgeX(edgexErr.KindNotAllowed, errMsg, nil)
}

// check ResourceOperation count does not exceed MaxCmdOps defined in configuration
configuration := container.ConfigurationFrom(c.dic.Get)
if len(ros) > configuration.Device.MaxCmdOps {
if len(dc.ResourceOperations) > configuration.Device.MaxCmdOps {
errMsg := fmt.Sprintf("GET command %s exceed device %s MaxCmdOps (%d)", c.cmd, c.device.Name, configuration.Device.MaxCmdOps)
return res, edgexErr.NewCommonEdgeX(edgexErr.KindServerError, errMsg, nil)
}

// prepare CommandRequests
reqs := make([]dsModels.CommandRequest, len(ros))
for i, op := range ros {
reqs := make([]dsModels.CommandRequest, len(dc.ResourceOperations))
for i, op := range dc.ResourceOperations {
drName := op.DeviceResource
// check the deviceResource in ResourceOperation actually exist
dr, ok := cache.Profiles().DeviceResource(c.device.ProfileName, drName)
Expand Down Expand Up @@ -305,16 +299,16 @@ func (c *CommandProcessor) WriteCommand() edgexErr.EdgeX {
lc := bootstrapContainer.LoggingClientFrom(c.dic.Get)
lc.Debugf("Application - writeCmd: writing command: %s; %s: %s", c.cmd, common.CorrelationHeader, c.correlationID)

// check SET ResourceOperation(s) exist for provided command
ros, e := cache.Profiles().ResourceOperations(c.device.ProfileName, c.cmd, common.SetCmdMethod)
if e != nil {
errMsg := fmt.Sprintf("SET ResourceOperation(s) for %s command not found", c.cmd)
return edgexErr.NewCommonEdgeX(edgexErr.KindNotAllowed, errMsg, e)
// check deviceCommand is not read-only
dc, _ := cache.Profiles().DeviceCommand(c.device.ProfileName, c.cmd)
if dc.ReadWrite == v2.ReadWrite_R {
errMsg := fmt.Sprintf("deviceCommand %s is marked as read-only", c.cmd)
return edgexErr.NewCommonEdgeX(edgexErr.KindNotAllowed, errMsg, nil)
}

// check ResourceOperation count does not exceed MaxCmdOps defined in configuration
configuration := container.ConfigurationFrom(c.dic.Get)
if len(ros) > configuration.Device.MaxCmdOps {
if len(dc.ResourceOperations) > configuration.Device.MaxCmdOps {
errMsg := fmt.Sprintf("SET command %s exceed device %s MaxCmdOps (%d)", c.cmd, c.device.Name, configuration.Device.MaxCmdOps)
return edgexErr.NewCommonEdgeX(edgexErr.KindServerError, errMsg, nil)
}
Expand All @@ -327,7 +321,7 @@ func (c *CommandProcessor) WriteCommand() edgexErr.EdgeX {

// create CommandValues
cvs := make([]*dsModels.CommandValue, 0, len(paramMap))
for _, ro := range ros {
for _, ro := range dc.ResourceOperations {
drName := ro.DeviceResource
// check the deviceResource in ResourceOperation actually exist
dr, ok := cache.Profiles().DeviceResource(c.device.ProfileName, drName)
Expand Down
3 changes: 1 addition & 2 deletions internal/transformer/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"

"github.com/edgexfoundry/device-sdk-go/v2/internal/cache"
"github.com/edgexfoundry/device-sdk-go/v2/internal/common"
"github.com/edgexfoundry/device-sdk-go/v2/internal/container"
"github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
)
Expand Down Expand Up @@ -81,7 +80,7 @@ func CommandValuesToEventDTO(cvs []*models.CommandValue, deviceName string, sour
}

// ResourceOperation mapping
ro, err := cache.Profiles().ResourceOperation(device.ProfileName, cv.DeviceResourceName, common.GetCmdMethod)
ro, err := cache.Profiles().ResourceOperation(device.ProfileName, cv.DeviceResourceName)
if err != nil {
// this allows SDK to directly read deviceResource without deviceCommands defined.
lc.Debugf("failed to read ResourceOperation: %v", err)
Expand Down
17 changes: 8 additions & 9 deletions pkg/service/managedprofiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,20 @@ func (s *DeviceService) UpdateDeviceProfile(profile models.DeviceProfile) errors
return err
}

// ResourceOperation retrieves the first matched ResourceOperation instance from cache according to
// the Device name, Device Resource name, and the method (get or set).
func (s *DeviceService) ResourceOperation(deviceName string, deviceResource string, method string) (models.ResourceOperation, bool) {
// DeviceCommand retrieves the specific DeviceCommand instance from cache according to
// the Device name and Command name
func (s *DeviceService) DeviceCommand(deviceName string, commandName string) (models.DeviceCommand, bool) {
device, ok := cache.Devices().ForName(deviceName)
if !ok {
s.LoggingClient.Errorf("failed to find device %s in cache", deviceName)
return models.ResourceOperation{}, false
return models.DeviceCommand{}, false
}

ro, err := cache.Profiles().ResourceOperation(device.ProfileName, deviceResource, method)
if err != nil {
s.LoggingClient.Error(err.Error())
return ro, false
dc, ok := cache.Profiles().DeviceCommand(device.ProfileName, commandName)
if !ok {
return dc, false
}
return ro, true
return dc, true
}

// DeviceResource retrieves the specific DeviceResource instance from cache according to
Expand Down

0 comments on commit 6bfecdd

Please sign in to comment.