Skip to content

Commit

Permalink
PS-579 Update device Properties field
Browse files Browse the repository at this point in the history
To allow updating the device Properties, modify Properties field on the DTO and model

Signed-off-by: bruce <weichou1229@gmail.com>
  • Loading branch information
weichou1229 committed Dec 11, 2023
1 parent bac57c8 commit d00fab0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Device struct {
AutoEvents []AutoEvent `json:"autoEvents,omitempty" validate:"dive"`
ProtocolName string `json:"protocolName,omitempty"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"required,gt=0"`
Properties map[string]interface{} `json:"properties"`
Properties map[string]any `json:"properties,omitempty"`
}

// UpdateDevice and its properties are defined in the APIv2 specification:
Expand All @@ -51,8 +51,9 @@ type UpdateDevice struct {
AutoEvents []AutoEvent `json:"autoEvents" validate:"dive"`
// we don't allow this to be updated
//ProtocolName *string `json:"protocolName" validate:"omitempty"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"omitempty,gt=0"`
Notify *bool `json:"notify"`
Protocols map[string]ProtocolProperties `json:"protocols" validate:"omitempty,gt=0"`
Properties map[string]any `json:"properties"`
Notify *bool `json:"notify"`
}

// ToDeviceModel transforms the Device DTO to the Device Model
Expand Down Expand Up @@ -118,6 +119,7 @@ func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice {
Tags: d.Tags,
AutoEvents: FromAutoEventModelsToDTOs(d.AutoEvents),
Protocols: FromProtocolModelsToDTOs(d.Protocols),
Properties: d.Properties,
Labels: d.Labels,
Notify: &d.Notify,
}
Expand Down
3 changes: 3 additions & 0 deletions dtos/requests/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func ReplaceDeviceModelFieldsWithDTO(device *models.Device, patch dtos.UpdateDev
if patch.Protocols != nil {
device.Protocols = dtos.ToProtocolModels(patch.Protocols)
}
if patch.Properties != nil {
device.Properties = patch.Properties
}
if patch.Notify != nil {
device.Notify = *patch.Notify
}
Expand Down
5 changes: 5 additions & 0 deletions dtos/requests/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var testProtocols = map[string]dtos.ProtocolProperties{
"UnitID": "1",
},
}
var testProperties = map[string]any{
"LastScan": float64(1702275547),
}
var testAddDevice = AddDeviceRequest{
BaseRequest: dtoCommon.BaseRequest{
RequestId: ExampleUUID,
Expand Down Expand Up @@ -88,6 +91,7 @@ func mockUpdateDevice() dtos.UpdateDevice {
d.Tags = map[string]interface{}{"1": TestTag1, "2": TestTag2}
d.AutoEvents = testAutoEvents
d.Protocols = testProtocols
d.Properties = testProperties
return d
}

Expand Down Expand Up @@ -434,6 +438,7 @@ func TestReplaceDeviceModelFieldsWithDTO(t *testing.T) {
assert.Equal(t, map[string]interface{}{"1": TestTag1, "2": TestTag2}, device.Tags)
assert.Equal(t, dtos.ToAutoEventModels(testAutoEvents), device.AutoEvents)
assert.Equal(t, dtos.ToProtocolModels(testProtocols), device.Protocols)
assert.Equal(t, testProperties, device.Properties)
}

func TestNewAddDeviceRequest(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion models/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Device struct {
// want as much info as we can find about the device (so for example in a big system they have a better idea of what
// actual device is being provisioned). So we added a properties field to carry this information. IMHO this is a valid
// generic requirement to support discovery.
Properties map[string]interface{}
Properties map[string]any
}

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

0 comments on commit d00fab0

Please sign in to comment.