-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
device.go
302 lines (280 loc) · 8.77 KB
/
device.go
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
package dtmanager
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"k8s.io/klog/v2"
"github.com/kubeedge/beehive/pkg/core/model"
messagepkg "github.com/kubeedge/kubeedge/edge/pkg/common/message"
"github.com/kubeedge/kubeedge/edge/pkg/common/modules"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtclient"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtcommon"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtcontext"
"github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dttype"
)
var (
//deviceActionCallBack map for action to callback
deviceActionCallBack map[string]CallBack
)
// DeviceWorker deal device event
type DeviceWorker struct {
Worker
Group string
}
// Start worker
func (dw DeviceWorker) Start() {
initDeviceActionCallBack()
for {
select {
case msg, ok := <-dw.ReceiverChan:
if !ok {
return
}
if dtMsg, isDTMessage := msg.(*dttype.DTMessage); isDTMessage {
if fn, exist := deviceActionCallBack[dtMsg.Action]; exist {
err := fn(dw.DTContexts, dtMsg.Identity, dtMsg.Msg)
if err != nil {
klog.Errorf("DeviceModule deal %s event failed: %v", dtMsg.Action, err)
}
} else {
klog.Errorf("DeviceModule deal %s event failed, not found callback", dtMsg.Action)
}
}
case v, ok := <-dw.HeartBeatChan:
if !ok {
return
}
if err := dw.DTContexts.HeartBeat(dw.Group, v); err != nil {
return
}
}
}
}
func initDeviceActionCallBack() {
deviceActionCallBack = make(map[string]CallBack)
deviceActionCallBack[dtcommon.DeviceUpdated] = dealDeviceAttrUpdate
deviceActionCallBack[dtcommon.DeviceStateUpdate] = dealDeviceStateUpdate
}
func dealDeviceStateUpdate(context *dtcontext.DTContext, resource string, msg interface{}) error {
message, ok := msg.(*model.Message)
if !ok {
return errors.New("msg not Message type")
}
updatedDevice, err := dttype.UnmarshalDeviceUpdate(message.Content.([]byte))
if err != nil {
klog.Errorf("Unmarshal device info failed, err: %#v", err)
return err
}
deviceID := resource
defer context.Unlock(deviceID)
context.Lock(deviceID)
doc, docExist := context.DeviceList.Load(deviceID)
if !docExist {
return nil
}
device, ok := doc.(*dttype.Device)
if !ok {
return nil
}
// state refers to definition in mappers-go/pkg/common/const.go
state := strings.ToLower(updatedDevice.State)
switch state {
case "online", "offline", "ok", "unknown", "disconnected":
default:
return nil
}
lastOnline := time.Now().Format("2006-01-02 15:04:05")
for i := 1; i <= dtcommon.RetryTimes; i++ {
err = dtclient.UpdateDeviceFields(
device.ID,
map[string]interface{}{
"last_online": lastOnline,
"state": updatedDevice.State,
})
if err == nil {
break
}
time.Sleep(dtcommon.RetryInterval)
}
if err != nil {
return err
}
device.State = updatedDevice.State
device.LastOnline = lastOnline
payload, err := dttype.BuildDeviceState(dttype.BuildBaseMessage(), *device)
if err != nil {
return err
}
topic := dtcommon.DeviceETPrefix + device.ID + dtcommon.DeviceETStateUpdateResultSuffix
context.Send(device.ID,
dtcommon.SendToEdge,
dtcommon.CommModule,
context.BuildModelMessage(modules.BusGroup, "", topic, messagepkg.OperationPublish, payload))
msgResource := "device/" + device.ID + dtcommon.DeviceETStateUpdateSuffix
context.Send(deviceID,
dtcommon.SendToCloud,
dtcommon.CommModule,
context.BuildModelMessage("resource", "", msgResource, model.UpdateOperation, string(payload)))
return nil
}
func dealDeviceAttrUpdate(context *dtcontext.DTContext, resource string, msg interface{}) error {
message, ok := msg.(*model.Message)
if !ok {
return errors.New("msg not Message type")
}
updatedDevice, err := dttype.UnmarshalDeviceUpdate(message.Content.([]byte))
if err != nil {
klog.Errorf("Unmarshal device info failed, err: %#v", err)
return err
}
deviceID := resource
context.Lock(deviceID)
UpdateDeviceAttr(context, deviceID, updatedDevice.Attributes, dttype.BaseMessage{EventID: updatedDevice.EventID}, 0)
context.Unlock(deviceID)
return nil
}
// UpdateDeviceAttr update device attributes
func UpdateDeviceAttr(context *dtcontext.DTContext, deviceID string, attributes map[string]*dttype.MsgAttr, baseMessage dttype.BaseMessage, dealType int) (interface{}, error) {
klog.Infof("Begin to update attributes of the device %s", deviceID)
var err error
doc, docExist := context.DeviceList.Load(deviceID)
if !docExist {
return nil, nil
}
Device, ok := doc.(*dttype.Device)
if !ok {
return nil, nil
}
dealAttrResult := DealMsgAttr(context, Device.ID, attributes, dealType)
if dealAttrResult.Err != nil {
return nil, nil
}
add, delete, update, result := dealAttrResult.Add, dealAttrResult.Delete, dealAttrResult.Update, dealAttrResult.Result
if len(add) != 0 || len(delete) != 0 || len(update) != 0 {
for i := 1; i <= dtcommon.RetryTimes; i++ {
err = dtclient.DeviceAttrTrans(add, delete, update)
if err == nil {
break
}
time.Sleep(dtcommon.RetryInterval)
}
now := time.Now().UnixNano() / 1e6
baseMessage.Timestamp = now
if err != nil {
SyncDeviceFromSqlite(context, deviceID)
klog.Errorf("Update device failed due to writing sql error: %v", err)
} else {
klog.Infof("Send update attributes of device %s event to edge app", deviceID)
payload, err := dttype.BuildDeviceAttrUpdate(baseMessage, result)
if err != nil {
//todo
klog.Errorf("Build device attribute update failed: %v", err)
}
topic := dtcommon.DeviceETPrefix + deviceID + dtcommon.DeviceETUpdatedSuffix
context.Send(deviceID, dtcommon.SendToEdge, dtcommon.CommModule,
context.BuildModelMessage(modules.BusGroup, "", topic, messagepkg.OperationPublish, payload))
}
}
return nil, nil
}
// DealMsgAttr get diff,0:update, 1:detail
func DealMsgAttr(context *dtcontext.DTContext, deviceID string, msgAttrs map[string]*dttype.MsgAttr, dealType int) dttype.DealAttrResult {
device, ok := context.GetDevice(deviceID)
if !ok {
return dttype.DealAttrResult{
Err: fmt.Errorf("can not get deviceID %s in DealMsgAttr", deviceID),
}
}
attrs := device.Attributes
if attrs == nil {
device.Attributes = make(map[string]*dttype.MsgAttr)
attrs = device.Attributes
}
add := make([]dtclient.DeviceAttr, 0)
deletes := make([]dtclient.DeviceDelete, 0)
update := make([]dtclient.DeviceAttrUpdate, 0)
result := make(map[string]*dttype.MsgAttr)
for key, msgAttr := range msgAttrs {
if attr, exist := attrs[key]; exist {
if msgAttr == nil && dealType == 0 {
if *attr.Optional {
deletes = append(deletes, dtclient.DeviceDelete{DeviceID: deviceID, Name: key})
result[key] = nil
delete(attrs, key)
}
continue
}
isChange := false
cols := make(map[string]interface{})
result[key] = &dttype.MsgAttr{}
if strings.Compare(attr.Value, msgAttr.Value) != 0 {
attr.Value = msgAttr.Value
cols["value"] = msgAttr.Value
result[key].Value = msgAttr.Value
isChange = true
}
if msgAttr.Metadata != nil {
msgMetaJSON, _ := json.Marshal(msgAttr.Metadata)
attrMetaJSON, _ := json.Marshal(attr.Metadata)
if strings.Compare(string(msgMetaJSON), string(attrMetaJSON)) != 0 {
cols["attr_type"] = msgAttr.Metadata.Type
meta := dttype.CopyMsgAttr(msgAttr)
attr.Metadata = meta.Metadata
msgAttr.Metadata.Type = ""
metaJSON, _ := json.Marshal(msgAttr.Metadata)
cols["metadata"] = string(metaJSON)
msgAttr.Metadata.Type = cols["attr_type"].(string)
result[key].Metadata = meta.Metadata
isChange = true
}
}
if msgAttr.Optional != nil {
if *msgAttr.Optional != *attr.Optional && *attr.Optional {
optional := *msgAttr.Optional
cols["optional"] = optional
attr.Optional = &optional
result[key].Optional = &optional
isChange = true
}
}
if isChange {
update = append(update, dtclient.DeviceAttrUpdate{DeviceID: deviceID, Name: key, Cols: cols})
} else {
delete(result, key)
}
} else {
deviceAttr := dttype.MsgAttrToDeviceAttr(key, msgAttr)
deviceAttr.DeviceID = deviceID
deviceAttr.Value = msgAttr.Value
if msgAttr.Optional != nil {
optional := *msgAttr.Optional
deviceAttr.Optional = optional
}
if msgAttr.Metadata != nil {
//todo
deviceAttr.AttrType = msgAttr.Metadata.Type
msgAttr.Metadata.Type = ""
metaJSON, _ := json.Marshal(msgAttr.Metadata)
msgAttr.Metadata.Type = deviceAttr.AttrType
deviceAttr.Metadata = string(metaJSON)
}
add = append(add, deviceAttr)
attrs[key] = msgAttr
result[key] = msgAttr
}
}
if dealType > 0 {
for key := range attrs {
if _, exist := msgAttrs[key]; !exist {
deletes = append(deletes, dtclient.DeviceDelete{DeviceID: deviceID, Name: key})
result[key] = nil
}
}
for _, v := range deletes {
delete(attrs, v.Name)
}
}
return dttype.DealAttrResult{Add: add, Delete: deletes, Update: update, Result: result, Err: nil}
}