-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
115 lines (104 loc) · 2.79 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"strconv"
)
var port = flag.Int("port", 80, "port for web interface")
func intData(w http.ResponseWriter, req *http.Request, arg string, def int64) (int64, error) {
valStr := req.FormValue(arg)
if valStr == "" {
return def, nil
}
val, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
http.Error(
w, fmt.Sprintf("could not parse %s: %v", arg, err),
http.StatusBadRequest)
return 0, err
}
return val, nil
}
func floatData(w http.ResponseWriter, req *http.Request, arg string) (float64, error) {
valStr := req.FormValue(arg)
if valStr == "" {
http.Error(
w, fmt.Sprintf("missing argument %s", arg), http.StatusBadRequest)
return 0, errors.New("argument not supplied in request")
}
val, err := strconv.ParseFloat(valStr, 64)
if err != nil {
http.Error(
w, fmt.Sprintf("could not parse %s: %v", arg, err),
http.StatusBadRequest)
return 0, err
}
return val, nil
}
func (s *SousVide) StartServer() {
http.HandleFunc("/api_data", func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-type", "application/json")
if len(s.History) == 0 {
resp.WriteHeader(http.StatusNoContent)
return
}
b, err := json.Marshal(s.History[len(s.History)-1])
if err != nil {
log.Panicf("could not marshal temp data to json: %v", err)
}
resp.Write(b)
})
http.HandleFunc("/params", func(resp http.ResponseWriter, req *http.Request) {
s.DataLock.Lock()
defer s.DataLock.Unlock()
t, err := floatData(resp, req, "target")
if err != nil {
t = float64(s.Target)
}
p, err := floatData(resp, req, "p")
if err != nil {
p = s.Pid.P
}
i, err := floatData(resp, req, "i")
if err != nil {
i = s.Pid.I
}
d, err := floatData(resp, req, "d")
if err != nil {
d = s.Pid.D
}
s.Pid.P = p
s.Pid.I = i
s.Pid.D = d
s.Target = Celsius(t)
s.checkpoint()
s.SavePid()
log.Printf("new pid parameters p=%f i=%f d=%f", p, i, d);
resp.Write([]byte("success"));
})
http.HandleFunc("/enable", func(w http.ResponseWriter, r *http.Request) {
s.Enabled = true
log.Printf("set enabled to %v", s.Enabled)
w.Write([]byte("success"))
})
http.HandleFunc("/disable", func(w http.ResponseWriter, r *http.Request) {
s.Enabled = false
log.Printf("set enabled to %v", s.Enabled)
w.Write([]byte("success"))
})
http.HandleFunc("/csv", func(w http.ResponseWriter, r *http.Request) {
s.DumpCsv(w, r)
})
http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
s.DumpJson(w, r)
})
http.HandleFunc("/timer", AddTimerHandler)
http.HandleFunc("/timers", GetTimersHandler)
http.HandleFunc("/delete_timer", DeleteTimerHandler)
http.Handle("/", http.FileServer(http.Dir("static/")))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}