summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-21 17:53:22 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-21 17:53:22 -0500
commit460093026a28744acf24cd176f83674d5ead63eb (patch)
treea66c9406a0cd45176713305eb9d45d31e4c0b828
parente9ebb8126224353c7090889c38e49415497a53cd (diff)
downloadsoen422-460093026a28744acf24cd176f83674d5ead63eb.zip
server dashboard: display duty cycle
-rw-r--r--Makefile4
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--server/dashboard.go37
-rw-r--r--server/duty.go11
-rw-r--r--server/server.go7
6 files changed, 46 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index dc7a4ad..6c97dae 100644
--- a/Makefile
+++ b/Makefile
@@ -14,10 +14,10 @@ UPLOADFLAGS = -p ${PORT} ${CFLAGS}
all: SensorStation/build HvacStation/build server/server
server/server: ${SERVER_SRC}
- go build -o $@ $^
+ go build -o $@ ./server
fmt: ${SERVER_SRC}
- gofmt -l -s -w $^
+ gofmt -l -s -w ./server
release:
GOOS=${RELEASE_GOOS} GOARCH=${RELEASE_GOARCH} go build -o server/server ${SERVER_SRC}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..413036e
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module git.samanthony.xyz/hvacserver
+
+go 1.23.1
+
+require github.com/sam-rba/share v0.2.1
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..897bc8b
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/sam-rba/share v0.2.1 h1:cqC+Li72A27hM3E/XdArv2cgbOX//MXutRDg1vzPuZA=
+github.com/sam-rba/share v0.2.1/go.mod h1:rDx/wFG1Vc6JO1F/F0eBnkcy2wHwV0T9100ig/hu12A=
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))