示例#1
0
def add_new_device():
    status_response = {}
    status_response['status'] = ''
    status_response['message'] = ''

    id_schema = Schema({Required('id'): constants.json_schema.ID_VALIDATOR})

    try:
        new_device_id = id_schema(request.json)['id']

        if helpers.get_device(new_device_id) is not None:
            status_response[
                'status'] = constants.strings.STATUS_CODE_DEVICE_EXISTS
            status_response['message'] = 'Device with that ID already exists!'
        else:
            new_device_data = {
                'id': new_device_id,
                'cluster': None,
                'type': None,
                'gateway': None
            }

            devices = file_io.read_devices()
            devices['data'].append(new_device_data)
            file_io.write_devices(devices)

            status_response['status'] = constants.strings.STATUS_CODE_SUCCESS
            status_response['message'] = 'New device successfully created!'
    except OSError:
        status_response['status'] = constants.strings.STATUS_CODE_ERROR
        status_response['message'] = 'Server Side Error Occurred!'
    except MultipleInvalid:
        abort(400)

    return jsonify(status_response)
示例#2
0
def register_device_to_cluster():
    json_validator = Schema({
        Required('device_id'):
        constants.json_schema.ID_VALIDATOR,
        Required('cluster_id'):
        constants.json_schema.ID_VALIDATOR
    })

    status_response = {}
    status_response['status'] = ''
    status_response['message'] = ''

    try:
        data = json_validator(request.json)

        device = helpers.get_device(data['device_id'])
        cluster = helpers.get_cluster(data['cluster_id'])

        device_exists = device is not None
        cluster_exists = cluster is not None
        is_free_device = False if device_exists is not True else device[
            'cluster'] is None

        if device_exists is not True:
            status_response[
                'status'] = constants.strings.STATUS_CODE_MISSING_DEVICE
            status_response['message'] = 'No Device with that ID exists!'
        elif cluster_exists is not True:
            status_response[
                'status'] = constants.strings.STATUS_CODE_MISSING_CLUSTER
            status_response['message'] = 'No Cluster with that ID exists!'
        elif is_free_device is not True:
            status_response[
                'status'] = constants.strings.STATUS_CODE_MEMBERSHIP_EXISTS
            status_response[
                'message'] = 'Device already a member of a cluster!'
        else:
            clusters = file_io.read_clusters()
            cluster_index = helpers.find_index(clusters, cluster['id'])
            clusters['data'][cluster_index]['devices'].append(device['id'])
            file_io.write_clusters(clusters)

            devices = file_io.read_devices()
            device_index = helpers.find_index(devices, device['id'])
            devices['data'][device_index]['cluster'] = cluster['id']
            file_io.write_devices(devices)

            status_response['status'] = constants.strings.STATUS_CODE_SUCCESS
            status_response[
                'message'] = 'Device registered to cluster successfully!'
    except MultipleInvalid:
        abort(400)

    return jsonify(status_response)
示例#3
0
def initialize_device():
    status_response = {}
    status_response['status'] = ''
    status_response['message'] = ''

    try:
        device_data = constants.json_schema.DEVICE_SCHEMA(request.json)

        device = helpers.get_device(device_data['id'])
        gateway = helpers.get_gateway(device_data['gateway'])

        device_exist = device is not None
        gateway_exist = gateway is not None
        cluster_match = device_data['cluster'] == device['cluster']

        if device_data['cluster'] is not None:
            cluster = helpers.get_cluster(device_data['cluster'])
            cluster_exist = cluster is not None
        else:
            cluster_exist = True

        if device_exist is not True:
            status_response['status'] = constants.strings.STATUS_CODE_MISSING_DEVICE
            status_response['message'] = 'No Device with that ID exists!'
        elif gateway_exist is not True:
            status_response['status'] = constants.strings.STATUS_CODE_MISSING_GATEWAY
            status_response['message'] = 'No Gateway with that ID exists!'
        elif cluster_exist is not True:
            status_response['status'] = constants.strings.STATUS_CODE_MISSING_CLUSTER
            status_response['message'] = 'No Cluster with that ID exists!'
        elif cluster_match is not True:
            status_response['status'] = constants.strings.STATUS_CODE_CLUSTER_MISMATCH
            status_response['message'] = 'Incorrect cluster defined!'
        else:
            if register_to_gateway(device_data) is True:
                status_response['status'] = constants.strings.STATUS_CODE_SUCCESS
                status_response['message'] = 'Device initialized successfully!'
            else:
                status_response['status'] = constants.strings.STATUS_CODE_FAILED
                status_response['message'] = 'Failed registering device to gateway!'

    except MultipleInvalid:
        abort(400)
    finally:
        return jsonify(status_response)
示例#4
0
def delete_device():
    status_response = {}
    status_response['status'] = ''
    status_response['message'] = ''

    id_schema = Schema({Required('id'): constants.json_schema.ID_VALIDATOR})

    try:
        device_id = id_schema(request.json)['id']
        target_device = helpers.get_device(device_id)

        if target_device is None:
            status_response[
                'status'] = constants.strings.STATUS_CODE_MISSING_DEVICE
            status_response['message'] = 'No device with that ID exists!'
        else:
            if target_device['cluster'] is not None:
                # Decouple from cluster
                clusters = file_io.read_clusters()
                cluster_index = helpers.find_index(clusters,
                                                   target_device['cluster'])
                clusters['data'][cluster_index]['devices'].remove(
                    target_device)
                file_io.write_clusters(clusters)

            devices = file_io.read_devices()
            device_index = helpers.find_index(devices, target_device['id'])
            devices['data'].pop(device_index)
            file_io.write_devices(devices)

            status_response['status'] = constants.strings.STATUS_CODE_SUCCESS
            status_response['message'] = 'Device deleted successfully!'
    except OSError:
        status_response['status'] = constants.strings.STATUS_CODE_ERROR
        status_response['message'] = 'Server Side Error Occurred!'
    except MultipleInvalid:
        abort(400)

    return jsonify(status_response)
示例#5
0
def ota_device():
    status_response = {}
    status_response['status'] = ''
    status_response['message'] = ''

    id_schema = Schema({Required('id'): constants.json_schema.ID_VALIDATOR})

    try:
        device_id = id_schema(request.json)['id']
        target_device = helpers.get_device(device_id)

        if target_device is None:
            status_response[
                'status'] = constants.strings.STATUS_CODE_MISSING_DEVICE
            status_response['message'] = 'No Device with that ID exists!'
        elif target_device['type'] is None:
            status_response[
                'status'] = constants.strings.STATUS_CODE_UNINITIALIZED
            status_response['message'] = 'Device has not been initialized!'
        else:
            start = timeit.default_timer()
            status_response = uftp_handlers.distribute_updated_code(
                target_device['id'])

            if status_response[
                    'status'] == constants.strings.STATUS_CODE_SUCCESS:
                print('Code Transfer succeeded in ' +
                      str(timeit.default_timer() - start) + ' seconds')
                target_topic = constants.mqtt.CLUSTER_TOPIC + '/' + target_device[
                    'cluster'] if target_device[
                        'cluster'] is not None else constants.mqtt.GLOBAL_TOPIC

                mqtt_client.publish(target_topic,
                                    'update|device|' + target_device['id'],
                                    qos=2)
    except MultipleInvalid:
        abort(400)

    return jsonify(status_response)
示例#6
0
def initialize_params(target_id, is_cluster=False):
    init_obj = {}
    init_obj['status_code'] = ''
    init_obj['message'] = ''
    gateway_ids = []
    target_dir = None

    if is_cluster is False:
        target_device = helper.get_device(target_id)

        if target_device is None:
            init_obj[
                'status_code'] = constants.strings.STATUS_CODE_MISSING_DEVICE
            init_obj['message'] = 'No Device with that ID exists!'
        else:
            gateway_ids = [] if target_device['gateway'] is None else [
                target_device['gateway']
            ]
            target_dir = constants.paths.BASE_DIR / 'devices' / target_device[
                'id']
    else:
        target_cluster = helper.get_cluster(target_id)

        if target_cluster is None:
            init_obj[
                'status_code'] = constants.strings.STATUS_CODE_MISSING_CLUSTER
            init_obj['message'] = 'No Cluster with that ID exists!'
        else:
            gateway_ids = target_cluster['gateways']
            target_dir = constants.paths.BASE_DIR / 'clusters' / target_cluster[
                'id']

    target_file = None

    if target_dir is not None:
        if target_dir.exists() is not True:
            init_obj[
                'status_code'] = constants.strings.STATUS_CODE_MISSING_DATA
            init_obj['message'] = 'Target Directory not found!'
        elif len(gateway_ids) == 0:
            init_obj[
                'status_code'] = constants.strings.STATUS_CODE_UNINITIALIZED
            init_obj[
                'message'] = 'Target Device/Cluster has not been initialized yet!'
        else:
            init_obj['status_code'] = constants.strings.STATUS_CODE_SUCCESS
            init_obj['message'] = 'Parameter Initialization Successful!'
            target_type = target_device[
                'type'] if is_cluster is False else target_cluster['type']
            target_file = target_dir / (
                target_id + constants.paths.FILE_EXTENSIONS[target_type])

            if target_file.exists() is not True:
                target_file = None

    if target_file is None:
        init_obj['status_code'] = constants.strings.STATUS_CODE_MISSING_DATA
        init_obj['message'] = 'Target file not found!'

    init_obj['target_file'] = None if target_file is None else str(target_file)
    init_obj['gateway_ids'] = gateway_ids

    return init_obj