-
Notifications
You must be signed in to change notification settings - Fork 1
/
annadata.py
280 lines (266 loc) · 18.6 KB
/
annadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# See readme.md for instructions on running this code.
import copy
from math import log10, floor
from zulip_bots.bots.annadata import utils
from typing import Any, Dict, List
from zulip_bots.lib import BotHandler
from soil import *
from fire import *
from pollen import *
from location import *
from pincode import *
from weather import *
from waterVapour import *
from air import *
class AnnadataHandler:
'''
This plugin allows users to receive various real time information about farming.
One can receive information about soil, fire alert, pollen and weed data, weather forecast, air quality and water vapour content.
User can receive this information by providing either their geolocation coordinates, pin code or city name
The message '@annadata help' posts a short description of how to use
the plugin, along with a list of all supported commands.
'''
def usage(self) -> str:
return '''
This plugin allows users to receive various real time information about farming.
One can receive information about soil, fire alert, pollen and weed data, weather forecast, air quality and water vapour content.
User can receive this information by providing either their geolocation coordinates, pin code or city name
The message '@annadata help' posts a short description of how to use
the plugin, along with a list of all supported commands.
'''
def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None:
bot_response = get_bot_converter_response(message, bot_handler)
bot_handler.send_reply(message, bot_response)
def get_bot_converter_response(message: Dict[str, str], bot_handler: BotHandler) -> str:
content = message['content']
words = content.lower().split()
convert_indexes = [i for i, word in enumerate(words) if word == "@annadata"]
convert_indexes = [-1] + convert_indexes
results = []
for convert_index in convert_indexes:
if (convert_index + 1) < len(words) and words[convert_index + 1] == 'help':
results.append(utils.HELP_MESSAGE)
continue
# soil geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'soil' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
soilInfo = getSoilInfoGeo(lat, lng)
results.append('Soil Information: \n scantime: {} \n soil_temperature: {} \n soil_moisture: {}'.format(soilInfo["scantime"],
soilInfo["soil_temperature"],
soilInfo["soil_moisture"]))
continue
# soil name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'soil' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
soilInfo = getSoilInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Soil Information: \n scantime: {} \n soil_temperature: {} \n soil_moisture: {}'.format(soilInfo["scantime"],
soilInfo["soil_temperature"],
soilInfo["soil_moisture"]))
continue
# soil pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'soil' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
soilInfo = getSoilInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Soil Information: \n scantime: {} \n soil_temperature: {} \n soil_moisture: {}'.format(soilInfo["scantime"],
soilInfo["soil_temperature"],
soilInfo["soil_moisture"]))
continue
# fire geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'fire' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
info = getFireInfoGeo(lat, lng)
results.append('Fire Information: \n confidence: {} \n Fire radiative power in MW: {} \n Event detection time: {} \n Distance (in kms) from fire location: {}'.format(info["confidence"],
info["frp"],
info["detection_time"],
info["distance"]))
continue
# fire name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'fire' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
info = getFireInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Fire Information: \n confidence: {} \n Fire radiative power in MW: {} \n Event detection time: {} \n Distance (in kms) from fire location: {}'.format(info["confidence"],
info["frp"],
info["detection_time"],
info["distance"]))
continue
# fire pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'fire' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
info = getFireInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Fire Information: \n confidence: {} \n Fire radiative power in MW: {} \n Event detection time: {} \n Distance (in kms) from fire location: {}'.format(info["confidence"],
info["frp"],
info["detection_time"],
info["distance"]))
continue
# pollen geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'pollen' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
info = getPollenInfoGeo(lat, lng)
results.append('Pollen Information: \n Grass Pollen Count: {} particles/m3 \n Grass Pollen Risk: {} \n Tree Pollen Count: {} particles/m3 \n Tree Pollen Risk: {} \n Weed Pollen Count: {} particles/m3 \n Weed Pollen Risk: {}'.format(info["grass_pollen_count"],
info["grass_pollen_risk"],
info["tree_pollen_count"],
info["tree_pollen_risk"],
info["weed_pollen_count"],
info["weed_pollen_risk"]))
continue
# pollen name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'pollen' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
info = getPollenInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Pollen Information: \n Grass Pollen Count: {} particles/m3 \n Grass Pollen Risk: {} \n Tree Pollen Count: {} particles/m3 \n Tree Pollen Risk: {} \n Weed Pollen Count: {} particles/m3 \n Weed Pollen Risk: {}'.format(info["grass_pollen_count"],
info["grass_pollen_risk"],
info["tree_pollen_count"],
info["tree_pollen_risk"],
info["weed_pollen_count"],
info["weed_pollen_risk"]))
continue
# pollen pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'pollen' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
info = getPollenInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Pollen Information: \n Grass Pollen Count: {} particles/m3 \n Grass Pollen Risk: {} \n Tree Pollen Count: {} particles/m3 \n Tree Pollen Risk: {} \n Weed Pollen Count: {} particles/m3 \n Weed Pollen Risk: {}'.format(info["grass_pollen_count"],
info["grass_pollen_risk"],
info["tree_pollen_count"],
info["tree_pollen_risk"],
info["weed_pollen_count"],
info["weed_pollen_risk"]))
continue
# weather geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'weather' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
info = getWeatherInfoGeo(lat, lng)
results.append('Weather Information: \n Temperature: {} Fahrenheit \n Dew Point: {} Fahrenheit \n Relative Humidity: {} \n Pressure: {} millibar \n Wind speed: {} miles per hour \n Wind Gust: {} miles per hour \n Wind Bearing: {} \n Cloud Cover Percentage: {} \n Visibility: {} miles\n Ozone: {} Dobson'.format(info["temperature"],
info["dewPoint"],
info["humidity"],
info["pressure"],
info["windSpeed"],
info["windGust"],
info["windBearing"],
info["cloudCover"],
info["visibility"],
info["ozone"]))
continue
# weather name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'weather' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
info = getWeatherInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Weather Information: \n Temperature: {} Fahrenheit \n Dew Point: {} Fahrenheit \n Relative Humidity: {} \n Pressure: {} millibar \n Wind speed: {} miles per hour \n Wind Gust: {} miles per hour \n Wind Bearing: {} \n Cloud Cover Percentage: {} \n Visibility: {} miles\n Ozone: {} Dobson'.format(info["temperature"],
info["dewPoint"],
info["humidity"],
info["pressure"],
info["windSpeed"],
info["windGust"],
info["windBearing"],
info["cloudCover"],
info["visibility"],
info["ozone"]))
continue
# weather pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'weather' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
info = getWeatherInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Weather Information: \n Temperature: {} Fahrenheit \n Dew Point: {} Fahrenheit \n Relative Humidity: {} \n Pressure: {} millibar \n Wind speed: {} miles per hour \n Wind Gust: {} miles per hour \n Wind Bearing: {} \n Cloud Cover Percentage: {} \n Visibility: {} miles\n Ozone: {} Dobson'.format(info["temperature"],
info["dewPoint"],
info["humidity"],
info["pressure"],
info["windSpeed"],
info["windGust"],
info["windBearing"],
info["cloudCover"],
info["visibility"],
info["ozone"]))
continue
# watervapour geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'watervapour' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
info = getWaterVapourInfoGeo(lat, lng)
results.append('Water vapor levels in meters (m): {}'.format(info["water_vapor"]))
continue
# watervapour name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'watervapour' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
info = getWaterVapourInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Water vapor levels in meters (m): {}'.format(info["water_vapor"]))
continue
# watervapour pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'watervapour' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
info = getWaterVapourInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Water vapor levels in meters (m): {}'.format(info["water_vapor"]))
continue
# air geolocation 28 77
if (convert_index + 4) < len(words) and words[convert_index + 1] == 'air' and words[convert_index + 2] == 'geolocation':
lat = words[convert_index + 3]
lng = words[convert_index + 4]
info = getAirInfoGeo(lat, lng)
results.append('Air Quality information: \n NO2: {} ppb \n PM10: {} ug/m3 \n PM2.5: {} ug/m3 \n CO: {} ppm \n SO2: {} ppb \n OZONE: {} ppb \n Air quality index (AQI): {} \n Pollutant: {} \n concentration {} \n category: {}'.format(info["NO2"],
info["PM10"],
info["PM25"],
info["CO"],
info["SO2"],
info["OZONE"],
info["AQI"],
info["pollutant"],
info["concentration"],
info["category"]))
continue
# air name bengaluru
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'air' and words[convert_index + 2] == 'name':
city_name = words[convert_index + 3]
coordinates = getLocationFromName(city_name)
info = getAirInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Air Quality information: \n NO2: {} ppb \n PM10: {} ug/m3 \n PM2.5: {} ug/m3 \n CO: {} ppm \n SO2: {} ppb \n OZONE: {} ppb \n Air quality index (AQI): {} \n Pollutant: {} \n concentration {} \n category: {}'.format(info["NO2"],
info["PM10"],
info["PM25"],
info["CO"],
info["SO2"],
info["OZONE"],
info["AQI"],
info["pollutant"],
info["concentration"],
info["category"]))
continue
# air pincode 121001
if (convert_index + 3) < len(words) and words[convert_index + 1] == 'air' and words[convert_index + 2] == 'pincode':
pincode = words[convert_index + 3]
city_name = getLocationFromPincode(pincode)
coordinates = getLocationFromName(city_name)
info = getAirInfoGeo(coordinates["lat"], coordinates["lng"])
results.append('Air Quality information: \n NO2: {} ppb \n PM10: {} ug/m3 \n PM2.5: {} ug/m3 \n CO: {} ppm \n SO2: {} ppb \n OZONE: {} ppb \n Air quality index (AQI): {} \n Pollutant: {} \n concentration {} \n category: {}'.format(info["NO2"],
info["PM10"],
info["PM25"],
info["CO"],
info["SO2"],
info["OZONE"],
info["AQI"],
info["pollutant"],
info["concentration"],
info["category"]))
continue
else:
results.append('Too few arguments given. ' + utils.QUICK_HELP)
new_content = ''
for idx, result in enumerate(results, 1):
new_content += ((str(idx) + '. Result: ') if len(results) > 1 else '') + result + '\n'
return new_content
handler_class = AnnadataHandler