This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
targets.go
212 lines (177 loc) · 4.65 KB
/
targets.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
package serving
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/iot-for-all/starling/pkg/models"
"github.com/iot-for-all/starling/pkg/storing"
"io/ioutil"
"net/http"
)
// listTargets lists all targets.
func listTargets(w http.ResponseWriter, _ *http.Request) {
items, err := storing.Targets.List()
if handleError(err, w) {
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(items)
handleError(err, w)
}
// listTargetDevicesByTarget lists all devices in a target.
func listTargetDevices(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
items, err := storing.TargetDevices.List(id)
if handleError(err, w) {
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(items)
handleError(err, w)
}
// getTarget gets a target by its id.
func getTarget(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
t, err := storing.Targets.Get(id)
if handleError(err, w) {
return
}
if t == nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(t)
handleError(err, w)
}
// listTargetModels lists all models for a target.
func getTargetModels(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
m, err := storing.TargetModels.Get(id)
if handleError(err, w) {
return
}
if m == nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(m.Models)
handleError(err, w)
}
// getTargetDevice gets a device in a target by its target id and deviceId
func getTargetDevice(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
deviceId := vars["deviceId"]
d, err := storing.TargetDevices.Get(id, deviceId)
if handleError(err, w) {
return
}
if d == nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(d)
handleError(err, w)
}
// upsertTarget adds a new or updates an existing target
func upsertTarget(w http.ResponseWriter, r *http.Request) {
req, err := ioutil.ReadAll(r.Body)
if handleError(err, w) {
return
}
var t models.SimulationTarget
err = json.Unmarshal(req, &t)
if handleError(err, w) {
return
}
upsertTargetInternal(w, r, t)
}
func upsertTargetInternal(w http.ResponseWriter, r *http.Request, t models.SimulationTarget) {
err := storing.Targets.Set(&t)
if handleError(err, w) {
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&t)
handleError(err, w)
}
// upsertTargetModels adds or updates existing models for a target.
func upsertTargetModels(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
req, err := ioutil.ReadAll(r.Body)
if handleError(err, w) {
return
}
var m []string
err = json.Unmarshal(req, &m)
if handleError(err, w) {
return
}
tm := models.SimulationTargetModels{
TargetID: id,
Models: m,
}
err = storing.TargetModels.Set(&tm)
handleError(err, w)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&tm)
handleError(err, w)
}
// upsertTargetDevice adds or updates existing device in a target.
func upsertTargetDevice(w http.ResponseWriter, r *http.Request) {
req, err := ioutil.ReadAll(r.Body)
if handleError(err, w) {
return
}
var d models.SimulationTargetDevice
err = json.Unmarshal(req, &d)
if handleError(err, w) {
return
}
err = storing.TargetDevices.Set(&d)
if handleError(err, w) {
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&d)
handleError(err, w)
}
// deleteTarget deletes an existing target
func deleteTarget(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
err := storing.Targets.Delete(id)
if handleError(err, w) {
return
}
err = storing.TargetModels.Delete(id)
handleError(err, w)
}
// deleteTargetModels deletes existing target models
func deleteTargetModels(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
err := storing.TargetModels.Delete(id)
handleError(err, w)
}
// deleteTargetDevice deletes existing target device
func deleteTargetDevice(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
deviceId := vars["deviceId"]
err := storing.TargetDevices.Delete(id, deviceId)
handleError(err, w)
}
// deleteAllTargetDevices deletes all devices from the target
func deleteAllTargetDevices(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
err := storing.TargetDevices.DeleteAll(id)
handleError(err, w)
}