Skip to content

Commit

Permalink
Merge pull request #926 from jinlinGuan/sdk-issue-1612
Browse files Browse the repository at this point in the history
feat: Add new discovery/profilescan APIs to DeviceServiceCommandClient
  • Loading branch information
cloudxxx8 authored Sep 2, 2024
2 parents f3e80ae + 9ff41d2 commit f224af7
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 22 deletions.
44 changes: 43 additions & 1 deletion clients/http/deviceservicecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,52 @@ func (client *deviceServiceCommandClient) SetCommandWithObject(ctx context.Conte
return response, nil
}

func (client *deviceServiceCommandClient) Discovery(ctx context.Context, baseUrl string) (dtoCommon.BaseResponse, errors.EdgeX) {
var response dtoCommon.BaseResponse
err := utils.PostRequest(ctx, &response, baseUrl, common.ApiDiscoveryRoute, nil, "", client.authInjector)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
return response, nil
}

// ProfileScan sends an HTTP POST request to the device service's profile scan API endpoint.
func (client *deviceServiceCommandClient) ProfileScan(ctx context.Context, baseUrl string, req requests.ProfileScanRequest) (dtoCommon.BaseResponse, errors.EdgeX) {
var response dtoCommon.BaseResponse
err := utils.PostRequestWithRawData(ctx, &response, baseUrl, common.ApiProfileScan, nil, req, client.authInjector)
err := utils.PostRequestWithRawData(ctx, &response, baseUrl, common.ApiProfileScanRoute, nil, req, client.authInjector)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
return response, nil
}

func (client *deviceServiceCommandClient) StopDeviceDiscovery(ctx context.Context, baseUrl string, requestId string, queryParams map[string]string) (dtoCommon.BaseResponse, errors.EdgeX) {
requestPath := common.ApiDiscoveryRoute
if len(requestId) != 0 {
requestPath = common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiDiscoveryRoute).SetPath(common.RequestId).SetNameFieldPath(requestId).BuildPath()
}
response := dtoCommon.BaseResponse{}
params := url.Values{}
for k, v := range queryParams {
params.Set(k, v)
}
err := utils.DeleteRequestWithParams(ctx, &response, baseUrl, requestPath, params, client.authInjector)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
return response, nil
}

func (client *deviceServiceCommandClient) StopProfileScan(ctx context.Context, baseUrl string, deviceName string, queryParams map[string]string) (dtoCommon.BaseResponse, errors.EdgeX) {
requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape).
SetPath(common.ApiProfileScanRoute).SetPath(common.Device).SetPath(common.Name).SetNameFieldPath(deviceName).BuildPath()
response := dtoCommon.BaseResponse{}
params := url.Values{}
for k, v := range queryParams {
params.Set(k, v)
}
err := utils.DeleteRequestWithParams(ctx, &response, baseUrl, requestPath, params, client.authInjector)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
41 changes: 40 additions & 1 deletion clients/http/deviceservicecommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package http
import (
"context"
"net/http"
"path"
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
Expand Down Expand Up @@ -91,13 +92,51 @@ func TestSetCommandWithObject(t *testing.T) {
func TestProfileScan(t *testing.T) {
requestId := uuid.New().String()
expectedResponse := dtoCommon.NewBaseResponse(requestId, "", http.StatusAccepted)
ts := newTestServer(http.MethodPost, common.ApiProfileScan, expectedResponse)
ts := newTestServer(http.MethodPost, common.ApiProfileScanRoute, expectedResponse)
defer ts.Close()

client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
res, err := client.ProfileScan(context.Background(), ts.URL, requests.ProfileScanRequest{})

require.NoError(t, err)
assert.Equal(t, requestId, res.RequestId)
}

func TestStopDeviceDiscovery(t *testing.T) {
id := uuid.New().String()
requestRoute := path.Join(common.ApiDiscoveryRoute, common.RequestId, id)

tests := []struct {
name string
requestId string
route string
}{
{"stop device discovery", "", common.ApiDiscoveryRoute},
{"stop device discovery with request id", id, requestRoute},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
ts := newTestServer(http.MethodDelete, testCase.route, dtoCommon.BaseResponse{})
//defer ts.Close()

client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
res, err := client.StopDeviceDiscovery(context.Background(), ts.URL, testCase.requestId, nil)

require.NoError(t, err)
assert.IsType(t, dtoCommon.BaseResponse{}, res)
ts.Close()
})
}
}

func TestStopProfileScan(t *testing.T) {
route := path.Join(common.ApiProfileScanRoute, common.Device, common.Name, TestDeviceName)
ts := newTestServer(http.MethodDelete, route, dtoCommon.BaseResponse{})
defer ts.Close()

client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
res, err := client.StopProfileScan(context.Background(), ts.URL, TestDeviceName, nil)

require.NoError(t, err)
assert.IsType(t, dtoCommon.BaseResponse{}, res)
}
6 changes: 6 additions & 0 deletions clients/interfaces/deviceservicecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type DeviceServiceCommandClient interface {
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)
// Discovery invokes device service's discovery API
Discovery(ctx context.Context, baseUrl string) (common.BaseResponse, errors.EdgeX)
// ProfileScan sends an HTTP POST request to the device service's profile scan API endpoint.
ProfileScan(ctx context.Context, baseUrl string, req requests.ProfileScanRequest) (common.BaseResponse, errors.EdgeX)
// StopDeviceDiscovery invokes device service's stop device discovery API
StopDeviceDiscovery(ctx context.Context, baseUrl string, requestId string, queryParams map[string]string) (common.BaseResponse, errors.EdgeX)
// StopProfileScan invokes device service's stop profile scan API
StopProfileScan(ctx context.Context, baseUrl string, deviceName string, queryParams map[string]string) (common.BaseResponse, errors.EdgeX)
}
92 changes: 91 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.

25 changes: 15 additions & 10 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,18 @@ const (
ApiSecretRoute = ApiBase + "/secret"
ApiUnitsOfMeasureRoute = ApiBase + "/uom"

ApiDeviceCallbackRoute = ApiBase + "/callback/device"
ApiDeviceCallbackNameRoute = ApiBase + "/callback/device/name/{name}"
ApiProfileCallbackRoute = ApiBase + "/callback/profile"
ApiProfileCallbackNameRoute = ApiBase + "/callback/profile/name/{name}"
ApiWatcherCallbackRoute = ApiBase + "/callback/watcher"
ApiWatcherCallbackNameRoute = ApiBase + "/callback/watcher/name/{name}"
ApiServiceCallbackRoute = ApiBase + "/callback/service"
ApiDiscoveryRoute = ApiBase + "/discovery"
ApiDeviceValidationRoute = ApiBase + "/validate/device"
ApiProfileScan = ApiBase + "/profilescan"
ApiDeviceCallbackRoute = ApiBase + "/callback/device"
ApiDeviceCallbackNameRoute = ApiBase + "/callback/device/name/{name}"
ApiProfileCallbackRoute = ApiBase + "/callback/profile"
ApiProfileCallbackNameRoute = ApiBase + "/callback/profile/name/{name}"
ApiWatcherCallbackRoute = ApiBase + "/callback/watcher"
ApiWatcherCallbackNameRoute = ApiBase + "/callback/watcher/name/{name}"
ApiServiceCallbackRoute = ApiBase + "/callback/service"
ApiDiscoveryRoute = ApiBase + "/discovery"
ApiDeviceValidationRoute = ApiBase + "/validate/device"
ApiProfileScanRoute = ApiBase + "/profilescan"
ApiDiscoveryByIdRoute = ApiDiscoveryRoute + "/" + RequestId + "/{" + RequestId + "}"
ApiProfileScanByDeviceNameRoute = ApiProfileScanRoute + "/" + Device + "/" + Name + "/{" + Name + "}"

ApiIntervalRoute = ApiBase + "/interval"
ApiAllIntervalRoute = ApiIntervalRoute + "/" + All
Expand Down Expand Up @@ -168,6 +170,7 @@ const (
Check = "check"
Profile = "profile"
Resource = "resource"
RequestId = "requestId"
Service = "service"
Services = "services"
Command = "command"
Expand Down Expand Up @@ -350,6 +353,8 @@ const (
SystemEventActionAdd = "add"
SystemEventActionUpdate = "update"
SystemEventActionDelete = "delete"
SystemEventActionDiscovery = "discovery"
SystemEventActionProfileScan = "profilescan"
)

const (
Expand Down
20 changes: 11 additions & 9 deletions common/echo_api_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ const (
ApiDeviceServiceByNameEchoRoute = ApiDeviceServiceRoute + "/" + Name + "/:" + Name
ApiDeviceServiceByIdEchoRoute = ApiDeviceServiceRoute + "/" + Id + "/:" + Id

ApiDeviceIdExistsEchoRoute = ApiDeviceRoute + "/" + Check + "/" + Id + "/:" + Id
ApiDeviceNameExistsEchoRoute = ApiDeviceRoute + "/" + Check + "/" + Name + "/:" + Name
ApiDeviceByIdEchoRoute = ApiDeviceRoute + "/" + Id + "/:" + Id
ApiDeviceByNameEchoRoute = ApiDeviceRoute + "/" + Name + "/:" + Name
ApiDeviceByProfileIdEchoRoute = ApiDeviceRoute + "/" + Profile + "/" + Id + "/:" + Id
ApiDeviceByProfileNameEchoRoute = ApiDeviceRoute + "/" + Profile + "/" + Name + "/:" + Name
ApiDeviceByServiceIdEchoRoute = ApiDeviceRoute + "/" + Service + "/" + Id + "/:" + Id
ApiDeviceByServiceNameEchoRoute = ApiDeviceRoute + "/" + Service + "/" + Name + "/:" + Name
ApiDeviceNameCommandNameEchoRoute = ApiDeviceByNameEchoRoute + "/:" + Command
ApiDeviceIdExistsEchoRoute = ApiDeviceRoute + "/" + Check + "/" + Id + "/:" + Id
ApiDeviceNameExistsEchoRoute = ApiDeviceRoute + "/" + Check + "/" + Name + "/:" + Name
ApiDeviceByIdEchoRoute = ApiDeviceRoute + "/" + Id + "/:" + Id
ApiDeviceByNameEchoRoute = ApiDeviceRoute + "/" + Name + "/:" + Name
ApiDeviceByProfileIdEchoRoute = ApiDeviceRoute + "/" + Profile + "/" + Id + "/:" + Id
ApiDeviceByProfileNameEchoRoute = ApiDeviceRoute + "/" + Profile + "/" + Name + "/:" + Name
ApiDeviceByServiceIdEchoRoute = ApiDeviceRoute + "/" + Service + "/" + Id + "/:" + Id
ApiDeviceByServiceNameEchoRoute = ApiDeviceRoute + "/" + Service + "/" + Name + "/:" + Name
ApiDeviceNameCommandNameEchoRoute = ApiDeviceByNameEchoRoute + "/:" + Command
ApiDiscoveryByIdEchoRoute = ApiDiscoveryRoute + "/" + RequestId + "/:" + RequestId
ApiProfileScanByDeviceNameEchoRoute = ApiProfileScanRoute + "/" + Device + "/" + Name + "/:" + Name

ApiProvisionWatcherByIdEchoRoute = ApiProvisionWatcherRoute + "/" + Id + "/:" + Id
ApiProvisionWatcherByNameEchoRoute = ApiProvisionWatcherRoute + "/" + Name + "/:" + Name
Expand Down

0 comments on commit f224af7

Please sign in to comment.