Added rsi injection code.

This commit is contained in:
Adam 2025-03-26 21:32:39 +00:00
parent 9140bbcffd
commit dce5a1d459
3 changed files with 55 additions and 1 deletions

View File

@ -7,6 +7,7 @@ from .rsi_client import RSIClient
from .rsi_graphing import RSIGraphing from .rsi_graphing import RSIGraphing
from .kuka_visualizer import KukaRSIVisualizer from .kuka_visualizer import KukaRSIVisualizer
from .krl_to_csv_parser import KRLParser from .krl_to_csv_parser import KRLParser
from .inject_rsi_to_krl import inject_rsi_to_krl
class RSIAPI: class RSIAPI:
"""RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval."""
@ -232,3 +233,19 @@ class RSIAPI:
return f"✅ KRL data successfully exported to {output_file}" return f"✅ KRL data successfully exported to {output_file}"
except Exception as e: except Exception as e:
return f"❌ Error parsing KRL files: {e}" 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}"

View File

@ -1,6 +1,7 @@
from .rsi_client import RSIClient from .rsi_client import RSIClient
from .kuka_visualizer import KukaRSIVisualizer from .kuka_visualizer import KukaRSIVisualizer
from .krl_to_csv_parser import KRLParser from .krl_to_csv_parser import KRLParser
from .inject_rsi_to_krl import inject_rsi_to_krl
class RSICommandLineInterface: class RSICommandLineInterface:
"""Command-Line Interface for controlling RSI Client.""" """Command-Line Interface for controlling RSI Client."""
@ -82,6 +83,11 @@ class RSICommandLineInterface:
elif cmd == "krlparse" and len(parts) == 4: elif cmd == "krlparse" and len(parts) == 4:
src_file, dat_file, output_file = parts[1], parts[2], parts[3] src_file, dat_file, output_file = parts[1], parts[2], parts[3]
self.krl_parse(src_file, dat_file, output_file) 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: else:
print("❌ Unknown command. Type 'help' for a list of commands.") print("❌ Unknown command. Type 'help' for a list of commands.")
@ -169,6 +175,15 @@ Available Commands:
except Exception as e: except Exception as e:
print(f"❌ Failed to parse KRL files: {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__": if __name__ == "__main__":
config_file = "RSI_EthernetConfig.xml" config_file = "RSI_EthernetConfig.xml"
cli = RSICommandLineInterface(config_file) cli = RSICommandLineInterface(config_file)

View File

@ -131,6 +131,28 @@ class TestRSIPI(unittest.TestCase):
os.remove(dat_file) os.remove(dat_file)
os.remove(output_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__': if __name__ == '__main__':
unittest.main() unittest.main()