74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
|
package fortop
|
||
|
|
||
|
import (
|
||
|
"encoding/xml"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
"git.fuyu.moe/5GPowerQuality/parser/data"
|
||
|
"golang.org/x/net/html/charset"
|
||
|
)
|
||
|
|
||
|
var start time.Time
|
||
|
|
||
|
func FetchData() {
|
||
|
if start.IsZero() {
|
||
|
start = data.GetStartDate(data.SourceFortop)
|
||
|
}
|
||
|
|
||
|
end := start.Add(time.Hour)
|
||
|
|
||
|
resp := getFortopData(start, end)
|
||
|
|
||
|
sets := data.Sets{}
|
||
|
for _, meter := range resp {
|
||
|
mID := data.GetMeterID(data.SourceFortop, meter.MeterID)
|
||
|
|
||
|
for _, v := range meter.Meetwaarde {
|
||
|
valName := v.Naam
|
||
|
|
||
|
for _, r := range v.Range {
|
||
|
key := data.Key{Meter: mID, Time: time.Time(r.Date)}
|
||
|
if _, ok := sets[key]; !ok {
|
||
|
sets[key] = data.Set{}
|
||
|
}
|
||
|
|
||
|
sets[key][valName] = r.Value
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
data.InsertSets(sets)
|
||
|
|
||
|
if end.Before(time.Now()) {
|
||
|
start = end.Add(time.Second)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
time.Sleep(time.Minute)
|
||
|
start = data.GetStartDate(data.SourceFortop)
|
||
|
}
|
||
|
|
||
|
func getFortopData(startDate, endDate time.Time) []Meter {
|
||
|
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 {
|
||
|
panic(`Got status code:` + strconv.Itoa(resp.StatusCode))
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|