39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
import xml.etree.ElementTree as ET
|
|
|
|
class XMLGenerator:
|
|
"""Converts send and receive variables into properly formatted XML messages."""
|
|
|
|
@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
|
|
|
|
for key, value in send_variables.items():
|
|
if key == "FREE":
|
|
continue # explicitly skip FREE
|
|
|
|
if isinstance(value, dict): # ✅ Handle dictionaries as elements with attributes
|
|
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
|
|
ET.SubElement(root, key).text = str(value)
|
|
|
|
return ET.tostring(root, encoding="utf-8").decode()
|
|
|
|
@staticmethod
|
|
def generate_receive_xml(receive_variables):
|
|
"""Generate the receive XML message dynamically based on receive variables."""
|
|
root = ET.Element("Rob", Type="KUKA") # ✅ Root with Type="KUKA"
|
|
|
|
for key, value in receive_variables.items():
|
|
if isinstance(value, dict): # ✅ Handle dictionaries as elements with attributes
|
|
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
|
|
ET.SubElement(root, key).text = str(value)
|
|
|
|
return ET.tostring(root, encoding="utf-8").decode()
|
|
|