weather/functions.go

113 lines
2.5 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
func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
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.WeatherVersion = weatherSection.Key("version").String()
wc.WeatherAppID = weatherSection.Key("appid").String()
wc.WeatherCities = weatherSection.Key("cities").Strings(",")
wc.WeatherMeasurements = weatherSection.Key("measurements").Strings(",")
wc.WeatherTable = weatherSection.Key("table").String()
2019-07-07 13:09:55 +02:00
influxdbSection := config.Section("influxdb")
wc.InfluxHost = influxdbSection.Key("hostname").String()
wc.InfluxPort, err = influxdbSection.Key("port").Int()
2019-07-14 21:10:20 +02:00
if err != nil {
return err
}
2019-07-07 13:09:55 +02:00
wc.InfluxUser = influxdbSection.Key("username").String()
wc.InfluxPass = influxdbSection.Key("password").String()
wc.InfluxDB = influxdbSection.Key("database").String()
*weatherconfig = wc
return err
}
2019-07-14 21:10:20 +02:00
func FetchData(city string) (Data, error) {
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,
}}
resp, err := c.Get(fmt.Sprintf("https://api.openweathermap.org/data/%s/weather?q=%s&appid=%s", wc.WeatherVersion, city, wc.WeatherAppID))
b, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(b, &d)
d.Humidity = d.Main["humidity"]
d.Temperature = kelvin + d.Main["temp"]
return d, err
2019-07-07 13:09:55 +02:00
}
2019-07-14 21:44:57 +02:00
// SendToInflux sends time series data to influxdb
func SendToInflux(wc *WeatherConfig, city string, measurement string, value float64) error {
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
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-07-15 01:04:07 +02:00
tags := map[string]string{"city": city}
2019-07-14 21:44:57 +02:00
fields := map[string]interface{}{"value": value}
point, _ := client.NewPoint(
2019-07-15 01:04:07 +02:00
measurement,
2019-07-14 21:44:57 +02:00
tags,
fields,
time.Now(),
)
bp.AddPoint(point)
err = httpClient.Write(bp)
if err != nil {
return err
}
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)
}