diff options
| author | KubeEdge Bot <48982446+kubeedge-bot@users.noreply.github.com> | 2024-07-25 20:11:07 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-25 20:11:07 +0800 |
| commit | c5e705a7545de9e7e36ed6b1fbb2072ad5b5e1d3 (patch) | |
| tree | 50b2e84dce0abe7e102981730a63de638ccc3767 | |
| parent | Merge pull request #5649 from JiaweiGithub/feat/dmi_state_cloud (diff) | |
| parent | fix edge device state sync error (diff) | |
| download | kubeedge-c5e705a7545de9e7e36ed6b1fbb2072ad5b5e1d3.tar.gz | |
Merge pull request #5750 from Shelley-BaoYue/automated-cherry-pick-of-#5747-upstream-release-1.18v1.18.0origin/release-1.18
Automated cherry pick of #5747: fix edge device state sync error
| -rw-r--r-- | edge/pkg/devicetwin/dtmanager/device.go | 2 | ||||
| -rw-r--r-- | edge/pkg/devicetwin/dttype/types.go | 11 | ||||
| -rw-r--r-- | edge/pkg/devicetwin/dttype/types_helper.go | 14 | ||||
| -rw-r--r-- | edge/pkg/devicetwin/dttype/types_helper_test.go | 15 |
4 files changed, 29 insertions, 13 deletions
diff --git a/edge/pkg/devicetwin/dtmanager/device.go b/edge/pkg/devicetwin/dtmanager/device.go index 7c5882b0f..1990ec1b5 100644 --- a/edge/pkg/devicetwin/dtmanager/device.go +++ b/edge/pkg/devicetwin/dtmanager/device.go @@ -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 } diff --git a/edge/pkg/devicetwin/dttype/types.go b/edge/pkg/devicetwin/dttype/types.go index 372c1e535..df7423990 100644 --- a/edge/pkg/devicetwin/dttype/types.go +++ b/edge/pkg/devicetwin/dttype/types.go @@ -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"` diff --git a/edge/pkg/devicetwin/dttype/types_helper.go b/edge/pkg/devicetwin/dttype/types_helper.go index b6b37c582..a9134515e 100644 --- a/edge/pkg/devicetwin/dttype/types_helper.go +++ b/edge/pkg/devicetwin/dttype/types_helper.go @@ -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 diff --git a/edge/pkg/devicetwin/dttype/types_helper_test.go b/edge/pkg/devicetwin/dttype/types_helper_test.go index 8e6588260..7b3dfd29c 100644 --- a/edge/pkg/devicetwin/dttype/types_helper_test.go +++ b/edge/pkg/devicetwin/dttype/types_helper_test.go @@ -458,7 +458,7 @@ 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{ @@ -466,9 +466,14 @@ func TestBuildDeviceState(t *testing.T) { 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 { @@ -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) } }) } |
