@@ -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
1417func 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+ }
0 commit comments