summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-07 18:33:17 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-07 18:33:17 -0500
commit4e55b06016f254947a21b3de3eb93392c0b627dc (patch)
treea1bd79353d23f52578c45a3e295b01afa1f62194
parenta506bde81eb70d6488a479086bc9afbf0c7025a9 (diff)
downloadsoen422-4e55b06016f254947a21b3de3eb93392c0b627dc.zip
server: handle /duty_cycle
-rw-r--r--Makefile7
-rw-r--r--server/Makefile10
-rw-r--r--server/server.go28
3 files changed, 34 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 089a0f4..91b6c05 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
SENSOR_SRC = SensorStation/SensorStation.ino
HVAC_SRC = HvacStation/HvacStation.ino
+SERVER_SRC = server/server.go server/record.go
BOARD = esp32:esp32:lilygo_t_display
PORT = /dev/ttyACM0
@@ -7,7 +8,11 @@ PORT = /dev/ttyACM0
CFLAGS = -b ${BOARD}
UPLOADFLAGS = -p ${PORT} ${CFLAGS}
-all: SensorStation/build HvacStation/build
+all: SensorStation/build HvacStation/build server/server
+
+server/server: ${SERVER_SRC}
+ go build -o $@ $^
+ gofmt -l -s -w $^
SensorStation/build: ${SENSOR_SRC}
arduino-cli compile ${CFLAGS} SensorStation
diff --git a/server/Makefile b/server/Makefile
deleted file mode 100644
index 8954492..0000000
--- a/server/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-SRC = server.go record.go
-
-server: ${SRC}
- go build $^
-
-fmt:
- gofmt -l -s -w ${SRC}
-
-clean:
- rm -f server
diff --git a/server/server.go b/server/server.go
index a658a00..88bfaa3 100644
--- a/server/server.go
+++ b/server/server.go
@@ -18,6 +18,7 @@ var rooms = []RoomID{
type Humidity float32
type RoomID string
+type DutyCycle float32
type HumidityHandler struct {
rooms map[RoomID]Record[Humidity]
@@ -28,12 +29,19 @@ type TargetHumidityHandler struct {
target Humidity
}
+type DutyCycleHandler struct {
+ mu sync.Mutex
+ dc DutyCycle
+}
+
func main() {
humidityHandler := newHumidityHandler(rooms)
defer humidityHandler.Close()
http.Handle("/humidity", humidityHandler)
http.Handle("/target_humidity", new(TargetHumidityHandler))
+ http.Handle("/duty_cycle", new(DutyCycleHandler))
+
fmt.Printf("Listening on %s...\n", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
@@ -150,6 +158,26 @@ func (h *TargetHumidityHandler) post(w http.ResponseWriter, r *http.Request) {
h.target = Humidity(target)
}
+func (h *DutyCycleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ log.Println(r.Method, r.URL)
+
+ if r.Method != http.MethodPost {
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ fmt.Fprintf(w, "invalid method: '%s'", r.Method)
+ return
+ }
+
+ dc, err := strconv.ParseFloat(r.URL.RawQuery, 32)
+ if err != nil {
+ badRequest(w, "invalid duty cycle: '%s'", r.URL.RawQuery)
+ return
+ }
+
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ h.dc = DutyCycle(dc)
+}
+
// Parse the value associated with each key in the query string. Returns a map of
// keys and values, or error if one of the keys is missing, or if there is no value
// associated with one of the keys.