iotile / coretools

Core python tools for building and using IOTile based devices
GNU General Public License v3.0
14 stars 7 forks source link

Virtual adapter should call periodic task to allow scanning for virtual devices immediately #193

Closed timburke closed 7 years ago

timburke commented 7 years ago

When you directly use a virtual device inside of HardwareManager, it does the following:

Create an AdapterStream object with a DeviceAdapter: https://github.com/iotile/coretools/blob/master/iotilecore/iotile/core/hw/hwmanager.py#L408 https://github.com/iotile/coretools/blob/master/iotilecore/iotile/core/hw/transport/adapterstream.py

This AdapterStream in turn registers itself with the DeviceAdapter to receive a callback when new IOTile devices are seen: https://github.com/iotile/coretools/blob/master/iotilecore/iotile/core/hw/transport/adapterstream.py#L35

However, the VirtualDeviceAdapter does not generate scan events on its own unless periodic_callback is called, which in this case it isn't until the user takes some action on HardwareManager: https://github.com/iotile/coretools/blob/master/iotilecore/iotile/core/hw/transport/virtualadapter.py#L409

Previously, there wasn't a good way to fix this because when the user calls HardwareManager.scan, there was no way to actually pass the call down to VirtualDeviceAdapter and get it to generate scan events.

However, now we do support this behavior by calling a probe_async function on DeviceAdapter. To trigger this behavior, the VirtualDeviceAdapter needs to say that it requires probing when the user wants to scan by setting a config variable probe_required.

See the AWS IOT DeviceAdapter for an example: https://github.com/iotile/coretools/blob/master/transport_plugins/awsiot/iotile_transport_awsiot/device_adapter.py#L75

TLDR: we need to call self.set_config('probe_required', True) in the __init__ function of VirtualDeviceAdapter so that scanning works correctly.

timburke commented 7 years ago

Checklist

timburke commented 7 years ago

The unit test should go into: https://github.com/iotile/coretools/blob/master/iotilecore/test/test_hw/test_virtualadapter.py

shih1 commented 7 years ago

Observed: Master is passing all test cases. Branch is passing all test cases. Expected: Master should fail the new test case. Brach should pass all test cases.

Issue: When emulating master in virtual environment, the scan functionality does not work. I'm currently doing a very similar test to an existing one, only difference is I'm using connect instead of connect_direct.

realtime_scan_hw.connect('1')
realtime_scan_hw.enable_streaming()

reports = realtime_scan_hw.wait_reports(10)
stream1 = [x for x in reports if x.visible_readings[0].stream == 0x100a]
assert len(stream1) != 0