示例#1
0
def login(content):
    """
    Try login
    """
    import datetime
    try:
        """
        Parameters requirements are here
        """
        username = content.get('username')
        password = content.get("password")
        assert((type(username) is str) and (len(username) < 30))
        assert((type(password) is str) and (len(password) < 30))
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # verify from db
    try:
        user = UserSvc.verify(username, password)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch

    session['username'] = user.username
    session['user_id'] = user.id
    session['email'] = user.email
    session['last_login'] = str(datetime.datetime.now())

    return response_json_ok({'username': user.username, 'email': user.email,
                             'user_id': user.id}, "Login successful.")
示例#2
0
def login(content):
    """
    Try login
    """
    import datetime
    try:
        """
        Parameters requirements are here
        """
        username = content.get('username')
        password = content.get("password")
        assert ((type(username) is str) and (len(username) < 30))
        assert ((type(password) is str) and (len(password) < 30))
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # verify from db
    try:
        user = UserSvc.verify(username, password)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch

    session['username'] = user.username
    session['user_id'] = user.id
    session['email'] = user.email
    session['last_login'] = str(datetime.datetime.now())

    return response_json_ok(
        {
            'username': user.username,
            'email': user.email,
            'user_id': user.id
        }, "Login successful.")
示例#3
0
def record_insert(content, username, location_name, device_name):
    """
    insert a record
    """
    # Get user id
    try:
        """
        Parameters requirements are here
        """
        user_id = session.get('user_id', None)
        if not user_id:
            password = content.get("password")
            user = UserSvc.verify(username, password)
            user_id = user.id
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # Get location, deices ids
    try:
        location = LocationSvc.get(user_id, name=location_name)
        device = DeviceSvc.get(user_id, name=device_name)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # No record

    # Not put data in there
    try:
        PropertySvc.save_record_dict(device.id, location.id,
                                     content.get("content"))
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # Publish MQTT data
    topic = "control/" + username + "/" + location_name + "/" + device_name
    mqtt_client.publish(topic=str(topic), payload=request.get_data(), qos=1)
    print("MQTT publish: " + topic + " payload: ")
    print(request.get_data())
    # return
    return response_json_ok({}, "Insertion good")
示例#4
0
def record_insert(content, username, location_name, device_name):
    """
    insert a record
    """
    # Get user id
    try:
        """
        Parameters requirements are here
        """
        user_id = session.get('user_id', None)
        if not user_id:
            password = content.get("password")
            user = UserSvc.verify(username, password)
            user_id = user.id
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # Get location, deices ids
    try:
        location = LocationSvc.get(user_id, name=location_name)
        device = DeviceSvc.get(user_id, name=device_name)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # No record

    # Not put data in there
    try:
        PropertySvc.save_record_dict(device.id, location.id, content.get("content"))
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # Publish MQTT data
    topic = "control/" + username + "/" + location_name + "/" + device_name
    mqtt_client.publish(topic=str(topic), payload=request.get_data(), qos=1)
    print("MQTT publish: " + topic + " payload: ")
    print(request.get_data())
    # return
    return response_json_ok({}, "Insertion good")
示例#5
0
def location_list():
    """
    get list of locations
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        locations = LocationSvc.get_list(user_id)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['locations'] = []
    for obj in locations:
        resp['locations'].append(dict(obj))

    return response_json_ok(resp, "Retrieve locations successful.")
示例#6
0
def property_list(device_id):
    """
    get list of parameter of device
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        parameters = PropertySvc.get_list(user_id, device_id)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['parameters'] = []
    for obj in parameters:
        resp['parameters'].append(dict(obj))

    return response_json_ok(resp, "Retrieve parameters successful.")
示例#7
0
def device_list_in_location(id):
    """
    get list of devices
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        devices = DeviceSvc.get_list(user_id, location_id=id)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['devices'] = []
    for obj in devices:
        resp['devices'].append(dict(obj))

    return response_json_ok(resp, "Retrieve devices successful.")
示例#8
0
def location_list():
    """
    get list of locations
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        locations = LocationSvc.get_list(user_id)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['locations'] = []
    for obj in locations:
        resp['locations'].append(dict(obj))

    return response_json_ok(resp, "Retrieve locations successful.")
示例#9
0
def property_list(device_id):
    """
    get list of parameter of device
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        parameters = PropertySvc.get_list(user_id, device_id)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['parameters'] = []
    for obj in parameters:
        resp['parameters'].append(dict(obj))

    return response_json_ok(resp, "Retrieve parameters successful.")
示例#10
0
def device_list_in_location(id):
    """
    get list of devices
    """
    # session variables required
    user_id = session.get('user_id', None)
    # verify from db
    try:
        devices = DeviceSvc.get_list(user_id, location_id=id)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch
    resp = dict()
    resp['devices'] = []
    for obj in devices:
        resp['devices'].append(dict(obj))

    return response_json_ok(resp, "Retrieve devices successful.")
示例#11
0
def logout():
    """
    Try logout
    """
    try:
        """
        Parameters requirements are here
        """
        pass    # No parameters, only session is required
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # remove the username and id from the session if it's there
    user = session.pop('username', None)
    user_id = session.pop('user_id', None)
    session.pop('last_login', None)
    session.pop('email', None)

    return response_json_ok({'username': user}, "Logout successful.")
示例#12
0
def logout():
    """
    Try logout
    """
    try:
        """
        Parameters requirements are here
        """
        pass  # No parameters, only session is required
    except Exception:
        return response_json_error({}, "Wrong parameter")

    # remove the username and id from the session if it's there
    user = session.pop('username', None)
    user_id = session.pop('user_id', None)
    session.pop('last_login', None)
    session.pop('email', None)

    return response_json_ok({'username': user}, "Logout successful.")
示例#13
0
def location_detail(id):
    """
    get list of locations
    """
    # session variables required
    user_id = session.get('user_id', None)

    try:
        location = LocationSvc.get(user_id, id=id)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch

    try:
        location.rooms = LocationSvc.get_list(user_id, house_id=id)
    except NoRecordError:
        # There is a case of no children rooms
        location.rooms = []

    resp = dict(location)

    return response_json_ok(resp, "Retrieve the location successful.")
示例#14
0
def device_detail(id):
    """
    get list of devices
    """
    # session variables required
    user_id = session.get('user_id', None)
    try:
        # Get devices
        device = DeviceSvc.get(user_id, id=id)
    except NoRecordError as error:
        return response_json_error({}, str(error))  # Usually username password mismatch

    try:
        # Get children devices
        device.children = DeviceSvc.get_list(user_id, mother_id=id)
    except NoRecordError:
        # There is a case of no children devices
        device.children = []

    resp = dict(device)

    return response_json_ok(resp, "Retrieve devices successful.")
示例#15
0
def location_detail(id):
    """
    get list of locations
    """
    # session variables required
    user_id = session.get('user_id', None)

    try:
        location = LocationSvc.get(user_id, id=id)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch

    try:
        location.rooms = LocationSvc.get_list(user_id, house_id=id)
    except NoRecordError:
        # There is a case of no children rooms
        location.rooms = []

    resp = dict(location)

    return response_json_ok(resp, "Retrieve the location successful.")
示例#16
0
def device_detail(id):
    """
    get list of devices
    """
    # session variables required
    user_id = session.get('user_id', None)
    try:
        # Get devices
        device = DeviceSvc.get(user_id, id=id)
    except NoRecordError as error:
        return response_json_error(
            {}, str(error))  # Usually username password mismatch

    try:
        # Get children devices
        device.children = DeviceSvc.get_list(user_id, mother_id=id)
    except NoRecordError:
        # There is a case of no children devices
        device.children = []

    resp = dict(device)

    return response_json_ok(resp, "Retrieve devices successful.")