コード例 #1
0
ファイル: heater.py プロジェクト: Kyushi/homectrl
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)
コード例 #2
0
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()
コード例 #3
0
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')
コード例 #4
0
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}'})
コード例 #5
0
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
コード例 #6
0
ファイル: index.py プロジェクト: Kyushi/homectrl
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)
コード例 #7
0
ファイル: index.py プロジェクト: Kyushi/homectrl
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)
コード例 #8
0
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')
コード例 #9
0
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')
コード例 #10
0
ファイル: index.py プロジェクト: Kyushi/homectrl
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)
コード例 #11
0
def get_room(room_name):
    db = get_db()
    room = db.execute("SELECT * FROM room WHERE name = ?;",
                      (room_name, )).fetchone()
    return room