weather/weather.go

135 lines
3.2 KiB
Go
Raw Permalink Normal View History

2019-08-22 17:37:45 +00:00
/*
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.
*/
2019-08-13 20:14:09 +00:00
package main
import (
2019-08-22 20:44:50 +00:00
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
2019-08-13 20:14:09 +00:00
2019-08-22 20:44:50 +00:00
influx "github.com/influxdata/influxdb1-client/v2"
2019-08-13 20:14:09 +00:00
)
2019-08-22 17:47:25 +00:00
// Weather is the main struct
2019-08-13 20:14:09 +00:00
type Weather struct {
2019-08-22 20:44:50 +00:00
Config Config
WeatherDatas []WeatherData
2019-08-13 20:14:09 +00:00
}
2019-08-22 17:47:25 +00:00
// WeatherData contains weather data from openweathermap
2019-08-13 20:14:09 +00:00
type WeatherData struct {
2019-08-22 20:44:50 +00:00
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"`
2019-08-13 20:14:09 +00:00
}
2019-08-22 17:47:25 +00:00
// FetchData from OpenWeatherMap API
2019-08-13 20:14:09 +00:00
func (w *Weather) FetchData() {
2019-08-22 20:44:50 +00:00
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)
}
2019-08-13 20:14:09 +00:00
}
2019-08-22 17:47:25 +00:00
// SendToInfluxDB send data in influxdb
2019-08-13 20:14:09 +00:00
func (w *Weather) SendToInfluxDB() error {
2019-08-22 20:44:50 +00:00
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
2019-08-13 20:14:09 +00:00
}