def delete_session():
    # 토큰의 uid와 입력된 uid가 일지하는지 검사한다.
    try:
        auth_token = request.headers['Authorization']
        decoded = session_token.decode(auth_token)
        uid = request.args.get('uid')
        if (decoded['uid'] != uid):
            return invalid_param('Invalid User')
        elif decoded['exp'] < int(time.time()):
            return session_exp()
    except KeyError:
        return invalid_session()

    # 세션 레코드를 삭제한다.
    connection = db.get_connection()
    try:
        result = session_model.delete(auth_token, connection)
        connection.commit()
    except IntegrityError as e:
        connection.rollback()
        return db_error.response(e)
    finally:
        connection.close()

    return success('OK')
def update_session():
    # 토큰의 uid와 입력된 uid가 일지하는지 검사한다.
    try:
        auth_token = request.headers['Authorization']
        decoded = session_token.decode(auth_token)
        data = request.json
        uid = data['uid']
        if (decoded['uid'] != uid):
            return invalid_param('Invalid User')
        elif decoded['exp'] < int(time.time()):
            return session_exp()
    except KeyError:
        return invalid_session()
    except jwt.exceptions.DecodeError:
        return invalid_session()

    # 새로운 토근을 발급한다
    connection = db.get_connection()
    try:
        new_token = session_token.encode(uid)
        session_model.update(auth_token, new_token, connection)
        connection.commit()
    except IntegrityError as e:
        connection.rollback()
        return db_error.response(e)
    finally:
        connection.close()

    return success({'uid': uid, 'token': new_token})
Beispiel #3
0
def report_measurement(room_id):
    # 토큰의 uid와 입력된 uid가 일치하는지 검사한다.
    try:
        access_token = request.headers['Authorization']
    except KeyError:
        return invalid_session()

    connection = db.get_connection()
    try:
        room_info = room_model.get_room(room_id, connection)

        if access_key.valid(access_token, room_info['ip'],
                            room_info['id']) == False:
            return invalid_session()

        data = request.json
        datetime = data["datetime"]
        temperature = data["temperature"]
        humidity = data["humidity"]

        measurement_model.insert((room_id, datetime, temperature, humidity),
                                 connection)
        connection.commit()
    except IntegrityError as e:
        connection.rollback()
        return db_error(e)
    except KeyError:
        connection.rollback()
        return invalid_param()
    finally:
        connection.close()

    return success('OK')
def get_room_info_from_request():
    data = request.json
    try:
        name = data["name"]
        ip = data["ip"]
    except KeyError:
        return invalid_param()

    try:
        max_temperature = data["max_temperature"]
    except KeyError:
        max_temperature = 26.00

    try:
        min_temperature = data["min_temperature"]
    except KeyError:
        #max_temperature = 28.00
        min_temperature = 18.00

    try:
        max_humidity = data["max_humidity"]
    except KeyError:
        max_humidity = 60.00

    try:
        min_humidity = data["min_humidity"]
    except KeyError:
        min_humidity = 30.00

    return (name, ip, max_temperature, min_temperature, max_humidity,
            min_humidity)
def update_humidifier(room_id):
    # 토큰의 uid와 입력된 uid가 일지하는지 검사한다.
    try:
        auth_token = request.headers['Authorization']
        decoded = session_token.decode(auth_token)
        if decoded == False:
            return invalid_session()
        elif decoded['exp'] < int(time.time()):
            return session_exp()
    except KeyError:
        return invalid_session()
    except jwt.exceptions.DecodeError:
        return invalid_session()

    connection = db.get_connection()

    try:
        ip = room_model.get_room_ip(room_id, connection)
        url = SENSOR_URL.format(ip, 'humidifier')
        headers = {'content-type': 'application/json'}
        data = request.json
        payload = {
            "status": data["status"]
        }
        response = requests.request("PUT", url, data=json.dumps(payload), headers=headers)

        if (response.status_code == 200 and data["status"] == 'ON'):
            print ('ON')
        else:
            print ('OFF')

    except IntegrityError as e:
        return db_error(e)
    finally:
        connection.close()

    if (response.status_code == 200):
        return success('OK')
    elif (response.status_code == 400):
        return invalid_param()