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

@ -4,136 +4,140 @@
"fields": [ "fields": [
{ {
"name": "c_gem_1", "name": "c_gem_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "c_gem_2", "name": "c_gem_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "c_gem_3", "name": "c_gem_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "ep_1", "name": "ep_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "ep_2", "name": "ep_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "ep_3", "name": "ep_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_gem_1", "name": "i_gem_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_gem_2", "name": "i_gem_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_gem_3", "name": "i_gem_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_max_1", "name": "i_max_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_max_2", "name": "i_max_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "i_max_3", "name": "i_max_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{
"name": "meter_id",
"data_type": "integer",
"size": 4
},
{ {
"name": "p_max_1", "name": "p_max_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "p_max_2", "name": "p_max_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "p_max_3", "name": "p_max_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "s_max_1", "name": "s_max_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "s_max_2", "name": "s_max_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "s_max_3", "name": "s_max_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "time", "name": "time",
"type": "timestamp with time zone", "data_type": "timestamp with time zone",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "u_gem_1", "name": "u_gem_1",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "u_gem_2", "name": "u_gem_2",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
}, },
{ {
"name": "u_gem_3", "name": "u_gem_3",
"type": "double precision", "data_type": "double precision",
"null": false,
"size": 8 "size": 8
} }
] ]
},
{
"name": "public.meter",
"alias": "me",
"fields": [
{
"name": "fortop_uid",
"data_type": "character varying",
"size": 50
},
{
"name": "id",
"data_type": "integer",
"size": 4
},
{
"name": "name",
"data_type": "character varying",
"size": 255
}
]
} }
] ]

View File

@ -49,8 +49,39 @@ func GetAttributes() (list []Attribute) {
return return
} }
// Meter represents a single meter
type Meter struct {
ID int `json:"id"`
Name string `json:"name"`
FortopUID string `json:"fortop_uid"`
}
// GetMeters returns a list of all known meters
func GetMeters() (list []Meter) {
me := model.Meter()
q := me.Select(me.ID, me.Name, me.FortopUID)
rows, err := db.Query(q)
if err != nil {
panic(err)
}
for rows.Next() {
var meter Meter
err = rows.Scan(&meter.ID, &meter.Name, &meter.FortopUID)
if err != nil {
panic(err)
}
list = append(list, meter)
}
return
}
// GetAttribute returns the specified attribute for all phases since the specified date // GetAttribute returns the specified attribute for all phases since the specified date
func GetAttribute(index int, date time.Time) (data []Phases) { func GetAttribute(index, meter int, date time.Time) (data []Phases) {
m := model.Measurement() m := model.Measurement()
r := reflect.ValueOf(m).Elem() r := reflect.ValueOf(m).Elem()
@ -61,7 +92,10 @@ func GetAttribute(index int, date time.Time) (data []Phases) {
q := m.Select(m.Time, f1, f2, f3). q := m.Select(m.Time, f1, f2, f3).
OrderBy(qb.Desc(m.Time)). OrderBy(qb.Desc(m.Time)).
Where(qc.Gte(m.Time, date)) Where(
qc.Gte(m.Time, date),
qc.Eq(m.MeterID, meter),
)
rows, err := db.Query(q) rows, err := db.Query(q)
if err != nil { if err != nil {

View File

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