some code refactoring

This commit is contained in:
Paul Lecuq 2019-08-12 18:13:30 +02:00
parent 764a678bd3
commit 47883aff12
4 changed files with 50 additions and 54 deletions

View File

@ -23,7 +23,6 @@ password=password
database=database database=database
[weather] [weather]
version=2.5
appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
cities=Caen,Paris,Saint-Lo cities=Caen,Paris,Saint-Lo
measurements=temp,humidity,pressure measurements=temp,humidity,pressure

View File

@ -15,7 +15,7 @@ import (
) )
// GetConfig fetch config from ini file // GetConfig fetch config from ini file
func GetConfig(configfile string, weatherconfig *WeatherConfig) error { func GetConfig(weatherconfig *WeatherConfig, configfile string) error {
flag.Usage = Usage flag.Usage = Usage
config, err := ini.Load(configfile) config, err := ini.Load(configfile)
@ -27,7 +27,6 @@ func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
weatherSection := config.Section("weather") weatherSection := config.Section("weather")
wc.WeatherVersion = weatherSection.Key("version").MustString("2.5")
wc.WeatherAppID = weatherSection.Key("appid").MustString("appid") wc.WeatherAppID = weatherSection.Key("appid").MustString("appid")
wc.WeatherCities = weatherSection.Key("cities").Strings(",") wc.WeatherCities = weatherSection.Key("cities").Strings(",")
if len(wc.WeatherCities) < 1 { if len(wc.WeatherCities) < 1 {
@ -53,7 +52,7 @@ func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
} }
// FetchData fetch data from api // FetchData fetch data from api
func FetchData(city string) (Data, error) { func FetchData(wc *WeatherConfig, city string) (Data, error) {
var d Data var d Data
pollTo := 30 * time.Millisecond pollTo := 30 * time.Millisecond
@ -63,22 +62,37 @@ func FetchData(city string) (Data, error) {
DisableCompression: false, DisableCompression: false,
}} }}
resp, err := c.Get(fmt.Sprintf("https://api.openweathermap.org/data/%s/weather?q=%s&appid=%s", wc.WeatherVersion, city, wc.WeatherAppID)) q := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", city, wc.WeatherAppID)
fmt.Println(q)
r, err := c.Get(q)
if err != nil {
return Data{}, err
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return Data{}, err
}
b, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(b, &d) err = json.Unmarshal(b, &d)
if err != nil {
return Data{}, err
}
d.Temperature = kelvin + d.Main["temp"] d.Points = map[string]interface{}{"temperature": d.Main.Temperature,
d.Humidity = int64(d.Main["humidity"]) "humidity": float64(d.Main.Humidity),
"pressure": float64(d.Main.Pressure)}
return d, err return d, nil
} }
// SendToInflux sends time series data to influxdb // SendToInflux sends time series data to influxdb
func SendToInflux(wc *WeatherConfig, d Data) error { func SendToInflux(wc *WeatherConfig, d Data) error {
wc.InfluxAddr = fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort)
httpClient, err := client.NewHTTPClient(client.HTTPConfig{ httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort), Addr: wc.InfluxAddr,
Username: wc.InfluxUser, Username: wc.InfluxUser,
Password: wc.InfluxPass, Password: wc.InfluxPass,
}) })
@ -93,36 +107,22 @@ func SendToInflux(wc *WeatherConfig, d Data) error {
return err return err
} }
tags := map[string]string{"city": d.City} for key, value := range d.Points {
fields := map[string]interface{}{"value": d.Temperature} tags := map[string]string{"city": d.City}
fields := map[string]interface{}{"value": value}
point, _ := client.NewPoint( point, _ := client.NewPoint(
"temperature", key,
tags, tags,
fields, fields,
time.Now(), time.Now(),
) )
bp.AddPoint(point) bp.AddPoint(point)
err = httpClient.Write(bp) err = httpClient.Write(bp)
if err != nil { if err != nil {
return err return err
} }
tags = map[string]string{"city": d.City}
fields = map[string]interface{}{"value": d.Humidity}
point, _ = client.NewPoint(
"humidity",
tags,
fields,
time.Now(),
)
bp.AddPoint(point)
err = httpClient.Write(bp)
if err != nil {
return err
} }
return nil return nil

View File

@ -2,21 +2,23 @@ package main
// WeatherConfig is the main configuration // WeatherConfig is the main configuration
type WeatherConfig struct { type WeatherConfig struct {
WeatherVersion string
WeatherAppID string WeatherAppID string
WeatherCities []string WeatherCities []string
WeatherMeasurements []string WeatherMeasurements []string
InfluxHost string InfluxHost string
InfluxPort int InfluxPort int
InfluxAddr string
InfluxUser string InfluxUser string
InfluxPass string InfluxPass string
InfluxDB string InfluxDB string
} }
// Data is object for temperature and humidity for a city // Data is the struct for temperature and humidity for a city
type Data struct { type Data struct {
City string `json:"name"` City string `json:"name"`
Temperature float64 Main struct {
Humidity int64 Temperature float64 `json:"temp"`
Main map[string]float64 Humidity float64 `json:"humidity"`
Pressure float64 `json:"pressure"`
}
} }

View File

@ -4,30 +4,25 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"time"
_ "github.com/influxdata/influxdb1-client" _ "github.com/influxdata/influxdb1-client"
) )
var wc WeatherConfig
var configpath string
var err error
var now = time.Now()
const kelvin = -273.15
func main() { func main() {
var wc WeatherConfig
var configpath string
var err error
flag.StringVar(&configpath, "configfile", "weather.ini", "config file to use with fuelprices section") flag.StringVar(&configpath, "configfile", "weather.ini", "config file to use with fuelprices section")
flag.Parse() flag.Parse()
err := GetConfig(configpath, &wc) err = GetConfig(&wc, configpath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
for _, city := range wc.WeatherCities { for _, city := range wc.WeatherCities {
d, err := FetchData(city) d, err := FetchData(&wc, city)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }