-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathac_module.py
More file actions
executable file
·75 lines (65 loc) · 2.83 KB
/
Copy pathac_module.py
File metadata and controls
executable file
·75 lines (65 loc) · 2.83 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
# ac_module.py
from flask import render_template, redirect, url_for
from requests.exceptions import ConnectionError
from models import Logs
import requests
class AC:
def __init__(self, arduino_url):
self.arduino_url = arduino_url
self.temp = 0
self.status = "..."
def update_ac_status(self):
try:
response_status = requests.get(f'{self.arduino_url}/acStatus')
response_status.raise_for_status() # Raise an exception for HTTP errors
ac_status = response_status.text
print (ac_status)
return ac_status
except ConnectionError as e:
return 'unknown' # Handle connection errors
def update_ac_temp(self):
try:
response_status = requests.get(f'{self.arduino_url}/acTemp')
response_status.raise_for_status() # Raise an exception for HTTP errors
ac_status = response_status.text
if ac_status:
self.temp = ac_status
else:
self.temp = f'ERROR!, {ac_status}'
return self.temp
except ConnectionError as e:
return 'unknown' # Handle connection errors
def toggle_ac(self, session, class_session_in_progress, db, socketio, ac_id):
if 'logged_in' in session:
if class_session_in_progress:
error = "Access to the AC is locked during class session."
return render_template('loggedIn.html', error=error)
response = requests.get(f'{self.arduino_url}/acControl{ac_id}')
print(f'{self.arduino_url}/acControl{ac_id}')
if response.text == "on":
action = "AC On!"
socketio.emit('update_AC_status', {'status': ['AC On!']})
else:
action = "AC OFF"
socketio.emit('update_AC_status', {'status': ['AC Off!']})
print(response.text)
log = Logs(username=session['username'], action=action)
db.session.add(log)
db.session.commit()
return action
else:
return redirect(url_for('login'))
def adjust_temp(self, session, class_session_in_progress, db, command, socketio):
if 'logged_in' in session:
if class_session_in_progress:
error = "Access to the AC is locked during class session."
return render_template('loggedIn.html', error=error)
response = requests.get(f'{self.arduino_url}/{command}')
action = f"{command} temp to {response.text}"
socketio.emit('update_AC_temp', {'status': action})
log = Logs(username=session['username'], action=action)
db.session.add(log)
db.session.commit()
return action
else:
return redirect(url_for('login'))