fuelprices/fuelprices.go

103 lines
1.9 KiB
Go
Raw Normal View History

2019-06-05 21:48:21 +02:00
package main
import (
"archive/zip"
"fmt"
"time"
2019-06-05 23:50:39 +02:00
"flag"
2019-06-05 21:48:21 +02:00
2019-06-05 22:58:16 +02:00
_ "github.com/influxdata/influxdb1-client"
2019-06-05 21:48:21 +02:00
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
2019-06-05 21:48:21 +02:00
type Outfile struct {
Content []byte
}
// FuelPricesConfig is the main configuration
2019-06-05 21:48:21 +02:00
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
2019-06-05 21:48:21 +02:00
type Price struct {
ID string
2019-06-05 21:48:21 +02:00
Fuel string
Amount float64
}
var szip Srcfile
var sxml Srcfile
var ofile Outfile
var err error
var zipfile *zip.Reader
2019-06-06 00:04:51 +02:00
var prices *[]Price
var now = time.Now()
var fpc FuelPricesConfig
var configpath string
2019-06-05 21:48:21 +02:00
func main() {
2019-06-05 23:50:39 +02:00
flag.StringVar(&configpath,"configfile","common.ini","config file to use with fuelprices section")
flag.Parse()
GetConfig(configpath, &fpc)
2019-06-05 21:48:21 +02:00
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,
})
2019-06-06 00:04:51 +02:00
HandleFatalError(err)
2019-06-05 21:48:21 +02:00
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: fpc.InfluxDB,
})
2019-06-06 00:04:51 +02:00
HandleFatalError(err)
2019-06-05 21:48:21 +02:00
err = DownloadFile(fpc.RemoteURL)
2019-06-06 00:04:51 +02:00
HandleFatalError(err)
2019-06-05 21:48:21 +02:00
err = ExtractZip()
2019-06-06 00:04:51 +02:00
HandleFatalError(err)
2019-06-05 21:48:21 +02:00
GetPrices(&prices, fpc.Pos, fpc.Types, fpc.XPathBase)
for _, p := range *prices {
tags := map[string]string{"pdv": p.ID, "fuel": p.Fuel}
2019-06-05 21:48:21 +02:00
fields := map[string]interface{}{"value": p.Amount}
point, _ := client.NewPoint(
"fuel_price",
tags,
fields,
now,
)
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
}
}