-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
557 lines (506 loc) · 16.6 KB
/
server.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
qrcode "github.com/skip2/go-qrcode"
)
// Handler will handle the http requests
type Handler struct {
ppc *PoolPumpController
}
// HostType is used to specify how to listen
type HostType uint8
const (
// LocalHost is 127.0.0.1
LocalHost HostType = iota
// AnyHost is 0.0.0.0
AnyHost
)
func (h HostType) String() string {
switch h {
case LocalHost:
return "127.0.0.1"
case AnyHost:
return "0.0.0.0"
}
return ""
}
// Server for the pool-controller
type Server struct {
port int
host HostType
handler *Handler
server http.Server
done chan bool
}
// NewServer creates a webserver
func NewServer(host HostType, port int, ppc *PoolPumpController) *Server {
s := Server{
port: port,
host: host,
handler: &Handler{
ppc: ppc,
},
done: make(chan bool),
}
addr := fmt.Sprintf("%s:%d", host, port)
Info("Creating server on %s", addr)
s.server = http.Server{
Addr: addr,
Handler: s.handler,
}
s.server.ErrorLog = NewLogger() // Direct errors to common log
return &s
}
func startServer(s *Server, cert, key string) {
err := s.server.ListenAndServeTLS(cert, key)
if err != nil {
Fatal("Error from Server: %s", err.Error())
}
s.done <- true
Info("Exiting HttpServer")
}
// Start the server
func (s *Server) Start(cert, key string) {
go startServer(s, cert, key)
Info("Starting HTTPS on %s:%d", s.host, s.port)
}
// Stop takes down the server
func (s *Server) Stop() {
interval := time.Second
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := s.server.Shutdown(ctx)
if err != nil {
Info("HttpServerShutdown: %s", err.Error())
}
for {
select {
case <-s.done:
return
case <-time.After(interval):
Info("Waiting for HttpServer to shut down")
}
}
}
const (
// PumpImage is the pump data graph
PumpImage = 0
// TempImage is the temperature data graph
TempImage = 1
)
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
Debug("Received: %s", r.URL)
switch r.URL.Path {
case "/":
h.rootHandler(w, r)
return
case "/pair":
h.pairHandler(w, r)
return
case "/qr":
h.qrHandler(w, r)
return
case "/pumps":
h.graphHandler(w, r, PumpImage)
return
case "/temps":
h.graphHandler(w, r, TempImage)
return
case "/config":
h.configHandler(w, r)
return
case "/runCalibration":
h.runCalibrationHandler(w, r)
return
case "/calibrate":
h.calibrateHandler(w, r)
return
default:
http.Error(w, "Unknown request type", 404)
}
}
func (h *Handler) setRefresh(w http.ResponseWriter, r *http.Request, seconds int) {
refresh := fmt.Sprintf("%d; url=%s", seconds, r.RequestURI)
Debug("Setting Refresh to: %s", refresh)
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Refresh", refresh)
}
func (h *Handler) writeResponse(w http.ResponseWriter, content []byte, ctype string) {
w.Header().Set("Content-Type", ctype)
w.WriteHeader(http.StatusOK)
w.Write(content)
}
func getscale(r *http.Request) string {
scale := ""
cookie, _ := r.Cookie("scale")
if cookie != nil {
scale = cookie.Value
}
return getFormValue(r, "scale", scale)
}
func duration(r *http.Request) time.Duration {
var num int
var let string
scale := getscale(r)
day := time.Hour * 24
if len(scale) > 1 {
fmt.Sscanf(scale, "%d%s", &num, &let)
d := time.Duration(num)
switch let {
case "m":
return d * time.Minute
case "h":
return d * time.Hour
case "d":
return d * day
case "w":
return d * 7 * day
default:
}
}
return day
}
func getFormValue(r *http.Request, name, defaultValue string) string {
value := r.FormValue(name)
if value == "" {
return defaultValue
}
return value
}
func (h *Handler) graphHandler(w http.ResponseWriter, r *http.Request, which int) {
var err error
var graph []byte
end := time.Now()
start := end.Add(-1 * duration(r))
width, _ := strconv.ParseUint(getFormValue(r, "width", "640"), 10, 32)
height, _ := strconv.ParseUint(getFormValue(r, "height", "300"), 10, 32)
if which == PumpImage {
h.ppc.pumpRrd.Grapher().SetSize(uint(width), uint(height))
_, graph, err = h.ppc.pumpRrd.Grapher().Graph(start, end)
} else if which == TempImage {
h.ppc.tempRrd.Grapher().SetSize(uint(width), uint(height))
_, graph, err = h.ppc.tempRrd.Grapher().Graph(start, end)
} else {
http.Error(w, "Unknown Graph", 404)
return
}
if err != nil {
Error("Could not produce graph: %s", err.Error())
}
h.setRefresh(w, r, 20) // Refresh image every 20 seconds
h.writeResponse(w, graph, "image/png")
}
func image(which string, width, height int, scale string) string {
return fmt.Sprintf("<img src=\"/%s?scale=%s&width=%d&height=%d\" width=%d height=%d "+
"alt=\"Temperatures and Solar Radiation\" />",
which, scale, width, height, width, height)
}
func indent(howmany int) string {
out := ""
for i := 0; i < howmany; i++ {
out += "\t"
}
return out
}
func (h *Handler) pin() string {
var p1, p2, p3 string
fmt.Sscanf(h.ppc.config.cfg.Pin, "%3s%2s%3s", &p1, &p2, &p3)
return fmt.Sprintf("%3s-%2s-%3s", p1, p2, p3)
}
func (h *Handler) pairHandler(w http.ResponseWriter, r *http.Request) {
html := "<html><head><title>HomeKit Pairing Codes</title></head><body><center>"
html += "<table><tr><th>" + h.pin() + "</th></tr>"
html += "<tr><td><img src=\"/qr\"></td></tr></table>"
html += nav()
html += "</center></body></html>"
h.writeResponse(w, []byte(html), "text/html")
}
func (h *Handler) qrHandler(w http.ResponseWriter, r *http.Request) {
png, _ := qrcode.Encode(h.ppc.config.cfg.Pin, qrcode.Medium, 256)
h.writeResponse(w, []byte(png), "image/png")
}
func nav() string {
out := "<p><font face=helvetica color=#444444 size=-2>"
out += "<table cellspacing=5><tr><td><a href=/>graphs</a></td><td> </td>\n"
out += "<td><a href=/pair>homekit</a></td><td> </td>\n"
out += "<td><a href=/calibrate>calibrate</a></td><td> </td>\n"
out += "<td><a href=/config>config</a></td></tr></table></font>\n"
return out
}
// TODO: update ui to set runtime and frequency
func (h *Handler) rootHandler(w http.ResponseWriter, r *http.Request) {
scale := getscale(r)
cookie := &http.Cookie{
Name: "scale",
Value: scale,
MaxAge: int(365 * 24 * time.Hour / time.Second),
}
http.SetCookie(w, cookie)
h.setRefresh(w, r, 60)
modeStr := "Auto"
if h.ppc.switches.ManualState(h.ppc.config.cfg.RunTime) {
modeStr = "Manual"
}
html := "<html><head><title>Pool Pump Controller</title></head><body><center>" +
"<table>\n"
html += indent(1) + "<tr><td colspan=2 align=center><font face=helvetica color=#444444 " +
"size=-1><form action=/ method=POST>Time Window:<input name=scale value=\"" +
scale + "\" size=5> ex. 12h (w, d, h, m)</form></font></td></tr>\n"
html += indent(1) + "<tr><td>" + image("temps", 640, 300, scale) + "</td>"
html += "<td align=left nowrap><font face=helvetica color=#444444 size=-1>"
html += fmt.Sprintf("Target: %0.1f F<br>", toFarenheit(h.ppc.config.cfg.Target))
html += fmt.Sprintf("Pool: %0.1f F<br>", toFarenheit(h.ppc.runningTemp.Temperature()))
html += fmt.Sprintf("Roof: %0.1f F<br>", toFarenheit(h.ppc.roofTemp.Temperature()))
html += "</font></td></tr>\n"
html += indent(1) + "<tr><td colspan=2><br></td></tr>"
html += indent(1) + "<tr>"
html += "<td>" + image("pumps", 640, 200, scale) + "</td>"
html += "<td align=left nowrap><font face=helvetica color=#444444 size=-1>"
html += fmt.Sprintf("Pump: %s<br>", h.ppc.switches.State())
html += fmt.Sprintf("Solar: %s<br>", h.ppc.switches.solar.Status())
html += fmt.Sprintf("Mode: %s", modeStr)
html += "</font></td></tr>\n"
html += indent(1) + "<tr><td align=center><font size=-1 color=#aaaaaa>" +
"4=SolarMixing, 3=SolarHeating, 2=Cleaning, 1=PumpRunning, 0=Off, " +
"-1=Disabled</font></td><td></td></tr>\n"
html += "<tr><td colspan=2><br></td></tr>\n"
html += indent(1) + "<tr><td align=center>" +
fmt.Sprintf("Updated: %.19s", time.Now().String()) +
"</td><td></td></tr>\n"
html += "<tr><td align=center>" + nav() + "</td><td></td></tr>\n"
html += "</table></font>"
html += "</center></body></html>"
h.writeResponse(w, []byte(html), "text/html")
}
func (h *Handler) calibrateHandler(w http.ResponseWriter, r *http.Request) {
html := "<html><head><title>Thermometer Calibration</title></head><body><center>"
html += `<font face=helvetica color=#444444 size=-1>To calibrate your system, please
insert resistors of known value across the terminals for BOTH temperature probes.
<b>Suggested value is 10,000Ohms.</b>, but you can measure it for increased
accuracy.</font><br>`
html += "<table><form action=/runCalibration method=POST>\n"
html += "<tr><td align=right><font face=helvetica color=#444444 size=-1>Pump Resistor Value</td>"
html += "<td><input name=pump_res value=10000 size=5></font> ohms</td></tr>\n"
html += "<tr><td align=right><font face=helvetica color=#444444 size=-1>Roof Resistor Value</td>"
html += "<td><input name=roof_res value=10000 size=5></font> ohms</td></tr>\n"
html += "<tr><td colspan=2 align=center><input type=submit name=submit value=Run Calibration></td></tr>\n"
html += "<tr><td colspan=2 align=center>" + nav() + "</td></tr>\n"
html += "</form></table></font></center></body></html>"
h.writeResponse(w, []byte(html), "text/html")
}
// Calibrate runs a routine to calibrate the thermometers using measured resistors
func (h *Handler) Calibrate(html *string, t Thermometer, resStr, name string) error {
r, err := strconv.ParseFloat(resStr, 64)
if err != nil {
*html += "<h2>Could not parse " + resStr + "for: " + name
*html += ", please correct the value.</h2><br>(" + err.Error() + ")"
return err
}
err = t.Calibrate(r)
if err != nil {
*html += "<h2>Calibration failed, please try again.</h2><br>(" + err.Error() + ")"
return err
}
return nil
}
func (h *Handler) runCalibrationHandler(w http.ResponseWriter, r *http.Request) {
pumpResistance := getFormValue(r, "pump_res", "")
roofResistance := getFormValue(r, "roof_res", "")
html := "<html><head><title>Thermometer Calibration</title></head><body><center>"
retry := http.Request{
URL: &url.URL{
RawPath: "/calibrate",
},
}
success := http.Request{
URL: &url.URL{
RawPath: "/",
},
}
if pumpResistance == "" || roofResistance == "" { // No values submitted
h.setRefresh(w, &retry, 10)
html += "<h2>Please provide valid resistance for each resistor.</h2> Redirecting..."
} else {
if h.Calibrate(&html, h.ppc.pumpTemp, pumpResistance, "Pump Probe") == nil &&
h.Calibrate(&html, h.ppc.roofTemp, roofResistance, "Roof Probe") == nil {
h.setRefresh(w, &success, 10)
h.ppc.PersistCalibration()
html += "<h2>Success</h2><br>"
p, ok := h.ppc.pumpTemp.(*GpioThermometer)
if ok {
html += fmt.Sprintf("<br>Pool Value: %0.3f", p.adjust)
}
p, ok = h.ppc.roofTemp.(*GpioThermometer)
if ok {
html += fmt.Sprintf("<br>Roof Value: %0.3f", p.adjust)
}
} else {
html += "<p>Redirecting...."
h.setRefresh(w, &retry, 10)
}
}
html += "</body></html>"
h.writeResponse(w, []byte(html), "text/html")
}
// Authenticate the user
func (h *Handler) Authenticate(r *http.Request) bool {
user, password, ok := r.BasicAuth()
if !ok || user != "admin" {
Error("Unknown user (%s) attempting to configure server", user)
return false
}
if h.ppc.config.Authorized(password) {
Debug("User %s logged in", user)
return true
}
Error("Login for User (%s) failed", user)
return false
}
func processBoolUpdate(r *http.Request, formname string, ptr *bool) bool {
value := false
strvalue := getFormValue(r, formname, "false")
Log("Update to boolean: %q = %q, was %t", formname, strvalue, *ptr)
if strvalue == "true" {
value = true
}
if value != *ptr {
Debug("Updating value for %s from %t to %t", formname, *ptr, value)
*ptr = value
return true
}
Debug("No update to %s, value(%t) orig(%t)", formname, value, *ptr)
return false
}
func processFloatUpdate(r *http.Request, formname string, ptr *float64) bool {
curvalue := fmt.Sprintf("%0.2f", *ptr)
value := getFormValue(r, formname, "")
if value != curvalue {
flt, err := strconv.ParseFloat(value, 64)
if err == nil {
Debug("Updating value for %s from %s to %s", formname, curvalue, value)
*ptr = flt
return true
}
}
Debug("No update to %s, value(%s) orig(%s)", formname, value, curvalue)
return false
}
func (h *Handler) configBoolRow(name, inputName string, value bool) string {
checkbox := "type=checkbox value=true"
if value {
checkbox += " checked"
}
return h.configRow(name, inputName, "", checkbox)
}
func (h *Handler) configRow(name, inputName, configValue, extraArgs string) string {
return fmt.Sprintf(
"<tr><td align=right>%s:</td><td><font size=-1><input name=\"%s\" size=20 %s></font></td><td>%s</td></tr>\n",
name, inputName, extraArgs, configValue)
}
func (h *Handler) processForm(r *http.Request, c *Config) {
var foundone bool
pw := getFormValue(r, "passcode", "")
if pw1 := getFormValue(r, "passcode2", ""); pw != "" && pw1 != "" && pw == pw1 {
c.SetAuth(pw1)
foundone = true
}
if processFloatUpdate(r, "adj_pump", &c.cfg.PumpAdjustment) {
h.ppc.SyncAdjustments()
foundone = true
}
if processFloatUpdate(r, "adj_roof", &c.cfg.RoofAdjustment) {
h.ppc.SyncAdjustments()
foundone = true
}
if processFloatUpdate(r, "target", &c.cfg.Target) {
foundone = true
}
if processFloatUpdate(r, "tolerance", &c.cfg.Tolerance) {
foundone = true
}
if processFloatUpdate(r, "mindelta", &c.cfg.DeltaT) {
foundone = true
}
if processBoolUpdate(r, "disabled", &c.cfg.Disabled) {
foundone = true
}
if processBoolUpdate(r, "button_disabled", &c.cfg.ButtonDisabled) {
foundone = true
}
if processBoolUpdate(r, "solar_disabled", &c.cfg.SolarDisabled) {
foundone = true
}
if processFloatUpdate(r, "daily_freq", &c.cfg.DailyFrequency) {
foundone = true
}
if processFloatUpdate(r, "run_time", &c.cfg.RunTime) {
foundone = true
}
if foundone {
c.Save()
}
// Don't persist this one
posted := getFormValue(r, "posted", "")
if posted == "true" { // only change on form submission
value := getFormValue(r, "debug", "")
if value == "on" {
EnableDebug()
Debug("Enabling Debug: value(%s) posted(%s)", value, posted)
} else {
Debug("Disabling Debug: value(%s) posted(%s)", value, posted)
DisableDebug()
}
}
}
func (h *Handler) configHandler(w http.ResponseWriter, r *http.Request) {
// TODO: move this to a form on the page.
w.Header().Set("WWW-Authenticate", "Basic") // realm=\"Bonnie Labs\"
if !h.Authenticate(r) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
c := h.ppc.config
Debug("Config: %+v", c.cfg)
posted := getFormValue(r, "posted", "")
if posted == "true" {
h.processForm(r, c)
}
passArgs := " type=\"password\" autocomplete=\"new-password\""
html := "<html><head><title>Pool Controller Configuration</title></head><body>"
html += "<center><font face=helvetica color=#444444>Pool Controller Configuration"
html += "<font size=-1>\n"
html += "<table border=0 cellpadding=3>\n"
html += "<form action=/config method=POST>\n"
html += "<tr><th align=left>Administrator:</th><td colspan=3></td></tr>\n"
html += h.configRow("Admin Password", "passcode", "", passArgs)
html += h.configRow("Confirm Password", "passcode2", "", passArgs)
html += "<tr><td colspan=3><br></td></tr>\n"
html += "<tr><th align=left>Temperature Sensor Adjustment:</th><td colspan=3></td></tr>\n"
html += h.configRow("Pump Tuning", "adj_pump", fmt.Sprintf("%0.2f", c.cfg.PumpAdjustment), "")
html += h.configRow("Roof Tuning", "adj_roof", fmt.Sprintf("%0.2f", c.cfg.RoofAdjustment), "")
html += "<tr><td colspan=3><br></td></tr>\n"
html += "<tr><th align=left>Solar Settings:</th><td colspan=3></td></tr>\n"
html += h.configRow("Target", "target", fmt.Sprintf("%0.2f°C", c.cfg.Target), "")
html += h.configRow("Tolerance", "tolerance", fmt.Sprintf("%0.2f°C", c.cfg.Tolerance), "")
html += h.configRow("MinDelta", "mindelta", fmt.Sprintf("%0.2f°C", c.cfg.DeltaT), "")
html += "<tr><td colspan=3><br></td></tr>\n"
html += h.configRow("Daily Run Frequency", "daily_freq", fmt.Sprintf("%0.2f Days", c.cfg.DailyFrequency), "")
html += h.configRow("Run period", "run_time", fmt.Sprintf("%0.2f hours", c.cfg.RunTime), "")
html += "<tr><td colspan=3><br></td></tr>\n"
html += "<tr><th align=left>Debug Settings:</th><td colspan=3></td></tr>\n"
html += h.configBoolRow("Debug Logging Enabled", "debug", doDebug)
html += h.configBoolRow("Disable all pumps", "disabled", c.cfg.Disabled)
html += h.configBoolRow("Disable button", "button_disabled", c.cfg.ButtonDisabled)
html += h.configBoolRow("Disable solar", "solar_disabled", c.cfg.SolarDisabled)
html += "<input type=hidden name=posted value=true>\n"
html += "</table><input type=submit value=Save></font></font></form>\n"
html += nav()
html += "</center></body></html>\n"
h.writeResponse(w, []byte(html), "text/html")
}