diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-21 16:47:08 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-21 16:47:08 -0500 |
| commit | dbc71139a5708b371ffb0b7580f562674707c558 (patch) | |
| tree | 1ee13dbfa44588a723a0d1fdf4eb24f5d76a9158 /server/humidity.go | |
| parent | a464e144485f69fd9ae6efee3e3fe5e00590e6de (diff) | |
| download | soen422-dbc71139a5708b371ffb0b7580f562674707c558.zip | |
server dashboard
Diffstat (limited to 'server/humidity.go')
| -rw-r--r-- | server/humidity.go | 41 |
1 files changed, 3 insertions, 38 deletions
diff --git a/server/humidity.go b/server/humidity.go index 3982545..8cffccc 100644 --- a/server/humidity.go +++ b/server/humidity.go @@ -11,21 +11,7 @@ import ( type Humidity float32 type HumidityHandler struct { - rooms map[RoomID]Record[Humidity] -} - -func newHumidityHandler(rooms []RoomID) HumidityHandler { - h := HumidityHandler{make(map[RoomID]Record[Humidity])} - for _, id := range rooms { - h.rooms[id] = newRecord[Humidity]() - } - return h -} - -func (h HumidityHandler) Close() { - for _, record := range h.rooms { - record.Close() - } + Building } func (h HumidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -42,7 +28,7 @@ func (h HumidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (h HumidityHandler) get(w http.ResponseWriter, r *http.Request) { - if humidity, ok := h.average(); ok { + if humidity, ok := h.Building.average(); ok { fmt.Fprintf(w, "%.2f", humidity) } else { w.WriteHeader(http.StatusGone) @@ -65,7 +51,7 @@ func (h HumidityHandler) post(w http.ResponseWriter, r *http.Request) { return } - record, ok := h.rooms[room] + record, ok := h.Building[room] if !ok { badRequest(w, "invalid room ID: '%s'", room) return @@ -74,27 +60,6 @@ func (h HumidityHandler) post(w http.ResponseWriter, r *http.Request) { record.put <- Humidity(humidity) } -// Calculate the average humidity in the building. Returns false if there is not enough data available. -func (h HumidityHandler) average() (Humidity, bool) { - var sum Humidity = 0 - nRooms := 0 - for room, record := range h.rooms { - c := make(chan Humidity) - record.getRecent <- c - if humidity, ok := <-c; ok { - sum += humidity - nRooms++ - } else { - log.Printf("Warning: no humidity for room '%s'\n", room) - } - } - if nRooms == 0 { - log.Println("Warning: not enough data to calculate average humidity") - return -1.0, false - } - return sum / Humidity(nRooms), true -} - // 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. |