dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/command.go (about) 1 // 2 // Copyright (C) 2021-2023 IOTech Ltd 3 // Copyright (C) 2023 Intel Corporation 4 // 5 // SPDX-License-Identifier: Apache-2.0 6 7 package http 8 9 import ( 10 "context" 11 "net/url" 12 "strconv" 13 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/http/utils" 15 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/clients/interfaces" 16 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 17 dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common" 18 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses" 19 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 20 ) 21 22 type CommandClient struct { 23 baseUrl string 24 authInjector interfaces.AuthenticationInjector 25 enableNameFieldEscape bool 26 } 27 28 // NewCommandClient creates an instance of CommandClient 29 func NewCommandClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.CommandClient { 30 return &CommandClient{ 31 baseUrl: baseUrl, 32 authInjector: authInjector, 33 enableNameFieldEscape: enableNameFieldEscape, 34 } 35 } 36 37 // AllDeviceCoreCommands returns a paginated list of MultiDeviceCoreCommandsResponse. The list contains all of the commands in the system associated with their respective device. 38 func (client *CommandClient) AllDeviceCoreCommands(ctx context.Context, offset int, limit int) ( 39 res responses.MultiDeviceCoreCommandsResponse, err errors.EdgeX) { 40 requestParams := url.Values{} 41 requestParams.Set(common.Offset, strconv.Itoa(offset)) 42 requestParams.Set(common.Limit, strconv.Itoa(limit)) 43 err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllDeviceRoute, requestParams, client.authInjector) 44 if err != nil { 45 return res, errors.NewCommonEdgeXWrapper(err) 46 } 47 return res, nil 48 } 49 50 // DeviceCoreCommandsByDeviceName returns all commands associated with the specified device name. 51 func (client *CommandClient) DeviceCoreCommandsByDeviceName(ctx context.Context, name string) ( 52 res responses.DeviceCoreCommandResponse, err errors.EdgeX) { 53 path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 54 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() 55 err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector) 56 if err != nil { 57 return res, errors.NewCommonEdgeXWrapper(err) 58 } 59 return res, nil 60 } 61 62 // IssueGetCommandByName issues the specified read command referenced by the command name to the device/sensor that is also referenced by name. 63 func (client *CommandClient) IssueGetCommandByName(ctx context.Context, deviceName string, commandName string, dsPushEvent bool, dsReturnEvent bool) (res *responses.EventResponse, err errors.EdgeX) { 64 requestParams := url.Values{} 65 requestParams.Set(common.PushEvent, strconv.FormatBool(dsPushEvent)) 66 requestParams.Set(common.ReturnEvent, strconv.FormatBool(dsReturnEvent)) 67 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 68 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 69 err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) 70 if err != nil { 71 return res, errors.NewCommonEdgeXWrapper(err) 72 } 73 return res, nil 74 } 75 76 func (client *CommandClient) IssueGetCommandByNameWithQueryParams(ctx context.Context, deviceName string, commandName string, queryParams map[string]string) (res *responses.EventResponse, err errors.EdgeX) { 77 requestParams := url.Values{} 78 for k, v := range queryParams { 79 requestParams.Set(k, v) 80 } 81 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 82 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 83 err = utils.GetRequest(ctx, &res, client.baseUrl, requestPath, requestParams, client.authInjector) 84 if err != nil { 85 return res, errors.NewCommonEdgeXWrapper(err) 86 } 87 return res, nil 88 } 89 90 // IssueSetCommandByName issues the specified write command referenced by the command name to the device/sensor that is also referenced by name. 91 func (client *CommandClient) IssueSetCommandByName(ctx context.Context, deviceName string, commandName string, settings map[string]string) (res dtoCommon.BaseResponse, err errors.EdgeX) { 92 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 93 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 94 err = utils.PutRequest(ctx, &res, client.baseUrl, requestPath, nil, settings, client.authInjector) 95 if err != nil { 96 return res, errors.NewCommonEdgeXWrapper(err) 97 } 98 return res, nil 99 } 100 101 // IssueSetCommandByNameWithObject issues the specified write command and the settings supports object value type 102 func (client *CommandClient) IssueSetCommandByNameWithObject(ctx context.Context, deviceName string, commandName string, settings map[string]interface{}) (res dtoCommon.BaseResponse, err errors.EdgeX) { 103 requestPath := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). 104 SetPath(common.ApiDeviceRoute).SetPath(common.Name).SetNameFieldPath(deviceName).SetNameFieldPath(commandName).BuildPath() 105 err = utils.PutRequest(ctx, &res, client.baseUrl, requestPath, nil, settings, client.authInjector) 106 if err != nil { 107 return res, errors.NewCommonEdgeXWrapper(err) 108 } 109 return res, nil 110 }