initial commit

This commit is contained in:
Paul 2019-07-07 13:09:55 +02:00
commit 26b409a39d
3 changed files with 135 additions and 0 deletions

65
functions.go Normal file
View File

@ -0,0 +1,65 @@
package main
import (
"flag"
"log"
"os"
"gopkg.in/ini.v1"
)
// ParseArgs is not yet implemented
func ParseArgs() error {
return nil
}
// GetConfig fetch config from ini file
func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
flag.Usage = Usage
config, err := ini.Load(configfile)
HandleFatalError(err)
var wc WeatherConfig
owmSection := config.Section("owm")
wc.OwmURL = owmSection.Key("url").String()
wc.OwmAppID = owmSection.Key("appid").String()
wc.OwmCities = owmSection.Key("cities").Strings(",")
wc.OwmMeasurements = owmSection.Key("measurements").Strings(",")
wc.OwmTable = owmSection.Key("table").String()
influxdbSection := config.Section("influxdb")
wc.InfluxHost = influxdbSection.Key("hostname").String()
wc.InfluxPort, err = influxdbSection.Key("port").Int()
HandleError(err)
wc.InfluxUser = influxdbSection.Key("username").String()
wc.InfluxPass = influxdbSection.Key("password").String()
wc.InfluxDB = influxdbSection.Key("database").String()
*weatherconfig = wc
return err
}
// HandleError handles errors to return err
func HandleError(err error) error {
if err != nil {
return err
}
return nil
}
// HandleFatalError fatal errors
func HandleFatalError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(2)
}
}
// Usage displays possible arguments
func Usage() {
flag.PrintDefaults()
os.Exit(1)
}

15
types.go Normal file
View File

@ -0,0 +1,15 @@
package main
// WeatherConfig is the main configuration
type WeatherConfig struct {
OwmURL string
OwmAppID string
OwmCities []string
OwmMeasurements []string
OwmTable string
InfluxHost string
InfluxPort int
InfluxUser string
InfluxPass string
InfluxDB string
}

55
weather.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"flag"
"fmt"
"time"
_ "github.com/influxdata/influxdb1-client"
client "github.com/influxdata/influxdb1-client/v2"
)
var err error
var now = time.Now()
var wc WeatherConfig
var configpath string
const kelvin = -273.15
func main() {
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
GetConfig(configpath, &wc)
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
Username: wc.InfluxUser,
Password: wc.InfluxPass,
})
HandleFatalError(err)
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: wc.InfluxDB,
})
HandleFatalError(err)
Get
for _, p := range *prices {
tags := map[string]string{"pdv": p.ID, "fuel": p.Fuel}
fields := map[string]interface{}{"value": p.Amount}
point, _ := client.NewPoint(
wc.InfluxTable,
tags,
fields,
now,
)
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
}
}