-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (67 loc) · 2.6 KB
/
app.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
#!/usr/bin/env python
# encoding: utf-8
from flask import Flask, jsonify, Response, request
from flask_cors import CORS
import requests
import json
from dateutil import parser
import datetime
import pymysql
from config import *
import xml.etree.ElementTree as ET
import WeatherParse as WP
class MyResponse(Response):
@classmethod
def force_type(cls, response, environ=None):
if isinstance(response, (list, dict)):
response = jsonify(response)
return super(Response, cls).force_type(response, environ)
app = Flask(__name__)
CORS(app)
app.response_class = MyResponse
@app.route('/index')
def index():
return jsonify({'message': 'hello'})
@app.route('/special_weather', methods=['GET'])
@app.route('/special_weather/<city>', methods=['GET'])
def cwb_special_weather(city=None):
xmlTree = WP.getWeatherXML('http://opendata.cwb.gov.tw/opendataapi?dataid=W-C0033-002&authorizationkey=%s' % CWB_AUTHORIZATION_KEY)
root = ET.fromstring(xmlTree)
resultJSON = WP.getAllData(root)
if city:
cityInfoSet = WP.sortHazardsCity(root)
temp = city.split(",")
bCheckCity = WP.filterHazardCity(cityInfoSet,set(temp))
if bCheckCity:
return jsonify(resultJSON)
else:
resultDict = {}
resultDict['WeatherAlarm'] = []
return jsonify(resultDict)
else:
return jsonify(resultJSON)
@app.route('/iot/data/<dev_id>/latest', methods=['GET'])
def latest_sensor_data(dev_id=None):
url = 'http://%s:%s/query?pretty=true&db=%s&q=SELECT * FROM "test" WHERE "id"=\'%s\' ORDER BY DESC LIMIT 1' % (INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_DB, dev_id)
print(url)
r = requests.get(url)
data = r.json()['results'][0]['series'][0]
ret_data = dict(zip(data['columns'], data['values'][0]))
ret_data['time'] = (parser.parse(ret_data['time']) + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
return ret_data
@app.route('/planting/resume/<planting_id>', methods=['GET'])
def crop_resume(planting_id=None):
conn = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_DB, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
c = conn.cursor()
c.execute("SELECT * FROM activities WHERE planting_uuid = '%s' ORDER BY date" % planting_id)
activities = c.fetchall()
ret = []
for a in activities:
_ = {}
_['action'] = a['action']
_['date'] = str(a['date'])
_['image'] = a['image_url']
ret.append(_)
return jsonify({'activities': ret})
if __name__ == '__main__':
app.run(debug=True, host=HOST, port=PORT)