def register(room_id): if g.user['usertype_fk'] != 1: response = make_response( "You don't have permission to register a heater", 401) return reponse db = get_db() if request.method == 'POST': error = None mac = request.form['mac'] name = request.form['name'] room_id = request.form['room_id'] if not name: error = "You have to give the heater a name" if db.execute("SELECT * FROM heater WHERE name = ?;", (name, )).fetchone() is not None: error = "This heater is already registered" if error is None: cur = db.execute( "INSERT INTO heater(mac, name, room_fk) VALUES(?, ?, ?);", (mac, name, room_id)) db.commit() return redirect(url_for('room.view', room_id=room_id)) rooms = db.execute("SELECT * FROM room;").fetchall() room = db.execute("SELECT * FROM room WHERE id = ?;", (room_id, )).fetchone() # Scan for thermostats, try to get current status service = DiscoveryService() devices = service.discover(5) macs = [mac for mac in devices if devices[mac] == 'CC-RT-BLE'] db = get_db() heaters = [] for mac in macs: h = {} h['mac'] = mac heater = db.execute( "SELECT h.name, r.name as room FROM heater h JOIN room r ON h.room_fk = r.id WHERE h.mac = ?;", (mac, )).fetchone() if heater is not None: h['status'] = 'registered' h['name'] = heater['name'] h['assigned_to'] = heater['room'] else: h['status'] = 'unassigned' t = eq3bt.Thermostat(mac) try: t.update() h['target_temp'] = t.target_temperature h['mode'] = t.mode_readable except BTLEDisconnectError as e: print(f"Unable to connect to thermostat: {mac}") heaters.append(h) return render_template('heater/register.html', rooms=rooms, room=room, heaters=heaters)
def load_logged_in_user(): user_id = session.get('uid') if user_id is None: g.user = None else: g.user = get_db().execute("SELECT * FROM user WHERE id = ?;", (user_id, )).fetchone()
def register(): db = get_db() has_user = db.execute("SELECT count(*) FROM user;").fetchone()[0] if has_user != 0 or (g.user is not None and g.user.usertype_fk != 1): return redirect(url_for('index')) if request.method == 'POST': error = None name = request.form['name'] email = request.form['email'] usertype = request.form['usertype'] password = request.form['password'] if not name: error = 'You forgot the name' if not email: error = 'You forgot the email' if not password: error = 'You forgot the password' if not usertype: error = "you forgot the user type" if error is None: cur = db.execute( """ INSERT INTO user (name, email, password, usertype_fk) VALUES (?, ?, ?, ?) """, (name, email, generate_password_hash(password), usertype)) db.commit() return redirect(url_for('index')) return render_template('auth/register.html')
def set_temp(room_name): db = get_db() room = get_room(room_name) heaters = get_heaters(room) new_temp = float(request.form['target_temp']) for heater in heaters: heater.thermostat.target_temperature = new_temp return jsonify({'success': f'target temperature set to {new_temp}'})
def get_heaters(room): db = get_db() heaters = db.execute("SELECT * FROM heater WHERE room_fk = ?;", (room['id'], )).fetchall() updated_heaters = [] for heater in heaters: h = Heater(heater) h.room_name = room['name'] h.thermostat = Thermostat(heater['mac']) h.thermostat.update() updated_heaters.append(h) return updated_heaters
def air_end(): db = get_db() heaters = db.execute("SELECT * FROM heater").fetchall() response = {'message': 'success'} for heater in heaters: h = Heater(heater) h.thermostat = Thermostat(h.mac) h.thermostat.mode = 2 # Set mode to auto to return to current desired temperature h.thermostat.update() response[h.name] = { 'temperature': h.thermostat.target_temperature, 'mode': h.thermostat.mode_readable } return jsonify(response)
def air_start(): db = get_db() heaters = db.execute("SELECT * FROM heater").fetchall() response = {'message': 'success'} for heater in heaters: h = Heater(heater) h.thermostat = Thermostat(h.mac) h.thermostat.mode = 3 # set to manual mode (3) h.thermostat.target_temperature = 5 h.thermostat.update() response[h.name] = { 'temperature': h.thermostat.target_temperature, 'mode': h.thermostat.mode_readable } return jsonify(response)
def login(): if request.method == 'POST': db = get_db() error = None email = request.form['email'] password = request.form['password'] user = db.execute("SELECT * FROM user WHERE email = ?", (email, )).fetchone() if user is None or not check_password_hash(user['password'], password): error = "Email unknown or password incorrect" if error is None: session.clear() session['uid'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def register(): if g.user['usertype_fk'] != 1: response = make_response( "You don't have permission to register a room", 401) return reponse if request.method == 'POST': db = get_db() error = None name = request.form['name'] if not name: error = "You have to give the room a name" if db.execute("SELECT * FROM room WHERE name = ?;", (name, )).fetchone() is not None: error = "This room is already registered" if error is None: cur = db.execute("INSERT INTO room(name) VALUES(?);", (name, )) db.commit() return redirect(url_for('index')) return render_template('room/register.html')
def index(): db = get_db() rooms = db.execute("SELECT * FROM room ORDER BY name;").fetchall() if not rooms: return redirect(url_for('room.register')) return render_template('index.html', rooms=rooms)
def get_room(room_name): db = get_db() room = db.execute("SELECT * FROM room WHERE name = ?;", (room_name, )).fetchone() return room