Exemplo n.º 1
0
    def code_externalprogram(self, decoding, inpt):
        errors = 0

        if decoding and self.external_decoder != "":
            output = self.charstr2bit(util.run_command(self.external_decoder, self.bit2str(inpt)))
        elif not decoding and self.external_encoder != "":
            output = self.charstr2bit(util.run_command(self.external_encoder, self.bit2str(inpt)))
        else:
            return [], 1, self.ErrorState.MISSING_EXTERNAL_PROGRAM

        return output, errors, self.ErrorState.SUCCESS
Exemplo n.º 2
0
    def code_externalprogram(self, decoding, inpt):
        errors = 0

        if decoding and self.external_decoder != "":
            output = self.charstr2bit(util.run_command(self.external_decoder, self.bit2str(inpt)))
        elif not decoding and self.external_encoder != "":
            output = self.charstr2bit(util.run_command(self.external_encoder, self.bit2str(inpt)))
        else:
            return [], 1, self.ErrorState.MISSING_EXTERNAL_PROGRAM

        return output, errors, self.ErrorState.SUCCESS
Exemplo n.º 3
0
    def generate_message_from_template(self, template_msg: SimulatorMessage):
        new_message = Message(template_msg.plain_bits,
                              pause=template_msg.pause,
                              rssi=0,
                              message_type=template_msg.message_type,
                              decoder=template_msg.decoder)

        for lbl in template_msg.children:  # type: SimulatorProtocolLabel
            if lbl.value_type_index == 2:
                # formula
                valid, _, node = self.expression_parser.validate_expression(
                    lbl.formula)
                assert valid
                result = self.expression_parser.evaluate_node(node)
            elif lbl.value_type_index == 3:
                transcript = self.transcript.get_for_participant(
                    template_msg.source if template_msg.source.
                    simulate else template_msg.destination)

                if template_msg.destination.simulate:
                    direction = "->" if template_msg.source.simulate else "<-"
                    transcript += "\n" + direction + new_message.plain_bits_str + "\n"

                cmd = self.__fill_counter_values(lbl.external_program)
                result = util.run_command(cmd, transcript, use_stdin=True)
                if len(result) != lbl.end - lbl.start:
                    log_msg = "Result value of external program {}: {} ({}) does not match label length {}"
                    logger.error(
                        log_msg.format(cmd, result, len(result),
                                       lbl.end - lbl.start))
                    continue

                try:
                    new_message[lbl.start:lbl.end] = array.array(
                        "B", (map(bool, map(int, result))))
                except Exception as e:
                    log_msg = "Could not assign {} to range because {}".format(
                        result, e)
                    logger.error(log_msg)

                continue
            elif lbl.value_type_index == 4:
                # random value
                result = numpy.random.randint(lbl.random_min,
                                              lbl.random_max + 1)
            else:
                continue

            self.set_label_value(new_message, lbl, result)

        return new_message
Exemplo n.º 4
0
    def generate_message_from_template(self, template_msg: SimulatorMessage):
        new_message = Message(template_msg.plain_bits, pause=template_msg.pause, rssi=0,
                              message_type=template_msg.message_type, decoder=template_msg.decoder)

        for lbl in template_msg.children:  # type: SimulatorProtocolLabel
            if lbl.value_type_index == 2:
                # formula
                valid, _, node = self.expression_parser.validate_expression(lbl.formula)
                assert valid
                result = self.expression_parser.evaluate_node(node)
            elif lbl.value_type_index == 3:
                transcript = self.transcript.get_for_participant(template_msg.source
                                                                 if template_msg.source.simulate
                                                                 else template_msg.destination)

                if template_msg.destination.simulate:
                    direction = "->" if template_msg.source.simulate else "<-"
                    transcript += "\n" + direction + new_message.plain_bits_str + "\n"

                cmd = self.__fill_counter_values(lbl.external_program)
                result = util.run_command(cmd, transcript, use_stdin=True)
                if len(result) != lbl.end - lbl.start:
                    log_msg = "Result value of external program {}: {} ({}) does not match label length {}"
                    logger.error(log_msg.format(cmd, result, len(result), lbl.end - lbl.start))
                    continue

                try:
                    new_message[lbl.start:lbl.end] = array.array("B", (map(bool, map(int, result))))
                except Exception as e:
                    log_msg = "Could not assign {} to range because {}".format(result, e)
                    logger.error(log_msg)

                continue
            elif lbl.value_type_index == 4:
                # random value
                result = numpy.random.randint(lbl.random_min, lbl.random_max + 1)
            else:
                continue

            self.set_label_value(new_message, lbl, result)

        return new_message
Exemplo n.º 5
0
    def simulate(self):
        self.simulation_started.emit()
        self.is_simulating = self.__wait_for_devices()

        if not self.is_simulating:
            # Simulation may have ended due to device errors
            self.stop("Devices not ready")
            return

        self.log_message("<b>Simulation is running</b>")

        while self.is_simulating and not self.simulation_is_finished():
            if self.current_item is self.simulator_config.rootItem:
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorProtocolLabel):
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorMessage):
                self.process_message()
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorGotoAction):
                next_item = self.current_item.target
                self.log_message("GOTO item " + next_item.index())

            elif isinstance(self.current_item, SimulatorTriggerCommandAction):
                next_item = self.current_item.next()
                command = self.__fill_counter_values(self.current_item.command)
                self.log_message("Calling {}".format(command))
                if self.current_item.pass_transcript:
                    transcript = "\n".join(self.transcript.get_for_all_participants(all_rounds=False))
                    result, rc = util.run_command(command, transcript, use_stdin=True, return_rc=True)
                else:
                    result, rc = util.run_command(command, param=None, detailed_output=True, return_rc=True)
                self.current_item.return_code = rc
                self.log_message(result)

            elif isinstance(self.current_item, SimulatorRule):
                condition = self.current_item.get_first_applying_condition()

                if condition is not None and condition.logging_active and condition.type != ConditionType.ELSE:
                    self.log_message("Rule condition " + condition.index() + " (" + condition.condition + ") applied")

                if condition is not None and condition.child_count() > 0:
                    next_item = condition.children[0]
                else:
                    next_item = self.current_item.next_sibling()

            elif isinstance(self.current_item, SimulatorRuleCondition):
                if self.current_item.type == ConditionType.IF:
                    next_item = self.current_item.parent()
                else:
                    next_item = self.current_item.parent().next_sibling()

            elif isinstance(self.current_item, SimulatorSleepAction):
                self.log_message(self.current_item.caption)
                time.sleep(self.current_item.sleep_time)
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorCounterAction):
                self.current_item.progress_value()
                self.log_message("Increase counter by {} to {}".format(self.current_item.step, self.current_item.value))
                next_item = self.current_item.next()

            elif self.current_item is None:
                self.current_repeat += 1
                next_item = self.simulator_config.rootItem
                self.transcript.start_new_round()

            else:
                raise ValueError("Unknown action {}".format(type(self.current_item)))

            self.current_item = next_item

            if self.do_restart:
                self.restart()

        self.stop(msg="Finished")
Exemplo n.º 6
0
    def simulate(self):
        self.simulation_started.emit()
        self.is_simulating = self.__wait_for_devices()

        if not self.is_simulating:
            # Simulation may have ended due to device errors
            self.stop("Devices not ready")
            return

        self.log_message("<b>Simulation is running</b>")

        while self.is_simulating and not self.simulation_is_finished():
            if self.current_item is self.simulator_config.rootItem:
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorProtocolLabel):
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorMessage):
                self.process_message()
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorGotoAction):
                next_item = self.current_item.target
                self.log_message("GOTO item " + next_item.index())

            elif isinstance(self.current_item, SimulatorTriggerCommandAction):
                next_item = self.current_item.next()
                command = self.__fill_counter_values(self.current_item.command)
                self.log_message("Calling {}".format(command))
                if self.current_item.pass_transcript:
                    transcript = "\n".join(self.transcript.get_for_all_participants(all_rounds=False))
                    result, rc = util.run_command(command, transcript, use_stdin=True, return_rc=True)
                else:
                    result, rc = util.run_command(command, param=None, detailed_output=True, return_rc=True)
                self.current_item.return_code = rc
                self.log_message(result)

            elif isinstance(self.current_item, SimulatorRule):
                condition = self.current_item.get_first_applying_condition()

                if condition is not None and condition.logging_active and condition.type != ConditionType.ELSE:
                    self.log_message("Rule condition " + condition.index() + " (" + condition.condition + ") applied")

                if condition is not None and condition.child_count() > 0:
                    next_item = condition.children[0]
                else:
                    next_item = self.current_item.next_sibling()

            elif isinstance(self.current_item, SimulatorRuleCondition):
                if self.current_item.type == ConditionType.IF:
                    next_item = self.current_item.parent()
                else:
                    next_item = self.current_item.parent().next_sibling()

            elif isinstance(self.current_item, SimulatorSleepAction):
                self.log_message(self.current_item.caption)
                time.sleep(self.current_item.sleep_time)
                next_item = self.current_item.next()

            elif isinstance(self.current_item, SimulatorCounterAction):
                self.current_item.progress_value()
                self.log_message("Increase counter by {} to {}".format(self.current_item.step, self.current_item.value))
                next_item = self.current_item.next()

            elif self.current_item is None:
                self.current_repeat += 1
                next_item = self.simulator_config.rootItem
                self.transcript.start_new_round()

            else:
                raise ValueError("Unknown action {}".format(type(self.current_item)))

            self.current_item = next_item

            if self.do_restart:
                self.restart()

        self.stop(msg="Finished")