weather/weather.go

135 lines
3.3 KiB
Go

/*
Copyright 2019 Adrien Waksberg
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
influx "github.com/influxdata/influxdb1-client/v2"
)
// Weather is the main struct
type Weather struct {
Config Config
WeatherDatas []WeatherData
}
// WeatherData contains weather data from openweathermap
type WeatherData struct {
City string `json:"name"`
Main struct {
Humidity int `json:"humidity"`
Pressure int `json:"pressure"`
Temperature float32 `json:"temp"`
} `json:"main"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Rain struct {
OneHour float32 `json:"1h"`
TreeHours float32 `json:"3h"`
} `json:"rain"`
Snow struct {
OneHour float32 `json:"1h"`
TreeHours float32 `json:"3h"`
} `json:"snow"`
Wind struct {
Speed float32 `json:"speed"`
Direction int `json:"deg"`
} `json:"Wind"`
Sys struct {
Country string `json:"country"`
} `json:"sys"`
}
// FetchData from OpenWeatherMap API
func (w *Weather) FetchData() {
w.WeatherDatas = []WeatherData{}
for _, city := range w.Config.Cities {
resp, err := http.Get(
fmt.Sprintf(
"%s/weather?q=%s&appid=%s&units=%s",
URI,
city,
w.Config.OpenWeatherMap.APIKey,
w.Config.OpenWeatherMap.Units,
),
)
if err != nil {
fmt.Printf("%s", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("%s", err)
}
weatherData := WeatherData{}
json.Unmarshal(data, &weatherData)
w.WeatherDatas = append(w.WeatherDatas, weatherData)
time.Sleep(500 * time.Millisecond)
}
}
// SendToInfluxDB send data in influxdb
func (w *Weather) SendToInfluxDB() error {
conn, err := influx.NewHTTPClient(influx.HTTPConfig{
Addr: w.Config.InfluxDB.URL,
Username: w.Config.InfluxDB.Username,
Password: w.Config.InfluxDB.Password,
})
if err != nil {
return err
}
defer conn.Close()
bps, _ := influx.NewBatchPoints(
influx.BatchPointsConfig{
Database: w.Config.InfluxDB.Database,
})
for _, weather := range w.WeatherDatas {
tags := map[string]string{"city": weather.City, "country": weather.Sys.Country}
fields := map[string]interface{}{
"temperature": weather.Main.Temperature,
"humidity": weather.Main.Humidity,
"pressure": weather.Main.Pressure,
"clouds": weather.Clouds.All,
"wind_speed": weather.Wind.Speed,
"wind_direction": weather.Wind.Direction,
"rain_1h": weather.Rain.OneHour,
"rain_3h": weather.Rain.TreeHours,
"snow_1h": weather.Snow.OneHour,
"snow_3h": weather.Snow.TreeHours,
}
point, _ := influx.NewPoint(w.Config.InfluxDB.Measurement, tags, fields, time.Now())
bps.AddPoint(point)
}
err = conn.Write(bps)
if err != nil {
return err
}
return nil
}