def test_get_and_set_resource_value(self): # an example: get and set a resource value from mbed_cloud.foundation import Device from mbed_cloud import ApiFilter from mbed_cloud.foundation.enums import DeviceStateEnum from mbed_cloud import ConnectAPI # Use the Foundation interface to find a registered device. api_filter = ApiFilter() api_filter.add_filter("state", "eq", DeviceStateEnum.REGISTERED) device = Device().list(max_results=2, filter=api_filter).next() # Use the Legacy interface to find resources connect_api = ConnectAPI() # Find an observable resource for resource in connect_api.list_resources(device.id): if resource.observable: break # Set a resource value connect_api.set_resource_value(device.id, resource.path, "12") # Get a resource value value = connect_api.get_resource_value(device.id, resource.path) print("Device %s, path %s, current value: %s" %(device.id, resource.path, value)) # end of example connect_api.stop_notifications()
def _main(): api = ConnectAPI() # calling start_notifications is required for getting/setting resource synchronously api.start_notifications() devices = api.list_connected_devices().data if not devices: raise Exception("No connected devices registered. Aborting") # Synchronously get the initial/current value of the resource value = api.get_resource_value(devices[0].id, WRITEABLE_RESOURCE) print("Current value: %r" % (value, )) # Set Resource value. Resource needs to have type == "writable_resource" api.set_resource_value(device_id=devices[0].id, resource_path=WRITEABLE_RESOURCE, resource_value='10') # Synchronously get the current value of the resource value = api.get_resource_value(devices[0].id, WRITEABLE_RESOURCE) print("Current value: %r" % (value, ))
def _main(): api = ConnectAPI() api.start_notifications() devices = api.list_connected_devices().data if not devices: raise Exception("No connected devices registered. Aborting") # Synchronously get the initial/current value of the resource value = api.get_resource_value(devices[0].id, BUTTON_RESOURCE) # Register a subscription for new values api.set_resource_value(device_id=devices[0].id, resource_path=WRITEABLE_RESOURCE, resource_value='10') queue = api.add_resource_subscription(devices[0].id, BUTTON_RESOURCE) while True: # Print the current value print("Current value: %r" % (value, )) # Get a new value, using the subscriptions value = queue.get(timeout=30)