-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·75 lines (58 loc) · 2.08 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
from multiprocessing import Process
from flask import Flask, render_template, redirect
from control import control_loop
from sensors import read_sensors
from switch import Switch
from interface import read_interface, write_interface
from settings import INTERFACE_TEMP, INTERFACE_ACTIVE, COOL_PIN
import asyncio
from functools import wraps
import datetime as dt
def async_action(f):
@wraps(f)
def wrapped(*args, **kwargs):
return asyncio.run(f(*args, **kwargs))
return wrapped
def create_app(test_config=None):
# create and configure the app
app = Flask('koelkast', instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
)
# a simple page that says hello
@app.route('/', methods=('GET', 'POST'))
@async_action
async def index():
"""
Main page with option to turn cooling on/off and change the goal temp.
"""
from forms.temperature_form import TemperatureForm
form = TemperatureForm()
# This slows the page down significantly.
# Acceptable since the page is hardly used.
current_temp = await read_sensors() # Final entry is average.
active = read_interface(INTERFACE_ACTIVE)
if form.validate_on_submit():
write_interface(INTERFACE_TEMP, form.temperature.data)
# Provide current goal temp.
form.temperature.data = read_interface(INTERFACE_TEMP)
return render_template('index.html', form=form, current_temp=current_temp[-1], active=active)
@app.route('/start/', methods=('GET', 'POST'))
def start():
"""
Route to activate the control loop
"""
write_interface(INTERFACE_ACTIVE, 1)
return redirect('/')
@app.route('/stop/', methods=('GET', 'POST'))
def stop():
"""
Route to deactivate the control loop
"""
write_interface(INTERFACE_ACTIVE, 0)
return redirect('/')
return app
# Start temperature control loop.
switch = Switch(COOL_PIN)
control_process = Process(target=control_loop, args=(switch,))
control_process.start()