diff --git a/examples/custom_m_codes.py b/examples/custom_m_codes.py index 8a78f20..af43ac9 100755 --- a/examples/custom_m_codes.py +++ b/examples/custom_m_codes.py @@ -12,6 +12,25 @@ from dsf.commands.code import CodeType from dsf.object_model import MessageType +# needed to resolve meta variables +from dsf.connections import CommandConnection +from dsf.commands.generic import evaluate_expression + + +# function to ask RRF to resolve a meta variable +def eval_expr( meta_var, channel): + command_connection = CommandConnection(debug=True) + command_connection.connect() + + try: + res = command_connection.perform_command(evaluate_expression( channel, meta_var )) + result = res.result + print(f"Evaluated expression: {result}") + finally: + command_connection.close() + + return result + def start_intercept(): filters = ["M1234", "M5678", "M7722"] @@ -50,10 +69,27 @@ def start_intercept(): # Exit this example return elif cde.type == CodeType.MCode and cde.majorNumber == 7722: + if cde.parameter("S") is None : + # if there isnt an S parameter set minutes to 1 + minutes = "1" + elif cde.parameter("S").is_expression : + # if there is an S parameter and it is an expression + # resolve the expression and set minutes equal to it + minutes = eval_expr( cde.parameter("S").string_value, cde.channel ) + else : + # if there is an S parameter and it isnt an expression + # set minutes to it + minutes = cde.parameter("S").string_value + + # add a + to the beginning of minutes + min_plus = "+" + str(minutes) + # We are going to shut down the SBC in one minute - subprocess.run(["sudo", "shutdown", "+1"]) + subprocess.run(["sudo", "shutdown", min_plus]) + # Resolve it with a custom response message text - intercept_connection.resolve_code(MessageType.Warn, "Shutting down SBC in 1min...") + warn_msg = "Shutting down SBC in " + str(minutes) + "min..." + intercept_connection.resolve_code(MessageType.Warning, warn_msg) else: # We did not handle it so we ignore it and it will be continued to be processed intercept_connection.ignore_code() diff --git a/src/dsf/commands/code_parameter.py b/src/dsf/commands/code_parameter.py index 24c95ed..88cd937 100644 --- a/src/dsf/commands/code_parameter.py +++ b/src/dsf/commands/code_parameter.py @@ -41,7 +41,7 @@ def __init__(self, letter: str, value, isString: bool = None, isDriverId: bool = self.letter = letter self.string_value = str(value) self.__parsed_value = value - self.is_expression = self.string_value.startswith("{}") and self.string_value.endswith("}") + self.is_expression = self.string_value.startswith("{") and self.string_value.endswith("}") return self.letter = letter @@ -60,7 +60,7 @@ def __init__(self, letter: str, value, isString: bool = None, isDriverId: bool = # Empty parameters are represented as integers with the value 0 (e.g. G92 XY => G92 X0 Y0) if not value: self.__parsed_value = 0 - elif value.startswith("{}") and value.endswith("}"): # It is an expression + elif value.startswith("{") and value.endswith("}"): # It is an expression self.is_expression = True self.__parsed_value = value elif ":" in value: # It is an array (or a string)