Beispiel #1
0
def main():
    """Main entry point for iotile-sgcompile."""

    arg_parser = build_args()
    args = arg_parser.parse_args()

    model = DeviceModel()

    parser = SensorGraphFileParser()
    parser.parse_file(args.sensor_graph)

    if args.format == u'ast':
        write_output(parser.dump_tree(), True, args.output)
        sys.exit(0)

    parser.compile(model)

    if not args.disable_optimizer:
        opt = SensorGraphOptimizer()
        opt.optimize(parser.sensor_graph, model=model)

    if args.format == u'nodes':
        output = u'\n'.join(parser.sensor_graph.dump_nodes()) + u'\n'
        write_output(output, True, args.output)
    else:
        if args.format not in KNOWN_FORMATS:
            print("Unknown output format: {}".format(args.format))
            sys.exit(1)

        output_format = KNOWN_FORMATS[args.format]
        output = output_format.format(parser.sensor_graph)

        write_output(output, output_format.text, args.output)
Beispiel #2
0
def usertick_gate_opt():
    """Optimize the user tick sensor graph."""

    model = DeviceModel()
    optimizer = SensorGraphOptimizer()

    raw_sg = compile_sg('user_tick.sgf')
    optimizer.optimize(raw_sg, model=model)
    return raw_sg
Beispiel #3
0
def complex_gate_opt():
    """Optimize the complex_gate sensor_graph."""

    model = DeviceModel()
    optimizer = SensorGraphOptimizer()

    raw_sg = compile_sg('complex_gates.sgf')
    optimizer.optimize(raw_sg, model=model)
    return raw_sg
Beispiel #4
0
def streamer_types():
    """Sensorgraph with lots of varieties of streamers."""

    model = DeviceModel()
    optimizer = SensorGraphOptimizer()

    raw_sg = compile_sg('streamers.sgf')
    optimizer.optimize(raw_sg, model=model)

    return raw_sg
Beispiel #5
0
def main():
    arg_parser = build_args()
    args = arg_parser.parse_args()

    model = DeviceModel()

    parser = SensorGraphFileParser()
    parser.parse_file(args.sensor_graph)

    outfile = sys.stdout

    if args.output is not None:
        outfile = open(args.output, "wb")

    if args.format == u'ast':
        outfile.write(parser.dump_tree().encode())
        outfile.close()
        sys.exit(0)

    parser.compile(model)

    if not args.disable_optimizer:
        opt = SensorGraphOptimizer()
        opt.optimize(parser.sensor_graph, model=model)

    if args.format == u'nodes':
        for node in parser.sensor_graph.dump_nodes():
            outfile.write((node + u'\n').encode())

    else:
        if args.format not in known_formats:
            print("Unknown output format: {}".format(args.format))
            outfile.close()
            sys.exit(1)

        output = known_formats[args.format](parser.sensor_graph)

        if args.format in (u'snippet', u'ascii', u'config'):
            outfile.write(output.encode())
        else:
            outfile.write(output)

    outfile.close()
Beispiel #6
0
    def load_sgf(self, sgf_data):
        """Load, persist a sensor_graph file.

        The data passed in `sgf_data` can either be a path or the already
        loaded sgf lines as a string.  It is determined to be sgf lines if
        there is a '\n' character in the data, otherwise it is interpreted as
        a path.

        Note that this scenario just loads the sensor_graph directly into the
        persisted sensor_graph inside the device.  You will still need to
        reset the device for the sensor_graph to enabled and run.

        Args:
            sgf_data (str): Either the path to an sgf file or its contents
                as a string.
        """

        if '\n' not in sgf_data:
            with open(sgf_data, "r") as infile:
                sgf_data = infile.read()

        model = DeviceModel()

        parser = SensorGraphFileParser()
        parser.parse_file(data=sgf_data)

        parser.compile(model)
        opt = SensorGraphOptimizer()
        opt.optimize(parser.sensor_graph, model=model)

        sensor_graph = parser.sensor_graph
        self._logger.info(
            "Loading sensor_graph with %d nodes, %d streamers and %d configs",
            len(sensor_graph.nodes), len(sensor_graph.streamers),
            len(sensor_graph.config_database))

        # Directly load the sensor_graph into our persisted storage
        self.sensor_graph.persisted_nodes = sensor_graph.dump_nodes()
        self.sensor_graph.persisted_streamers = sensor_graph.dump_streamers()

        self.sensor_graph.persisted_constants = []
        for stream, value in sorted(sensor_graph.constant_database.items(),
                                    key=lambda x: x[0].encode()):
            reading = IOTileReading(stream.encode(), 0, value)
            self.sensor_graph.persisted_constants.append((stream, reading))

        self.sensor_graph.persisted_exists = True

        # Clear all config variables and load in those from this sgf file
        self.config_database.clear()

        for slot in sorted(sensor_graph.config_database,
                           key=lambda x: x.encode()):
            for conf_var, (conf_type, conf_val) in sorted(
                    sensor_graph.config_database[slot].items()):
                self.config_database.add_direct(slot, conf_var, conf_type,
                                                conf_val)

        # If we have an app tag and version set program them in
        app_tag = sensor_graph.metadata_database.get('app_tag')
        app_version = sensor_graph.metadata_database.get('app_version')

        if app_tag is not None:
            if app_version is None:
                app_version = "0.0"

            self.app_info = (app_tag, app_version)
Beispiel #7
0
def main(argv=None):
    """Main entry point for iotile sensorgraph simulator.

    This is the iotile-sgrun command line program.  It takes
    an optional set of command line parameters to allow for
    testing.

    Args:
        argv (list of str): An optional set of command line
            parameters.  If not passed, these are taken from
            sys.argv.
    """

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

    try:
        executor = None
        parser = build_args()
        args = parser.parse_args(args=argv)

        model = DeviceModel()

        parser = SensorGraphFileParser()
        parser.parse_file(args.sensor_graph)
        parser.compile(model)

        if not args.disable_optimizer:
            opt = SensorGraphOptimizer()
            opt.optimize(parser.sensor_graph, model=model)

        graph = parser.sensor_graph
        sim = SensorGraphSimulator(graph)

        for stop in args.stop:
            sim.stop_condition(stop)

        for watch in args.watch:
            watch_sel = DataStreamSelector.FromString(watch)
            graph.sensor_log.watch(watch_sel, watch_printer)

        # If we are semihosting, create the appropriate executor connected to the device
        if args.semihost_device is not None:
            executor = SemihostedRPCExecutor(args.port, args.semihost_device)
            sim.rpc_executor = executor

        for mock in args.mock_rpc:
            slot, rpc_id, value = process_mock_rpc(mock)
            sim.rpc_executor.mock(slot, rpc_id, value)

        for stim in args.stimulus:
            sim.stimulus(stim)

        graph.load_constants()

        if args.trace is not None:
            sim.record_trace()

        try:
            if args.connected:
                sim.step(user_connected, 8)

            sim.run(accelerated=not args.realtime)
        except KeyboardInterrupt:
            pass

        if args.trace is not None:
            sim.trace.save(args.trace)
    finally:
        if executor is not None:
            executor.hw.close()

    return 0