diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-29 15:17:06 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-29 15:17:06 -0500 |
| commit | cee1d301809ec0b517962816b1b232c22a41eb3b (patch) | |
| tree | 767c424653b71555919ae81261d0300aefc1faf0 /server/chart.go | |
| parent | 6c8ae5c3a0d40c34a8db1aa4eac27734c321ca7b (diff) | |
| download | soen422-cee1d301809ec0b517962816b1b232c22a41eb3b.zip | |
server: record duty cycle over time
Diffstat (limited to 'server/chart.go')
| -rw-r--r-- | server/chart.go | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/server/chart.go b/server/chart.go index d417021..4d7298b 100644 --- a/server/chart.go +++ b/server/chart.go @@ -11,11 +11,15 @@ import ( "time" ) -type ChartHandler struct { +type HumidityChartHandler struct { building Building } -func (h ChartHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +type DutyCycleChartHandler struct { + dc Record[DutyCycle] +} + +func (h HumidityChartHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Println(r.Method, r.URL) if r.Method != http.MethodGet { @@ -33,7 +37,7 @@ func (h ChartHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ValueFormatter: chart.TimeMinuteValueFormatter, }, YAxis: chart.YAxis{ - Range: &chart.ContinuousRange{Min: 0.0, Max: 100.0}, + Range: &chart.ContinuousRange{Min: minHumidity, Max: maxHumidity}, }, } graph.Elements = []chart.Renderable{ @@ -46,6 +50,45 @@ func (h ChartHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func (h DutyCycleChartHandler) 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 + } + + var x []time.Time + var y []float64 + c := make(chan Entry[DutyCycle]) + h.dc.get <- c + for e := range c { + x = append(x, e.t) + y = append(y, float64(e.v)) + } + + graph := chart.Chart{ + Background: chart.Style{ + Padding: chart.Box{Top: 20, Left: 20}, + }, + Series: []chart.Series{ + chart.TimeSeries{XValues: x, YValues: y}, + }, + XAxis: chart.XAxis{ + ValueFormatter: chart.TimeMinuteValueFormatter, + }, + YAxis: chart.YAxis{ + Range: &chart.ContinuousRange{Min: minDutyCycle, Max: maxDutyCycle}, + }, + } + + w.Header().Set("Content-Type", "image/png") + if err := graph.Render(chart.PNG, w); err != nil { + log.Println(err) + } +} + // Build a time series for each humidity record in the building. // The returned series' are sorted by room name. func buildSortedSeries(building Building) []chart.Series { |