Skip to content

Commit

Permalink
Merge pull request kubeedge#5747 from JiaweiGithub/feat/fix_devicestatus
Browse files Browse the repository at this point in the history
fix edge device state sync error
  • Loading branch information
kubeedge-bot authored Jul 24, 2024
2 parents a575dd7 + b61619b commit bb58552
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion edge/pkg/devicetwin/dtmanager/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func dealDeviceStateUpdate(context *dtcontext.DTContext, resource string, msg in
}
device.State = updatedDevice.State
device.LastOnline = lastOnline
payload, err := dttype.BuildDeviceState(dttype.BuildBaseMessage(), *device)
payload, err := dttype.BuildDeviceCloudMsgState(dttype.BuildBaseMessage(), *device)
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions edge/pkg/devicetwin/dttype/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ type Device struct {
Twin map[string]*MsgTwin `json:"twin,omitempty"`
}

// DeviceCloudMsg used to synchronize device data to the cloud
type DeviceCloudMsg struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
State string `json:"state,omitempty"`
LastOnlineTime string `json:"lastOnlineTime,omitempty"`
Attributes map[string]*MsgAttr `json:"attributes,omitempty"`
Twin map[string]*MsgTwin `json:"twin,omitempty"`
}

// BaseMessage the base struct of event message
type BaseMessage struct {
EventID string `json:"event_id"`
Expand Down
14 changes: 7 additions & 7 deletions edge/pkg/devicetwin/dttype/types_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ func MsgTwinToDeviceTwin(name string, msgTwin *MsgTwin) dtclient.DeviceTwin {
// DeviceMsg the struct of device state msg
type DeviceMsg struct {
BaseMessage
Device Device `json:"device"`
DeviceCloudMsg DeviceCloudMsg `json:"device"`
}

// BuildDeviceState build the msg
func BuildDeviceState(baseMessage BaseMessage, device Device) ([]byte, error) {
// BuildDeviceCloudMsgState build the msg
func BuildDeviceCloudMsgState(baseMessage BaseMessage, device Device) ([]byte, error) {
result := DeviceMsg{
BaseMessage: baseMessage,
Device: Device{
Name: device.Name,
State: device.State,
LastOnline: device.LastOnline}}
DeviceCloudMsg: DeviceCloudMsg{
Name: device.Name,
State: device.State,
LastOnlineTime: device.LastOnline}}
payload, err := json.Marshal(result)
if err != nil {
return []byte(""), err
Expand Down
15 changes: 10 additions & 5 deletions edge/pkg/devicetwin/dttype/types_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,17 +458,22 @@ func TestMsgTwinToDeviceTwin(t *testing.T) {
}
}

// TestBuildDeviceState is function to test BuildDeviceState().
// TestBuildDeviceState is function to test BuildDeviceCloudMsgState().
func TestBuildDeviceState(t *testing.T) {
baseMessage := BaseMessage{EventID: uuid.New().String(), Timestamp: time.Now().UnixNano() / 1e6}
device := Device{
Name: "SensorTag",
State: "ON",
LastOnline: "Today",
}
deviceCloudMsg := DeviceCloudMsg{
Name: "SensorTag",
State: "ON",
LastOnlineTime: "Today",
}
deviceMsg := DeviceMsg{
BaseMessage: baseMessage,
Device: device,
BaseMessage: baseMessage,
DeviceCloudMsg: deviceCloudMsg,
}
want, _ := json.Marshal(deviceMsg)
tests := []struct {
Expand All @@ -488,13 +493,13 @@ func TestBuildDeviceState(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := BuildDeviceState(test.baseMessage, test.device)
got, err := BuildDeviceCloudMsgState(test.baseMessage, test.device)
if !reflect.DeepEqual(err, test.wantErr) {
t.Errorf("Error Got = %v,Want =%v", err, test.wantErr)
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("BuildDeviceState() = %v, want %v", got, test.want)
t.Errorf("BuildDeviceCloudMsgState() = %v, want %v", got, test.want)
}
})
}
Expand Down

0 comments on commit bb58552

Please sign in to comment.