def main(argv=None, loop=SharedLoop):
    """Serve access to a virtual IOTile device using a virtual iotile interface."""

    if argv is None:
        argv = sys.argv[1:]

    list_parser = argparse.ArgumentParser(add_help=False)
    list_parser.add_argument('-l', '--list', action='store_true', help="List all known installed interfaces and devices and then exit")
    list_parser.add_argument('-v', '--verbose', action="count", default=0, help="Increase logging level (goes error, warn, info, debug)")

    parser = argparse.ArgumentParser(description="Serve acess to a virtual IOTile device using a virtual IOTile interface")

    parser.add_argument('interface', help="The name of the virtual device interface to use")
    parser.add_argument('device', help="The name of the virtual device to create")
    parser.add_argument('-c', '--config', help="An optional JSON config file with arguments for the interface and device")
    parser.add_argument('-l', '--list', action='store_true', help="List all known installed interfaces and devices and then exit")
    parser.add_argument('-n', '--scenario', help="Load a test scenario from the given file")
    parser.add_argument('-s', '--state', help="Load a given state into the device before starting to serve it.  Only works with emulated devices.")
    parser.add_argument('-d', '--dump', help="Dump the device's state when we exit the program.  Only works with emulated devices.")
    parser.add_argument('-t', '--track', help="Track all changes to the device's state.  Only works with emulated devices.")
    parser.add_argument('-v', '--verbose', action="count", default=0, help="Increase logging level (goes error, warn, info, debug)")

    args, _rest = list_parser.parse_known_args(argv)

    if args.list:
        configure_logging(args.verbose)

        reg = ComponentRegistry()
        print("Installed Device Servers:")
        for name, _iface in reg.load_extensions('iotile.device_server', class_filter=AbstractDeviceServer):
            print('- {}'.format(name))

        print("\nInstalled Virtual Devices:")
        for name, dev in reg.load_extensions('iotile.virtual_device', class_filter=VirtualIOTileDevice,
                                             product_name="virtual_device"):
            print('- {}: {}'.format(name, one_line_desc(dev)))

        return 0

    args = parser.parse_args(argv)

    configure_logging(args.verbose)

    config = {}
    if args.config is not None:
        with open(args.config, "r") as conf_file:
            config = json.load(conf_file)

    started = False
    device = None
    stop_immediately = args.interface == 'null'
    try:
        server = instantiate_interface(args.interface, config, loop)
        device = instantiate_device(args.device, config, loop)

        if args.state is not None:
            print("Loading device state from file %s" % args.state)
            device.load_state(args.state)

        if args.scenario is not None:
            print("Loading scenario from file %s" % args.scenario)

            with open(args.scenario, "r") as infile:
                scenario = json.load(infile)

            # load_metascenario expects a list of scenarios even when there is only one
            if isinstance(scenario, dict):
                scenario = [scenario]

            device.load_metascenario(scenario)

        if args.track is not None:
            print("Tracking all state changes to device")
            device.state_history.enable()

        adapter = VirtualDeviceAdapter(devices=[device], loop=loop)
        server.adapter = adapter

        loop.run_coroutine(adapter.start())

        try:
            loop.run_coroutine(server.start())
        except:
            loop.run_coroutine(adapter.stop())
            adapter = None
            raise

        started = True

        print("Starting to serve virtual IOTile device")

        if stop_immediately:
            return 0

        # We need to periodically process events that are queued up in the interface
        while True:
            time.sleep(0.5)

    except KeyboardInterrupt:
        print("Break received, cleanly exiting...")
    finally:
        if args.dump is not None and device is not None:
            print("Dumping final device state to %s" % args.dump)
            device.save_state(args.dump)

        if started:
            loop.run_coroutine(server.stop())
            loop.run_coroutine(adapter.stop())

        if args.track is not None and device is not None:
            print("Saving state history to file %s" % args.track)
            device.state_history.dump(args.track)

    return 0
def main ( argv = None , loop = SharedLoop ) : if argv is None : argv = sys . argv [ 1 : ] list_parser = argparse . ArgumentParser ( add_help = False ) list_parser . add_argument ( '-l' , '--list' , action = 'store_true' , help = "List all known installed interfaces and devices and then exit" ) list_parser . add_argument ( '-v' , '--verbose' , action = "count" , default = 0 , help = "Increase logging level (goes error, warn, info, debug)" ) parser = argparse . ArgumentParser ( description = "Serve acess to a virtual IOTile device using a virtual IOTile interface" ) parser . add_argument ( 'interface' , help = "The name of the virtual device interface to use" ) parser . add_argument ( 'device' , help = "The name of the virtual device to create" ) parser . add_argument ( '-c' , '--config' , help = "An optional JSON config file with arguments for the interface and device" ) parser . add_argument ( '-l' , '--list' , action = 'store_true' , help = "List all known installed interfaces and devices and then exit" ) parser . add_argument ( '-n' , '--scenario' , help = "Load a test scenario from the given file" ) parser . add_argument ( '-s' , '--state' , help = "Load a given state into the device before starting to serve it.  Only works with emulated devices." ) parser . add_argument ( '-d' , '--dump' , help = "Dump the device's state when we exit the program.  Only works with emulated devices." ) parser . add_argument ( '-t' , '--track' , help = "Track all changes to the device's state.  Only works with emulated devices." ) parser . add_argument ( '-v' , '--verbose' , action = "count" , default = 0 , help = "Increase logging level (goes error, warn, info, debug)" ) args , _rest = list_parser . parse_known_args ( argv ) if args . list : configure_logging ( args . verbose ) reg = ComponentRegistry ( ) print ( "Installed Device Servers:" ) for name , _iface in reg . load_extensions ( 'iotile.device_server' , class_filter = AbstractDeviceServer ) : print ( '- {}' . format ( name ) ) print ( "\nInstalled Virtual Devices:" ) for name , dev in reg . load_extensions ( 'iotile.virtual_device' , class_filter = VirtualIOTileDevice , product_name = "virtual_device" ) : print ( '- {}: {}' . format ( name , one_line_desc ( dev ) ) ) return 0 args = parser . parse_args ( argv ) configure_logging ( args . verbose ) config = { } if args . config is not None : with open ( args . config , "r" ) as conf_file : config = json . load ( conf_file ) started = False device = None stop_immediately = args . interface == 'null' try : server = instantiate_interface ( args . interface , config , loop ) device = instantiate_device ( args . device , config , loop ) if args . state is not None : print ( "Loading device state from file %s" % args . state ) device . load_state ( args . state ) if args . scenario is not None : print ( "Loading scenario from file %s" % args . scenario ) with open ( args . scenario , "r" ) as infile : scenario = json . load ( infile ) if isinstance ( scenario , dict ) : scenario = [ scenario ] device . load_metascenario ( scenario ) if args . track is not None : print ( "Tracking all state changes to device" ) device . state_history . enable ( ) adapter = VirtualDeviceAdapter ( devices = [ device ] , loop = loop ) server . adapter = adapter loop . run_coroutine ( adapter . start ( ) ) try : loop . run_coroutine ( server . start ( ) ) except : loop . run_coroutine ( adapter . stop ( ) ) adapter = None raise started = True print ( "Starting to serve virtual IOTile device" ) if stop_immediately : return 0 while True : time . sleep ( 0.5 ) except KeyboardInterrupt : print ( "Break received, cleanly exiting..." ) finally : if args . dump is not None and device is not None : print ( "Dumping final device state to %s" % args . dump ) device . save_state ( args . dump ) if started : loop . run_coroutine ( server . stop ( ) ) loop . run_coroutine ( adapter . stop ( ) ) if args . track is not None and device is not None : print ( "Saving state history to file %s" % args . track ) device . state_history . dump ( args . track ) return 0
Serve access to a virtual IOTile device using a virtual iotile interface.