blob: 9a9c0b50e3f0b5908e885794443c0d7c3b94f069 (
plain) (
blame)
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
|
package main
import (
_ "embed"
"fmt"
"github.com/sam-rba/share"
"html/template"
"log"
"net/http"
)
//go:embed dashboard.html
var dashboardHtml string
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}
}
|