Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Upgrade core-contracts lib to use redesigned device profile #816

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions example/cmd/device-simple/res/Simple-Driver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,44 @@ description: "Example of Simple Device"
deviceResources:
-
name: "SwitchButton"
isHidden: false
description: "Switch On/Off."
properties:
valueType: "Bool"
readWrite: "RW"
defaultValue: "true"
-
name: "Image"
isHidden: false
description: "Visual representation of Switch state."
properties:
valueType: "Binary"
readWrite: "R"
mediaType: "image/jpeg"
-
name: "Xrotation"
isHidden: true
description: "X axis rotation rate"
properties:
valueType: "Int32"
readWrite: "RW"
-
name: "Yrotation"
isHidden: true
description: "Y axis rotation rate"
properties:
valueType: "Int32"
readWrite: "RW"
-
name: "Zrotation"
isHidden: true
description: "Z axis rotation rate"
properties:
valueType: "Int32"
readWrite: "RW"
-
name: "Uint8Array"
isHidden: false
description: "Unsigned 8bit array"
properties:
valueType: "Uint8Array"
Expand All @@ -49,35 +55,21 @@ deviceResources:
deviceCommands:
cloudxxx8 marked this conversation as resolved.
Show resolved Hide resolved
-
name: "Switch"
get:
- { deviceResource: "SwitchButton" }
set:
- { deviceResource: "SwitchButton", parameter: "false" }
isHidden: false
readWrite: "RW"
resourceOperations:
- { deviceResource: "SwitchButton", defaultValue: "false" }
-
name: "Image"
get:
isHidden: false
readWrite: "R"
resourceOperations:
- { deviceResource: "Image" }
-
name: "Rotation"
get:
- { deviceResource: "Xrotation" }
- { deviceResource: "Yrotation" }
- { deviceResource: "Zrotation" }
set:
- { deviceResource: "Xrotation", parameter: "0" }
- { deviceResource: "Yrotation", parameter: "0" }
- { deviceResource: "Zrotation", parameter: "0" }

coreCommands:
-
name: "Switch"
get: true
set: true
-
name: "Image"
get: true
set: false
-
name: "Rotation"
get: true
set: true
isHidden: false
readWrite: "RW"
resourceOperations:
- { deviceResource: "Xrotation", defaultValue: "0" }
- { deviceResource: "Yrotation", defaultValue: "0" }
- { deviceResource: "Zrotation", defaultValue: "0" }
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require (
bitbucket.org/bertimus9/systemstat v0.0.0-20180207000608-0eeff89b0690
github.com/OneOfOne/xxhash v1.2.8
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.0.0-dev.14
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.46
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.48
github.com/edgexfoundry/go-mod-registry/v2 v2.0.0-dev.3
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
Expand Down
2 changes: 1 addition & 1 deletion internal/transformer/transformparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
)

func TransformWriteParameter(cv *dsModels.CommandValue, pv models.PropertyValue) errors.EdgeX {
func TransformWriteParameter(cv *dsModels.CommandValue, pv models.ResourceProperties) errors.EdgeX {
if !isNumericValueType(cv) {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/transformer/transformresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
NaN = "NaN"
)

func TransformReadResult(cv *dsModels.CommandValue, pv models.PropertyValue) errors.EdgeX {
func TransformReadResult(cv *dsModels.CommandValue, pv models.ResourceProperties) errors.EdgeX {
if !isNumericValueType(cv) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/v2/application/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func (c *CommandProcessor) WriteCommand() edgexErr.EdgeX {
// check request body contains the deviceResource
value, ok := paramMap[ro.DeviceResource]
if !ok {
if ro.Parameter != "" {
value = ro.Parameter
if ro.DefaultValue != "" {
value = ro.DefaultValue
} else if dr.Properties.DefaultValue != "" {
value = dr.Properties.DefaultValue
} else {
Expand Down
25 changes: 6 additions & 19 deletions internal/v2/cache/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/edgexfoundry/device-sdk-go/v2/internal/common"
Expand All @@ -38,7 +39,6 @@ type profileCache struct {
deviceResourceMap map[string]map[string]models.DeviceResource
getResourceOperationsMap map[string]map[string][]models.ResourceOperation
setResourceOperationsMap map[string]map[string][]models.ResourceOperation
commandsMap map[string]map[string]models.Command
mutex sync.RWMutex
}

Expand All @@ -48,20 +48,18 @@ func newProfileCache(profiles []models.DeviceProfile) ProfileCache {
drMap := make(map[string]map[string]models.DeviceResource, defaultSize)
getRoMap := make(map[string]map[string][]models.ResourceOperation, defaultSize)
setRoMap := make(map[string]map[string][]models.ResourceOperation, defaultSize)
cmdMap := make(map[string]map[string]models.Command, defaultSize)
for _, dp := range profiles {
dpMap[dp.Name] = &dp
drMap[dp.Name] = deviceResourceSliceToMap(dp.DeviceResources)
getRoMap[dp.Name], setRoMap[dp.Name] = deviceCommandSliceToMap(dp.DeviceCommands)
cmdMap[dp.Name] = commandSliceToMap(dp.CoreCommands)
}

pc = &profileCache{
deviceProfileMap: dpMap,
deviceResourceMap: drMap,
getResourceOperationsMap: getRoMap,
setResourceOperationsMap: setRoMap,
commandsMap: cmdMap}
}
return pc
}

Expand Down Expand Up @@ -109,7 +107,6 @@ func (p *profileCache) add(profile models.DeviceProfile) errors.EdgeX {
p.deviceProfileMap[profile.Name] = &profile
p.deviceResourceMap[profile.Name] = deviceResourceSliceToMap(profile.DeviceResources)
p.getResourceOperationsMap[profile.Name], p.setResourceOperationsMap[profile.Name] = deviceCommandSliceToMap(profile.DeviceCommands)
p.commandsMap[profile.Name] = commandSliceToMap(profile.CoreCommands)
return nil
}

Expand All @@ -126,26 +123,17 @@ func deviceCommandSliceToMap(deviceCommands []models.DeviceCommand) (map[string]
getResult := make(map[string][]models.ResourceOperation, len(deviceCommands))
setResult := make(map[string][]models.ResourceOperation, len(deviceCommands))
for _, deviceCommand := range deviceCommands {
if len(deviceCommand.Get) > 0 {
getResult[deviceCommand.Name] = deviceCommand.Get
if strings.Contains(deviceCommand.ReadWrite, v2.ReadWrite_R) {
getResult[deviceCommand.Name] = deviceCommand.ResourceOperations
}
if len(deviceCommand.Set) > 0 {
setResult[deviceCommand.Name] = deviceCommand.Set
if strings.Contains(deviceCommand.ReadWrite, v2.ReadWrite_W) {
setResult[deviceCommand.Name] = deviceCommand.ResourceOperations
}
}

return getResult, setResult
}

func commandSliceToMap(commands []models.Command) map[string]models.Command {
result := make(map[string]models.Command, len(commands))
for _, cmd := range commands {
result[cmd.Name] = cmd
}

return result
}

// Update updates the profile in the cache
func (p *profileCache) Update(profile models.DeviceProfile) errors.EdgeX {
p.mutex.Lock()
Expand Down Expand Up @@ -177,7 +165,6 @@ func (p *profileCache) removeByName(name string) errors.EdgeX {
delete(p.deviceResourceMap, name)
delete(p.getResourceOperationsMap, name)
delete(p.setResourceOperationsMap, name)
delete(p.commandsMap, name)
return nil
}

Expand Down
17 changes: 9 additions & 8 deletions internal/v2/cache/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cache
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"
"github.com/stretchr/testify/assert"
)
Expand All @@ -19,13 +20,13 @@ var testProfile = models.DeviceProfile{
},
DeviceCommands: []models.DeviceCommand{
models.DeviceCommand{
Name: TestDeviceCommand,
Get: []models.ResourceOperation{
Name: TestDeviceCommand,
ReadWrite: v2.ReadWrite_R,
ResourceOperations: []models.ResourceOperation{
models.ResourceOperation{DeviceResource: TestDeviceResource},
},
},
},
CoreCommands: nil,
}

var newProfile = models.DeviceProfile{
Expand All @@ -35,13 +36,13 @@ var newProfile = models.DeviceProfile{
},
DeviceCommands: []models.DeviceCommand{
models.DeviceCommand{
Name: "newCommand",
Get: []models.ResourceOperation{
Name: "newCommand",
ReadWrite: v2.ReadWrite_R,
ResourceOperations: []models.ResourceOperation{
models.ResourceOperation{DeviceResource: "newResource"},
},
},
},
CoreCommands: nil,
}

func Test_profileCache_ForName(t *testing.T) {
Expand Down Expand Up @@ -178,7 +179,7 @@ func Test_profileCache_ResourceOperations(t *testing.T) {
{"Invalid - nonexistent Profile name", "nil", TestDeviceCommand, "GET", nil, true},
{"Invalid - nonexistent Command name", TestProfile, "nil", "GET", nil, true},
{"Invalid - invalid method", TestProfile, TestDeviceCommand, "INVALID", nil, true},
{"Valid", TestProfile, TestDeviceCommand, "GET", testProfile.DeviceCommands[0].Get, false},
{"Valid", TestProfile, TestDeviceCommand, "GET", testProfile.DeviceCommands[0].ResourceOperations, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -208,7 +209,7 @@ func Test_profileCache_ResourceOperation(t *testing.T) {
{"Invalid - nonexistent Profile name", "nil", TestDeviceResource, "GET", models.ResourceOperation{}, true},
{"Invalid - nonexistent DeviceResource name", TestProfile, "nil", "GET", models.ResourceOperation{}, true},
{"Invalid - invalid method", TestProfile, TestDeviceResource, "INVALID", models.ResourceOperation{}, true},
{"Valid", TestProfile, TestDeviceResource, "Get", testProfile.DeviceCommands[0].Get[0], false},
{"Valid", TestProfile, TestDeviceResource, "Get", testProfile.DeviceCommands[0].ResourceOperations[0], false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading