Commit f8bc20c7 authored by GromeTT's avatar GromeTT
Browse files

ENH: Added weather service

parent 1789f138
......@@ -24,7 +24,6 @@ def __init__(self, name, ref_managing_actor):
def predict(self):
pass
def clamp_moisture(self, moisture):
if moisture < 0:
moisture = 0
......@@ -32,35 +31,61 @@ def clamp_moisture(self, moisture):
moisture = 1024
return moisture
def calculate_soil_humidity(self, humidity, day):
new_humidity = humidity
weather_main = ""
weather = day.get("weather", [])
for item in weather:
if "main" in item:
weather_main = item.get("main", "").lower()
break
if "rain" in weather_main:
val = 40
new_humidity -= val
else:
val = 20
new_humidity -= val
new_humidity = self.clamp_moisture(new_humidity)
return new_humidity
def predictMoistures(self, location, moisture):
results = {}
today = datetime.date.today()
time_delta = datetime.timedelta(days=1)
# city = location
# url = "https://api.openweathermap.org/data/2.5/weather?q={}&appid=9d388754797e766959195d9320522f94&units=metric".format(
# city
# )
# res = requests.get(url)
# data = res.json()
# temp = data["main"]["humidity"]
city = location
url = "https://api.openweathermap.org/data/2.5/forecast?q={}&appid=9d388754797e766959195d9320522f94&units=metric".format(
city
)
res = requests.get(url)
data = res.json()
days = data.get("list", [])
if len(days) >= 3:
day0 = days[0]
new_humidity = self.calculate_soil_humidity(moisture, day0)
results["moisture_0"] = new_humidity
day1 = days[1]
new_humidity = self.calculate_soil_humidity(moisture, day1)
results["moisture_1"] = new_humidity
day2 = days[2]
new_humidity = self.calculate_soil_humidity(moisture, day2)
results["moisture_2"] = new_humidity
else:
results["moisture_0"] = self.clamp_moisture(moisture + 20)
results["moisture_1"] = self.clamp_moisture(moisture - 50)
results["moisture_2"] = self.clamp_moisture(moisture + 20)
day_0 = today + time_delta
results["moisture_0"] = moisture + 20
# results["moisture_0"] = clamp_moisture(moisture + 20)
results["valid_from_0"] = to_start_datetime(day_0)
results["valid_to_0"] = to_end_datetime(day_0)
day_1 = day_0 + time_delta
results["moisture_1"] = moisture - 50
# results["moisture_1"] = clamp_moisture(moisture - 50)
results["valid_from_1"] = to_start_datetime(day_1)
results["valid_to_1"] = to_end_datetime(day_1)
day_2 = day_1 + time_delta
results["moisture_2"] = moisture +20
# results["moisture_2"] = clamp_moisture(moisture + 20)
results["valid_from_2"] = to_start_datetime(day_2)
results["valid_to_2"] = to_end_datetime(day_2)
return results
......@@ -403,7 +403,6 @@ def update_soil_sensor(self):
humidity_data = {"properties": {"humidity": humidity}}
res = s3i_repo.updateFeature(thing_id, "id12", humidity_data)
print(res)
m_report_feature = self.features.get("fml40::MoisturePredictionReport", None)
m_report_proxy = m_report_feature.proxy()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment