-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflaskTest.py
More file actions
89 lines (72 loc) · 2.33 KB
/
flaskTest.py
File metadata and controls
89 lines (72 loc) · 2.33 KB
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
# A very simple Flask Hello World app for you to get started with...
from flask import Flask
from flask import request
import json
import requests
app = Flask(__name__)
def getWeather(la, lo):
lat = str(la)
lon = str(lo)
app_key = '0b0daf2f59f49d1b7d050208c7abb95f'
api_url_weather = (
'https://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&appid=' + app_key)
response = requests.get(api_url_weather)
raining = json.loads(response.text) # set string to dictionary
print(raining['weather'][0]['main'])
return (raining['weather'][0]['main'])
@app.route('/', methods=['POST'])
def index():
'''Receive post information and get weather'''
try:
dictPartText = request.get_data()
dictPart = json.loads(dictPartText)
except:
result = "{'Error': 'Can't parse data'}'"
result = str.encode(result)
return (result)
try:
print(dictPart)
metadata = dictPart["metadata"]
print(metadata)
metadata = metadata["gateways"][0]
except:
result = "{'Error': 'Can't find gateway-data'}'"
result = str.encode(result)
return (result)
try:
lat = metadata["latitude"]
lon = metadata["longitude"]
except:
result = "{'Error': 'Can't find lon-lat data'}'"
result = str.encode(result)
return (result)
try:
result = str(getWeather(lat, lon))
result = str(result)
except:
result = "{'Error': 'Can't acces the WeatherAPI'}'"
result = str.encode(result)
return (result)
try:
downlink = dictPart["downlink_url"]
dev_id = dictPart['dev_id']
result_weather = {"raintype": str(result)}
data1 = {"dev_id":dev_id,"payload_fields":result_weather}
print(result)
print(data1)
r = requests.post(downlink, json=data1)
print(r.status_code)
print(r.content)
except:
result = "{'Error': 'Error, cant post result'}'"
result = str.encode(result)
return (result)
return (result)
@app.route('/', methods=['GET'])
def index1():
'''Just a test for connecting, returns hello world'''
if(request.method == 'GET'):
return 'Hello UserGET'
if __name__ == '__main__':
#run webserver
app.run(debug=True, use_reloader=False)