func HistoryHandler(db *models.Mysql) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Request /hist:", r) sensor := r.FormValue("sensor") if len(sensor) == 0 { return } dimensions, err := db.History(sensor) if err != nil { return } val := "?([" s := "" for _, d := range dimensions { val += fmt.Sprintf("[Date(\"%s\"),%f]%s\n", d.Timestamp, float64(d.Value)/1000.0, s) //val += "[Date(" + string(d.Timestamp) + ")," + d.Value/1000.0 + "]" + s + "\n" s = "," } val += "]);" // b, err := json.Marshal(dimensions) // if err != nil { // return // } fmt.Fprintf(w, "%s", val) }) }
func SensorHandler(db *models.Mysql) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { http.Error(w, http.StatusText(405), 405) return } dimension, err := db.GetLast("28-021500d05fff") if err != nil { http.Error(w, http.StatusText(500), 500) return } fmt.Fprintf(w, "%s\n", dimension) }) }
func SensorsHandler(db *models.Mysql) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { http.Error(w, http.StatusText(405), 405) return } sensors, err := db.GetSensors() if err != nil { http.Error(w, http.StatusText(500), 500) return } for _, v := range sensors { fmt.Fprintf(w, "%s\n", v) } }) }
func RootHandler(tpl *template.Template, db *models.Mysql) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println("Request:", r) if tpl == nil { http.Error(w, http.StatusText(404), 404) return } sensors, err := db.GetSensors() if err != nil { http.Error(w, http.StatusText(500), 500) return } err = tpl.Execute(w, sensors) if err != nil { fmt.Println(err) http.Error(w, http.StatusText(404), 404) return } }) }