parser/main.go

147 lines
2.6 KiB
Go

package main
import (
"database/sql"
"encoding/xml"
"fmt"
"net/http"
"strconv"
"time"
"git.fuyu.moe/5GPowerQuality/parser/internal/model"
"git.fuyu.moe/Fuyu/flog"
"git.ultraware.nl/NiseVoid/qb/qbdb"
"git.ultraware.nl/NiseVoid/qb/qf"
"golang.org/x/net/html/charset"
)
var db *qbdb.DB
func main() {
defer func() {
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
}
}()
setLogger()
flog.Info(`Starting`)
catchSignals()
initDB()
start := GetStartDate()
for {
end := start.Add(time.Hour)
data := GetFortopData(start, end)
ranges := map[string][]Range{}
for _, v := range data {
ranges[v.Naam] = v.Range
}
sets := Sets{}
for k, v := range ranges {
for _, r := range v {
date := time.Time(r.Date)
set, ok := sets[date]
if !ok {
sets[date] = Set{}
set = sets[date]
}
set[k] = r.Value
}
}
insertSets(sets)
if end.Before(time.Now()) {
start = end.Add(time.Second)
continue
}
time.Sleep(time.Minute)
start = GetStartDate()
}
}
// GetStartDate gets the last measurement date
func GetStartDate() time.Time {
m := model.Measurement()
var t *time.Time
q := m.Select(qf.Max(m.Time))
err := db.QueryRow(q).Scan(&t)
if t == nil || err == sql.ErrNoRows {
return time.Date(2018, 4, 1, 0, 0, 0, 0, time.Local)
}
if err != nil {
panic(err)
}
*t = t.Add(time.Second)
return *t
}
func insertSets(sets Sets) {
if len(sets) == 0 {
return
}
m := model.Measurement()
q := m.Insert(m.Time,
m.CGem1, m.CGem2, m.CGem3,
m.Ep1, m.Ep2, m.Ep3,
m.IGem1, m.IGem2, m.IGem3,
m.IMax1, m.IMax2, m.IMax3,
m.PMax1, m.PMax2, m.PMax3,
m.SMax1, m.SMax2, m.SMax3,
m.UGem1, m.UGem2, m.UGem3,
)
for k, v := range sets {
q.Values(k,
v[`CGem1`], v[`CGem2`], v[`CGem3`],
v[`EP1`], v[`EP2`], v[`EP3`],
v[`IGem1`], v[`IGem2`], v[`IGem3`],
v[`IMax1`], v[`IMax2`], v[`IMax3`],
v[`PMax1`], v[`PMax2`], v[`PMax3`],
v[`SMax1`], v[`SMax2`], v[`SMax3`],
v[`UGem1`], v[`UGem2`], v[`UGem3`],
)
}
err := db.Exec(q)
if err != nil {
panic(err)
}
}
// GetFortopData retrieves data from fortop
func GetFortopData(startDate, endDate time.Time) []Meetwaarde {
url := `https://energy4all.energyportal.online/index?p5g=S_1_` + strconv.FormatInt(startDate.Unix(), 10) + `_` + strconv.FormatInt(endDate.Unix(), 10)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
fmt.Println(`Got status code:`, resp.StatusCode)
return nil
}
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReaderLabel
var data FortopFormat
err = decoder.Decode(&data)
if err != nil {
panic(err)
}
return data.Trafo.Meter.Meetwaarde
}