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: Add Client API to Support object value type in Set Command #676

Merged
merged 2 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions clients/http/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceNa
}
return res, nil
}

// IssueSetCommandByNameWithObject issues the specified write command and the settings supports object value type
func (client *CommandClient) IssueSetCommandByNameWithObject(ctx context.Context, deviceName string, commandName string, settings map[string]interface{}) (res dtoCommon.BaseResponse, err errors.EdgeX) {
requestPath := path.Join(common.ApiDeviceRoute, common.Name, url.QueryEscape(deviceName), url.QueryEscape(commandName))
err = utils.PutRequest(ctx, &res, client.baseUrl+requestPath, settings)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
18 changes: 18 additions & 0 deletions clients/http/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,21 @@ func TestIssueIssueSetCommandByName(t *testing.T) {
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
}

func TestIssueIssueSetCommandByNameWithObject(t *testing.T) {
deviceName := "Simple-Device01"
cmdName := "SwitchButton"
settings := map[string]interface{}{
"SwitchButton": map[string]interface{}{
"kind": "button",
"value": "on",
},
}
path := path.Join(common.ApiDeviceRoute, common.Name, deviceName, cmdName)
ts := newTestServer(http.MethodPut, path, dtoCommon.BaseResponse{})
defer ts.Close()
client := NewCommandClient(ts.URL)
res, err := client.IssueSetCommandByNameWithObject(context.Background(), deviceName, cmdName, settings)
require.NoError(t, err)
require.IsType(t, dtoCommon.BaseResponse{}, res)
}
11 changes: 11 additions & 0 deletions clients/http/deviceservicecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ func (client *deviceServiceCommandClient) SetCommand(ctx context.Context, baseUr
}
return response, nil
}

// SetCommandWithObject invokes device service's set command API and the settings supports object value type
func (client *deviceServiceCommandClient) SetCommandWithObject(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string, settings map[string]interface{}) (dtoCommon.BaseResponse, errors.EdgeX) {
var response dtoCommon.BaseResponse
requestPath := path.Join(common.ApiDeviceRoute, common.Name, url.QueryEscape(deviceName), url.QueryEscape(commandName))
err := utils.PutRequest(ctx, &response, baseUrl+requestPath+"?"+queryParams, settings)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
return response, nil
}
19 changes: 19 additions & 0 deletions clients/http/deviceservicecommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ func TestSetCommand(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, requestId, res.RequestId)
}

func TestSetCommandWithObject(t *testing.T) {
requestId := uuid.New().String()
expectedResponse := dtoCommon.NewBaseResponse(requestId, "", http.StatusOK)
ts := newTestServer(http.MethodPut, common.ApiDeviceRoute+"/"+common.Name+"/"+TestDeviceName+"/"+TestCommandName, expectedResponse)
defer ts.Close()
settings := map[string]interface{}{
"SwitchButton": map[string]interface{}{
"kind": "button",
"value": "on",
},
}

client := NewDeviceServiceCommandClient()
res, err := client.SetCommandWithObject(context.Background(), ts.URL, TestDeviceName, TestCommandName, "", settings)

require.NoError(t, err)
assert.Equal(t, requestId, res.RequestId)
}
2 changes: 2 additions & 0 deletions clients/interfaces/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ type CommandClient interface {
IssueGetCommandByName(ctx context.Context, deviceName string, commandName string, dsPushEvent string, dsReturnEvent string) (*responses.EventResponse, errors.EdgeX)
// IssueSetCommandByName issues the specified write command referenced by the command name to the device/sensor that is also referenced by name.
IssueSetCommandByName(ctx context.Context, deviceName string, commandName string, settings map[string]string) (common.BaseResponse, errors.EdgeX)
// IssueSetCommandByNameWithObject issues the specified write command and the settings supports object value type
IssueSetCommandByNameWithObject(ctx context.Context, deviceName string, commandName string, settings map[string]interface{}) (common.BaseResponse, errors.EdgeX)
}
2 changes: 2 additions & 0 deletions clients/interfaces/deviceservicecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type DeviceServiceCommandClient interface {
GetCommand(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string) (*responses.EventResponse, errors.EdgeX)
// SetCommand invokes device service's command API for issuing set(write) command
SetCommand(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string, settings map[string]string) (common.BaseResponse, errors.EdgeX)
// SetCommandWithObject invokes device service's set command API and the settings supports object value type
SetCommandWithObject(ctx context.Context, baseUrl string, deviceName string, commandName string, queryParams string, settings map[string]interface{}) (common.BaseResponse, errors.EdgeX)
}
25 changes: 24 additions & 1 deletion clients/interfaces/mocks/DeviceServiceCommandClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dtos/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func ValidateDeviceProfileDTO(profile DeviceProfile) error {
// deviceResources validation
dupCheck := make(map[string]bool)
for _, resource := range profile.DeviceResources {
if (resource.Properties.ValueType == common.ValueTypeBinary || resource.Properties.ValueType == common.ValueTypeObject) &&
if resource.Properties.ValueType == common.ValueTypeBinary &&
strings.Contains(resource.Properties.ReadWrite, common.ReadWrite_W) {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("write permission not support %s and %s value type for resource '%s'", common.ValueTypeBinary, common.ValueTypeObject, resource.Name), nil)
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("write permission not support %s value type for resource '%s'", common.ValueTypeBinary, resource.Name), nil)
}
// deviceResource name should not duplicated
if dupCheck[resource.Name] {
Expand Down
4 changes: 0 additions & 4 deletions dtos/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ func TestDeviceProfileDTOValidation(t *testing.T) {
binaryWithWritePermission := profileData()
binaryWithWritePermission.DeviceResources[0].Properties.ValueType = common.ValueTypeBinary
binaryWithWritePermission.DeviceResources[0].Properties.ReadWrite = common.ReadWrite_RW
objectWithWritePermission := profileData()
objectWithWritePermission.DeviceResources[0].Properties.ValueType = common.ValueTypeObject
objectWithWritePermission.DeviceResources[0].Properties.ReadWrite = common.ReadWrite_W

tests := []struct {
name string
Expand All @@ -112,7 +109,6 @@ func TestDeviceProfileDTOValidation(t *testing.T) {
{"mismatched resource", mismatchedResource, true},
{"invalid ReadWrite permission", invalidReadWrite, true},
{"write permission not support Binary value type", binaryWithWritePermission, true},
{"write permission not support Object value type", objectWithWritePermission, true},
}

for _, tt := range tests {
Expand Down