From 4a9b918078e19902fd52a686da7ec3a919d1272a Mon Sep 17 00:00:00 2001 From: Michael Cargile <32649874+vicimikec@users.noreply.github.com> Date: Tue, 14 Mar 2023 18:26:27 -0400 Subject: [PATCH 1/4] Improved Example Improved the custom_m_codes.py script to include handling parameters as well as Meta Variables. --- examples/custom_m_codes.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/examples/custom_m_codes.py b/examples/custom_m_codes.py index 8a78f20..60064d4 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 getMetaVar( 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,25 @@ def start_intercept(): # Exit this example return elif cde.type == CodeType.MCode and cde.majorNumber == 7722: + if ( cde.parameter("S") == None ): + minutes = "1" + else: + minutes = cde.parameter("S").string_value + + # check if minutes starts with a { if so it is a meta variable from a macro + if ( minutes.startswith('{') ): + # resolve the meta variable + minutes = getMetaVar( minutes, cde.channel ) + + # 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() From 35b38a27fddf521f83a525a229ffe47c96f4de56 Mon Sep 17 00:00:00 2001 From: Christian Hammacher Date: Wed, 15 Mar 2023 15:59:07 +0100 Subject: [PATCH 2/4] Update custom_m_codes.py --- examples/custom_m_codes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/custom_m_codes.py b/examples/custom_m_codes.py index 8a78f20..f7a31a5 100755 --- a/examples/custom_m_codes.py +++ b/examples/custom_m_codes.py @@ -53,7 +53,7 @@ def start_intercept(): # We are going to shut down the SBC in one minute subprocess.run(["sudo", "shutdown", "+1"]) # Resolve it with a custom response message text - intercept_connection.resolve_code(MessageType.Warn, "Shutting down SBC in 1min...") + intercept_connection.resolve_code(MessageType.Warning, "Shutting down SBC in 1min...") else: # We did not handle it so we ignore it and it will be continued to be processed intercept_connection.ignore_code() From f0e1c05f576b2df2b7040a7dca831d329b836c6b Mon Sep 17 00:00:00 2001 From: Michael Cargile <32649874+vicimikec@users.noreply.github.com> Date: Fri, 17 Mar 2023 20:29:57 -0400 Subject: [PATCH 3/4] fixed checked used for is_expression --- src/dsf/commands/code_parameter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) From 63ae7ee50c594d3f4f054b7e11513093cd56e040 Mon Sep 17 00:00:00 2001 From: Michael Cargile <32649874+vicimikec@users.noreply.github.com> Date: Fri, 17 Mar 2023 20:41:27 -0400 Subject: [PATCH 4/4] switched to use is_expression Switched from using startswith() for checking if a S parameter is an expression to using is_expression. Also made formatting changes. --- examples/custom_m_codes.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/custom_m_codes.py b/examples/custom_m_codes.py index 60064d4..af43ac9 100755 --- a/examples/custom_m_codes.py +++ b/examples/custom_m_codes.py @@ -18,7 +18,7 @@ # function to ask RRF to resolve a meta variable -def getMetaVar( meta_var, channel): +def eval_expr( meta_var, channel): command_connection = CommandConnection(debug=True) command_connection.connect() @@ -69,16 +69,18 @@ def start_intercept(): # Exit this example return elif cde.type == CodeType.MCode and cde.majorNumber == 7722: - if ( cde.parameter("S") == None ): + if cde.parameter("S") is None : + # if there isnt an S parameter set minutes to 1 minutes = "1" - else: + 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 - # check if minutes starts with a { if so it is a meta variable from a macro - if ( minutes.startswith('{') ): - # resolve the meta variable - minutes = getMetaVar( minutes, cde.channel ) - # add a + to the beginning of minutes min_plus = "+" + str(minutes)