Skip to content

Commit 25f3618

Browse files
committed
支持动作响应
1 parent e7e6bc3 commit 25f3618

File tree

10 files changed

+157
-11
lines changed

10 files changed

+157
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/busy-cloud/boat-ui v0.5.1
99
github.com/busy-cloud/dash v0.5.0
1010
github.com/busy-cloud/influxdb v0.2.3
11-
github.com/busy-cloud/modbus v0.3.1
11+
github.com/busy-cloud/modbus v0.3.3
1212
github.com/busy-cloud/tcp-server v0.0.10
1313
github.com/busy-cloud/user v0.5.0
1414
github.com/gin-gonic/gin v1.10.1

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/busy-cloud/dash v0.5.0 h1:v7onTsSlRKKPN/OHnczyAfPL8+h0pIzrF6FHNh1IJT8
1818
github.com/busy-cloud/dash v0.5.0/go.mod h1:YpLLCh50vwRy47NM9FFrAeg07Mh0qLxz6dzqYPJZm9c=
1919
github.com/busy-cloud/influxdb v0.2.3 h1:VW+5/q+/ClImAzrxwNT9zaXIbuHKch6rhKy9sJfOygg=
2020
github.com/busy-cloud/influxdb v0.2.3/go.mod h1:6mfMxknjGxBWcCOaLAkjig6YmssQSQbDPCeGeccCNPM=
21-
github.com/busy-cloud/modbus v0.3.1 h1:oCIJEsOwkrBRXMTzuFOKp4caxlSL6/gs/9B4/IIZjwk=
22-
github.com/busy-cloud/modbus v0.3.1/go.mod h1:FDL+VYI5rCdW1UWXTsG9nxnvo7O8P+FhTmCXT0AEnPU=
21+
github.com/busy-cloud/modbus v0.3.3 h1:T5kp9fgsVOVgrWRiUBl9gmkh7Kh8qdIh7WUh43gL2N4=
22+
github.com/busy-cloud/modbus v0.3.3/go.mod h1:7qxnq+A4TzRmnLa+f661RtlXrOwdocLrhrEjy7LJTZE=
2323
github.com/busy-cloud/tcp-server v0.0.10 h1:RoG0F77NYc89e3IG0DxocQh6cTBqE31D4DKdEd2wqZM=
2424
github.com/busy-cloud/tcp-server v0.0.10/go.mod h1:OOirzSeZQkmYsQDoWaGz8j1h1BF+7VL9cnSS6YRMcKM=
2525
github.com/busy-cloud/user v0.5.0 h1:Td5MdA9ichR9PCHp3o+FueezP/RW/TVXhYJE3/jGIEY=
@@ -178,8 +178,6 @@ github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9yS
178178
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
179179
github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
180180
github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo=
181-
github.com/spf13/cast v1.8.0 h1:gEN9K4b8Xws4EX0+a0reLmhq8moKn7ntRlQYgjPeCDk=
182-
github.com/spf13/cast v1.8.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
183181
github.com/spf13/cast v1.9.2 h1:SsGfm7M8QOFtEzumm7UZrZdLLquNdzFYfIbEXntcFbE=
184182
github.com/spf13/cast v1.9.2/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
185183
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=

internal/device_api.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ func init() {
99
api.Register("GET", "iot/device/:id/values", deviceValues)
1010
api.Register("GET", "iot/device/:id/status", deviceStatus)
1111
api.Register("GET", "iot/device/:id/sync", deviceSync)
12+
api.Register("GET", "iot/device/:id/read", deviceRead)
13+
api.Register("POST", "iot/device/:id/write", deviceWrite)
14+
api.Register("POST", "iot/device/:id/action/:action", deviceAction)
1215
}
1316

1417
func deviceValues(ctx *gin.Context) {
@@ -38,3 +41,67 @@ func deviceSync(ctx *gin.Context) {
3841

3942
api.OK(ctx, nil)
4043
}
44+
45+
func deviceRead(ctx *gin.Context) {
46+
d := devices.Load(ctx.Param("id"))
47+
if d == nil {
48+
api.Fail(ctx, "设备未上线")
49+
return
50+
}
51+
52+
points := ctx.QueryArray("point")
53+
values, err := d.Read(points, 30)
54+
if err != nil {
55+
api.Error(ctx, err)
56+
return
57+
}
58+
59+
api.OK(ctx, values)
60+
}
61+
62+
func deviceWrite(ctx *gin.Context) {
63+
d := devices.Load(ctx.Param("id"))
64+
if d == nil {
65+
api.Fail(ctx, "设备未上线")
66+
return
67+
}
68+
69+
var values map[string]any
70+
err := ctx.ShouldBind(&values)
71+
if err != nil {
72+
api.Error(ctx, err)
73+
return
74+
}
75+
76+
result, err := d.Write(values, 30)
77+
if err != nil {
78+
api.Error(ctx, err)
79+
return
80+
}
81+
82+
api.OK(ctx, result)
83+
}
84+
85+
func deviceAction(ctx *gin.Context) {
86+
d := devices.Load(ctx.Param("id"))
87+
if d == nil {
88+
api.Fail(ctx, "设备未上线")
89+
return
90+
}
91+
action := ctx.Param("action")
92+
93+
var values map[string]any
94+
err := ctx.ShouldBind(&values)
95+
if err != nil {
96+
api.Error(ctx, err)
97+
return
98+
}
99+
100+
result, err := d.Action(action, values, 30)
101+
if err != nil {
102+
api.Error(ctx, err)
103+
return
104+
}
105+
106+
api.OK(ctx, result)
107+
}

pages/device-action.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"title": "执行动作",
3+
"template": "form",
4+
"fields": [],
5+
"mount": "this.content.fields = this.params.parameters",
6+
"submit_api": "iot/device/:id/action/:action"
7+
}

pages/device-actions.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "动作响应",
3+
"template": "info",
4+
"toolbar": [
5+
6+
],
7+
"items": [],
8+
"mount": "this.load_device(); this.btn_action={type:'dialog',page:'iot/device-action',params_func:'return {id:this.params.id, action:data}'}",
9+
"methods": {
10+
"load_device": "this.request.get('iot/device/'+this.params.id).subscribe(res=>{if(res.error)return; this.load_model(res.data.product_id)})",
11+
"load_model": ["pid","this.request.get('iot/product/'+pid+'/model').subscribe(res=>{if(res.error)return; this.content.toolbar=res.data.actions.map(p=>{return{type:'button', label:p.label||p.name, action:{type:'dialog',page:'iot/device-action',params:{id:this.params.id, action:p.name, parameters:p.parameters}}}}); })"]
12+
}
13+
}

pages/device-detail.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
"page": "iot/device-values",
8383
"params_func": "return {id: params.id}"
8484
},
85+
{
86+
"title": "动作响应",
87+
"page": "iot/device-actions",
88+
"params_func": "return {id: params.id}"
89+
},
8590
{
8691
"title": "数据配置",
8792
"page": "iot/device-values-setting",

pages/device-values.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"mount": "this.load_device(); this.value_action={type:'dialog',page:'iot/device-history',params_func:'return {id:this.params.id, point:data.key}'}",
1818
"methods": {
1919
"load_values": "this.request.get('iot/device/'+this.params.id+'/values').subscribe(res=>{if(res.error)return; this.data=res.data; this.content.toolbar[0].type='text'; label=res.data.__update})",
20-
"refresh_values": "this.request.get('iot/device/'+this.params.id+'/values/refresh').subscribe(res=>{if(res.error)return; this.data=res.data})",
20+
"refresh_values": "this.request.get('iot/device/'+this.params.id+'/values/sync').subscribe(res=>{if(res.error)return; this.data=res.data})",
2121
"load_device": "this.request.get('iot/device/'+this.params.id).subscribe(res=>{if(res.error)return; this.load_model(res.data.product_id)})",
2222
"load_model": ["pid","this.request.get('iot/product/'+pid+'/model').subscribe(res=>{if(res.error)return; this.content.items=res.data.properties.map(p=>{return{key:p.name,label:p.label,suffix:p.unit,span:6,action:this.value_action}}); })"]
2323
}

pages/product-model.json

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"fields": [
55
{
66
"key": "properties",
7-
"label": "属性表",
7+
"label": "属性",
88
"type": "table",
99
"children": [
1010
{
@@ -58,6 +58,60 @@
5858
}
5959
]
6060
},
61+
{
62+
"key": "actions",
63+
"label": "动作",
64+
"type": "list",
65+
"children": [
66+
{
67+
"key": "name",
68+
"label": "名称",
69+
"type": "text"
70+
},
71+
{
72+
"key": "label",
73+
"label": "显示",
74+
"type": "text"
75+
},
76+
{
77+
"key": "parameters",
78+
"type": "table",
79+
"label": "参数",
80+
"children": [
81+
{
82+
"key": "key",
83+
"label": "变量",
84+
"type": "text"
85+
},
86+
{
87+
"key": "label",
88+
"label": "显示",
89+
"type": "text"
90+
},
91+
{
92+
"key": "type",
93+
"label": "类型",
94+
"type": "select",
95+
"default": "number",
96+
"options": [
97+
{
98+
"label": "数值",
99+
"value": "number"
100+
},
101+
{
102+
"label": "布尔",
103+
"value": "switch"
104+
},
105+
{
106+
"label": "字符串",
107+
"value": "text"
108+
}
109+
]
110+
}
111+
]
112+
}
113+
]
114+
},
61115
{
62116
"key": "validators",
63117
"label": "属性检查",

product/model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ type Property struct {
2020
}
2121

2222
type Parameter struct {
23-
Name string `json:"name,omitempty"`
24-
Type string `json:"type,omitempty"`
23+
Key string `json:"key,omitempty"`
24+
Label string `json:"label,omitempty"`
25+
Type string `json:"type,omitempty"`
2526
}
2627

2728
type Event struct {
@@ -32,6 +33,7 @@ type Event struct {
3233

3334
type Action struct {
3435
Name string `json:"name,omitempty"`
36+
Label string `json:"label,omitempty"`
3537
Description string `json:"description,omitempty"`
3638
Parameters []Parameter `json:"parameters,omitempty"`
3739
Returns []Parameter `json:"returns,omitempty"`

protocol/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ type ActionRequest struct {
5151
MsgId string `json:"msg_id,omitempty"`
5252
DeviceId string `json:"device_id"`
5353
Action string `json:"action"`
54-
Parameters map[string]any `json:"parameters"`
54+
Parameters map[string]any `json:"parameters,omitempty"`
5555
}
5656

5757
type ActionResponse struct {
5858
MsgId string `json:"msg_id"`
5959
Error string `json:"error,omitempty"`
6060
DeviceId string `json:"device_id"`
61-
Result map[string]any `json:"result"`
61+
Result map[string]any `json:"result,omitempty"`
6262
}

0 commit comments

Comments
 (0)