1package weather
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"io"
  7	"log"
  8	"net/http"
  9)
 10
 11func checkError(error error) {
 12	if error != nil {
 13		log.Fatal(error)
 14	}
 15}
 16
 17var Temperature float64
 18var Conditions string
 19var AQI int
 20var Components APIComponents
 21var Sunrise int
 22var Sunset int
 23
 24var Latitude float32
 25var Longitude float32
 26var CityOfObservation string
 27var OpenWeatherMapAPIKey string
 28
 29type APICoords struct {
 30	Lon float64
 31	Lat float64
 32}
 33
 34type APIWeather struct {
 35	Id          int
 36	Main        string
 37	Description string
 38	Icon        string
 39}
 40
 41type APIWeatherMain struct {
 42	Temp      float64
 43	FeelsLike float64
 44	TempMin   float64
 45	TempMax   float64
 46	Pressure  int
 47	Humidity  int
 48}
 49
 50type APIWind struct {
 51	Speed float64
 52	Deg   float64
 53}
 54
 55type APIClouds struct {
 56	All int
 57}
 58
 59type APISys struct {
 60	Type    int
 61	Id      int
 62	Country string
 63	Sunrise int
 64	Sunset  int
 65}
 66
 67type APIWeatherData struct {
 68	Coord      APICoords
 69	Weather    []APIWeather
 70	Base       string
 71	Main       APIWeatherMain
 72	Visibility int
 73	Wind       APIWind
 74	Clouds     APIClouds
 75	DT         int
 76	Sys        APISys
 77	Timezone   int
 78	Id         int
 79	Name       string
 80	Cod        int
 81}
 82
 83type APIComponents struct {
 84	CO    float64
 85	NO    float64
 86	NO2   float64
 87	O3    float64
 88	SO2   float64
 89	PM2_5 float64
 90	PM10  float64
 91	NH3   float64
 92}
 93
 94type APIPollutionMain struct {
 95	AQI int
 96}
 97
 98type APIPollutionDataObject struct {
 99	Main       APIPollutionMain
100	Components APIComponents
101	DT         int
102}
103
104type APIPollutionData struct {
105	Coord APICoords
106	List  []APIPollutionDataObject
107}
108
109func UpdateCurrentWeather() {
110	// Normal weather info
111	resp, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s", Latitude, Longitude, OpenWeatherMapAPIKey))
112	checkError(err)
113	defer resp.Body.Close()
114	body, err := io.ReadAll(resp.Body)
115	var APIWeatherDataParsed APIWeatherData
116	json.Unmarshal(body, &APIWeatherDataParsed)
117	Temperature = APIWeatherDataParsed.Main.Temp - 273.15
118	Conditions = APIWeatherDataParsed.Weather[0].Main
119	Sunrise = APIWeatherDataParsed.Sys.Sunrise
120	Sunset = APIWeatherDataParsed.Sys.Sunset
121	// Air pollution info
122	respPollution, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/air_pollution?lat=%f&lon=%f&appid=%s", Latitude, Longitude, OpenWeatherMapAPIKey))
123	checkError(err)
124	defer respPollution.Body.Close()
125	bodyPollution, err := io.ReadAll(respPollution.Body)
126	var APIPollutionDataParsed APIPollutionData
127	json.Unmarshal(bodyPollution, &APIPollutionDataParsed)
128	AQI = APIPollutionDataParsed.List[0].Main.AQI
129	Components = APIPollutionDataParsed.List[0].Components
130}