github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/weather_owm/types.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package weather_owm 20 21 import ( 22 "time" 23 24 "github.com/e154/smart-home/common" 25 m "github.com/e154/smart-home/models" 26 "github.com/e154/smart-home/plugins/weather" 27 "github.com/e154/smart-home/system/supervisor" 28 ) 29 30 const ( 31 // EntityWeatherOwm ... 32 EntityWeatherOwm = string("weather_owm") 33 // DefaultApiUrl ... 34 DefaultApiUrl = "https://api.openweathermap.org/data/2.5/onecall" 35 // Attribution ... 36 Attribution = "Weather forecast from openweathermap api" 37 // FuncEntityAction ... 38 FuncEntityAction = "entityAction" 39 40 Version = "0.0.1" 41 ) 42 43 // GeoPos ... 44 type GeoPos struct { 45 Lat float64 `json:"lat"` // latitude 46 Lon float64 `json:"lon"` // longitude 47 } 48 49 // City ... 50 type City struct { 51 Id int64 `json:"id"` // City ID 52 Name string `json:"name"` // City name 53 Coord GeoPos `json:"coord"` // City geo location 54 Country string `json:"country"` // Country code (GB, JP etc.) 55 Population int64 `json:"population"` // 56 Timezone int64 `json:"timezone"` // Shift in seconds from UTC 57 Sunrise int64 `json:"sunrise"` // Sunrise time 58 Sunset int64 `json:"sunset"` // Sunset time 59 } 60 61 // ProductMain ... 62 type ProductMain struct { 63 Temp float64 `json:"temp"` // Temperature. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. 64 FeelsLike float64 `json:"feels_like"` // This temperature parameter accounts for the human perception of weather. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. 65 TempMin float64 `json:"temp_min"` // Minimum temperature at the moment of calculation. This is minimal forecasted temperature (within large megalopolises and urban areas), use this parameter optionally. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. 66 TempMax float64 `json:"temp_max"` // Maximum temperature at the moment of calculation. This is maximal forecasted temperature (within large megalopolises and urban areas), use this parameter optionally. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. 67 Pressure float64 `json:"pressure"` // Atmospheric pressure on the sea level by default, hPa 68 SeaLevel float64 `json:"sea_level"` // Atmospheric pressure on the sea level, hPa 69 GrndLevel float64 `json:"grnd_level"` // Atmospheric pressure on the ground level, hPa 70 Humidity float64 `json:"humidity"` // Humidity, % 71 TempKf float64 `json:"temp_kf"` // Internal parameter 72 } 73 74 // ProductWeather ... 75 type ProductWeather struct { 76 Id int64 `json:"id"` // Weather condition id 77 Main string `json:"main"` // Group of weather parameters (Rain, Snow, Extreme etc.) 78 Description string `json:"description"` // Weather condition within the group. You can get the output in your language. 79 Icon string `json:"icon"` // Weather icon id 80 } 81 82 // ProductClouds ... 83 type ProductClouds struct { 84 All int64 `json:"all"` // Cloudiness, % 85 } 86 87 // ProductWind ... 88 type ProductWind struct { 89 Speed float64 `json:"speed"` // Wind speed. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour. 90 Deg float64 `json:"deg"` // Wind direction, degrees (meteorological) 91 Gust float64 `json:"gust"` // Wind gust. Unit Default: meter/sec, Metric: meter/sec, Imperial: miles/hour 92 } 93 94 // ProductSnow ... 95 type ProductSnow struct { 96 Last3Hours float64 `json:"3h"` // Snow volume for last 3 hours 97 } 98 99 // ProductRain ... 100 type ProductRain struct { 101 Last3Hours float64 `json:"3h"` // Rain volume for last 3 hours, mm 102 } 103 104 // ProductSys ... 105 type ProductSys struct { 106 Pod string `json:"pod"` // Part of the day (n - night, d - day) 107 } 108 109 // Product ... 110 type Product struct { 111 Dt int64 `json:"dt"` // Time of data forecasted, unix, UTC 112 Visibility int64 `json:"visibility"` // Average visibility, metres 113 Pop float64 `json:"pop"` // Probability of precipitation 114 Main ProductMain `json:"main"` 115 Weather ProductWeather `json:"weather"` 116 Clouds *ProductClouds `json:"clouds,omitempty"` 117 Wind *ProductWind `json:"wind,omitempty"` 118 Rain *ProductRain `json:"rain,omitempty"` 119 Snow *ProductSnow `json:"snow,omitempty"` 120 Sys *ProductSys `json:"sys,omitempty"` 121 DtTxt time.Time `json:"dt_txt"` 122 } 123 124 // Products ... 125 type Products []Product 126 127 // Len ... 128 func (p Products) Len() int { 129 return len(p) 130 } 131 132 // Swap ... 133 func (p Products) Swap(a, b int) { 134 p[a], p[b] = p[b], p[a] 135 } 136 137 // Less ... 138 func (p Products) Less(a, b int) bool { 139 return p[a].Dt < p[b].Dt 140 } 141 142 // WeatherFor5Days ... 143 type WeatherFor5Days struct { 144 Cod string `json:"cod"` // Internal parameter 145 Message int `json:"message"` // Internal parameter 146 Cnt int `json:"cnt"` // A number of timestamps returned in the API response 147 Products Products `json:"list"` 148 City City `json:"city"` 149 } 150 151 // WeatherCurrentRain ... 152 type WeatherCurrentRain struct { 153 LastHour float64 `json:"1h"` // Rain volume for last hour, mm 154 } 155 156 // WeatherCurrentSnow ... 157 type WeatherCurrentSnow struct { 158 LastHour float64 `json:"1h"` // Snow volume for last hour, mm 159 } 160 161 // WeatherCurrent ... 162 type WeatherCurrent struct { 163 Dt int64 `json:"dt"` // Current time, Unix, UTC 164 Sunrise int64 `json:"sunrise"` // Sunrise time, Unix, UTC 165 Sunset int64 `json:"sunset"` // Sunset time, Unix, UTC 166 Temp float64 `json:"temp"` // Temperature. Units - default: kelvin, metric: Celsius, imperial: Fahrenheit. 167 FeelsLike float64 `json:"feels_like"` // Temperature. This temperature parameter accounts for the human perception of weather 168 Pressure float64 `json:"pressure"` // Atmospheric pressure on the sea level, hPa 169 Humidity float64 `json:"humidity"` // Humidity, % 170 DewPoint float64 `json:"dew_point"` // Atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form. 171 Uvi float64 `json:"uvi"` // Current UV index 172 Clouds float64 `json:"clouds"` // Cloudiness, % 173 Visibility int64 `json:"visibility"` // Average visibility, metres 174 WindSpeed float64 `json:"wind_speed,omitempty"` // Wind speed. Wind speed. 175 WindDeg float64 `json:"wind_deg,omitempty"` // Wind direction, degrees (meteorological) 176 WindGust *float64 `json:"wind_gust,omitempty"` // (where available) Wind gust. 177 Rain *WeatherCurrentRain `json:"rain"` // 178 Snow *WeatherCurrentSnow `json:"snow"` // 179 Weather []ProductWeather `json:"weather"` // 180 } 181 182 // WeatherHourly ... 183 type WeatherHourly struct { 184 Dt int64 `json:"dt"` // Current time, Unix, UTC 185 Temp float64 `json:"temp"` // Temperature. Units - default: kelvin, metric: Celsius, imperial: Fahrenheit. 186 FeelsLike float64 `json:"feels_like"` // Temperature. This temperature parameter accounts for the human perception of weather 187 Pressure float64 `json:"pressure"` // Atmospheric pressure on the sea level, hPa 188 Humidity float64 `json:"humidity"` // Humidity, % 189 DewPoint float64 `json:"dew_point"` // Atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form. 190 Uvi float64 `json:"uvi"` // Current UV index 191 Clouds float64 `json:"clouds"` // Cloudiness, % 192 Visibility int64 `json:"visibility"` // Average visibility, metres 193 WindSpeed float64 `json:"wind_speed,omitempty"` // Wind speed. Wind speed. 194 WindDeg float64 `json:"wind_deg,omitempty"` // Wind direction, degrees (meteorological) 195 WindGust *float64 `json:"wind_gust,omitempty"` // (where available) Wind gust. 196 Rain *WeatherCurrentRain `json:"rain"` // 197 Snow *WeatherCurrentSnow `json:"snow"` // 198 Weather []ProductWeather `json:"weather"` // 199 Pop float64 `json:"pop"` // Probability of precipitation 200 } 201 202 // WeatherDailyTemp ... 203 type WeatherDailyTemp struct { 204 Day float64 `json:"day"` 205 Min float64 `json:"min"` 206 Max float64 `json:"max"` 207 Night float64 `json:"night"` 208 Eve float64 `json:"eve"` // Evening temperature. 209 Morn float64 `json:"morn"` 210 } 211 212 // WeatherDailyFeelsLike ... 213 type WeatherDailyFeelsLike struct { 214 Day float64 `json:"day"` 215 Min float64 `json:"min"` 216 Max float64 `json:"max"` 217 Night float64 `json:"night"` 218 Eve float64 `json:"eve"` // Evening temperature. 219 Morn float64 `json:"morn"` 220 } 221 222 // Alert ... 223 type Alert struct { 224 SenderName string `json:"sender_name"` // Name of the alert source 225 Event string `json:"event"` // Alert event name 226 Start int64 `json:"start"` // Date and time of the start of the alert, Unix, UTC 227 End int64 `json:"end"` // Date and time of the end of the alert, Unix, UTC 228 Description string `json:"description"` // Description of the alert 229 Tags []string `json:"tags"` // Type of severe weather 230 } 231 232 // WeatherDaily ... 233 type WeatherDaily struct { 234 Dt int64 `json:"dt"` // Current time, Unix, UTC 235 Sunrise int64 `json:"sunrise"` // Sunrise time, Unix, UTC 236 Sunset int64 `json:"sunset"` // Sunset time, Unix, UTC 237 Moonrise int64 `json:"moonrise"` // The time of when the moon rises for this day, Unix, UTC 238 Moonset int64 `json:"moonset"` // The time of when the moon sets for this day, Unix, UTC 239 MoonPhase float64 `json:"moon_phase"` // Moon phase. 0 and 1 are 'new moon', 0.25 is 'first quarter moon', 0.5 is 'full moon' and 0.75 is 'last quarter moon'. The periods in between are called 'waxing crescent', 'waxing gibous', 'waning gibous', and 'waning crescent', respectively. 240 Temp WeatherDailyTemp `json:"temp"` // Temperature. Units - default: kelvin, metric: Celsius, imperial: Fahrenheit. 241 FeelsLike WeatherDailyFeelsLike `json:"feels_like"` // Temperature. This temperature parameter accounts for the human perception of weather 242 Pressure int64 `json:"pressure"` // Atmospheric pressure on the sea level, hPa 243 Humidity float64 `json:"humidity"` // Humidity, % 244 DewPoint float64 `json:"dew_point"` // Atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form. 245 Uvi float64 `json:"uvi"` // Current UV index 246 Clouds float64 `json:"clouds"` // Cloudiness, % 247 Visibility float64 `json:"visibility"` // Average visibility, metres 248 WindSpeed float64 `json:"wind_speed,omitempty"` // Wind speed. Wind speed. 249 WindDeg float64 `json:"wind_deg,omitempty"` // Wind direction, degrees (meteorological) 250 WindGust *float64 `json:"wind_gust,omitempty"` // (where available) Wind gust. 251 Rain float64 `json:"rain"` // 252 Snow float64 `json:"snow"` // 253 Weather []ProductWeather `json:"weather"` // 254 } 255 256 // WeatherFor8Days ... 257 type WeatherFor8Days struct { 258 GeoPos 259 Timezone string `json:"timezone"` // Timezone name for the requested location 260 TimezoneOffset int64 `json:"timezone_offset"` // Shift in seconds from UTC 261 Current WeatherCurrent `json:"current"` 262 Hourly []WeatherHourly `json:"hourly"` 263 Daily []WeatherDaily `json:"daily"` 264 Alerts []Alert `json:"alerts"` 265 } 266 267 // Zone ... 268 type Zone struct { 269 Name string `json:"name"` 270 Lat float64 `json:"lat"` 271 Lon float64 `json:"lon"` 272 Weatherdata *WeatherFor8Days `json:"weatherdata"` 273 LoadedAt *time.Time `json:"loaded_at"` 274 } 275 276 const ( 277 // AttrAppid ... 278 AttrAppid = "appid" 279 // AttrUnits ... 280 AttrUnits = "units" 281 // AttrLang ... 282 AttrLang = "lang" 283 ) 284 285 // NewSettings ... 286 func NewSettings() map[string]*m.Attribute { 287 return map[string]*m.Attribute{ 288 weather.AttrLat: { 289 Name: weather.AttrLat, 290 Type: common.AttributeFloat, 291 }, 292 weather.AttrLon: { 293 Name: weather.AttrLon, 294 Type: common.AttributeFloat, 295 }, 296 weather.AttrTheme: { 297 Name: weather.AttrTheme, 298 Type: common.AttributeString, 299 }, 300 weather.AttrWinter: { 301 Name: weather.AttrWinter, 302 Type: common.AttributeBool, 303 }, 304 AttrAppid: { 305 Name: AttrAppid, 306 Type: common.AttributeEncrypted, 307 }, 308 AttrUnits: { 309 Name: AttrUnits, 310 Type: common.AttributeString, 311 }, 312 AttrLang: { 313 Name: AttrLang, 314 Type: common.AttributeString, 315 }, 316 } 317 } 318 319 // "id": 500, 320 // "main": "Rain", 321 // "description": "небольшой дождь", 322 // "icon": "10d" 323 func WeatherCondition(w ProductWeather) (state supervisor.ActorState) { 324 325 //fmt.Println("------") 326 //debug.Println(w) 327 328 var n, winter bool 329 n = string(w.Icon[len(w.Icon)-1]) == "n" 330 331 switch w.Id { 332 333 // Thunderstorm 334 335 //thunderstorm with light rain 336 case 200: 337 state = weather.GetActorState(weather.StateLightRainAndThunder, "", n, winter) 338 //thunderstorm with rain 339 case 201: 340 state = weather.GetActorState(weather.StateRainAndThunder, "", n, winter) 341 //thunderstorm with heavy rain 342 case 202: 343 state = weather.GetActorState(weather.StateHeavyRainAndThunder, "", n, winter) 344 //light thunderstorm 345 case 210: 346 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 347 //thunderstorm 348 case 211: 349 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 350 //heavy thunderstorm 351 case 212: 352 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 353 //ragged thunderstorm 354 case 221: 355 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 356 //thunderstorm with light drizzle 357 case 230: 358 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 359 //thunderstorm with drizzle 360 case 231: 361 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 362 //thunderstorm with heavy drizzle 363 case 232: 364 state = weather.GetActorState(weather.StateHeavyRainShowersAndThunder, "", n, winter) 365 366 // Drizzle 367 368 //light intensity drizzle 369 case 300: 370 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 371 //drizzle 372 case 301: 373 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 374 //heavy intensity drizzle 375 case 302: 376 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 377 //light intensity drizzle rain 378 case 310: 379 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 380 //drizzle rain 381 case 311: 382 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 383 //heavy intensity drizzle rain 384 case 312: 385 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 386 //shower rain and drizzle 387 case 313: 388 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 389 //heavy shower rain and drizzle 390 case 314: 391 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 392 //shower drizzle 393 case 321: 394 state = weather.GetActorState(weather.StateLightRainShowers, "", n, winter) 395 396 // Rain 397 398 //light rain 399 case 500: 400 state = weather.GetActorState(weather.StateLightRain, "", n, winter) 401 //moderate rain 402 case 501: 403 state = weather.GetActorState(weather.StateRain, "", n, winter) 404 //heavy intensity rain 405 case 502: 406 state = weather.GetActorState(weather.StateHeavyRain, "", n, winter) 407 //very heavy rain 408 case 503: 409 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 410 //extreme rain 411 case 504: 412 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 413 //freezing rain 414 case 511: 415 state = weather.GetActorState(weather.StateHeavySleetShowers, "", n, winter) 416 //light intensity shower rain 417 case 520: 418 // todo fix 419 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 420 //shower rain 421 case 521: 422 // todo fix 423 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 424 //heavy intensity shower rain 425 case 522: 426 // todo fix 427 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 428 //ragged shower rain 429 case 531: 430 // todo fix 431 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 432 433 // Snow 434 435 //light snow 436 case 600: 437 state = weather.GetActorState(weather.StateLightSnow, "", n, winter) 438 //Snow 439 case 601: 440 state = weather.GetActorState(weather.StateSnow, "", n, winter) 441 //Heavy snow 442 case 602: 443 state = weather.GetActorState(weather.StateHeavySnow, "", n, winter) 444 //Sleet 445 case 611: 446 state = weather.GetActorState(weather.StateLightSleetShowers, "", n, winter) 447 //Light shower sleet 448 case 612: 449 state = weather.GetActorState(weather.StateSleetShowers, "", n, winter) 450 //Shower sleet 451 case 613: 452 state = weather.GetActorState(weather.StateHeavySleetShowers, "", n, winter) 453 //Light rain and snow 454 case 615: 455 state = weather.GetActorState(weather.StateLightSleetShowers, "", n, winter) 456 //Rain and snow 457 case 616: 458 state = weather.GetActorState(weather.StateLightSleetShowers, "", n, winter) 459 //Light shower snow 460 case 620: 461 state = weather.GetActorState(weather.StateLightSnowShowers, "", n, winter) 462 //Shower snow 463 case 621: 464 state = weather.GetActorState(weather.StateSnowShowers, "", n, winter) 465 //Heavy shower snow 466 case 622: 467 state = weather.GetActorState(weather.StateHeavySnowShowers, "", n, winter) 468 469 // Atmosphere 470 471 //Mist 472 case 701: 473 state = weather.GetActorState(weather.StateFog, "", n, winter) 474 //Smoke 475 case 711: 476 state = weather.GetActorState(weather.StateFog, "", n, winter) 477 //Haze 478 case 721: 479 state = weather.GetActorState(weather.StateFog, "", n, winter) 480 //Dust sand/dust whirls 481 case 731: 482 state = weather.GetActorState(weather.StateFog, "", n, winter) 483 //Fog 484 case 741: 485 state = weather.GetActorState(weather.StateFog, "", n, winter) 486 //Sand 487 case 751: 488 state = weather.GetActorState(weather.StateFog, "", n, winter) 489 //Dust 490 case 761: 491 state = weather.GetActorState(weather.StateFog, "", n, winter) 492 //Ash volcanic ash 493 case 762: 494 state = weather.GetActorState(weather.StateFog, "", n, winter) 495 //Squall 496 case 771: 497 state = weather.GetActorState(weather.StateFog, "", n, winter) 498 //Tornado 499 case 781: 500 state = weather.GetActorState(weather.StateFog, "", n, winter) 501 502 // Clear 503 504 //clear sky 505 case 800: 506 state = weather.GetActorState(weather.StateClearSky, "", n, winter) 507 508 // Clouds 509 510 // few clouds: 11-25% 511 case 801: 512 state = weather.GetActorState(weather.StateFair, "", n, winter) 513 // scattered clouds: 25-50% 514 case 802: 515 state = weather.GetActorState(weather.StateFair, "", n, winter) 516 // broken clouds: 51-84% 517 case 803: 518 state = weather.GetActorState(weather.StatePartlyCloudy, "", n, winter) 519 // overcast clouds: 85-100% 520 case 804: 521 state = weather.GetActorState(weather.StateCloudy, "", n, winter) 522 523 default: 524 log.Errorf("unknown weather id %d", w.Id) 525 } 526 527 //debug.Println(state) 528 529 return 530 }