Add support for seperate meters

This commit is contained in:
Nise Void 2018-06-08 13:06:20 +02:00
parent 85165d5c74
commit b0909c6f7e
Signed by: NiseVoid
GPG key ID: FBA14AC83EA602F3
3 changed files with 97 additions and 48 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"strconv"
"strings"
"time"
@ -31,9 +32,10 @@ func main() {
pq := e.Group(`/pq`)
pq.GET(`/count`, count)
pq.GET(`/list`, listAttr)
pq.GET(`/meters`, listMeters)
for _, v := range app.GetAttributes() {
pq.GET(`/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index))
pq.GET(`/:meter/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index))
attrs = append(attrs, strings.ToLower(v.Name))
}
@ -51,13 +53,22 @@ func listAttr(c echo.Context) error {
return c.JSON(200, attrs)
}
func listMeters(c echo.Context) error {
return c.JSON(200, app.GetMeters())
}
func getAttr(index int) echo.HandlerFunc {
return func(c echo.Context) error {
meter, err := strconv.Atoi(c.Param(`meter`))
if err != nil {
return c.NoContent(400)
}
date, err := time.Parse(time.RFC3339, c.Param(`date`))
if err != nil {
return c.NoContent(400)
}
return c.JSON(200, app.GetAttribute(index, date))
return c.JSON(200, app.GetAttribute(index, meter, date))
}
}