From dce5a1d4599c1fa65899cd6f9aab000e5a64428f Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 26 Mar 2025 21:32:39 +0000 Subject: [PATCH] Added rsi injection code. --- src/RSIPI/rsi_api.py | 19 ++++++++++++++++++- src/RSIPI/rsi_cli.py | 15 +++++++++++++++ src/RSIPI/test_rsipi.py | 22 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/RSIPI/rsi_api.py b/src/RSIPI/rsi_api.py index fa4d8a6..63d8df4 100644 --- a/src/RSIPI/rsi_api.py +++ b/src/RSIPI/rsi_api.py @@ -7,6 +7,7 @@ from .rsi_client import RSIClient from .rsi_graphing import RSIGraphing from .kuka_visualizer import KukaRSIVisualizer from .krl_to_csv_parser import KRLParser +from .inject_rsi_to_krl import inject_rsi_to_krl class RSIAPI: """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" @@ -231,4 +232,20 @@ class RSIAPI: parser.export_csv(output_file) return f"✅ KRL data successfully exported to {output_file}" except Exception as e: - return f"❌ Error parsing KRL files: {e}" \ No newline at end of file + return f"❌ Error parsing KRL files: {e}" + + def inject_rsi(self, input_krl, output_krl=None, rsi_config="RSIGatewayv1.rsi"): + """ + Inject RSI commands into a KRL (.src) program file. + + Args: + input_krl (str): Path to the input KRL file. + output_krl (str, optional): Path to the output file (defaults to overwriting input). + rsi_config (str, optional): RSI configuration file name. + """ + try: + inject_rsi_to_krl(input_krl, output_krl, rsi_config) + output_path = output_krl if output_krl else input_krl + return f"✅ RSI successfully injected into {output_path}" + except Exception as e: + return f"❌ RSI injection failed: {e}" diff --git a/src/RSIPI/rsi_cli.py b/src/RSIPI/rsi_cli.py index 5360d39..6e08e84 100644 --- a/src/RSIPI/rsi_cli.py +++ b/src/RSIPI/rsi_cli.py @@ -1,6 +1,7 @@ from .rsi_client import RSIClient from .kuka_visualizer import KukaRSIVisualizer from .krl_to_csv_parser import KRLParser +from .inject_rsi_to_krl import inject_rsi_to_krl class RSICommandLineInterface: """Command-Line Interface for controlling RSI Client.""" @@ -82,6 +83,11 @@ class RSICommandLineInterface: elif cmd == "krlparse" and len(parts) == 4: src_file, dat_file, output_file = parts[1], parts[2], parts[3] self.krl_parse(src_file, dat_file, output_file) + elif cmd == "inject_rsi" and len(parts) >= 2: + input_krl = parts[1] + output_krl = parts[2] if len(parts) >= 3 else None + rsi_config = parts[3] if len(parts) == 4 else "RSIGatewayv1.rsi" + self.inject_rsi(input_krl, output_krl, rsi_config) else: print("❌ Unknown command. Type 'help' for a list of commands.") @@ -169,6 +175,15 @@ Available Commands: except Exception as e: print(f"❌ Failed to parse KRL files: {e}") + def inject_rsi(self, input_krl, output_krl=None, rsi_config="RSIGatewayv1.rsi"): + """Inject RSI commands into a KRL file via CLI.""" + try: + inject_rsi_to_krl(input_krl, output_krl, rsi_config) + output_path = output_krl if output_krl else input_krl + print(f"✅ RSI commands successfully injected into '{output_path}'") + except Exception as e: + print(f"❌ Failed to inject RSI commands: {e}") + if __name__ == "__main__": config_file = "RSI_EthernetConfig.xml" cli = RSICommandLineInterface(config_file) diff --git a/src/RSIPI/test_rsipi.py b/src/RSIPI/test_rsipi.py index 4f8a5cf..7f4aa6c 100644 --- a/src/RSIPI/test_rsipi.py +++ b/src/RSIPI/test_rsipi.py @@ -131,6 +131,28 @@ class TestRSIPI(unittest.TestCase): os.remove(dat_file) os.remove(output_file) + def test_inject_rsi(self): + input_krl = "test_program.src" + output_krl = "test_program_rsi.src" + + with open(input_krl, "w") as file: + file.write("DEF Test()\n") + file.write(" ;ENDFOLD (INI)\n") + file.write("END\n") + + response = self.api.inject_rsi(input_krl, output_krl) + self.assertIn("✅ RSI successfully injected", response) + + with open(output_krl, "r") as file: + content = file.read() + self.assertIn("RSI_CREATE", content) + self.assertIn("RSI_ON", content) + self.assertIn("RSI_OFF", content) + + # Cleanup + import os + os.remove(input_krl) + os.remove(output_krl) if __name__ == '__main__': unittest.main() \ No newline at end of file