fuelprices/fuelprices.go
2019-06-06 12:09:02 +02:00

102 lines
1.9 KiB
Go

package main
import (
"archive/zip"
"flag"
"fmt"
"time"
_ "github.com/influxdata/influxdb1-client"
client "github.com/influxdata/influxdb1-client/v2"
)
// Srcfile source file
type Srcfile struct {
Filename string
Filepath string
Content []byte
}
// Outfile is the output XML file
type Outfile struct {
Content []byte
}
// FuelPricesConfig is the main configuration
type FuelPricesConfig struct {
RemoteURL string
RemoteFilename string
XPathBase string
Pos []string
Types []string
TmpDir string
InfluxHost string
InfluxPort int
InfluxUser string
InfluxPass string
InfluxDB string
}
// Price contains price of points of sale
type Price struct {
ID string
Fuel string
Amount float64
}
var szip Srcfile
var sxml Srcfile
var ofile Outfile
var err error
var zipfile *zip.Reader
var prices *[]Price
var now = time.Now()
var fpc FuelPricesConfig
var configpath string
func main() {
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
GetConfig(configpath, &fpc)
sxml.Filename = fpc.RemoteFilename
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
Username: fpc.InfluxUser,
Password: fpc.InfluxPass,
})
HandleFatalError(err)
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: fpc.InfluxDB,
})
HandleFatalError(err)
err = DownloadFile(fpc.RemoteURL)
HandleFatalError(err)
err = ExtractZip()
HandleFatalError(err)
GetPrices(&prices, fpc.Pos, fpc.Types, fpc.XPathBase)
for _, p := range *prices {
tags := map[string]string{"pdv": p.ID, "fuel": p.Fuel}
fields := map[string]interface{}{"value": p.Amount}
point, _ := client.NewPoint(
"fuel_price",
tags,
fields,
now,
)
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
}
}