forked from Nna2291/home1517
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (130 loc) · 3.7 KB
/
main.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
from time import sleep
from flask import Flask, jsonify, redirect, render_template
from flask import request
app = Flask(__name__)
queue = []
temp_history = []
status = {'vent': 0, 'heat': 0, 'auto_light1': 0, 'auto_light2': 0, 'temp': -1.0, 'hum': -1.0, 'temp_limit': 25,
'red1': 0, 'green1': 0, 'blue1': 0, 'red2': 0, 'green2': 0, 'blue2': 0, 'light_val': 0, 'light_status1': 0,
'light_status2': 0, 'door': 0}
@app.route('/')
def index():
return render_template('home.html')
@app.route('/cool', methods=["POST"])
def cool():
if request.json['cool']:
status['vent'] = 1
elif not request.json['cool']:
status['vent'] = 0
return 'ok'
@app.route('/heat', methods=["POST"])
def heat():
if request.json['heat']:
status['heat'] = 1
elif not request.json['heat']:
status['heat'] = 0
return 'ok'
@app.route('/first_floor')
def first_floor():
return jsonify(status)
@app.route('/door', methods=["POST", "GET"])
def door():
if request.method == "POST":
if status["door"] == 1:
status["door"] = 0
else:
status["door"] = 1
return jsonify({'door_stat': status["door"]})
return render_template('door.html')
@app.route('/lights')
def light():
return render_template('lights.html')
@app.route('/other')
def other():
return render_template('other.html')
@app.route('/get_data')
def data():
return jsonify(status)
@app.route('/change_color1', methods=["POST"])
def colch1():
tt = request.json
status['red1'] = tt['r']
status['blue1'] = tt['b']
status['green1'] = tt['g']
return 'oka'
@app.route('/change_color2', methods=["POST"])
def colch2():
tt = request.json
status['red2'] = tt['r']
status['blue2'] = tt['b']
status['green2'] = tt['g']
return 'oka'
@app.route('/change_light1', methods=["POST"])
def change_light1():
tt = request.json
if tt['light']:
status['light_status1'] = 1
else:
status['light_status1'] = 0
return 'oka'
@app.route('/change_light2', methods=["POST"])
def change_light2():
tt = request.json
if tt['light']:
status['light_status2'] = 1
else:
status['light_status2'] = 0
return 'oka'
@app.route('/change_auto1', methods=["POST"])
def change_auto1():
tt = request.json
if tt['auto']:
status['auto_light1'] = 1
else:
status['auto_light1'] = 0
return 'oka'
@app.route('/change_auto2', methods=["POST"])
def change_auto2():
tt = request.json
if tt['auto']:
status['auto_light2'] = 1
else:
status['auto_light2'] = 0
return 'oka'
@app.route('/second_floor')
def second_floor():
jio = request.args.get('light_val')
status['light_val'] = float(jio)
hum = request.args.get('hum')
temp = request.args.get('temp')
try:
status['hum'] = float(hum)
status['temp'] = float(temp)
except:
status['hum'] = 0
status['temp'] = 0
return jsonify(status)
@app.route('/queue')
def queue():
global queue
if queue:
q = jsonify(queue)
queue = []
return q
else:
sleep(5)
return 0
@app.route('/temperature_and_humidity', methods=['GET', 'POST'])
def temp_and_hum():
global temp_history, status
if request.method == 'POST':
temp, hum = float(request.form['temp']), float(request.form['hum'])
if len(temp_history) >= 4:
temp_history = [temp] + temp_history[:3]
if temp_history[-1] - temp > 5 and temp > status['temp_limit']:
pass
status['temp'] = temp
status['hum'] = hum
return redirect('/')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')