def open_ws(gateway_event): gateway_event.wait() gateway_event.clear() hw = HardwareManager(port="ws:127.0.0.1:5120/iotile/v1") hw.enable_broadcasting() gateway_event.wait() hw.close()
def recipe_fixture(request, resman): """Create a fixture with a hardware manager connected to our reference dev.""" recipe = resman.get_recipe(request.param) hw = HardwareManager(port="virtual:reference_1_0") try: recipe.run(None, {'hardware': MockHardwareManResource(hw, 1)}) yield recipe, hw, hw.stream.adapter.devices[1] finally: hw.close()
def recipe_fixture(request, resman): """Create a fixture with a hardware manager connected to our reference dev.""" if sys.version_info < (3, 5): pytest.skip("test requires iotile-emulate on python 3.5+") recipe = resman.get_recipe(request.param) hw = HardwareManager(port="virtual:reference_1_0") try: recipe.run(None, {'hardware': MockHardwareManResource(hw, 1)}) yield recipe, hw, hw.stream.adapter.devices[1] finally: hw.close()
class HardwareManagerResource(SharedResource): """A shared HardwareManager instance. Arguments: port (str): Optional port argument that should be used to connect to a DeviceAdapter. If not specified, the virtual environment default is used. connect: (str or int): The UUID of a device to connect to when this resource is created and disconnect from when it is destroyed. This is an optional parameter. If it is not specified the HardwareManager is not connected upon creation. connect_direct: (str or int): The connection string of a device to connect directly to when this resource is created and disconnect from when it is destroyed. This is an optional parameter. If it is not specified the HardwareManager is not connected upon creation. """ ARG_SCHEMA = RESOURCE_ARG_SCHEMA def __init__(self, args): super(HardwareManagerResource, self).__init__() self._port = args.get('port') self._connect_id = args.get('connect') self._connection_string = args.get('connect_direct') self.hwman = None if self._connect_id is not None and not isinstance( self._connect_id, int): self._connect_id = int(self._connect_id, 0) def open(self): """Open and potentially connect to a device.""" self.hwman = HardwareManager(port=self._port) self.opened = True if self._connection_string is not None: try: self.hwman.connect_direct(self._connection_string) except HardwareError: self.hwman.close() raise elif self._connect_id is not None: try: self.hwman.connect(self._connect_id) except HardwareError: self.hwman.close() raise def close(self): """Close and potentially disconnect from a device.""" if self.hwman.stream.connected: self.hwman.disconnect() self.hwman.close() self.opened = False