1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
package dttype
import (
"encoding/json"
"errors"
"strings"
"time"
"github.com/google/uuid"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtclient"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtcommon"
)
// Device the struct of device
type Device struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
State string `json:"state,omitempty"`
LastOnline string `json:"last_online,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"`
Timestamp int64 `json:"timestamp"`
}
var ErrorUnmarshal = errors.New("Unmarshal update request body failed, please check the request")
var ErrorUpdate = errors.New("Update twin error, key:twin does not exist")
var ErrorKey = errors.New("The key of twin must only include upper or lowercase letters, number, english, and special letter - _ . , : / @ # and the length of key should be less than 128 bytes")
var ErrorValue = errors.New("The value of twin must only include upper or lowercase letters, number, english, and special letter - _ . , : / @ # and the length of value should be less than 512 bytes")
// SetEventID set event id
func (bs *BaseMessage) SetEventID(eventID string) {
bs.EventID = eventID
}
// BuildBaseMessage build base msg
func BuildBaseMessage() BaseMessage {
now := time.Now().UnixNano() / 1e6
return BaseMessage{
EventID: uuid.New().String(),
Timestamp: now}
}
// Parameter container para
type Parameter struct {
EventID string
Code int
Reason string
}
// Result the struct of Result for sending
type Result struct {
BaseMessage
Code int `json:"code,omitempty"`
Reason string `json:"reason,omitempty"`
}
// MembershipDetail the struct of membership detail
type MembershipDetail struct {
BaseMessage
Devices []Device `json:"devices"`
}
// MembershipUpdate the struct of membership update
type MembershipUpdate struct {
BaseMessage
AddDevices []Device `json:"added_devices"`
RemoveDevices []Device `json:"removed_devices"`
}
// MarshalMembershipUpdate marshal membership update
func MarshalMembershipUpdate(result MembershipUpdate) ([]byte, error) {
for i := range result.AddDevices {
if result.AddDevices[i].Twin != nil {
for k, v := range result.AddDevices[i].Twin {
if v.Metadata != nil && strings.Compare(v.Metadata.Type, dtcommon.TypeDeleted) == 0 {
result.AddDevices[i].Twin[k] = nil
}
v.ActualVersion = nil
v.ExpectedVersion = nil
}
}
}
for i := range result.RemoveDevices {
if result.RemoveDevices[i].Twin != nil {
for k, v := range result.RemoveDevices[i].Twin {
if v.Metadata != nil && strings.Compare(v.Metadata.Type, dtcommon.TypeDeleted) == 0 {
result.RemoveDevices[i].Twin[k] = nil
}
v.ActualVersion = nil
v.ExpectedVersion = nil
}
}
}
resultJSON, err := json.Marshal(result)
return resultJSON, err
}
// MsgAttr the struct of device attr
type MsgAttr struct {
Value string `json:"value"`
Optional *bool `json:"optional,omitempty"`
Metadata *TypeMetadata `json:"metadata,omitempty"`
}
// MsgTwin the struct of device twin
type MsgTwin struct {
Expected *TwinValue `json:"expected,omitempty"`
Actual *TwinValue `json:"actual,omitempty"`
Optional *bool `json:"optional,omitempty"`
Metadata *TypeMetadata `json:"metadata,omitempty"`
ExpectedVersion *TwinVersion `json:"expected_version,omitempty"`
ActualVersion *TwinVersion `json:"actual_version,omitempty"`
}
// TwinValue the struct of twin value
type TwinValue struct {
Value *string `json:"value,omitempty"`
Metadata *ValueMetadata `json:"metadata,omitempty"`
}
// TwinVersion twin version
type TwinVersion struct {
CloudVersion int64 `json:"cloud"`
EdgeVersion int64 `json:"edge"`
}
// TypeMetadata the meta of value type
type TypeMetadata struct {
Type string `json:"type,omitempty"`
}
// ValueMetadata the meta of value
type ValueMetadata struct {
Timestamp int64 `json:"timestamp,omitempty"`
}
// UpdateCloudVersion update cloud version
func (tv *TwinVersion) UpdateCloudVersion() {
tv.CloudVersion = tv.CloudVersion + 1
}
// UpdateEdgeVersion update edge version while dealing edge update
func (tv *TwinVersion) UpdateEdgeVersion() {
tv.EdgeVersion = tv.EdgeVersion + 1
}
// CompareWithCloud compare with cloud vershon while dealing cloud update req
func (tv TwinVersion) CompareWithCloud(tvCloud TwinVersion) bool {
return tvCloud.EdgeVersion >= tv.EdgeVersion
}
// UpdateCloudVersion update cloud version
func UpdateCloudVersion(version string) (string, error) {
var twinversion TwinVersion
err := json.Unmarshal([]byte(version), &twinversion)
if err != nil {
return "", err
}
twinversion.UpdateCloudVersion()
result, err := json.Marshal(twinversion)
if err != nil {
return "", err
}
return string(result), nil
}
// UpdateEdgeVersion update Edge version
func UpdateEdgeVersion(version string) (string, error) {
var twinversion TwinVersion
err := json.Unmarshal([]byte(version), &twinversion)
if err != nil {
return "", err
}
twinversion.UpdateEdgeVersion()
result, err := json.Marshal(twinversion)
if err != nil {
return "", err
}
return string(result), nil
}
// CompareVersion compare cloud version
func CompareVersion(cloudversion string, edgeversion string) bool {
var twincloudversion TwinVersion
err := json.Unmarshal([]byte(cloudversion), &twincloudversion)
if err != nil {
return false
}
var twinedgeversion TwinVersion
err1 := json.Unmarshal([]byte(edgeversion), &twinedgeversion)
if err1 != nil {
return false
}
return twinedgeversion.CompareWithCloud(twincloudversion)
}
// ConnectedInfo connected info
type ConnectedInfo struct {
EventType string `json:"event_type"`
TimeStamp int64 `json:"timestamp"`
}
// UnmarshalConnectedInfo unmarshal connected info
func UnmarshalConnectedInfo(payload []byte) (ConnectedInfo, error) {
var connectedInfo ConnectedInfo
err := json.Unmarshal(payload, &connectedInfo)
if err != nil {
return connectedInfo, err
}
return connectedInfo, nil
}
// DeviceTwinDocument the struct of twin document
type DeviceTwinDocument struct {
BaseMessage
Twin map[string]*TwinDoc `json:"twin"`
}
// TwinDoc the struct of twin document
type TwinDoc struct {
LastState *MsgTwin `json:"last"`
CurrentState *MsgTwin `json:"current"`
}
// DeviceTwinUpdate the struct of device twin update
type DeviceTwinUpdate struct {
BaseMessage
Twin map[string]*MsgTwin `json:"twin"`
}
// UnmarshalDeviceTwinDocument unmarshal device twin document
func UnmarshalDeviceTwinDocument(payload []byte) (*DeviceTwinDocument, error) {
var deviceTwinUpdate DeviceTwinDocument
err := json.Unmarshal(payload, &deviceTwinUpdate)
if err != nil {
return &deviceTwinUpdate, err
}
return &deviceTwinUpdate, nil
}
// UnmarshalDeviceTwinUpdate unmarshal device twin update
func UnmarshalDeviceTwinUpdate(payload []byte) (*DeviceTwinUpdate, error) {
var deviceTwinUpdate DeviceTwinUpdate
err := json.Unmarshal(payload, &deviceTwinUpdate)
if err != nil {
return &deviceTwinUpdate, ErrorUnmarshal
}
if deviceTwinUpdate.Twin == nil {
return &deviceTwinUpdate, ErrorUpdate
}
for key, value := range deviceTwinUpdate.Twin {
match := dtcommon.ValidateTwinKey(key)
if !match {
return &deviceTwinUpdate, ErrorKey
}
if value != nil {
if value.Expected != nil {
if value.Expected.Value != nil {
if *value.Expected.Value != "" {
match := dtcommon.ValidateTwinValue(*value.Expected.Value)
if !match {
return &deviceTwinUpdate, ErrorValue
}
}
}
}
if value.Actual != nil {
if value.Actual.Value != nil {
if *value.Actual.Value != "" {
match := dtcommon.ValidateTwinValue(*value.Actual.Value)
if !match {
return &deviceTwinUpdate, ErrorValue
}
}
}
}
}
}
return &deviceTwinUpdate, nil
}
// DealTwinResult the result of dealing twin
type DealTwinResult struct {
Add []dtclient.DeviceTwin
Delete []dtclient.DeviceDelete
Update []dtclient.DeviceTwinUpdate
Result map[string]*MsgTwin
SyncResult map[string]*MsgTwin
Document map[string]*TwinDoc
Err error
}
// DealAttrResult the result of dealing attr
type DealAttrResult struct {
Add []dtclient.DeviceAttr
Delete []dtclient.DeviceDelete
Update []dtclient.DeviceAttrUpdate
Result map[string]*MsgAttr
Err error
}
|