-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv.go
38 lines (31 loc) · 921 Bytes
/
csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func (h HistorySample) ToCsv() string {
return fmt.Sprintf("%d,%v,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",
h.Time.Unix(), h.Heating, h.Temp, h.Target, h.AbsError, h.Pid.P,
h.Pid.I, h.Pid.D, h.POutput, h.IOutput, h.DOutput, h.COutput)
}
func (s *SousVide) DumpCsv(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("Time,Heating,Temperature,Target,Error,\"P Coeff\",\"I " +
"Coeff\",D Coeff\",\"P Term\",\"I Term\",\"D Term\",Controller\n"))
for _, h := range s.History {
w.Write([]byte(h.ToCsv()))
}
}
func (s *SousVide) DumpJson(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-type", "application/json")
if len(s.History) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
b, err := json.Marshal(s.History)
if err != nil {
log.Panicf("could not marshal historical data to json: %v", err)
}
w.Write(b)
}