Skip to content

Commit 8c01665

Browse files
committed
采集完数据,自动更新
1 parent 071b930 commit 8c01665

2 files changed

Lines changed: 42 additions & 18 deletions

File tree

iot/device-mqtt.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -342,49 +342,72 @@ func mqttSubscribeDevice() {
342342
ss := strings.Split(topic, "/")
343343
id := ss[1]
344344
dev := devices.Load(id)
345-
if dev == nil {
346-
return
345+
if dev != nil {
346+
dev.onSyncResponse(resp)
347+
}
348+
349+
if resp.DeviceId != "" && resp.DeviceId != id {
350+
dev = devices.Load(resp.DeviceId)
351+
if dev != nil {
352+
dev.onSyncResponse(resp)
353+
}
347354
}
348-
dev.onSyncResponse(resp)
349355
})
350356

351357
mqtt.SubscribeStruct[ReadResponse]("device/+/read/response", func(topic string, resp *ReadResponse) {
352358
ss := strings.Split(topic, "/")
353359
id := ss[1]
354360
dev := devices.Load(id)
355-
if dev == nil {
356-
return
361+
if dev != nil {
362+
dev.onReadResponse(resp)
363+
}
364+
365+
if resp.DeviceId != "" && resp.DeviceId != id {
366+
dev = devices.Load(resp.DeviceId)
367+
if dev != nil {
368+
dev.onReadResponse(resp)
369+
}
357370
}
358-
dev.onReadResponse(resp)
359371
})
360372

361373
mqtt.SubscribeStruct[WriteResponse]("device/+/write/response", func(topic string, resp *WriteResponse) {
362374
ss := strings.Split(topic, "/")
363375
id := ss[1]
364376
dev := devices.Load(id)
365-
if dev == nil {
366-
return
377+
if dev != nil {
378+
dev.onWriteResponse(resp)
379+
}
380+
381+
if resp.DeviceId != "" && resp.DeviceId != id {
382+
dev = devices.Load(resp.DeviceId)
383+
if dev != nil {
384+
dev.onWriteResponse(resp)
385+
}
367386
}
368-
dev.onWriteResponse(resp)
369387
})
370388

371389
mqtt.SubscribeStruct[ActionResponse]("device/+/action/response", func(topic string, resp *ActionResponse) {
372390
ss := strings.Split(topic, "/")
373391
id := ss[1]
374392
dev := devices.Load(id)
375-
if dev == nil {
376-
return
393+
if dev != nil {
394+
dev.onActionResponse(resp)
395+
}
396+
397+
if resp.DeviceId != "" && resp.DeviceId != id {
398+
dev = devices.Load(resp.DeviceId)
399+
if dev != nil {
400+
dev.onActionResponse(resp)
401+
}
377402
}
378-
dev.onActionResponse(resp)
379403
})
380404

381405
mqtt.SubscribeStruct[SettingResponse]("device/+/setting/response", func(topic string, resp *SettingResponse) {
382406
ss := strings.Split(topic, "/")
383407
id := ss[1]
384408
dev := devices.Load(id)
385-
if dev == nil {
386-
return
409+
if dev != nil {
410+
dev.onSettingResponse(resp)
387411
}
388-
dev.onSettingResponse(resp)
389412
})
390413
}

pages/device-values.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"toolbar": [
55
{
66
"type": "button",
7-
"label": "更新数据",
7+
"label": "采集数据",
88
"action": {
99
"type": "script",
1010
"script": "if(this.device.gateway_id) this.refresh_child_values(); else this.refresh_values();"
@@ -18,8 +18,9 @@
1818
"mount": "this.load_device(); this.value_action={type:'dialog',page:'device-history',params_func:'return {id:this.params.id, point:data.key}'}",
1919
"methods": {
2020
"load_values": "this.request.get('device/'+this.params.id+'/values').subscribe(res=>{if(res.error)return; this.data=res.data; })",
21-
"refresh_values": "this.request.get('device/'+this.params.id+'/sync').subscribe(res=>{if(res.error)return; this.load_values()})",
22-
"refresh_child_values": "this.request.get('device/'+this.device.gateway_id+'/sync/'+this.params.id).subscribe(res=>{if(res.error)return; this.load_values()})",
21+
"load_values_delay": ["delay", "setTimeout(()=>this.load_values(), delay || 1000)"],
22+
"refresh_values": "this.request.get('device/'+this.params.id+'/sync').subscribe(res=>{if(res.error)return; this.load_values_delay();})",
23+
"refresh_child_values": "this.request.get('device/'+this.device.gateway_id+'/sync/'+this.params.id).subscribe(res=>{if(res.error)return; this.load_values_delay()})",
2324
"load_device": "this.request.get('table/device/detail/'+this.params.id).subscribe(res=>{if(res.error)return; this.device=res.data; this.load_model(res.data.product_id)})",
2425
"load_model": ["pid","this.request.get('product/'+pid+'/setting/model').subscribe(res=>{if(res.error)return; res.data.content && this.render_properties(res.data.content); setTimeout(()=>this.render_values(),100); })"],
2526
"render_properties": ["properties", "properties && properties.map(p=>{ this.content.children.push({span: 24, content:{title:p.name,template:'statistic', items: this.render_points(p.points)} }) })"],

0 commit comments

Comments
 (0)