-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_controller_without_globals.py
More file actions
354 lines (296 loc) · 10.5 KB
/
temp_controller_without_globals.py
File metadata and controls
354 lines (296 loc) · 10.5 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
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
#-*- coding: utf-8 -*-
import eventlet
eventlet.monkey_patch()
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 Lock, Event, Timer, Thread
from datetime import datetime
from time import sleep
import atexit
from queue import Queue
async_mode = None
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
stop_event = Event()
pool = ThreadPoolExecutor(6)
q = Queue()
future_1 = None
future_2 = None
future_3 = None
future_4 = None
future_5 = None
future_6 = None
condenser_heating_cycle_flag = False
compressor_cut_off_temp = 33.0
compressor_cut_in_temp = 36.0
temp_differential = 1.5
stop_event.set()
set_temperature = 75
timer_run_flag = False
room_temperature = 0
condenser_temperature = 0
def initialize_queue():
print('init')
global q
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
q.put(compressor_state)
q.put(fan_state)
q.put(heating_coil_state)
q.put(heater_1_state)
q.put(heater_2_state)
def kill_all_outputs():
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
def output_control():
global condenser_temperature
condenser_temperature = condenser_temperature - 1.0
if set_temperature < 75:
cooling_control()
elif set_temperature >= 75:
heating_control()
def cooling_control():
global q
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
global condenser_temperature
if condenser_temperature <= compressor_cut_off_temp:
while condenser_temperature <= compressor_cut_in_temp:
compressor_state = 0
fan_state = 0
heating_coil_state = 1
condenser_temperature = condenser_temperature + 1
if q.empty() == False:
with q.mutex:
q.queue.clear()
q.put(compressor_state)
q.put(fan_state)
q.put(heating_coil_state)
q.put(heater_1_state)
q.put(heater_2_state)
socketio.emit('timeChange', {'data': 'Condenser Heating Cycle'}, broadcast=True, namespace='/test')
eventlet.sleep(1)
if room_temperature <= (float(set_temperature) - temp_differential):
compressor_state = 0
fan_state = 1
elif room_temperature >= (float(set_temperature) + temp_differential):
compressor_state = 1
fan_state = 1
if q.empty() == False:
with q.mutex:
q.queue.clear()
q.put(compressor_state)
q.put(fan_state)
q.put(heating_coil_state)
q.put(heater_1_state)
q.put(heater_2_state)
def heating_control():
global q
compressor_state = 0
fan_state = 0
heating_coil_state = 0
heater_1_state = 0
heater_2_state = 0
if room_temperature >= (float(set_temperature) + temp_differential):
heater_1_state = 0
heater_2_state = 0
elif room_temperature <= (float(set_temperature) - temp_differential):
heater_1_state = 1
heater_2_state = 1
if q.empty() == False:
with q.mutex:
q.queue.clear()
q.put(compressor_state)
q.put(fan_state)
q.put(heating_coil_state)
q.put(heater_1_state)
q.put(heater_2_state)
def outputs_task():
global q
on = 'On'
off = 'Off'
while True:
if q.empty() == True:
pass
else:
compressor_state = q.get()
fan_state = q.get()
heating_coil_state = q.get()
heater_1_state = q.get()
heater_2_state = q.get()
print(compressor_state)
print(fan_state)
print(heating_coil_state)
print(heater_1_state)
print(heater_2_state)
q.task_done()
if compressor_state == 0:
socketio.emit('compressorStateIO', {'comp': off}, namespace='/test', broadcast=True)
else:
socketio.emit('compressorStateIO', {'comp': on}, namespace='/test', broadcast=True)
if fan_state == 0:
socketio.emit('fanStateIO', {'fan': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('fanStateIO', {'fan': 'On'}, namespace='/test', broadcast=True)
if heating_coil_state == 0:
socketio.emit('heatCoilStateIO', {'heatCoil': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heatCoilStateIO', {'heatCoil': 'On'}, namespace='/test', broadcast=True)
if heater_1_state == 0:
socketio.emit('heater1StateIO', {'heater1': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heater1StateIO', {'heater1': 'On'}, namespace='/test', broadcast=True)
if heater_2_state == 0:
socketio.emit('heater2StateIO', {'heater2': 'Off'}, namespace='/test', broadcast=True)
else:
socketio.emit('heater2StateIO', {'heater2': 'On'}, namespace='/test', broadcast=True)
eventlet.sleep(1)
def temp_1_task():
global room_temperature
room_temperature = 60.0
while True:
eventlet.sleep(2)
#room_temperature = '{0:.2f}'.format(room_temperature)
# tempfile1 = open('/sys/bus/w1/devices/28-000009b7715e/w1_slave')
# thetext1 = tempfile1.read()
# tempfile1.close()
# tempdata1 = thetext1.split("\n")[1].split(" ")[9]
# room_temperature = ((float(tempdata1[2:])/1000) *1.8) + 32
# room_temperature = '{0:.2f}'.format((room_temperature))
def temp_2_task():
global condenser_temperature
condenser_temperature = 55.0
while True:
eventlet.sleep(2)
#condenser_temperature = '{0:.2f}'.format(condenser_temperature)
# tempfile2 = open('/sys/bus/w1/devices/28-000009b55d17/w1_slave')
# thetext2 = tempfile2.read()
# tempfile2.close()
# tempdata2 = thetext2.split('\n')[1].split(" ")[9]
# condenser_temperature = ((float(tempdata2[2:])/1000) * 1.8) + 32
# condenser_temperature = '{0:.2f}'.format((condenser_temperature))
@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 timer_task(setup_time):
print('starting....')
setup_time = setup_time * 60
global timer_run_flag
timer_run_flag = True
#socketio.emit('refresh', broadcast=True, namespace='/test')
while (not stop_event.is_set() and setup_time >= 0):
eventlet.sleep(1)
output_control()
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
kill_all_outputs()
stop_event.clear()
timer_run_flag = False
socketio.emit('timeChange', {'data': ""}, broadcast=True, namespace='/test')
def temp_out_task():
global room_temperature
global condenser_temperature
while True:
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)
@socketio.on('connect_event', namespace='/test')
def connect_start():
global timer_run_flag
timer_run_flag = False
global future_1
global future_2
global future_3
global future_5
socketio.emit('tempChange', {'data': set_temperature}, broadcast=True, namespace='/test')
pool = ThreadPoolExecutor(5)
future_1 = pool.submit(temp_1_task)
future_2 = pool.submit(temp_2_task)
future_3 = pool.submit(temp_out_task)
future_5 = pool.submit(outputs_task)
@socketio.on('inc_temp_event', namespace='/test')
def increment_temp(message):
temp = str(message['data'])
temp = temp.strip('F')
temp = temp.strip(u'\u00B0')
temp = int(temp)
if temp >= 150:
temp = 150
else:
temp = temp + 5
global set_temperature
set_temperature = temp
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
@socketio.on('dec_temp_event', namespace='/test')
def decrement_temp(message):
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
global set_temperature
set_temperature = temp
socketio.emit('tempChange', {'data': temp}, broadcast=True, namespace='/test')
@socketio.on('start_event', namespace='/test')
def start_timer(message):
global future_4
if future_4 != None:
future_4.cancel()
future_4 = None
global timer_run_flag
if timer_run_flag is False:
stop_event.clear()
hours = int(message['data1'])
minutes = int(message['data2'])
setup_time = (hours * 60) + minutes
future_4 = pool.submit(timer_task, (setup_time))
#socketio.emit('disableStartButton', broadcast=True, namespace='/test')
else:
pass
@socketio.on('stop_event', namespace='/test')
def stop_timer():
kill_all_outputs()
global timer_run_flag
timer_run_flag = False
global future_4
global future_6
if future_4 != None:
future_4.cancel()
future_4 = None
stop_event.set()
#atexit.register(kill_all_outputs)
if __name__ == '__main__':
initialize_queue()
socketio.run(app)