Modify the API format a bit and fix bug with the packet parsing logic
This commit is contained in:
parent
25d3788478
commit
0ca4da4b6c
|
@ -22,10 +22,10 @@ func CreateTable(db *sql.DB) error {
|
|||
}
|
||||
|
||||
type Datapoint struct {
|
||||
Time uint64
|
||||
Temperature float32
|
||||
Humidity float32
|
||||
HumidifierVolts float32
|
||||
Time uint64 `json:"t"`
|
||||
Temperature float32 `json:"temp"`
|
||||
Humidity float32 `json:"hum"`
|
||||
HumidifierVolts float32 `json:"hv"`
|
||||
}
|
||||
|
||||
func QueryHistory(db *sql.DB, start int64) ([]Datapoint, error) {
|
||||
|
|
|
@ -42,9 +42,9 @@ func (s *ShroomStatus) Update() {
|
|||
func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) {
|
||||
data := ShroomData{
|
||||
Time: 0,
|
||||
Temperature: -1,
|
||||
Humidity: -1,
|
||||
HumidifierVolts: -1,
|
||||
Temperature: -274,
|
||||
Humidity: -100,
|
||||
HumidifierVolts: -100,
|
||||
Status: 0,
|
||||
}
|
||||
err := json.Unmarshal(line, &data)
|
||||
|
@ -54,7 +54,7 @@ func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) {
|
|||
log.Println(line)
|
||||
return
|
||||
}
|
||||
if data.Time > 0 && data.Temperature > 0 && data.Humidity > 0 && data.HumidifierVolts > 0 {
|
||||
if data.Time > 0 && data.Temperature > -274 && data.Humidity > -100 && data.HumidifierVolts > -100 {
|
||||
err = InsertRow(db, &data)
|
||||
if err != nil {
|
||||
log.Println("unable to write to database: ", err)
|
||||
|
@ -68,7 +68,7 @@ func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) {
|
|||
status.Unlock()
|
||||
status.Update()
|
||||
} else {
|
||||
log.Println("unknown packet: ", line)
|
||||
log.Println("unknown packet: ", line, string(line))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ import (
|
|||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -27,10 +29,19 @@ type statusJson struct {
|
|||
Humidifier bool `json:"humidifier"`
|
||||
}
|
||||
|
||||
func dumpData(db *sql.DB, offset int64) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, _req *http.Request) {
|
||||
func dumpData(db *sql.DB, multiplier int64) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
now := time.Now().Unix()
|
||||
t := now + offset
|
||||
path := strings.Split(req.URL.Path, "/")
|
||||
last := path[len(path)-1]
|
||||
count, err := strconv.Atoi(last)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte("could not read integer in path: " + err.Error()))
|
||||
return
|
||||
}
|
||||
offset := int64(count) * multiplier
|
||||
t := now - offset
|
||||
msg, err := s.GetRows(db, t)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
|
@ -65,10 +76,10 @@ func main() {
|
|||
log.Fatal("unable to use subdirectory of embedded fs: ", err)
|
||||
}
|
||||
|
||||
dumpWeek := dumpData(db, -7*24*60*60)
|
||||
dumpDay := dumpData(db, -24*60*60)
|
||||
dumpHour := dumpData(db, -60*60)
|
||||
dumpMinute := dumpData(db, -60)
|
||||
dumpWeek := dumpData(db, 7*24*60*60*1000)
|
||||
dumpDay := dumpData(db, 24*60*60*1000)
|
||||
dumpHour := dumpData(db, 60*60*1000)
|
||||
dumpMinute := dumpData(db, 60*1000)
|
||||
|
||||
lastPoint := func(w http.ResponseWriter, _req *http.Request) {
|
||||
msg, err := s.LastTime(db)
|
||||
|
@ -115,10 +126,10 @@ func main() {
|
|||
|
||||
http.Handle("/d/", http.StripPrefix("/d/", http.FileServer(http.Dir("./dev"))))
|
||||
http.Handle("/", http.FileServer(http.FS(contentSub)))
|
||||
http.HandleFunc("/api/last/week", dumpWeek)
|
||||
http.HandleFunc("/api/last/day", dumpDay)
|
||||
http.HandleFunc("/api/last/hour", dumpHour)
|
||||
http.HandleFunc("/api/last/minute", dumpMinute)
|
||||
http.HandleFunc("/api/last/weeks/", dumpWeek)
|
||||
http.HandleFunc("/api/last/days/", dumpDay)
|
||||
http.HandleFunc("/api/last/hours/", dumpHour)
|
||||
http.HandleFunc("/api/last/minutes/", dumpMinute)
|
||||
http.HandleFunc("/api/latest", lastPoint)
|
||||
http.HandleFunc("/api/status", getStatus)
|
||||
http.HandleFunc("/api/admin", adminHandler)
|
||||
|
|
Loading…
Reference in New Issue