-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
109 lines (93 loc) · 2.85 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys,os
import psycopg2 as psql
import json
import requests
from relay import relay
from flask import Flask
from water import water
from turn import turn
from flask import jsonify
from sensors import getserial
from flask import request
from mycronfile import schedulingaturn
from mycronfile import schedulingawater
import json
app = Flask(__name__)
from flask.ext.bcrypt import Bcrypt
bcrypt=Bcrypt(app)
@app.route('/test', methods=['GET'])
def index():
return jsonify({"result":"test"})
@app.route('/')
def test():
from sht1x.Sht1x import Sht1x
data = 24
clock = 23
#print ">>> mysht1x = Sht1x(%d, %d, Sht1x.GPIO_BCM)" % (data,clock)
mysht1x = Sht1x(data, clock, Sht1x.GPIO_BCM)
#print ">>> mysht1x.read_temperature_C()"
temp = mysht1x.read_temperature_C()
# print "temp", temp
rh = mysht1x.read_humidity()
#print "rh =",rh
dewPoint = mysht1x.calculate_dew_point(temp, rh)
#print "dewpoint=", dewPoint
pidata={"temp": temp, "humidity": rh, "dewPoint":dewPoint}
#conv = [pidata]
s_data = json.dumps(pidata)
return s_data
@app.route('/water', methods=['POST'])
def watering ():
water()
return jsonify({"result":"finished water"})
@app.route('/turn', methods=['POST'])
def turning ():
turn()
return jsonify({"result":"finished turn"})
@app.route('/data', methods=['GET'])
def data():
conn = psql.connect("dbname=piData")
cur = conn.cursor()
cur.execute("Select * from data")
#r = [dict((cur.description[i][0], value) \
# for i, value in enumerate(row)) for row in cur.fetchall()]
#conn.close()
#return jsonify(**r[0])
x= dict([("data", cur.fetchall())])
conn.close()
return jsonify(x)
@app.route('/watersched', methods=['POST'])
def waterschedule():
parsed_json = json.loads(request.data)
#print "1", request.data
#print "2", parsed_json['m']
mins= parsed_json['min']
hours=parsed_json['hours']
dom=parsed_json['dom']
print "dom", dom
m=parsed_json['m']
dow=parsed_json['dow']
#print request.data
schedulingawater(mins, hours, dom, m, dow);
return jsonify({"result":"did it!"})
@app.route('/turnsched', methods=['POST'])
def turnschedule():
parsed_json = json.loads(request.data)
#print "1", request.data
#print "2", parsed_json['m']
mins= parsed_json['min']
hours=parsed_json['hours']
dom=parsed_json['dom']
m=parsed_json['m']
dow=parsed_json['dow']
#print request.data
schedulingaturn(mins, hours, dom, m, dow);
return jsonify({"result":"did it!"})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)