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
|
package main
import (
"fmt"
"github.com/sam-rba/share"
"html/template"
"log"
"net/http"
)
const dashboardHtml = `
<!DOCTYPE html>
<html>
<head>
<title>HVAC Dashboard</title>
</head>
<body>
<p>Average humidity:
{{/* A value less than 0 means no data. */}}
{{- if ge .Average 0.0 -}}
{{ printf "%.1f%%" .Average }}
{{- else -}}
unknown
{{- end -}}
</p>
<p>Duty cycle:
{{/* A value less than 0 means no data. */}}
{{- if ge .DutyCycle 0.0 -}}
{{ printf "%.1f%%" .DutyCycle }}
{{- else -}}
unknown
{{- end -}}
</p>
<table>
<tr><th>Room</th><th>Humidity</th></tr>
{{ range $id, $humidity := .Rooms }}
<tr>
<td>{{ $id }}</td>
<td>
{{/* A value less than 0 means no data. */}}
{{- if ge $humidity 0.0 -}}
{{ printf "%.1f%%" $humidity }}
{{- else -}}
unknown
{{- end -}}
</td>
</tr>
{{ end }}
</table>
</body>
</html>`
var dashboard = template.Must(template.New("dashboard").Parse(dashboardHtml))
type Dashboard struct {
Average Humidity
DutyCycle DutyCycle
Rooms map[RoomID]Humidity
}
type DashboardHandler struct {
building Building
dutyCycle share.Val[DutyCycle]
}
func (h DashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL)
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "invalid method: '%s'", r.Method)
return
}
db := h.buildDashboard()
err := dashboard.Execute(w, db)
if err != nil {
log.Println(err)
}
}
func (h DashboardHandler) buildDashboard() Dashboard {
average, ok := h.building.average()
if !ok {
average = -1
}
var duty DutyCycle
if dutyp, ok := h.dutyCycle.TryGet(); ok {
duty = *dutyp
} else {
duty = -1
}
rooms := make(map[RoomID]Humidity)
for id, record := range h.building {
c := make(chan Humidity)
record.getRecent <- c
humidity, ok := <-c
if !ok {
humidity = -1
}
rooms[id] = humidity
}
return Dashboard{average, duty, rooms}
}
|