def exec(cmd, data_consumer=None, dev='serial:///dev/ttyAMA1'): registry = DeviceRegistry() registry.register(dev) with registry.get_device(dev) as repl: res = repl.exec(cmd, data_consumer) try: res = res.decode() except: pass return res
def main(): logging.getLogger().setLevel(logging.INFO) # create device scanners DiscoverSerial() DiscoverNet() # run code on all discovered devices while True: for dev in DeviceRegistry.devices(): try: print( f"\n{'*'*20} {dev.name} {dev.connection} {'*'*(80-len(str(dev.name))-len(dev.connection))}" ) with dev as repl: output = Output() demo_eval(repl) demo_functions(repl) demo_rsync(repl) except (ConnectionResetError, ConnectionRefusedError): pass time.sleep(1)
import pytest from iot_device import DeviceRegistry, RemoteError registry = DeviceRegistry() @pytest.mark.parametrize("device", registry.devices) def test_softreset(device): with device as repl: repl.exec("a = 1234") assert repl.exec("print(a)").decode().strip() == '1234' repl.softreset() try: repl.exec("print(a)") except RemoteError as e: assert "NameError" in str(e) @pytest.mark.parametrize("device", registry.devices) def test_abort(device): with device as repl: assert repl.exec("print(2**8)").decode().strip() == '256' # doesn't really do anything since no program is running anyway ... repl.abort() assert repl.exec("print(2**10)").decode().strip() == '1024'
def rlist(dev='serial:///dev/ttyAMA1'): registry = DeviceRegistry() registry.register(dev) with registry.get_device(dev) as repl: repl.rlist(data_consumer=lambda x: print(x, end=''), show=True)
def rsync(dry_run=True, dev='serial:///dev/ttyAMA1'): registry = DeviceRegistry() registry.register(dev) with registry.get_device(dev) as repl: repl.rsync(data_consumer=lambda x: print(x, end=''), dry_run=dry_run)