weather/functions.go

135 lines
2.9 KiB
Go
Raw Normal View History

2019-07-07 13:09:55 +02:00
package main
import (
2019-07-14 21:10:20 +02:00
"encoding/json"
2019-07-07 13:09:55 +02:00
"flag"
2019-07-14 21:10:20 +02:00
"fmt"
"io/ioutil"
"net/http"
2019-07-07 13:09:55 +02:00
"os"
2019-07-14 21:10:20 +02:00
"time"
2019-07-07 13:09:55 +02:00
2019-07-14 21:44:57 +02:00
client "github.com/influxdata/influxdb1-client/v2"
2019-07-07 13:09:55 +02:00
"gopkg.in/ini.v1"
)
// GetConfig fetch config from ini file
2019-08-12 18:13:30 +02:00
func GetConfig(weatherconfig *WeatherConfig, configfile string) error {
2019-07-07 13:09:55 +02:00
flag.Usage = Usage
config, err := ini.Load(configfile)
2019-07-14 21:10:20 +02:00
if err != nil {
return err
}
2019-07-07 13:09:55 +02:00
var wc WeatherConfig
2019-07-14 21:10:20 +02:00
weatherSection := config.Section("weather")
wc.WeatherAppID = weatherSection.Key("appid").MustString("appid")
2019-07-14 21:10:20 +02:00
wc.WeatherCities = weatherSection.Key("cities").Strings(",")
if len(wc.WeatherCities) < 1 {
return fmt.Errorf("No cities provided in config")
}
2019-07-14 21:10:20 +02:00
wc.WeatherMeasurements = weatherSection.Key("measurements").Strings(",")
if len(wc.WeatherMeasurements) < 1 {
return fmt.Errorf("No measurements provided in config")
2019-07-14 21:10:20 +02:00
}
influxdbSection := config.Section("influxdb")
wc.InfluxHost = influxdbSection.Key("hostname").MustString("localhost")
wc.InfluxPort = influxdbSection.Key("port").MustInt(8086)
wc.InfluxUser = influxdbSection.Key("username").MustString("username")
wc.InfluxPass = influxdbSection.Key("password").MustString("password")
wc.InfluxDB = influxdbSection.Key("database").MustString("me")
2019-07-07 13:09:55 +02:00
*weatherconfig = wc
return err
}
// FetchData fetch data from api
2019-08-12 18:13:30 +02:00
func FetchData(wc *WeatherConfig, city string) (Data, error) {
2019-07-14 21:10:20 +02:00
var d Data
2019-07-07 13:09:55 +02:00
2019-07-14 21:10:20 +02:00
pollTo := 30 * time.Millisecond
c := &http.Client{Timeout: pollTo * time.Second, Transport: &http.Transport{
IdleConnTimeout: pollTo,
DisableCompression: false,
}}
2019-08-12 18:13:30 +02:00
q := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", city, wc.WeatherAppID)
r, err := c.Get(q)
if err != nil {
return Data{}, err
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return Data{}, err
}
2019-07-14 21:10:20 +02:00
err = json.Unmarshal(b, &d)
2019-08-12 18:13:30 +02:00
if err != nil {
return Data{}, err
}
2019-07-14 21:10:20 +02:00
2019-08-12 18:13:30 +02:00
d.Points = map[string]interface{}{"temperature": d.Main.Temperature,
2019-08-12 18:18:32 +02:00
"humidity": d.Main.Humidity,
"pressure": d.Main.Pressure}
2019-07-14 21:10:20 +02:00
2019-08-12 18:13:30 +02:00
return d, nil
2019-07-07 13:09:55 +02:00
}
2019-07-14 21:44:57 +02:00
// SendToInflux sends time series data to influxdb
2019-07-15 01:16:36 +02:00
func SendToInflux(wc *WeatherConfig, d Data) error {
2019-07-14 21:44:57 +02:00
2019-08-12 18:13:30 +02:00
wc.InfluxAddr = fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort)
2019-07-14 21:44:57 +02:00
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
2019-08-12 18:13:30 +02:00
Addr: wc.InfluxAddr,
2019-07-14 21:44:57 +02:00
Username: wc.InfluxUser,
Password: wc.InfluxPass,
})
if err != nil {
return err
}
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: wc.InfluxDB,
})
if err != nil {
return err
}
2019-08-12 18:13:30 +02:00
for key, value := range d.Points {
tags := map[string]string{"city": d.City}
fields := map[string]interface{}{"value": value}
point, err := client.NewPoint(
2019-08-12 18:13:30 +02:00
key,
tags,
fields,
time.Now(),
)
if err != nil {
return err
}
2019-08-12 18:13:30 +02:00
bp.AddPoint(point)
err = httpClient.Write(bp)
if err != nil {
return err
}
2019-07-14 21:44:57 +02:00
}
return nil
}
2019-07-14 21:10:20 +02:00
2019-07-07 13:09:55 +02:00
// Usage displays possible arguments
func Usage() {
flag.PrintDefaults()
os.Exit(1)
}