summaryrefslogtreecommitdiffstats
path: root/server/dashboard.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-21 16:47:08 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-21 16:47:08 -0500
commitdbc71139a5708b371ffb0b7580f562674707c558 (patch)
tree1ee13dbfa44588a723a0d1fdf4eb24f5d76a9158 /server/dashboard.go
parenta464e144485f69fd9ae6efee3e3fe5e00590e6de (diff)
downloadsoen422-dbc71139a5708b371ffb0b7580f562674707c558.zip
server dashboard
Diffstat (limited to 'server/dashboard.go')
-rw-r--r--server/dashboard.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/server/dashboard.go b/server/dashboard.go
new file mode 100644
index 0000000..0e9cd41
--- /dev/null
+++ b/server/dashboard.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+ "fmt"
+ "html/template"
+ "log"
+ "net/http"
+)
+
+const dashboardHtml = `
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>HVAC Dashboard</title>
+ </head>
+ <body>
+ <p>Average humidity: {{ printf "%.1f %%" .Average }}</p>
+ <table>
+ <tr><th>Room</th><th>Humidity</th></tr>
+ {{ range .Rooms }}
+ <tr><td>{{ .RoomID }}</td><td>{{ printf "%.1f %%" .Humidity }}</td></tr>
+ {{ end }}
+ </table>
+ </body>
+</html>`
+
+var dashboard = template.Must(template.New("dashboard").Parse(dashboardHtml))
+
+type Dashboard struct {
+ Average Humidity
+ Rooms []Room
+}
+
+type Room struct {
+ RoomID
+ Humidity
+}
+
+type DashboardHandler struct {
+ Building
+}
+
+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 := newDashboard(h.Building)
+ err := dashboard.Execute(w, db)
+ if err != nil {
+ log.Println(err)
+ }
+}
+
+func newDashboard(b Building) Dashboard {
+ average, ok := b.average()
+ if !ok {
+ average = -1
+ }
+
+ // TODO: sort by room ID.
+ rooms := make([]Room, 0, len(b))
+ for id, record := range b {
+ c := make(chan Humidity)
+ record.getRecent <- c
+ humidity, ok := <-c
+ if !ok {
+ humidity = -1
+ }
+ rooms = append(rooms, Room{id, humidity})
+ }
+
+ return Dashboard{average, rooms}
+}