def test_live_device_state_change(self): api = ConnectAPI() d = api.list_connected_devices().first() api.delete_device_subscriptions(d.id) # request the `manufacturer` field channel = channels.CurrentResourceValue(device_id=d.id, resource_path='/3/0/0') observer = api.subscribe(channel) value = observer.next().block(timeout=2) # should be, in this case, TLV self.assertEqual(b'6465765f6d616e756661637475726572', value)
def test_live_device_state_change(self): # integration - DeviceStateChanges local filter from mbed_cloud.connect import ConnectAPI api = ConnectAPI() d = api.list_connected_devices().first() observer = api.subscribe(api.subscribe.channels.DeviceStateChanges(device_id=d.id)) # cheat, waiting takes too long api.subscribe.notify({ channels.ChannelIdentifiers.reg_updates: [ dict(a=1, b=2, device_id=d.id), dict(a=1, b=2, device_id='A'), dict(a=1, b=2, device_id='B'), ] }) r = observer.next().block(timeout=2) self.assertTrue(r)
def test_live_device_state_change(self): api = ConnectAPI() api.delete_presubscriptions() d = api.list_connected_devices().first() api.delete_device_subscriptions(d.id) channel = channels.ResourceValues(device_id=d.id, resource_path=['/3/0/*']) observer = api.subscribe(channel) # don't actually care about notifications, we want to see subscriptions current_subs = api.list_device_subscriptions(d.id) self.assertGreaterEqual(len(current_subs), 3) for path in current_subs: self.assertIn('/3/0', path) channel.stop() current_subs = api.list_device_subscriptions(d.id) self.assertIsNone(None, current_subs)
def test_live_create_and_cleanup(self): # integration - ResourceValueCurrent cleans up from mbed_cloud.connect import ConnectAPI api = ConnectAPI() d = api.list_connected_devices().first() r = api.subscribe(api.subscribe.channels.ResourceValues( device_id=d.id, resource_path='/3/0/0', )).next().block(timeout=2) self.assertTrue(r) # wait for notifications to be stopped api.stop_notifications() # check that the routing table is cleaned up for i in range(10): time.sleep(0.01) if not api.subscribe.list_all(): break else: self.fail('Routing table not empty')
import os import pprint from mbed_cloud.connect import ConnectAPI TOKEN = "YOUR_ACCESS_KEY" # set up the Python SDK config = {} config['api_key'] = os.getenv('TOKEN') or TOKEN config['host'] = 'https://api.us-east-1.mbedcloud.com' api = ConnectAPI(config) api.start_notifications() devices = list( api.list_connected_devices(filters={'device_type': 'light-system'})) print("Found %d lights" % (len(devices)), [c.id for c in devices]) for device in devices: def pir_callback(device_id, path, count): print("Motion detected at %s, new count is %s" % (device_id, count)) api.add_resource_subscription_async(device.id, '/3201/0/5700', pir_callback) print("subscribed to resource") pink = 0xff69b4 api.set_resource_value(device.id, '/3311/0/5706', pink) print("set color to pink")