diff --git a/src/openweather.py b/src/openweather.py new file mode 100755 index 0000000000000000000000000000000000000000..b0ef358971ff185fc42898cc40e69d088bf22e1a --- /dev/null +++ b/src/openweather.py @@ -0,0 +1,112 @@ +#!/usr/bin/python3 + +import sys +import rospy +import std_msgs.msg +import json +import requests + +import lrs_msgs_common.msg +from pyutil.wdbutil import geo_to_world, nswtogp, wtogp + +api_key = "2de5fb3719ab37651e9c2d73a3a57d67" #Tommy +api_key = "d684d103cd8904100468d5835afdc4a1" # Ali + +# "lat": 48.21, +# "lon": 16.37, +# "timezone": "Europe/Vienna", +# "timezone_offset": 7200, +# "current": { +# "dt": 1599750851, +# "sunrise": 1599711904, +# "sunset": 1599758285, +# "temp": 23.46, +# "feels_like": 23.02, +# "pressure": 1019, +# "humidity": 64, +# "dew_point": 16.26, +# "uvi": 5.12, +# "clouds": 20, +# "visibility": 10000, +# "wind_speed": 3.6, +# "wind_deg": 340, +# "weather": [ +# { +# "id": 801, +# "main": "Clouds", +# "description": "few clouds", +# "icon": "02d" +# } +# ] +# }, +# +# + +def seconds_timer_callback(data): + print("." , end="", flush=True) + +def minute_timer_callback(data): + print("+", flush=True) + +def timer_callback(data): + global pub + resp = wtogp(0, 0, 0) + lat = resp.latitude + lon = resp.longitude + + if len(api_key) != 32: + print("API KEY NOT VALID:", api_key) + return + + print("Getting Weather For:", lat, lon) + + url = "https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s&units=metric" % (lat, lon, api_key) + response = requests.get(url) + data = json.loads(response.text) + print(data) + msg = lrs_msgs_common.msg.OpenWeather() + msg.lat = data["coord"]["lat"] + msg.lon = data["coord"]["lon"] + msg.id = data["id"] + msg.timezone = data["timezone"] + msg.dt = data["dt"] + msg.sunrise = data["sys"]["sunrise"] + msg.sunset = data["sys"]["sunset"] + msg.temp = data["main"]["temp"] + msg.temp_min = data["main"]["temp_min"] + msg.temp_max = data["main"]["temp_max"] + msg.feels_like = data["main"]["feels_like"] + msg.pressure = data["main"]["pressure"] + msg.humidity = data["main"]["humidity"] + msg.clouds = data["clouds"]["all"] + msg.visibility = data["visibility"] + msg.wind_speed = data["wind"]["speed"] + msg.wind_deg = data["wind"]["deg"] + msg.weather_id = data["weather"][0]["id"] + msg.weather_main = data["weather"][0]["main"] + msg.weather_description = data["weather"][0]["description"] + msg.weather_icon = data["weather"][0]["icon"] + pub.publish(msg) + +if __name__ == '__main__': + rospy.init_node('openweather', anonymous=True) + + ns = rospy.get_namespace ().rstrip("/") + print("NS:", ns) + + api_key = rospy.get_param('~apikey', api_key) + + print("APIKEY:", api_key) + + pub = rospy.Publisher ("/openweather", lrs_msgs_common.msg.OpenWeather, queue_size = 10, latch=True) + + rospy.Timer(rospy.Duration(10), seconds_timer_callback, oneshot=False) + + rospy.Timer(rospy.Duration(60), minute_timer_callback, oneshot=False) + + rospy.Timer(rospy.Duration(2), timer_callback, oneshot=True) + + rospy.Timer(rospy.Duration(600), timer_callback, oneshot=False) + + rospy.spin() +