dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/device.go (about) 1 // 2 // Copyright (C) 2020-2023 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package dtos 7 8 import ( 9 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 10 ) 11 12 type Device struct { 13 DBTimestamp `json:",inline"` 14 Id string `json:"id,omitempty" yaml:"id,omitempty" validate:"omitempty,uuid"` 15 Name string `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string"` 16 Description string `json:"description,omitempty" yaml:"description,omitempty"` 17 AdminState string `json:"adminState" yaml:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"` 18 OperatingState string `json:"operatingState" yaml:"operatingState" validate:"oneof='UP' 'DOWN' 'UNKNOWN'"` 19 Labels []string `json:"labels,omitempty" yaml:"labels,omitempty"` 20 Location interface{} `json:"location,omitempty" yaml:"location,omitempty"` 21 ServiceName string `json:"serviceName" yaml:"serviceName" validate:"required,edgex-dto-none-empty-string"` 22 ProfileName string `json:"profileName" yaml:"profileName" validate:"required,edgex-dto-none-empty-string"` 23 AutoEvents []AutoEvent `json:"autoEvents,omitempty" yaml:"autoEvents,omitempty" validate:"dive"` 24 Protocols map[string]ProtocolProperties `json:"protocols" yaml:"protocols" validate:"required,gt=0"` 25 Tags map[string]any `json:"tags,omitempty" yaml:"tags,omitempty"` 26 Properties map[string]any `json:"properties,omitempty" yaml:"properties,omitempty"` 27 Manufacturer string `json:"manufacturer,omitempty"` 28 Model string `json:"model,omitempty"` 29 Serial string `json:"serial,omitempty"` 30 } 31 32 type UpdateDevice struct { 33 Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"` 34 Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string"` 35 Description *string `json:"description" validate:"omitempty"` 36 AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"` 37 OperatingState *string `json:"operatingState" validate:"omitempty,oneof='UP' 'DOWN' 'UNKNOWN'"` 38 ServiceName *string `json:"serviceName" validate:"omitempty,edgex-dto-none-empty-string"` 39 ProfileName *string `json:"profileName" validate:"omitempty,edgex-dto-none-empty-string"` 40 Labels []string `json:"labels"` 41 Location interface{} `json:"location"` 42 AutoEvents []AutoEvent `json:"autoEvents" validate:"dive"` 43 Protocols map[string]ProtocolProperties `json:"protocols" validate:"omitempty,gt=0"` 44 Tags map[string]any `json:"tags"` 45 Properties map[string]any `json:"properties"` 46 Manufacturer *string `json:"manufacturer"` 47 Model *string `json:"model"` 48 Serial *string `json:"serial"` 49 } 50 51 // ToDeviceModel transforms the Device DTO to the Device Model 52 func ToDeviceModel(dto Device) models.Device { 53 var d models.Device 54 d.Id = dto.Id 55 d.Name = dto.Name 56 d.Description = dto.Description 57 d.ServiceName = dto.ServiceName 58 d.Serial = dto.Serial 59 d.ProfileName = dto.ProfileName 60 d.AdminState = models.AdminState(dto.AdminState) 61 d.OperatingState = models.OperatingState(dto.OperatingState) 62 d.Labels = dto.Labels 63 d.Location = dto.Location 64 d.AutoEvents = ToAutoEventModels(dto.AutoEvents) 65 d.Protocols = ToProtocolModels(dto.Protocols) 66 d.Manufacturer = dto.Manufacturer 67 d.Model = dto.Model 68 d.Tags = dto.Tags 69 d.Properties = dto.Properties 70 return d 71 } 72 73 // FromDeviceModelToDTO transforms the Device Model to the Device DTO 74 func FromDeviceModelToDTO(d models.Device) Device { 75 var dto Device 76 dto.DBTimestamp = DBTimestamp(d.DBTimestamp) 77 dto.Id = d.Id 78 dto.Name = d.Name 79 dto.Description = d.Description 80 dto.ServiceName = d.ServiceName 81 dto.Serial = d.Serial 82 dto.ProfileName = d.ProfileName 83 dto.AdminState = string(d.AdminState) 84 dto.OperatingState = string(d.OperatingState) 85 dto.Labels = d.Labels 86 dto.Location = d.Location 87 dto.AutoEvents = FromAutoEventModelsToDTOs(d.AutoEvents) 88 dto.Protocols = FromProtocolModelsToDTOs(d.Protocols) 89 dto.Manufacturer = d.Manufacturer 90 dto.Model = d.Model 91 dto.Tags = d.Tags 92 dto.Properties = d.Properties 93 return dto 94 } 95 96 // FromDeviceModelToUpdateDTO transforms the Device Model to the UpdateDevice DTO 97 func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice { 98 adminState := string(d.AdminState) 99 operatingState := string(d.OperatingState) 100 dto := UpdateDevice{ 101 Id: &d.Id, 102 Name: &d.Name, 103 Description: &d.Description, 104 AdminState: &adminState, 105 OperatingState: &operatingState, 106 ServiceName: &d.ServiceName, 107 Serial: &d.Serial, 108 ProfileName: &d.ProfileName, 109 Location: d.Location, 110 AutoEvents: FromAutoEventModelsToDTOs(d.AutoEvents), 111 Protocols: FromProtocolModelsToDTOs(d.Protocols), 112 Labels: d.Labels, 113 Manufacturer: &d.Manufacturer, 114 Model: &d.Model, 115 Tags: d.Tags, 116 Properties: d.Properties, 117 } 118 return dto 119 }