dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/device_history.go (about) 1 // 2 // Copyright (C) 2020-2021 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 // AddDeviceHistoryRequest defines the Request Content for POST Device DTO. 19 20 type AddDeviceHistoryRequest struct { 21 dtoCommon.BaseRequest `json:",inline"` 22 DeviceHistory dtos.DeviceHistory `json:"deviceHistory"` 23 } 24 25 // Validate satisfies the Validator interface 26 func (d AddDeviceHistoryRequest) Validate() error { 27 err := common.Validate(d) 28 return err 29 } 30 31 // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceHistoryRequest type 32 func (d *AddDeviceHistoryRequest) UnmarshalJSON(b []byte) error { 33 var alias struct { 34 dtoCommon.BaseRequest 35 DeviceHistory dtos.DeviceHistory 36 } 37 if err := json.Unmarshal(b, &alias); err != nil { 38 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 39 } 40 41 *d = AddDeviceHistoryRequest(alias) 42 43 // validate AddDeviceHistoryRequest DTO 44 if err := d.Validate(); err != nil { 45 return err 46 } 47 return nil 48 } 49 50 // AddDeviceReqToDeviceHistoryModels transforms the AddDeviceHistoryRequest DTO array to the Device model array 51 func AddDeviceHistoryReqToDeviceHistoryModels(addRequests []AddDeviceHistoryRequest) (DevicesHistory []models.DeviceHistory) { 52 for _, req := range addRequests { 53 d := dtos.ToDeviceHistoryModel(req.DeviceHistory) 54 DevicesHistory = append(DevicesHistory, d) 55 } 56 return DevicesHistory 57 } 58 59 // UpdateDeviceHistoryRequest defines the Request Content for PUT event as pushed DTO. 60 // This object and its properties correspond to the UpdateDeviceHistoryRequest object in the APIv2 specification: 61 // https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.1.0#/UpdateDeviceHistoryRequest 62 type UpdateDeviceHistoryRequest struct { 63 dtoCommon.BaseRequest `json:",inline"` 64 DeviceHistory dtos.UpdateDeviceHistory `json:"deviceHistory"` 65 } 66 67 // Validate satisfies the Validator interface 68 func (d UpdateDeviceHistoryRequest) Validate() error { 69 err := common.Validate(d) 70 return err 71 } 72 73 // UnmarshalJSON implements the Unmarshaler interface for the UpdateDeviceHistoryRequest type 74 func (d *UpdateDeviceHistoryRequest) UnmarshalJSON(b []byte) error { 75 var alias struct { 76 dtoCommon.BaseRequest 77 DeviceHistory dtos.UpdateDeviceHistory 78 } 79 if err := json.Unmarshal(b, &alias); err != nil { 80 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 81 } 82 83 *d = UpdateDeviceHistoryRequest(alias) 84 85 // validate UpdateDeviceHistoryRequest DTO 86 if err := d.Validate(); err != nil { 87 return err 88 } 89 return nil 90 } 91 92 // TODO: add missing patch fields (Tags) 93 // ReplaceDeviceModelFieldsWithDTO replace existing Device's fields with DTO patch 94 func ReplaceDeviceHistoryModelFieldsWithDTO(deviceHistory *models.DeviceHistory, patch dtos.UpdateDeviceHistory) { 95 if patch.Description != nil { 96 deviceHistory.Description = *patch.Description 97 } 98 if patch.AdminState != nil { 99 deviceHistory.AdminState = models.AdminState(*patch.AdminState) 100 } 101 if patch.OperatingState != nil { 102 deviceHistory.OperatingState = models.OperatingState(*patch.OperatingState) 103 } 104 105 if patch.ServiceName != nil { 106 deviceHistory.ServiceName = *patch.ServiceName 107 } 108 if patch.ProfileName != nil { 109 deviceHistory.ProfileName = *patch.ProfileName 110 } 111 if patch.Labels != nil { 112 deviceHistory.Labels = patch.Labels 113 } 114 if patch.Location != nil { 115 deviceHistory.Location = patch.Location 116 } 117 if patch.AutoEvents != nil { 118 deviceHistory.AutoEvents = dtos.ToAutoEventModels(patch.AutoEvents) 119 } 120 if patch.Protocols != nil { 121 deviceHistory.Protocols = dtos.ToProtocolModels(patch.Protocols) 122 } 123 if patch.HistoryNotes != nil { 124 deviceHistory.HistoryNotes = *patch.HistoryNotes 125 } 126 if patch.HistoryUser != nil { 127 deviceHistory.HistoryUser = *patch.HistoryUser 128 } 129 if patch.HistoryType != nil { 130 deviceHistory.HistoryType = *patch.HistoryType 131 } 132 } 133 134 func NewAddDeviceHistoryRequest(dto dtos.DeviceHistory) AddDeviceHistoryRequest { 135 return AddDeviceHistoryRequest{ 136 BaseRequest: dtoCommon.NewBaseRequest(), 137 DeviceHistory: dto, 138 } 139 } 140 141 func NewUpdateDeviceHistoryRequest(dto dtos.UpdateDeviceHistory) UpdateDeviceHistoryRequest { 142 return UpdateDeviceHistoryRequest{ 143 BaseRequest: dtoCommon.NewBaseRequest(), 144 DeviceHistory: dto, 145 } 146 }