/
deviceaction.go
216 lines (186 loc) · 5.43 KB
/
deviceaction.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
package controllers
import (
"encoding/json"
"github.com/astaxie/beego/validation"
"fmt"
"github.com/e154/smart-home/api/models"
"github.com/e154/smart-home/api/core"
"net/url"
)
// DeviceActionController operations for DeviceAction
type DeviceActionController struct {
CommonController
}
// URLMapping ...
func (c *DeviceActionController) URLMapping() {
c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
c.Mapping("Search", c.Search)
}
// Post ...
// @Title Create
// @Description create DeviceAction
// @Param body body models.DeviceAction true "body for DeviceAction content"
// @Success 201 {object} models.DeviceAction
// @Failure 403 body is empty
// @router / [post]
func (c *DeviceActionController) Post() {
var action models.DeviceAction
json.Unmarshal(c.Ctx.Input.RequestBody, &action)
// validation
valid := validation.Validation{}
b, err := valid.Valid(&action)
if err != nil {
c.ErrHan(403, err.Error())
return
}
if !b {
var msg string
for _, err := range valid.Errors {
msg += fmt.Sprintf("%s: %s\r", err.Key, err.Message)
}
c.ErrHan(403, msg)
return
}
//....
nid, err := models.AddDeviceAction(&action)
if err != nil {
c.ErrHan(403, err.Error())
return
} else {
c.Data["json"] = map[string]interface{}{"id": nid}
}
c.Ctx.Output.SetStatus(201)
c.ServeJSON()
}
// GetOne ...
// @Title GetOne
// @Description get DeviceAction by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.DeviceAction
// @Failure 403 :id is empty
// @router /:id [get]
func (c *DeviceActionController) GetOne() {
id, _ := c.GetInt(":id")
action, err := models.GetDeviceActionById(int64(id))
if err != nil {
c.ErrHan(403, err.Error())
return
}
c.Data["json"] = map[string]interface{}{"action": action}
c.ServeJSON()
}
// GetAll ...
// @Title GetAll
// @Description get DeviceAction
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.DeviceAction
// @Failure 403
// @router / [get]
func (c *DeviceActionController) GetAll() {
ml, meta, err := models.GetAllDeviceAction(c.pagination())
if err != nil {
c.ErrHan(403, err.Error())
return
}
c.Data["json"] = &map[string]interface{}{"actions": ml, "meta": meta}
c.ServeJSON()
}
// Put ...
// @Title Put
// @Description update the DeviceAction
// @Param id path string true "The id you want to update"
// @Param body body models.DeviceAction true "body for DeviceAction content"
// @Success 200 {object} models.DeviceAction
// @Failure 403 :id is not int
// @router /:id [put]
func (c *DeviceActionController) Put() {
id, _ := c.GetInt(":id")
var action models.DeviceAction
json.Unmarshal(c.Ctx.Input.RequestBody, &action)
action.Id = int64(id)
if err := models.UpdateDeviceActionById(&action); err != nil {
c.ErrHan(403, err.Error())
return
}
// restart worker
workers, err := models.GetWorkersByDeviceAction(&action)
for _, worker := range workers {
if err = core.CorePtr().UpdateWorker(worker); err != nil {
c.ErrHan(403, err.Error())
return
}
}
c.ServeJSON()
}
// Delete ...
// @Title Delete
// @Description delete the DeviceAction
// @Param id path string true "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *DeviceActionController) Delete() {
id, _ := c.GetInt(":id")
// restart worker
workers, err := models.GetWorkersByDeviceAction(&models.DeviceAction{Id: int64(id)})
for _, worker := range workers {
if err = core.CorePtr().RemoveWorker(worker); err != nil {
c.ErrHan(403, err.Error())
return
}
if err = models.DeleteWorker(worker.Id); err != nil {
c.ErrHan(403, err.Error())
return
}
}
if err := models.DeleteDeviceAction(int64(id)); err != nil {
c.ErrHan(403, err.Error())
return
}
c.ServeJSON()
}
func (c *DeviceActionController) Search() {
query, fields, sortby, order, offset, limit := c.pagination()
link, _ := url.ParseRequestURI(c.Ctx.Request.URL.String())
q := link.Query()
//TODO fix search by description
if val, ok := q["query"]; ok {
for _, v := range val {
query["name__icontains"] = v
//query["description__icontains"] = v
}
}
ml, meta, err := models.GetAllDeviceAction(query, fields, sortby, order, offset, limit)
if err != nil {
c.ErrHan(403, err.Error())
return
}
c.Data["json"] = &map[string]interface{}{"actions": ml, "meta": meta}
c.ServeJSON()
}
// GetByDevice ...
// @Title GetByDevice
// @Description get DeviceState by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.DeviceState
// @Failure 403 :id is empty
// @router /:id [get]
func (c *DeviceActionController) GetByDevice() {
id, _ := c.GetInt(":id")
device_states, err := models.GetAllDeviceActionByDevice(int64(id))
if err != nil {
c.ErrHan(403, err.Error())
return
}
c.Data["json"] = map[string]interface{}{"device_actions": device_states}
c.ServeJSON()
}