Esempio n. 1
0
def get_device_settings(device, key):
    """ Get device settings from flow cloud

    :param FlowDevice device: flow device object
    :param str key: key  parameter whose associated settings is to be retrieved
    :return: settings
    :rtype: str
    :raises: ValueError, FlowCoreException
    """
    if not (device and key):
        raise ValueError("Invalid function parameters")

    memory_manager = get_memory_manager()
    try:
        cached_device = get_new_device(memory_manager)
        if not cached_device:
            raise FlowCoreException("Failed to create new device",
                                    flow_core.Flow_GetLastError())

        flow_core.FlowDevice_CopyFrom(cached_device, device)
        if not flow_core.FlowDevice_CanRetrieveSettings(cached_device):
            raise FlowCoreException("Device does not have settings",
                                    flow_core.Flow_GetLastError())

        setting = flow_core.FlowDevice_RetrieveSetting(cached_device, key)
        if not setting:
            raise FlowCoreException("None settings retrieved",
                                    flow_core.Flow_GetLastError())

        cached_value = copy.deepcopy(flow_core.FlowSetting_GetValue(setting))

    finally:
        flow_core.FlowClearMemory(memory_manager)

    return cached_value
Esempio n. 2
0
def get_user_owned_devices(logged_in_user):
    """ Get devices owned by user

    :param: loggedInUser
    :return: user devices
    :rtype: list
    :raises: ValueError, FlowCoreException
    """
    all_devices = []
    if not logged_in_user:
        raise ValueError("Invalid function parameters")

    memory_manager = get_memory_manager()
    try:
        cached_user = flow_core.FlowUser_New(memory_manager)
        if not cached_user:
            raise FlowCoreException("Failed to create new user",
                                    flow_core.Flow_GetLastError())

        flow_core.FlowUser_CopyFrom(cached_user, logged_in_user)
        my_devices = flow_core.FlowUser_RetrieveOwnedDevices(
            cached_user, flow_core.FLOW_DEFAULT_PAGE_SIZE)
        if not my_devices:
            raise FlowCoreException("User has no devices",
                                    flow_core.Flow_GetLastError())

        for index in range(flow_core.FlowDevices_GetCount(my_devices)):
            device = flow_core.FlowDevices_GetItem(my_devices, index)
            if not device:
                raise FlowCoreException(
                    "Failed to get a device from device list",
                    flow_core.Flow_GetLastError())
            cached_device = flow_core.FlowDevice_New(None)
            if not cached_device:
                raise FlowCoreException("Failed to create new device",
                                        flow_core.Flow_GetLastError())
            flow_core.FlowDevice_CopyFrom(cached_device, device)
            all_devices.append(cached_device)

    finally:
        flow_core.FlowClearMemory(memory_manager)

    return all_devices
Esempio n. 3
0
def get_device_type(device):
    """ Get Type of device

    :param FlowDevice device: flow device object
    :return: device Type
    :rtype: str
    :raises: ValueError, FlowCoreException
    """
    if not device:
        raise ValueError("Invalid function parameter")

    memory_manager = get_memory_manager()
    try:
        cached_device = get_new_device(memory_manager)
        flow_core.FlowDevice_CopyFrom(cached_device, device)
        cached_device_type = copy.deepcopy(
            flow_core.FlowDevice_GetDeviceType(cached_device))

    finally:
        flow_core.FlowClearMemory(memory_manager)

    return cached_device_type
Esempio n. 4
0
def save_device_settings(device, key, value):
    """ Save device settings to flow cloud which is in key-value format

    :param FlowDevice device: flow device object
    :param str key: Associated key to device settings
    :param str value: device settings value which is to be saved
    :raises: ValueError, FlowCoreException
    """
    if not (device and key and value):
        raise ValueError("Invalid function parameters")

    memory_manager = get_memory_manager()
    try:
        cached_device = get_new_device(memory_manager)
        if not cached_device:
            raise FlowCoreException("Failed to create new device",
                                    flow_core.Flow_GetLastError())

        flow_core.FlowDevice_CopyFrom(cached_device, device)
        if not flow_core.FlowDevice_CanRetrieveSettings(cached_device):
            raise FlowCoreException("Device does not have settings",
                                    flow_core.Flow_GetLastError())

        dev_settings = flow_core.FlowDevice_RetrieveSettings(
            cached_device, flow_core.FLOW_DEFAULT_PAGE_SIZE)
        if not dev_settings:
            raise FlowCoreException("Failed to retrieve device settings",
                                    flow_core.Flow_GetLastError())

        flow_core.FlowSettings_SaveSetting(memory_manager, dev_settings, key,
                                           value)
        if flow_core.Flow_GetLastError() != flow_core.FlowError_NoError:
            raise FlowCoreException("Save settings failed",
                                    flow_core.Flow_GetLastError())

    finally:
        flow_core.FlowClearMemory(memory_manager)