some code refactoring
This commit is contained in:
parent
764a678bd3
commit
47883aff12
@ -23,7 +23,6 @@ password=password
|
||||
database=database
|
||||
|
||||
[weather]
|
||||
version=2.5
|
||||
appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
cities=Caen,Paris,Saint-Lo
|
||||
measurements=temp,humidity,pressure
|
||||
|
74
functions.go
74
functions.go
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// GetConfig fetch config from ini file
|
||||
func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
|
||||
func GetConfig(weatherconfig *WeatherConfig, configfile string) error {
|
||||
flag.Usage = Usage
|
||||
|
||||
config, err := ini.Load(configfile)
|
||||
@ -27,7 +27,6 @@ func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
|
||||
|
||||
weatherSection := config.Section("weather")
|
||||
|
||||
wc.WeatherVersion = weatherSection.Key("version").MustString("2.5")
|
||||
wc.WeatherAppID = weatherSection.Key("appid").MustString("appid")
|
||||
wc.WeatherCities = weatherSection.Key("cities").Strings(",")
|
||||
if len(wc.WeatherCities) < 1 {
|
||||
@ -53,7 +52,7 @@ func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
|
||||
}
|
||||
|
||||
// FetchData fetch data from api
|
||||
func FetchData(city string) (Data, error) {
|
||||
func FetchData(wc *WeatherConfig, city string) (Data, error) {
|
||||
var d Data
|
||||
|
||||
pollTo := 30 * time.Millisecond
|
||||
@ -63,22 +62,37 @@ func FetchData(city string) (Data, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return Data{}, err
|
||||
}
|
||||
|
||||
d.Temperature = kelvin + d.Main["temp"]
|
||||
d.Humidity = int64(d.Main["humidity"])
|
||||
d.Points = map[string]interface{}{"temperature": d.Main.Temperature,
|
||||
"humidity": float64(d.Main.Humidity),
|
||||
"pressure": float64(d.Main.Pressure)}
|
||||
|
||||
return d, err
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// SendToInflux sends time series data to influxdb
|
||||
func SendToInflux(wc *WeatherConfig, d Data) error {
|
||||
|
||||
wc.InfluxAddr = fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort)
|
||||
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
|
||||
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
|
||||
Addr: wc.InfluxAddr,
|
||||
Username: wc.InfluxUser,
|
||||
Password: wc.InfluxPass,
|
||||
})
|
||||
@ -93,36 +107,22 @@ func SendToInflux(wc *WeatherConfig, d Data) error {
|
||||
return err
|
||||
}
|
||||
|
||||
tags := map[string]string{"city": d.City}
|
||||
fields := map[string]interface{}{"value": d.Temperature}
|
||||
for key, value := range d.Points {
|
||||
tags := map[string]string{"city": d.City}
|
||||
fields := map[string]interface{}{"value": value}
|
||||
|
||||
point, _ := client.NewPoint(
|
||||
"temperature",
|
||||
tags,
|
||||
fields,
|
||||
time.Now(),
|
||||
)
|
||||
point, _ := client.NewPoint(
|
||||
key,
|
||||
tags,
|
||||
fields,
|
||||
time.Now(),
|
||||
)
|
||||
|
||||
bp.AddPoint(point)
|
||||
err = httpClient.Write(bp)
|
||||
if err != nil {
|
||||
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
|
||||
bp.AddPoint(point)
|
||||
err = httpClient.Write(bp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
14
types.go
14
types.go
@ -2,21 +2,23 @@ package main
|
||||
|
||||
// WeatherConfig is the main configuration
|
||||
type WeatherConfig struct {
|
||||
WeatherVersion string
|
||||
WeatherAppID string
|
||||
WeatherCities []string
|
||||
WeatherMeasurements []string
|
||||
InfluxHost string
|
||||
InfluxPort int
|
||||
InfluxAddr string
|
||||
InfluxUser string
|
||||
InfluxPass 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 {
|
||||
City string `json:"name"`
|
||||
Temperature float64
|
||||
Humidity int64
|
||||
Main map[string]float64
|
||||
City string `json:"name"`
|
||||
Main struct {
|
||||
Temperature float64 `json:"temp"`
|
||||
Humidity float64 `json:"humidity"`
|
||||
Pressure float64 `json:"pressure"`
|
||||
}
|
||||
}
|
||||
|
15
weather.go
15
weather.go
@ -4,30 +4,25 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "github.com/influxdata/influxdb1-client"
|
||||
)
|
||||
|
||||
var wc WeatherConfig
|
||||
var configpath string
|
||||
var err error
|
||||
var now = time.Now()
|
||||
|
||||
const kelvin = -273.15
|
||||
|
||||
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.Parse()
|
||||
|
||||
err := GetConfig(configpath, &wc)
|
||||
err = GetConfig(&wc, configpath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, city := range wc.WeatherCities {
|
||||
d, err := FetchData(city)
|
||||
d, err := FetchData(&wc, city)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user