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 create_new_room():
    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()

    params = get_room_info_from_request()

    connection = db.get_connection()
    try:
        room_id = room_model.create_room(params, connection)
        room_model.update_ip(room_id, params[1], connection)
        connection.commit()
        room_info = room_model.get_room(room_id, connection)
    except IntegrityError as e:
        connection.rollback()
        return db_error(e)
    finally:
        connection.close()

    return success(room_info)
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})
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()
def get_users():
    # 토큰의 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:
        user_list = user_model.get_users(connection)
    except IntegrityError as e:
        return db_error(e)
    finally:
        connection.close()

    return success(user_list)
Beispiel #6
0
def get_measurements(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()

    data = request.json

    try:
        start = request.args.get('start')
    except KeyError:
        start = int(time.time()) - (60 * 60 * 24)

    try:
        end = request.args.get('end')
    except KeyError:
        end = start + (60 * 60 * 24)

    connection = db.get_connection()
    try:
        user_list = measurement_model.get_measurements(room_id, start, end,
                                                       connection)
    except IntegrityError as e:
        return db_error(e)
    finally:
        connection.close()

    return success(user_list)
Beispiel #7
0
def insert(token, connection):
    decoded = session_token.decode(token)

    with connection.cursor() as cursor:
        sql = 'INSERT INTO `sessions` (`uid`, `token`, `expiration_date`) VALUES (%s, %s, %s);'
        cursor.execute(sql, (decoded['uid'], token, decoded['exp']))
Beispiel #8
0
def update(old_token, new_token, connection):
    decoded = session_token.decode(new_token)

    with connection.cursor() as cursor:
        sql = 'UPDATE `sessions` SET `token` = %s, `expiration_date` = %s WHERE `token` = %s;'
        cursor.execute(sql, (new_token, decoded['exp'], old_token))