dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/device.go (about) 1 // 2 // Copyright (C) 2020-2023 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package requests 7 8 import ( 9 "encoding/json" 10 11 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 12 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos" 13 dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common" 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 15 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 16 ) 17 18 // AddDeviceRequest defines the Request Content for POST Device DTO. 19 type AddDeviceRequest struct { 20 dtoCommon.BaseRequest `json:",inline"` 21 Device dtos.Device `json:"device"` 22 } 23 24 // Validate satisfies the Validator interface 25 func (d AddDeviceRequest) Validate() error { 26 err := common.Validate(d) 27 return err 28 } 29 30 // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceRequest type 31 func (d *AddDeviceRequest) UnmarshalJSON(b []byte) error { 32 var alias struct { 33 dtoCommon.BaseRequest 34 Device dtos.Device 35 } 36 if err := json.Unmarshal(b, &alias); err != nil { 37 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 38 } 39 40 *d = AddDeviceRequest(alias) 41 42 // validate AddDeviceRequest DTO 43 if err := d.Validate(); err != nil { 44 return err 45 } 46 return nil 47 } 48 49 // AddDeviceReqToDeviceModels transforms the AddDeviceRequest DTO array to the Device model array 50 func AddDeviceReqToDeviceModels(addRequests []AddDeviceRequest) (Devices []models.Device) { 51 for _, req := range addRequests { 52 d := dtos.ToDeviceModel(req.Device) 53 Devices = append(Devices, d) 54 } 55 return Devices 56 } 57 58 // UpdateDeviceRequest defines the Request Content for PUT event as pushed DTO. 59 type UpdateDeviceRequest struct { 60 dtoCommon.BaseRequest `json:",inline"` 61 Device dtos.UpdateDevice `json:"device"` 62 } 63 64 // Validate satisfies the Validator interface 65 func (d UpdateDeviceRequest) Validate() error { 66 err := common.Validate(d) 67 return err 68 } 69 70 // UnmarshalJSON implements the Unmarshaler interface for the UpdateDeviceRequest type 71 func (d *UpdateDeviceRequest) UnmarshalJSON(b []byte) error { 72 var alias struct { 73 dtoCommon.BaseRequest 74 Device dtos.UpdateDevice 75 } 76 if err := json.Unmarshal(b, &alias); err != nil { 77 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 78 } 79 80 *d = UpdateDeviceRequest(alias) 81 82 // validate UpdateDeviceRequest DTO 83 if err := d.Validate(); err != nil { 84 return err 85 } 86 return nil 87 } 88 89 // ReplaceDeviceModelFieldsWithDTO replace existing Device's fields with DTO patch 90 func ReplaceDeviceModelFieldsWithDTO(device *models.Device, patch dtos.UpdateDevice) { 91 if patch.Description != nil { 92 device.Description = *patch.Description 93 } 94 if patch.AdminState != nil { 95 device.AdminState = models.AdminState(*patch.AdminState) 96 } 97 if patch.OperatingState != nil { 98 device.OperatingState = models.OperatingState(*patch.OperatingState) 99 } 100 if patch.ServiceName != nil { 101 device.ServiceName = *patch.ServiceName 102 } 103 if patch.Serial != nil { 104 device.Serial = *patch.Serial 105 } 106 if patch.ProfileName != nil { 107 device.ProfileName = *patch.ProfileName 108 } 109 if patch.Labels != nil { 110 device.Labels = patch.Labels 111 } 112 if patch.Location != nil { 113 device.Location = patch.Location 114 } 115 if patch.AutoEvents != nil { 116 device.AutoEvents = dtos.ToAutoEventModels(patch.AutoEvents) // Fix: Convert to []models.AutoEvent 117 } 118 if patch.Protocols != nil { 119 device.Protocols = dtos.ToProtocolModels(patch.Protocols) 120 } 121 if patch.Manufacturer != nil { 122 device.Manufacturer = *patch.Manufacturer 123 } 124 if patch.Model != nil { 125 device.Model = *patch.Model 126 } 127 128 if patch.Tags != nil { 129 device.Tags = patch.Tags 130 } 131 if patch.Properties != nil { 132 device.Properties = patch.Properties 133 } 134 } 135 136 func NewAddDeviceRequest(dto dtos.Device) AddDeviceRequest { 137 return AddDeviceRequest{ 138 BaseRequest: dtoCommon.NewBaseRequest(), 139 Device: dto, 140 } 141 } 142 143 func NewUpdateDeviceRequest(dto dtos.UpdateDevice) UpdateDeviceRequest { 144 return UpdateDeviceRequest{ 145 BaseRequest: dtoCommon.NewBaseRequest(), 146 Device: dto, 147 } 148 }