From 57a53ff5696b3e329fbd0e708c43a5409f6712ea Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 1 Apr 2025 21:22:09 +0100 Subject: [PATCH] Code ready for testing. --- src/RSIPI/main.py | 51 ++++++++++++++++++++++-------------- src/RSIPI/network_handler.py | 10 ++++--- src/RSIPI/rsi_api.py | 10 ++++--- src/RSIPI/rsi_echo_server.py | 2 +- src/RSIPI/xml_handler.py | 15 +++++++---- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/RSIPI/main.py b/src/RSIPI/main.py index 284f863..5deb3f5 100644 --- a/src/RSIPI/main.py +++ b/src/RSIPI/main.py @@ -2,27 +2,40 @@ from src.RSIPI.rsi_client import RSIClient from time import sleep if __name__ == "__main__": - config_file = "RSI_EthernetConfig.xml" # Ensure this file exists in the working directory - client = RSIClient(config_file) - client.start() - - # print("done") + # config_file = "RSI_EthernetConfig.xml" # Ensure this file exists in the working directory + # client = RSIClient(config_file) + # client.start() # - # # client.stop() + # # print("done") + # # + # # # client.stop() + # sleep(5) + # print("rdfsfsdfsfsdfjsjfhakjshfd") + # client.update_send_variable("EStr", "Testing 123 Testing") + # sleep(20) + # client.stop() + from src.RSIPI.rsi_api import RSIAPI + from time import sleep + + api = RSIAPI() + api.start_rsi() + sleep(4) + # Dynamically update a variable + api.update_variable("EStr", "Tessting 123") + sleep(5) - print("rdfsfsdfsfsdfjsjfhakjshfd") - client.update_send_variable("EStr", "Testing 123 Testing") - sleep(20) - client.stop() -# from rsi_api import RSIAPI + + api.stop_rsi() + +# from src.RSIPI.rsi_api import RSIAPI # from time import sleep +# def main(): +# api = RSIAPI() +# response = api.start_rsi() +# sleep(50) +# print(response) # -# api = RSIAPI() -# api.start_rsi() -# sleep(5) -# # Dynamically update a variable -# api.update_variable("EStr", "Tessting 123") # -# while True: -# pass -# # api.stop_rsi() \ No newline at end of file +# +# if __name__ == "__main__": +# main() \ No newline at end of file diff --git a/src/RSIPI/network_handler.py b/src/RSIPI/network_handler.py index ef89af4..745181b 100644 --- a/src/RSIPI/network_handler.py +++ b/src/RSIPI/network_handler.py @@ -57,12 +57,16 @@ class NetworkProcess(multiprocessing.Process): try: self.udp_socket.settimeout(5) data_received, self.controller_ip_and_port = self.udp_socket.recvfrom(1024) - print(data_received) + print("Receive: ", data_received) + print("HERE 1") message = data_received.decode() - + print("HERE 2") self.process_received_data(message) + print("HERE 3") + print("Network :", self.send_variables) send_xml = XMLGenerator.generate_send_xml(self.send_variables, self.config_parser.network_settings) - print(send_xml) + print("HERE 4") + print("Send:", send_xml) self.udp_socket.sendto(send_xml.encode(), self.controller_ip_and_port) # ✅ If logging is active, write data to CSV diff --git a/src/RSIPI/rsi_api.py b/src/RSIPI/rsi_api.py index 63d8df4..ab76abb 100644 --- a/src/RSIPI/rsi_api.py +++ b/src/RSIPI/rsi_api.py @@ -8,6 +8,7 @@ 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 +import threading # (Put this at the top of the file) class RSIAPI: """RSI API for programmatic control, including alerts, logging, graphing, and data retrieval.""" @@ -17,10 +18,13 @@ class RSIAPI: self.client = RSIClient(config_file) self.graph_process = None # Store graphing process + import threading # (Put this at the top of the file) + def start_rsi(self): - """Start the RSI client.""" - self.client.start() - return "✅ RSI started." + """Start the RSI client in a background thread.""" + self.thread = threading.Thread(target=self.client.start, daemon=True) + self.thread.start() + return "✅ RSI started in background." def stop_rsi(self): """Stop the RSI client.""" diff --git a/src/RSIPI/rsi_echo_server.py b/src/RSIPI/rsi_echo_server.py index 787091b..8ce4a91 100644 --- a/src/RSIPI/rsi_echo_server.py +++ b/src/RSIPI/rsi_echo_server.py @@ -3,7 +3,7 @@ import time import xml.etree.ElementTree as ET import logging import threading -from .rsi_config import RSIConfig +from rsi_config import RSIConfig # ✅ Configure Logging LOGGING_ENABLED = True diff --git a/src/RSIPI/xml_handler.py b/src/RSIPI/xml_handler.py index 9e4639c..a01b2d8 100644 --- a/src/RSIPI/xml_handler.py +++ b/src/RSIPI/xml_handler.py @@ -3,22 +3,27 @@ import xml.etree.ElementTree as ET class XMLGenerator: """Converts send and receive variables into properly formatted XML messages.""" + @staticmethod @staticmethod def generate_send_xml(send_variables, network_settings): """Generate the send XML message dynamically based on send variables.""" - root = ET.Element("Sen", Type=network_settings["sentype"]) # ✅ Root with Type from config - + root = ET.Element("Sen", Type=network_settings["sentype"]) + print("XML 1") + print("XML : ", send_variables) for key, value in send_variables.items(): if key == "FREE": - continue # explicitly skip FREE + continue - if isinstance(value, dict): # ✅ Handle dictionaries as elements with attributes + if isinstance(value, dict): element = ET.SubElement(root, key) for sub_key, sub_value in value.items(): element.set(sub_key, f"{float(sub_value):.2f}") - else: # ✅ Handle standard elements with text values + else: ET.SubElement(root, key).text = str(value) + if key == "EStr": + print(f"[DEBUG] XMLGenerator sees EStr: {value}") + print("XML 2") return ET.tostring(root, encoding="utf-8").decode() @staticmethod