-
Notifications
You must be signed in to change notification settings - Fork 2
/
twin_runner.go
330 lines (303 loc) · 9.92 KB
/
twin_runner.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package driver
import (
"context"
"github.com/patrickmn/go-cache"
"github.com/thingio/edge-device-std/errors"
"github.com/thingio/edge-device-std/models"
"github.com/thingio/edge-device-std/operations"
"sync"
"time"
)
const (
PropertyCacheExpiration = 30 * time.Second
PropertyCacheCleanupInterval = 2 * PropertyCacheExpiration
)
func NewTwinRunner(driver *DeviceDriver, device *models.Device) (TwinRunner, error) {
if driver == nil {
return nil, errors.DeviceTwin.Error("the driver cannot be nil")
}
if device == nil {
return nil, errors.DeviceTwin.Error("the device cannot be nil")
}
return &twinRunner{
driver: driver,
device: device,
}, nil
}
type TwinRunner interface {
Initialize(ctx context.Context) error
Start() error
Stop(force bool) error
HealthCheck() (*models.DeviceStatus, error)
// Read indicates soft read, it will read the specified property from the cache with TTL.
// Specially, when propertyID is "*", it indicates read all properties.
Read(propertyID models.ProductPropertyID) (map[models.ProductPropertyID]*models.DeviceData, error)
// HardRead indicates head read, it will read the specified property from the real device.
// Specially, when propertyID is "*", it indicates read all properties.
HardRead(propertyID models.ProductPropertyID) (map[models.ProductPropertyID]*models.DeviceData, error)
Write(propertyID models.ProductPropertyID, values map[models.ProductPropertyID]*models.DeviceData) error
Call(methodID models.ProductMethodID, ins map[models.ProductPropertyID]*models.DeviceData) (outs map[models.ProductPropertyID]*models.DeviceData, err error)
}
type twinRunner struct {
driver *DeviceDriver
product *models.Product
device *models.Device
twin models.DeviceTwin
properties map[models.ProductPropertyID]*models.ProductProperty // for property's reading and writing
watchScheduler map[time.Duration][]*models.ProductProperty // for property's watching
propertyCache *cache.Cache // for property's soft reading
methods map[models.ProductMethodID]*models.ProductMethod // for method's calling
once sync.Once
lock sync.Mutex
parent context.Context
ctx context.Context
cancel context.CancelFunc
}
func (r *twinRunner) Initialize(ctx context.Context) error {
r.parent = ctx
if product, err := r.driver.getProduct(r.device.ProductID); err != nil {
return err
} else if twin, err := r.driver.twinBuilder(product, r.device); err != nil {
return err
} else {
r.product = product
r.twin = twin
}
if err := r.initProperties(); err != nil {
return err
}
if err := r.initMethods(); err != nil {
return err
}
return r.twin.Initialize(r.driver.logger)
}
func (r *twinRunner) Start() error {
cfg := r.driver.cfg.DriverOptions
if cfg.DeviceAutoReconnect {
go r.once.Do(r.autoReconnect)
}
return r.start()
}
func (r *twinRunner) autoReconnect() {
cfg := r.driver.cfg.DriverOptions
interval := time.Duration(cfg.DeviceAutoReconnectIntervalSecond) * time.Second
ticker := time.NewTicker(interval)
defer func() {
ticker.Stop()
}()
for {
select {
case <-ticker.C:
status, _ := r.twin.HealthCheck()
switch status.State {
case models.DeviceStateConnected, models.DeviceStateReconnecting:
continue
case models.DeviceStateDisconnected:
_ = r.Stop(false)
return
case models.DeviceStateException:
if err := r.start(); err != nil {
r.driver.logger.Errorf("fail to start the twin runner for the device[%s]", r.device.ID)
continue
}
}
case <-r.ctx.Done():
return
}
}
}
func (r *twinRunner) start() error {
if r.cancel != nil {
r.cancel()
}
r.ctx, r.cancel = context.WithCancel(r.parent)
if err := r.twin.Start(r.ctx); err != nil {
r.device.DeviceStatus = models.DeviceStateException
_ = r.driver.dc.PublishDeviceStatus(r.driver.protocol.ID, r.product.ID, r.device.ID, &models.DeviceStatus{
Device: r.device,
State: r.device.DeviceStatus,
StateDetail: err.Error(),
})
return err
}
r.device.DeviceStatus = models.DeviceStateConnected
_ = r.driver.dc.PublishDeviceStatus(r.driver.protocol.ID, r.product.ID, r.device.ID, &models.DeviceStatus{
Device: r.device,
State: r.device.DeviceStatus,
})
if err := r.watch(); err != nil {
return err
}
if err := r.subscribe(); err != nil {
return err
}
return nil
}
func (r *twinRunner) Stop(force bool) error {
defer func() {
r.cancel()
}()
return r.twin.Stop(force)
}
func (r *twinRunner) HealthCheck() (*models.DeviceStatus, error) {
return r.twin.HealthCheck()
}
func (r *twinRunner) Read(propertyID models.ProductPropertyID) (map[models.ProductPropertyID]*models.DeviceData, error) {
values := make(map[models.ProductPropertyID]*models.DeviceData)
if propertyID == models.DeviceDataMultiPropsID {
for _, property := range r.properties {
value, ok := r.propertyCache.Get(property.Id)
if !ok {
return nil, errors.NotFound.Error("the property[%s] hasn't been ready", property.Id)
}
values[property.Id] = value.(*models.DeviceData)
}
} else { // single property
if _, ok := r.properties[propertyID]; !ok {
return nil, errors.BadRequest.Error("undefined property: %s", propertyID)
}
if value, ok := r.propertyCache.Get(propertyID); !ok {
return nil, errors.NotFound.Error("the property[%s] hasn't been ready", propertyID)
} else {
values[propertyID] = value.(*models.DeviceData)
}
}
r.driver.logger.Debugf("success to softly read the property[%s] of the device[%s], returns %+v",
propertyID, r.device.ID, values)
return values, nil
}
func (r *twinRunner) HardRead(propertyID models.ProductPropertyID) (map[models.ProductPropertyID]*models.DeviceData, error) {
values, err := r.twin.Read(propertyID)
if err != nil {
return nil, err
}
for key, value := range values {
r.propertyCache.SetDefault(key, value)
}
r.driver.logger.Debugf("success to hardly read the property[%s] of the device[%s], returns %+v",
propertyID, r.device.ID, values)
return values, nil
}
func (r *twinRunner) Write(propertyID models.ProductPropertyID, values map[models.ProductPropertyID]*models.DeviceData) error {
for _, value := range values {
propertyID = value.Name
property, ok := r.properties[propertyID]
if !ok {
return errors.NotFound.Error("undefined property: %s", propertyID)
}
if !property.Writeable {
return errors.DeviceTwin.Error("the property[%s] is read-only", propertyID)
}
}
if err := r.twin.Write(propertyID, values); err != nil {
return err
}
r.driver.logger.Debugf("success to write the property[%s] of the device[%s] with values %+v",
propertyID, r.device.ID, values)
return nil
}
func (r *twinRunner) Call(methodID models.ProductMethodID, ins map[models.ProductPropertyID]*models.DeviceData) (
outs map[models.ProductPropertyID]*models.DeviceData, err error) {
method, ok := r.methods[methodID]
if !ok {
return nil, errors.NotFound.Error("undefined method: %s", methodID)
}
for _, in := range method.Ins {
if _, ok := ins[in.Id]; !ok {
return nil, errors.BadRequest.Error("missing method input: %+v", in)
}
}
outs, err = r.twin.Call(methodID, ins)
for _, out := range method.Outs {
if _, ok := outs[out.Id]; !ok {
return nil, errors.BadRequest.Error("missing method output: %+v", out)
}
}
r.driver.logger.Debugf("success to call the method[%s] of the device[%s], input %+v, output %+v",
methodID, r.device.ID, ins, outs)
return outs, nil
}
func (r *twinRunner) initProperties() error {
r.properties = make(map[models.ProductPropertyID]*models.ProductProperty)
for _, property := range r.product.Properties {
r.properties[property.Id] = property
}
r.propertyCache = cache.New(PropertyCacheExpiration, PropertyCacheCleanupInterval)
r.watchScheduler = make(map[time.Duration][]*models.ProductProperty)
for _, property := range r.properties {
if property.ReportMode != operations.DeviceDataReportModePeriodical {
continue
}
duration, err := time.ParseDuration(property.Interval)
if err != nil {
return errors.DeviceTwin.Cause(err, "fail to parse the reporting interval: %s", property.Interval)
} else if duration <= 0 {
continue
}
_, ok := r.watchScheduler[duration]
if !ok {
r.watchScheduler[duration] = make([]*models.ProductProperty, 0)
}
r.watchScheduler[duration] = append(r.watchScheduler[duration], property)
}
return nil
}
func (r *twinRunner) initMethods() error {
r.methods = make(map[models.ProductMethodID]*models.ProductMethod)
for _, method := range r.product.Methods {
r.methods[method.Id] = method
}
return nil
}
func (r *twinRunner) watch() error {
multiRead := func(properties []*models.ProductProperty) map[models.ProductPropertyID]*models.DeviceData {
result := map[models.ProductPropertyID]*models.DeviceData{}
for _, property := range properties {
pairs, err := r.HardRead(property.Id)
if err != nil {
r.driver.logger.WithError(err).Errorf("watch properiodly properties[%s]", property.Id)
continue
}
for key, value := range pairs {
result[key] = value
}
}
return result
}
for duration, properties := range r.watchScheduler {
go func(d time.Duration, pps []*models.ProductProperty) {
ticker := time.NewTicker(d)
defer func() {
ticker.Stop()
}()
for {
select {
case <-ticker.C:
propertyID := models.DeviceDataMultiPropsID
if len(pps) == 1 {
propertyID = pps[0].Id
}
r.driver.propsBus <- &models.DeviceDataWrapper{
ProductID: r.product.ID,
DeviceID: r.device.ID,
FuncID: propertyID,
Properties: multiRead(pps),
}
case <-r.ctx.Done():
return
}
}
}(duration, properties)
}
r.driver.logger.Debugf("success to watch the device[%s]", r.device.ID)
return nil
}
func (r *twinRunner) subscribe() error {
for _, event := range r.product.Events {
if err := r.twin.Subscribe(event.Id, r.driver.eventBus); err != nil {
return errors.DeviceTwin.Cause(err, "fail to subscribe the event: %s", event.Id)
}
r.driver.logger.Debugf("success to subscribe the event[%s]", r.device.ID)
}
return nil
}