diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-21 17:53:22 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-21 17:53:22 -0500 |
| commit | 460093026a28744acf24cd176f83674d5ead63eb (patch) | |
| tree | a66c9406a0cd45176713305eb9d45d31e4c0b828 /server | |
| parent | e9ebb8126224353c7090889c38e49415497a53cd (diff) | |
| download | soen422-460093026a28744acf24cd176f83674d5ead63eb.zip | |
server dashboard: display duty cycle
Diffstat (limited to 'server')
| -rw-r--r-- | server/dashboard.go | 37 | ||||
| -rw-r--r-- | server/duty.go | 11 | ||||
| -rw-r--r-- | server/server.go | 7 |
3 files changed, 37 insertions, 18 deletions
diff --git a/server/dashboard.go b/server/dashboard.go index abe68f1..df71edb 100644 --- a/server/dashboard.go +++ b/server/dashboard.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/sam-rba/share" "html/template" "log" "net/http" @@ -20,7 +21,16 @@ const dashboardHtml = ` {{ printf "%.1f%%" .Average }} {{- else -}} unknown - {{- end }}</p> + {{- 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 }} @@ -43,12 +53,14 @@ const dashboardHtml = ` var dashboard = template.Must(template.New("dashboard").Parse(dashboardHtml)) type Dashboard struct { - Average Humidity - Rooms map[RoomID]Humidity + Average Humidity + DutyCycle DutyCycle + Rooms map[RoomID]Humidity } type DashboardHandler struct { - Building + building Building + dutyCycle share.Val[DutyCycle] } func (h DashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -60,21 +72,28 @@ func (h DashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - db := newDashboard(h.Building) + db := h.buildDashboard() err := dashboard.Execute(w, db) if err != nil { log.Println(err) } } -func newDashboard(b Building) Dashboard { - average, ok := b.average() +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 b { + for id, record := range h.building { c := make(chan Humidity) record.getRecent <- c humidity, ok := <-c @@ -84,5 +103,5 @@ func newDashboard(b Building) Dashboard { rooms[id] = humidity } - return Dashboard{average, rooms} + return Dashboard{average, duty, rooms} } diff --git a/server/duty.go b/server/duty.go index ae32ecb..82bf60f 100644 --- a/server/duty.go +++ b/server/duty.go @@ -2,20 +2,19 @@ package main import ( "fmt" + "github.com/sam-rba/share" "log" "net/http" "strconv" - "sync" ) type DutyCycle float32 type DutyCycleHandler struct { - mu sync.Mutex - dc DutyCycle + dc share.Val[DutyCycle] } -func (h *DutyCycleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h DutyCycleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Println(r.Method, r.URL) if r.Method != http.MethodPost { @@ -30,7 +29,5 @@ func (h *DutyCycleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - h.mu.Lock() - defer h.mu.Unlock() - h.dc = DutyCycle(dc) + h.dc.Set <- DutyCycle(dc) } diff --git a/server/server.go b/server/server.go index 7d5e977..b104bbb 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/sam-rba/share" "log" "net/http" ) @@ -17,12 +18,14 @@ type RoomID string func main() { building := newBuilding(roomIDs) + dutyCycle := share.NewVal[DutyCycle]() defer building.Close() + defer dutyCycle.Close() - http.Handle("/", DashboardHandler{building}) + http.Handle("/", DashboardHandler{building, dutyCycle}) http.Handle("/humidity", HumidityHandler{building}) http.Handle("/target_humidity", new(TargetHumidityHandler)) - http.Handle("/duty_cycle", new(DutyCycleHandler)) + http.Handle("/duty_cycle", DutyCycleHandler{dutyCycle}) fmt.Printf("Listening on %s...\n", addr) log.Fatal(http.ListenAndServe(addr, nil)) |