Skip to content

Commit

Permalink
feat: Add new Properties field to Device DTO and Model
Browse files Browse the repository at this point in the history
Add an extendable field into Device DTO and Model:
Properties map[string]any
This is useful for some devices that requires extra information. For example, a BACnet device may have properties such as DeviceInstance, Firmware, InstanceID, and ObjectName.

fixes #769

Signed-off-by: Jude Hung <[email protected]>
  • Loading branch information
judehung committed Feb 16, 2023
1 parent 57cbd60 commit 66fa623
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Device struct {
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"required,gt=0"`
Tags map[string]any `json:"tags,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
}

// UpdateDevice and its properties are defined in the APIv2 specification:
Expand All @@ -47,6 +48,7 @@ type UpdateDevice struct {
Protocols map[string]ProtocolProperties `json:"protocols" validate:"omitempty,gt=0"`
Notify *bool `json:"notify"`
Tags map[string]any `json:"tags"`
Properties map[string]any `json:"properties"`
}

// ToDeviceModel transforms the Device DTO to the Device Model
Expand All @@ -66,6 +68,7 @@ func ToDeviceModel(dto Device) models.Device {
d.AutoEvents = ToAutoEventModels(dto.AutoEvents)
d.Protocols = ToProtocolModels(dto.Protocols)
d.Tags = dto.Tags
d.Properties = dto.Properties
return d
}

Expand All @@ -87,6 +90,7 @@ func FromDeviceModelToDTO(d models.Device) Device {
dto.AutoEvents = FromAutoEventModelsToDTOs(d.AutoEvents)
dto.Protocols = FromProtocolModelsToDTOs(d.Protocols)
dto.Tags = d.Tags
dto.Properties = d.Properties
return dto
}

Expand All @@ -110,6 +114,7 @@ func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice {
Labels: d.Labels,
Notify: &d.Notify,
Tags: d.Tags,
Properties: d.Properties,
}
return dto
}
1 change: 1 addition & 0 deletions dtos/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ func TestFromDeviceModelToUpdateDTO(t *testing.T) {
assert.Equal(t, model.ProfileName, *dto.ProfileName)
assert.Equal(t, model.Location, dto.Location)
assert.Equal(t, model.Tags, dto.Tags)
assert.Equal(t, model.Properties, dto.Properties)
}
6 changes: 6 additions & 0 deletions dtos/requests/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ func ReplaceDeviceModelFieldsWithDTO(device *models.Device, patch dtos.UpdateDev
if patch.Notify != nil {
device.Notify = *patch.Notify
}
if patch.Tags != nil {
device.Tags = patch.Tags
}
if patch.Properties != nil {
device.Properties = patch.Properties
}
}

func NewAddDeviceRequest(dto dtos.Device) AddDeviceRequest {
Expand Down
2 changes: 2 additions & 0 deletions dtos/requests/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ func TestUpdateDeviceRequest_UnmarshalJSON_NilField(t *testing.T) {
assert.Nil(t, req.Device.AutoEvents)
assert.Nil(t, req.Device.Protocols)
assert.Nil(t, req.Device.Notify)
assert.Nil(t, req.Device.Tags)
assert.Nil(t, req.Device.Properties)
}

func TestUpdateDeviceRequest_UnmarshalJSON_EmptySlice(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions models/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Device struct {
AutoEvents []AutoEvent
Notify bool
Tags map[string]any
Properties map[string]any
}

// ProtocolProperties contains the device connection information in key/value pair
Expand Down

0 comments on commit 66fa623

Please sign in to comment.