def test_observer_timeout(self): """Test that the observer timeouts""" from mbed_cloud.connect import ConnectAPI api = ConnectAPI() observer = api.subscribe( api.subscribe.channels.DeviceStateChanges(device_id=123456)) with self.assertRaises(CloudTimeoutError) as timeout_error: observer.next().block(timeout=2) self.assertEqual(str(timeout_error.exception), "No data received after 2.0 seconds.")
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')
def test_registration_notifications(self): """Test a all registration notifications in a single message""" from mbed_cloud.connect import ConnectAPI device_id = "015f3850a657000000000001001002ab" api = ConnectAPI() registrations_observer = api.subscribe( api.subscribe.channels.DeviceStateChanges( device_id=device_id, channel=channels.ChannelIdentifiers.registrations), provider=False) de_registrations_observer = api.subscribe( api.subscribe.channels.DeviceStateChanges( device_id=device_id, channel=channels.ChannelIdentifiers.de_registrations)) reg_updates_observer = api.subscribe( api.subscribe.channels.DeviceStateChanges( device_id=device_id, channel=channels.ChannelIdentifiers.reg_updates)) registrations_expired_observer = api.subscribe( api.subscribe.channels.DeviceStateChanges( device_id=device_id, channel=channels.ChannelIdentifiers.registrations_expired)) # cheat, waiting takes too long example_data = { "registrations": [{ "q": False, "original-ep": "my-device-123", "ept": "Light", "resources": [{ "path": "/sen/light", "ct": "text/plain", "obs": True, "rt": "light_sensor", "if": "sensor" }], "ep": device_id }], "reg_updates": [{ "q": False, "original-ep": "my-device-123", "ept": "Light", "resources": [{ "path": "/sen/light", "ct": "text/plain", "obs": True, "rt": "light_sensor", "if": "sensor" }], "ep": device_id }], "async_responses": [{ "ct": "text/plain", "payload": "My4zMQ==", "max-age": "60", "id": "9e3c96b8-c4d7-496a-ab90-cc732b9b560e", "error": "TIMEOUT", "status": 200 }], "notifications": [{ "path": "/sen/light", "ct": "text/plain", "payload": "My4zMQ==", "max-age": "60", "ep": device_id }], "de_registrations": [device_id], "registrations_expired": [device_id], } api.subscribe.notify(example_data) self.assertEqual( registrations_observer.next().block(timeout=2), { 'alias': None, 'channel': 'registrations', 'device_id': '015f3850a657000000000001001002ab', 'device_type': 'Light', 'queue_mode': False, 'resources': [{ 'content_type': 'text/plain', 'observable': True, 'path': '/sen/light', 'type': 'light_sensor' }] }) self.assertEqual( de_registrations_observer.next().block(timeout=2), { 'channel': 'de_registrations', 'device_id': '015f3850a657000000000001001002ab' }) self.assertEqual( reg_updates_observer.next().block(timeout=2), { 'alias': None, 'channel': 'reg_updates', 'device_id': '015f3850a657000000000001001002ab', 'device_type': 'Light', 'queue_mode': False, 'resources': [{ 'content_type': 'text/plain', 'observable': True, 'path': '/sen/light', 'type': 'light_sensor' }] }) self.assertEqual( registrations_expired_observer.next().block(timeout=2), { 'channel': 'registrations_expired', 'device_id': '015f3850a657000000000001001002ab' })