-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_controller_run_server.py
373 lines (303 loc) · 10.2 KB
/
temp_controller_run_server.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#-*- coding: utf-8 -*-
#import os
import atexit
import eventlet
eventlet.monkey_patch()
from time import sleep
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, render_template, url_for, redirect, session
from flask_socketio import SocketIO, emit, disconnect
from threading import Event
from datetime import datetime
async_mode = None
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
stop_event = Event()
stop_event.set()
thread_pool = ThreadPoolExecutor(4)
thread_1 = None
thread_2 = None
thread_3 = None
timer_run_flag = False
connect_flag = 0
def outputs_task():
on = 'On'
off = 'Off'
while True:
try:
tempfile = open('outputs.txt', 'r')
outputs = tempfile.readlines()
tempfile.close()
compressor_state = int(outputs[0])
fan_state = int(outputs[1])
heat_coil_state = int(outputs[2])
heater_1_state = int(outputs[3])
heater_2_state = int(outputs[4])
if compressor_state == 0:
socketio.emit('compressorStateIO', {'comp': off}, namespace='/test', broadcast=True)
elif compressor_state == 1:
socketio.emit('compressorStateIO', {'comp': on}, namespace='/test', broadcast=True)
if fan_state == 0:
socketio.emit('fanStateIO', {'fan': off}, namespace='/test', broadcast=True)
elif fan_state == 1:
socketio.emit('fanStateIO', {'fan': on}, namespace='/test', broadcast=True)
if heat_coil_state == 0:
socketio.emit('heatCoilStateIO', {'heatCoil': off}, namespace='/test', broadcast=True)
elif heat_coil_state == 1:
socketio.emit('heatCoilStateIO', {'heatCoil': on}, namespace='/test', broadcast=True)
if heater_1_state == 0:
socketio.emit('heater1StateIO', {'heater1': off}, namespace='/test', broadcast=True)
elif heater_1_state == 1:
socketio.emit('heater1StateIO', {'heater1': on}, namespace='/test', broadcast=True)
if heater_2_state == 0:
socketio.emit('heater2StateIO', {'heater2': off}, namespace='/test', broadcast=True)
elif heater_2_state == 1:
socketio.emit('heater2StateIO', {'heater2': on}, namespace='/test', broadcast=True)
except:
print('Error in outputs_task()')
eventlet.sleep(2)
@app.route('/')
def tempURL():
return redirect(url_for('temp'))
@app.route('/temperature')
def temp():
return render_template('temperature.html')
@app.route('/IO_monitor')
def condTemp():
return render_template('IOmonitor.html')
def update():
return redirect(url_for('temp'))
def temp_out_task():
while True:
try:
tempfile = open('room_temp.txt', 'r')
room_temperature = tempfile.read()
tempfile.close()
tempfile = open('cond_temp.txt', 'r')
condenser_temperature = tempfile.read()
tempfile.close()
eventlet.sleep(2)
socketio.emit('currentTemp', {'temp1': room_temperature}, namespace='/test', broadcast=True)
socketio.emit('currentTempIO', {'temp1': room_temperature}, namespace='/test', broadcast=True)
socketio.emit('condenserTempIO', {'temp2': condenser_temperature}, namespace='/test', broadcast=True)
except:
print('Error in temp_out_task()')
@socketio.on('inc_temp_event', namespace='/test')
def increment_temp(message):
try:
temp = str(message['data'])
temp = temp.strip('F')
temp = temp.strip(u'\u00B0')
temp = int(temp)
if temp >= 160:
temp = 160
else:
temp = temp + 5
f = open('set_temp.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(temp) + '\n')
f.flush()
f.close()
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
except:
print('Error in increment_temp()')
@socketio.on('dec_temp_event', namespace='/test')
def decrement_temp(message):
try:
temp = str(message['data'])
temp = temp.strip('F')
temp = temp.strip(u'\u00B0')
temp =int(temp)
if temp <= 35:
temp = 35
else:
temp = temp - 5
f = open('set_temp.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(temp) + '\n')
f.flush()
f.close()
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
except:
print('Error in decrement_temp()')
def continuous_timer_task():
global timer_run_flag
timer_run_flag = True
while (not stop_event.is_set()):
eventlet.sleep(1)
try:
socketio.emit('timeChange', {'data': 'Running in continuous mode...'}, broadcast=True, namespace='/test')
except:
print('Error in continuous_timer_task()')
run_flag = 0
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
stop_event.clear()
timer_run_flag = False
socketio.emit('timeChange', {'data': ""}, broadcast=True, namespace='/test')
def timer_task(setup_time):
global thread_2
global timer_run_flag
setup_time = setup_time * 60
timer_run_flag = True
while (not stop_event.is_set() and setup_time > 0):
try:
hours = int(setup_time/3600)
minutes = int(setup_time/60) % 60
seconds = int(setup_time) % 60
if minutes < 10:
minutes = '0' + str(minutes)
if seconds < 10:
seconds = '0' + str(seconds)
hours_minutes_seconds = str(hours) + ':' + str(minutes) + ':' + str(seconds)
socketio.emit('timeChange', {'data': hours_minutes_seconds}, broadcast=True, namespace='/test')
setup_time = setup_time - 1
except:
print('Error in timer_task()')
eventlet.sleep(1)
run_flag = 0
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
#thread_2 = thread_pool.submit(timer_task, (setup_time))
stop_event.clear()
timer_run_flag = False
socketio.emit('timeChange', {'data': ""}, broadcast=True, namespace='/test')
@socketio.on('start_event', namespace='/test')
def start_timer(message):
global thread_2
global timer_run_flag
try:
if timer_run_flag == False:
if thread_2 != None:
thread_2.cancel()
thread_2 = None
stop_event.clear()
hours = (message['data1'])
minutes = (message['data2'])
if hours == '':
hours = 0
if minutes == '':
minutes = 0
setup_time = (int(hours) * 60) + int(minutes)
if setup_time == 0:
run_flag = 1
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
thread_2 = thread_pool.submit(continuous_timer_task)
else:
run_flag = 1
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
thread_2 = thread_pool.submit(timer_task, (setup_time))
else:
pass
except:
print('Error in start_timer()')
@socketio.on('stop_event', namespace='/test')
def stop_timer():
global thread_2
global timer_run_flag
try:
timer_run_flag = False
run_flag = 0
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
if thread_2 != None:
thread_2.cancel()
thread_2 = None
stop_event.set()
except:
print('Error in stop_timer()')
def clear_run_flag():
run_flag = 0
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
def timed_start():
count = 0
global thread_2
while count <= 720:
print(datetime.now())
eventlet.sleep(60)
count = count + 1
try:
global timer_run_flag
if timer_run_flag == False:
if thread_2 != None:
thread_2.cancel()
thread_2 = None
stop_event.clear()
hours = 0
minutes = 0
if hours == '':
hours = 0
if minutes == '':
minutes = 0
setup_time = (int(hours) * 60) + int(minutes)
if setup_time == 0:
run_flag = 1
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
thread_2 = thread_pool.submit(continuous_timer_task)
else:
run_flag = 1
f = open('run_flag.txt', 'a')
f.seek(0)
f.truncate()
f.write(str(run_flag))
f.flush()
f.close()
thread_2 = thread_pool.submit(timer_task, (setup_time))
else:
pass
except:
print('Error in start_timer()')
@socketio.on('connect_event', namespace='/test')
def connect_start():
try:
tempfile = open('set_temp.txt', 'r')
set_temperature = tempfile.read()
tempfile.close()
socketio.emit('tempChange', {'data': set_temperature}, broadcast=True, namespace='/test')
except:
print('Error in connect_start()')
def cleanup_routine():
clear_run_flag()
if __name__ == '__main__':
clear_run_flag()
thread_1 = thread_pool.submit(temp_out_task)
thread_3 = thread_pool.submit(outputs_task)
socketio.run(app)
atexit.register(cleanup_routine)
#thread_4 = thread_pool.submit(timed_start)