コード例 #1
0
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()
コード例 #2
0
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()
コード例 #3
0
def test_loading_scenario(tmpdir):
    """Make sure we can load a test scenario."""

    scen_file = save_device_args(tmpdir,
                                 'scenario.json',
                                 data=[{
                                     'name': 'loaded_counter',
                                     'args': {
                                         'counter': 15
                                     },
                                     'tile': 11
                                 }])

    saved = str(tmpdir.join("state.json"))

    with HardwareManager(
            port='emulated:emulation_demo@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9'
    ) as hw:
        hw.connect(1)
        debug = hw.debug()

        debug.open_scenario(scen_file)
        debug.save_snapshot(saved)

    with open(saved, "r") as infile:
        json.load(infile)
コード例 #4
0
def test_saving_changes(tmpdir):
    """Make sure we can track and save changes to a device."""

    scen_file = save_device_args(tmpdir,
                                 'scenario.json',
                                 data=[{
                                     'name': 'loaded_counter',
                                     'args': {
                                         'counter': 15,
                                     },
                                     'tile': 11
                                 }])

    change_file = tmpdir.join('out.csv')

    with HardwareManager(
            port='emulated:emulation_demo@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9'
    ) as hw:
        hw.connect(1)
        debug = hw.debug()

        debug.track_changes()
        debug.open_scenario(scen_file)
        debug.track_changes(enabled=False)
        debug.save_changes(str(change_file))

    assert change_file.exists()
コード例 #5
0
def basic_device():
    """A preprogrammed basic sensorgraph for testing."""

    device = ReferenceDevice({'simulate_time': False})
    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(system input 2 always) => output 1 using copy_latest_a",
        "(system input 3 always) => output 2 using copy_latest_a",
        "(system input 5 always) => output 3 using copy_latest_a",
        "(system input 6 always) => output 4 using copy_latest_a"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8)
        sensor_graph = con.sensor_graph()

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.add_streamer('output 10', 'controller', True, 'individual', 'telegram')

        yield hw, device
コード例 #6
0
def basic_device():
    """A preprogrammed basic sensorgraph for testing."""

    device = ReferenceDevice({})
    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(system input 2 always) => output 1 using copy_latest_a",
        "(system input 3 always) => output 2 using copy_latest_a",
        "(system input 5 always) => output 3 using copy_latest_a",
        "(system input 6 always) => output 4 using copy_latest_a"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=True)
        sensor_graph = find_proxy_plugin(
            'iotile_standard_library/lib_controller', 'SensorGraphPlugin')(con)

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.add_streamer('output 10', 'controller', True,
                                  'individual', 'telegram')

        yield hw, device
コード例 #7
0
def streaming_sg():
    """A preprogrammed basic sensorgraph for testing streaming."""

    device = ReferenceDevice({'simulate_time': False})

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(input 1 when count >= 1) => output 1 using copy_latest_a",
        "(input 2 when count >= 1) => output 2 using copy_latest_a",
        "(input 3 when count >= 1 && constant 1 always) => unbuffered 1 using trigger_streamer"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=True)
        sensor_graph = find_proxy_plugin('iotile_standard_library/lib_controller', 'SensorGraphPlugin')(con)

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.push_reading('constant 1', 0)

        sensor_graph.add_streamer('output 1', 'controller', False, 'individual', 'telegram')
        sensor_graph.add_streamer('output 2', 'controller', False, 'hashedlist', 'telegram', withother=0)

        yield sensor_graph, hw, device
コード例 #8
0
def test_saving_changes(tmpdir):
    """Make sure we can track and save changes to a device."""

    scen_file = save_device_args(tmpdir,
                                 'scenario.json',
                                 data=[{
                                     'name': 'loaded_counters',
                                     'args': {
                                         'tracked_counter': 15,
                                         'manual_counter': 10
                                     }
                                 }])

    change_file = tmpdir.join('out.csv')

    with HardwareManager(port='emulated:emulation_test') as hw:
        hw.connect(1)
        debug = hw.debug()

        debug.track_changes()
        debug.open_scenario(scen_file)
        debug.track_changes(enabled=False)
        debug.save_changes(str(change_file))

    assert change_file.exists()
コード例 #9
0
def test_loading_scenario(tmpdir):
    """Make sure we can load a test scenario."""

    scen_file = save_device_args(tmpdir,
                                 'scenario.json',
                                 data=[{
                                     'name': 'loaded_counters',
                                     'args': {
                                         'tracked_counter': 15,
                                         'manual_counter': 10
                                     }
                                 }])

    saved = str(tmpdir.join("state.json"))

    with HardwareManager(port='emulated:emulation_test') as hw:
        hw.connect(1)
        debug = hw.debug()

        debug.open_scenario(scen_file)
        debug.save_snapshot(saved)

    with open(saved, "r") as infile:
        state = json.load(infile)

    assert state['tracked_counter'] == 15
    assert state['manual_counter'] == 10
コード例 #10
0
def basic_sg():
    """A preprogrammed basic sensorgraph for testing."""

    device = ReferenceDevice({'simulate_time': False})
    peripheral = EmulatedPeripheralTile(11, device)
    peripheral.declare_config_variable("test 1", 0x8000, 'uint16_t')
    peripheral.declare_config_variable('test 2', 0x8001, 'uint32_t[5]')

    device.add_tile(11, peripheral)

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(input 1 always) => counter 1024 using copy_latest_a",
        "(counter 1024 when count >= 1 && constant 1030 when value == 1) => counter 1030 using copy_latest_a",
        "(counter 1030 when count >= 4) => output 1 using copy_all_a"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=True)
        sensor_graph = find_proxy_plugin('iotile_standard_library/lib_controller', 'SensorGraphPlugin')(con)

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.add_streamer('output 10', 'controller', True, 'individual', 'telegram')

        yield sensor_graph, hw, device
コード例 #11
0
def test_basic_usage():
    """Make sure we can import and use the objects."""

    with HardwareManager(port='emulated:reference_1_0@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9') as hw:
        hw.connect(1)
        debug = hw.debug()

        state = debug.dump_snapshot()
        debug.restore_snapshot(state)
コード例 #12
0
def test_saving_and_loading_state(tmpdir):
    """Make sure we can save and load state."""

    saved = str(tmpdir.join("state.json"))
    with HardwareManager(port='emulated:emulation_test') as hw:
        hw.connect(1)
        debug = hw.debug()

        debug.save_snapshot(saved)
        debug.load_snapshot(saved)
コード例 #13
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
コード例 #14
0
def test_basic_functionality():
    """Make sure we can connect to a device."""

    with HardwareManager(port='emulated:emulation_test') as hw:
        results = hw.scan()
        assert len(results) == 1
        assert results[0]['uuid'] == 1

        hw.connect(1)
        _con = hw.controller()
        hw.disconnect()
コード例 #15
0
ファイル: test_appmatching.py プロジェクト: palagap/coretools
def virtual_app():
    path = os.path.join(os.path.dirname(__file__), 'virtual_app_device.py')
    hw = HardwareManager(port="virtual:%s" % path)

    hw.connect(1)
    yield hw
    hw.disconnect()
コード例 #16
0
def test_basic_functionality():
    """Make sure we can connect to a device."""

    with HardwareManager(
            port='emulated:emulation_demo@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9'
    ) as hw:
        results = hw.scan()
        assert len(results) == 1
        assert results[0]['uuid'] == 1

        hw.connect(1)
        hw.get(8, basic=True)
        hw.disconnect()
コード例 #17
0
def empty_sg():
    """An empty sensorgraph for testing."""

    device = ReferenceDevice({'simulate_time': False})

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=False)
        sensor_graph = con.sensor_graph()
        yield sensor_graph, hw, device
コード例 #18
0
def uploader_app():
    """A reference device with a streamer and loaded with data."""

    with HardwareManager(port="emulated:reference_1_0") as hw:
        con = hw.get(8, uuid=1, force='NRF52 ')
        sg = con.sensor_graph()

        sg.add_streamer('output 1', 'controller', False, 'hashedlist',
                        'telegram')
        sg.push_many('output 1', 10, 100)

        app = hw.app(name='cloud_uploader')

        yield app
コード例 #19
0
def test_racefree_reset():
    """Make sure we can reset at will."""

    with HardwareManager(
            port='emulated:emulation_demo@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9'
    ) as hw:
        hw.connect(1)

        proxy = hw.get(8, basic=True)

        for i in range(0, 10):
            print("starting reset %d" % i)
            proxy.reset(wait=0)
            print("finished reset %d" % i)
コード例 #20
0
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()
コード例 #21
0
def reference_hw():
    """Get a reference device and connected HardwareManager."""

    device = ReferenceDevice({'simulate_time': False})
    peripheral = EmulatedPeripheralTile(11, device)
    peripheral.declare_config_variable("test 1", 0x8000, 'uint16_t')
    peripheral.declare_config_variable('test 2', 0x8001, 'uint32_t[5]')

    device.add_tile(11, peripheral)

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        yield hw, device, peripheral
コード例 #22
0
def test_async_rpc():
    """Make sure we can send an asynchronous rpc."""

    with HardwareManager(
            port='emulated:emulation_demo@#eyJzaW11bGF0ZV90aW1lIjogZmFsc2V9'
    ) as hw:
        hw.connect(1)

        proxy = hw.get(11, basic=True)

        # This RPC is async
        echo, = proxy.rpc_v2(0x8000, "L", "L", 5)
        assert echo == 5

        # This RPC is sync
        echo, = proxy.rpc_v2(0x8001, "L", "L", 6)
        assert echo == 6
コード例 #23
0
def sg_device():
    """Get a reference device and connected HardwareManager."""

    device = ReferenceDevice({'simulate_time': False})
    peripheral = EmulatedPeripheralTile(11, device)
    peripheral.declare_config_variable("test 1", 0x8000, 'uint16_t')
    peripheral.declare_config_variable('test 2', 0x8001, 'uint32_t[5]')

    device.add_tile(11, peripheral)

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=False)
        sensor_graph = con.sensor_graph()

        yield sensor_graph, hw
コード例 #24
0
def sg_device():
    """Get a reference device and connected HardwareManager."""

    device = ReferenceDevice({'simulate_time': False})
    peripheral = EmulatedPeripheralTile(11, device)
    peripheral.declare_config_variable("test 1", 0x8000, 'uint16_t')
    peripheral.declare_config_variable('test 2', 0x8001, 'uint32_t[5]')

    device.add_tile(11, peripheral)

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=True)
        sensor_graph = find_proxy_plugin('iotile_standard_library/lib_controller', 'SensorGraphPlugin')(con)

        yield sensor_graph, hw
コード例 #25
0
def running_gateway():
    """A basic gateway with some virtual devices."""

    realtime_config = {
        "iotile_id": 10,
        "streams": {
            "0x5001": [0.001, 100],
            "0x100a": [0.001, 200]
        },
        "broadcast": {
            "0x1001": [0.020, 100],
            "0x2001": [0.020, 200]
        }
    }

    enc = base64.b64encode(
        json.dumps(realtime_config).encode('utf-8')).decode('utf-8')

    config = {
        'agents': [{
            "name": "websockets",
            "args": {
                "port": "unused"
            }
        }],
        'adapters': [{
            "name": "virtual",
            "port": "realtime_test@#" + enc
        }]
    }

    gateway = IOTileGateway(config)

    gateway.start()
    gateway.loaded.wait(2.0)

    hw = HardwareManager(port="ws:127.0.0.1:%d/iotile/v1" %
                         gateway.agents[0].port)

    yield hw, gateway

    gateway.stop()
コード例 #26
0
def running_gateway(loop):
    """A basic gateway with some virtual devices."""

    realtime_config = {
        "iotile_id": 10,
        "streams": {
            "0x5001": [0.001, 100],
            "0x100a": [0.001, 200]
        },
        "broadcast": {
            "0x1001": [0.020, 100],
            "0x2001": [0.020, 200]
        }
    }

    enc = base64.b64encode(
        json.dumps(realtime_config).encode('utf-8')).decode('utf-8')

    config = {
        'servers': [{
            "name": "websockets",
            "args": {
                "port": None
            }
        }],
        'adapters': [{
            "name": "virtual",
            "port": "realtime_test@#" + enc + ";simple"
        }]
    }

    gateway = IOTileGateway(config, loop=loop)

    loop.run_coroutine(gateway.start())

    port = gateway.servers[0].implementation.port
    hw = HardwareManager(port="ws:127.0.0.1:%d/iotile/v1" % port)

    yield hw, gateway

    loop.run_coroutine(gateway.stop())
コード例 #27
0
def rpc_sg():
    """A basic sg for testing the rpc executor."""

    device = DemoEmulatedDevice({'simulate_time': False})

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(input 1 always) => unbuffered 1024 using copy_latest_a",
        "(unbuffered 1024 when count == 1 && constant 1024 always) => output 1 using call_rpc"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=False)
        sensor_graph = con.sensor_graph()

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.push_reading('constant 1024', 753666)

        yield sensor_graph, hw, device
コード例 #28
0
def rpc_sg():
    """A basic sg for testing the rpc executor."""

    device = DemoEmulatedDevice({'simulate_time': False})

    adapter = EmulatedDeviceAdapter(None, devices=[device])

    nodes = [
        "(input 1 always) => unbuffered 1024 using copy_latest_a",
        "(unbuffered 1024 when count == 1 && constant 1024 always) => output 1 using call_rpc"
    ]

    with HardwareManager(adapter=adapter) as hw:
        hw.connect(1)

        con = hw.get(8, basic=True)
        sensor_graph = find_proxy_plugin('iotile_standard_library/lib_controller', 'SensorGraphPlugin')(con)

        for node in nodes:
            sensor_graph.add_node(node)

        sensor_graph.push_reading('constant 1024', 753666)

        yield sensor_graph, hw, device
コード例 #29
0
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
コード例 #30
0
ファイル: test_appmatching.py プロジェクト: palagap/coretools
def register_apps():
    HardwareManager.ClearDevelopmentApps()
    HardwareManager.RegisterDevelopmentApp(FakeApp)

    yield
    HardwareManager.ClearDevelopmentApps()