-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
272 lines (235 loc) · 6.38 KB
/
handlers.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"sync"
"time"
"html/template"
"path"
)
type SensorData struct {
Location string `json:"location"`
Serial_Number int `json:"serial_number"`
Temperature float64 `json:"temperature"`
Pressure float64 `json:"pressure"`
Humidity float64 `json:"humidity"`
Date_Time time.Time `json:"datetime"`
Water_Level float64 `json:"water_level"`
}
type MetricLimits struct {
TempMax float64
TempMin float64
HumMax float64
HumMin float64
PresMax float64
WLevelMax float64
}
type FaultEntry struct {
DataEntry *SensorData
FaultMessages []string
}
type Impl struct {
sd_table map[string]SensorData
faults map[string][]*FaultEntry
limits MetricLimits
mu sync.Mutex
}
func InitGlobal() *Impl {
var i Impl
i.sd_table = make(map[string]SensorData)
i.faults = make(map[string][]*FaultEntry)
i.limits = MetricLimits{30.0, 20.0, 20, 0.0, 1.1, 10}
return &i
}
func checkForFaults(sd SensorData, lim MetricLimits) *FaultEntry {
var msgs []string
if sd.Temperature < lim.TempMin || lim.TempMax < sd.Temperature {
msgs = append(msgs, "Temperature exceeded threshold!")
}
if sd.Humidity < lim.HumMin || lim.HumMax < sd.Humidity {
msgs = append(msgs, "Humidity exceeded threshold!")
}
if sd.Pressure > lim.PresMax {
msgs = append(msgs, "Pressure exceeded threshold!")
}
if sd.Water_Level > lim.WLevelMax {
msgs = append(msgs, "Water level exceeded threshold!")
}
if len(msgs) == 0 {
return nil
}
return &FaultEntry{&sd, msgs}
}
/***
______ _ _____ _
| ___| | | | ___| | |
| |_ _ __ ___ _ __ | |_ | |__ _ __ __| |
| _| '__/ _ \| '_ \| __| | __| '_ \ / _` |
| | | | | (_) | | | | |_ | |__| | | | (_| |
\_| |_| \___/|_| |_|\__| \____|_| |_|\__,_|
***/
func (i *Impl) Index(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
i.mu.Lock()
data := i.sd_table
i.mu.Unlock()
fp := path.Join("templates", "index.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(rw, data); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
type nodedata struct {
SD SensorData
Faults []*FaultEntry
IsFaulting bool
}
func (i *Impl) NodeStatus(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
sensor_id := ps.ByName("sensor_id")
var data nodedata
i.mu.Lock()
if _, ok := i.sd_table[sensor_id]; ok {
data.SD = i.sd_table[sensor_id]
} else {
i.mu.Unlock()
rw.WriteHeader(404)
rw.Write([]byte("Can't find sendor data for the id specified"))
return
}
i.mu.Unlock()
if _, ok := i.faults[sensor_id]; ok {
data.Faults = i.faults[sensor_id]
data.IsFaulting = true
} else {
data.IsFaulting = false
}
//fmt.Printf("decoded to %#v\n", data)
fp := path.Join("templates", "rpi.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(rw, data); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
/***
______ _____ _____ _____ ___ ______ _____
| ___ | ___/ ___|_ _| / _ \| ___ |_ _|
| |_/ | |__ \ `--. | | / /_\ | |_/ / | |
| /| __| `--. \ | | | _ | __/ | |
| |\ \| |___/\__/ / | | | | | | | _| |_
\_| \_\____/\____/ \_/ \_| |_\_| \___/
***/
func (i *Impl) POST_SensorData_ID(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var sd SensorData
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&sd)
if err != nil {
panic(err)
}
fmt.Printf("decoded to %#v\n", sd)
sd.Pressure = sd.Pressure/100000.0
faults := checkForFaults(sd, i.limits)
sensor_id := ps.ByName("sensor_id")
i.mu.Lock()
i.sd_table[sensor_id] = sd
if faults != nil {
i.faults[sensor_id] = append(i.faults[sensor_id], faults)
}
i.mu.Unlock()
}
func (i *Impl) GET_SensorData_ID(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
sensor_id := ps.ByName("sensor_id")
i.mu.Lock()
sd, exist := i.sd_table[sensor_id]
i.mu.Unlock()
if !exist {
rw.WriteHeader(404)
rw.Write([]byte("Can't find sendor data for the id specified"))
return
}
js, err := json.Marshal(sd)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
panic(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func (i *Impl) GET_SensorData(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
i.mu.Lock()
js, err := json.Marshal(i.sd_table)
i.mu.Unlock()
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
panic(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func (i *Impl) GET_Faults_ID(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
sensor_id := ps.ByName("sensor_id")
i.mu.Lock()
fault, ok := i.faults[sensor_id]
if !ok {
rw.WriteHeader(404)
rw.Write([]byte("Can't find fault data for the id specified"))
i.mu.Unlock()
return
}
js, err := json.Marshal(fault[len(fault)-1])
i.mu.Unlock()
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
panic(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func (i *Impl) getLatestFaults() map[string]*FaultEntry {
res := make(map[string]*FaultEntry)
i.mu.Lock()
if len(i.faults) > 0 {
for key, val := range i.faults {
res[key] = val[len(val)-1]
}
}
i.mu.Unlock()
return res
}
func (i *Impl) GET_Faults(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
js, err := json.Marshal(i.getLatestFaults())
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
panic(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func (i *Impl) DELETE_Faults_ID(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
sensor_id := ps.ByName("sensor_id")
i.mu.Lock()
flts, exist := i.faults[sensor_id]
if !exist {
rw.WriteHeader(404)
rw.Write([]byte("Can't find fault data for the id specified"))
i.mu.Unlock()
return
}
js, err := json.Marshal(flts)
delete(i.faults, sensor_id)
i.mu.Unlock()
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
panic(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}