示例#1
0
def update_account(update_account):  # noqa: E501
    """Update account password

    Update a given account. The user name cannot be modified. # noqa: E501

    :param update_account:
    :type update_account: dict | bytes

    :rtype: str
    """

    if connexion.request.is_json:
        update_account = UpdateAccount.from_dict(connexion.request.get_json())  # noqa: E501
    db = PostgresDB()
    info = db.get_account_id_by_username_and_password(update_account.username, update_account.old_password)
    if "Error" in info:
        return info
    if len(info) > 0:
        info = info[0]
        if update_account.old_password == update_account.new_password:
            return jsonify(msg='Passwords match'), 409
        error = db.update_account(info[0], update_account.new_password)
        if error:
            return jsonify(msg=error)
        return jsonify(msg="Password changed"), 200
    return jsonify(msg='Incorrect password'), 204
def get_legitimate_person_info(device_mac):  # noqa: E501
    """Return info of a legitimate person given a device MAC

    Return information of a legitimate person given MAC device # noqa: E501

    :param device_mac: Legitimate person's Device MAC
    :type device_mac: str

    :rtype: str
    """
    db = PostgresDB()
    legitimate_person_info = db.get_legitimate_person_info(device_mac)
    if "Error" in legitimate_person_info:
        return legitimate_person_info
    data = {"legitimate": []}
    for row in legitimate_person_info:
        data['legitimate'].append({
            "id": row[0],
            "person_MAC": row[1],
            "person_name": row[2],
            "person_phone_number": row[3],
            "notification": row[4],
            "relation": row[5]
        })

    return data
def put_legitimate_person(update_legitimate_person):  # noqa: E501
    """Update a device MAC

    Update a legitimate person # noqa: E501

    :param update_legitimate_person: 
    :type update_legitimate_person: dict | bytes

    :rtype: str
    """
    db = PostgresDB()

    if connexion.request.is_json:
        update_legitimate_person = UpdateLegitimatePerson.from_dict(
            connexion.request.get_json())  # noqa: E501

    legitimate_person = db.get_legitimate_person_info(
        update_legitimate_person.old_mac)
    if "Error" in legitimate_person:
        return legitimate_person

    error = db.update_legitimate_person(update_legitimate_person.new_mac,
                                        update_legitimate_person.old_mac)
    if error:
        return error
    return "Record updated successfully into legitimate table"
def delete_door(door_id):  # noqa: E501
    """Delete a door

    Delete a door # noqa: E501

    :param door_id: ID of door
    :type door_id: int

    :rtype: str
    """
    db = PostgresDB()
    error = db.delete_door(door_id)
    if error:
        return error
    return 'Door deleted successfully'
def delete_legitimate_person(device_mac):  # noqa: E501
    """Delete the legitimate person info.

    Delete the legitimate person given a device MAC. # noqa: E501

    :param device_mac: Legitimate person's Device MAC
    :type device_mac: str

    :rtype: str
    """
    db = PostgresDB()
    error = db.delete_legitimate_person(device_mac)
    if error:
        return error
    return "Record deleted successfully"
def update_door_state(door):  # noqa: E501
    """Update door state

    Update door state # noqa: E501

    :param body: 
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        door = UpdateDoor.from_dict(connexion.request.get_json())  # noqa: E501

    db = PostgresDB()
    error = db.update_door_state(door.state, door.name)
    if error:
        return error
    return 'Door updated successfully'
def add_door(door):  # noqa: E501
    """Add a new door to the system

    Add a new door to the system # noqa: E501

    :param body: 
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        door = Door.from_dict(connexion.request.get_json())  # noqa: E501

    db = PostgresDB()
    error = db.add_new_door(door.name)
    if error:
        return error
    return "Door inserted successfully"
def add_location(location):  # noqa: E501
    """Add a new user location to the system

    Add a new location of the user # noqa: E501

    :param location:
    :type location: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        location = Location.from_dict(
            connexion.request.get_json())  # noqa: E501
    db = PostgresDB()
    error = db.insert_new_location(location.location)
    if error:
        return jsonify(msg=error)
    return jsonify(msg='Human detected at %s' % location.location)
def get_location():  # noqa: E501
    """Get user location

    Get user location # noqa: E501


    :rtype: str
    """
    db = PostgresDB()
    user_location = db.get_last_location()
    if "Error" in user_location:
        return jsonify(msg=user_location)
    if len(user_location) > 0:
        loc = user_location[0]
        if loc is None:
            return '', 204
        return jsonify({"location": loc[1]})
    return abort(404)
def get_historic_location():  # noqa: E501
    """Get a historic of locations

    Get a historic of locations # noqa: E501


    :rtype: str
    """
    db = PostgresDB()
    historial = db.get_locations()
    if "Error" in historial:
        return jsonify(msg=historial)
    if len(historial) > 0:
        data = {"historial": []}
        for row in historial:
            data['historial'].append({"id": row[0], "name": row[1]})
        return jsonify(data), 200
    else:
        return '', 204
def add_visit(visit):  # noqa: E501
    """Add a visit to historic

    Add a visit to historic # noqa: E501

    :param visit: 
    :type visit: dict | bytes

    :rtype: str
    """

    if connexion.request.is_json:
        visit = Visit.from_dict(connexion.request.get_json())  # noqa: E501

    db = PostgresDB()
    error = db.add_visit(visit.person_mac, visit.date, visit.time)
    if error:
        return error
    return "Record inserted successfully into historic table"
def get_all_doors_state():  # noqa: E501
    """Get all doors state

    Get all doors state # noqa: E501


    :rtype: str
    """
    db = PostgresDB()
    doors = db.get_all_doors_state()
    if "Error" in doors:
        return doors
    data = {"doors": []}

    for row in doors:
        data['doors'].append({
            "id": row[1],
            "name": row[0],
            "status": row[2],
        })
    return jsonify(data)
示例#13
0
def get_account(account_id):  # noqa: E501
    """Get account info

    Get a given user name # noqa: E501

    :param account_id: The user id of the  logged in user
    :type account_id: int

    :rtype: str
    """
    db = PostgresDB()
    account = db.get_account_by_id(account_id)
    if "Error" in account:
        return jsonify(msg=account)
    if len(account) > 0:
        account = account[0]
        return jsonify({"username": account[1],
                        "birthdate": account[3],
                        "age": account[4]})

    return jsonify(msg='Account not found'), 404
示例#14
0
def delete_account(account_id):  # noqa: E501
    """Delete the account

    Delete a given user account # noqa: E501

    :param account_id: Account id
    :type account_id: int

    :rtype: str
    """
    db = PostgresDB()

    account = db.get_account_by_id(account_id)
    if "Error" in account:
        return jsonify(msg=account)
    if len(account) > 0:
        error = db.delete_account(account_id)
        if error:
            return jsonify(msg=error)
        return jsonify(msg='The account has been removed'), 200
    else:
        return jsonify(msg='Account not found' % account_id), 404
示例#15
0
def add_account(account):  # noqa: E501
    """Add a new account to the system

    Adds a user account to the system # noqa: E501

    :param account:
    :type account: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        account = Account.from_dict(connexion.request.get_json())  # noqa: E501
    db = PostgresDB()
    info = db.get_account_id_by_username(account.username)
    if "Error" in info:
        return jsonify(msg=info)
    if len(info) > 0:
        return jsonify(msg='The user %s already exists' % account.username), 409
    error = db.add_new_account(account.username, account.password, account.birthdate, account.age)
    if error:
        return jsonify(msg=error)
    return jsonify(msg="OK. User %s created" % account.username), 200
def get_door_state(door_id):  # noqa: E501
    """Get a door state

    Get a door state # noqa: E501

    :param door_id: ID of door
    :type door_id: int

    :rtype: str
    """
    db = PostgresDB()
    door = db.get_door_state(door_id)
    if "Error" in door:
        return door
    data = {"door": []}

    for row in door:
        data['door'].append({
            "id": row[1],
            "name": row[0],
            "status": row[2],
        })
    return jsonify(data)
def get_all_historic():  # noqa: E501
    """Get all visits from historic

    Get all visits from historic # noqa: E501


    :rtype: str
    """
    db = PostgresDB()
    historic_records = db.get_all_historic()
    if "Error" in historic_records:
        return historic_records

    data = {"historic": []}

    for row in historic_records:
        data['historic'].append({
            "id": row[0],
            "person_MAC": row[1],
            "date": row[2],
            "hour": row[3]
        })
    return jsonify(data)
def get_all_legitimate_person_info():  # noqa: E501
    """Return all the information of each legitimate person

    Return information of all the legitimate people # noqa: E501


    :rtype: str
    """
    db = PostgresDB()
    legitimate_records = db.get_all_legitimate_person_info()
    if "Error" in legitimate_records:
        return legitimate_records
    data = {"legitimate": []}
    for row in legitimate_records:
        data['legitimate'].append({
            "id": row[0],
            "person_MAC": row[1],
            "person_name": row[2],
            "person_phone_number": row[3],
            "notification": row[4],
            "relation": row[5]
        })

    return data
def post_legitimate_person(legitimate_person):  # noqa: E501
    """Add a new legitimate person

    Addition of a new legitimate person # noqa: E501

    :param legitimate_person: 
    :type legitimate_person: dict | bytes

    :rtype: str
    """

    if connexion.request.is_json:
        legitimate_person = LegitimatePerson.from_dict(
            connexion.request.get_json())  # noqa: E501

    db = PostgresDB()
    error = db.add_legitimate_person(legitimate_person.person_mac,
                                     legitimate_person.person_name,
                                     legitimate_person.person_phone_number,
                                     legitimate_person.notification,
                                     legitimate_person.relation)
    if error:
        return error
    return "Record inserted successfully into legitimate table"
def get_visit(id):  # noqa: E501
    """Get one visit information

    Get one visit information # noqa: E501

    :param id: ID of visit to get information
    :type id: int

    :rtype: str
    """
    db = PostgresDB()
    visit = db.get_visit(id)
    if "Error" in visit:
        return visit
    data = {"visit": []}
    for row in visit:
        data['visit'].append({
            "id": row[0],
            "person_MAC": row[1],
            "date": row[2],
            "hour": row[3]
        })

    return jsonify(data)