159 lines
5.4 KiB
Python
159 lines
5.4 KiB
Python
"""
|
|
OB16E Digital Output Module L5X Generator (Boilerplate-based)
|
|
============================================================
|
|
|
|
This module provides functionality to generate OB16E module L5X files by
|
|
loading a boilerplate template and modifying specific fields.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Optional, Dict
|
|
import xml.etree.ElementTree as ET
|
|
from .base_boilerplate_model import BaseModuleConfig, BaseBoilerplateGenerator
|
|
|
|
|
|
@dataclass
|
|
class OB16EModuleConfig(BaseModuleConfig):
|
|
"""Configuration for an OB16E module."""
|
|
name: str
|
|
boilerplate_path: str = "boilerplate/SLOT6_OB16E_Module.L5X"
|
|
slot_address: str = "6"
|
|
parent_module: str = "Local"
|
|
parent_port_id: str = "1"
|
|
output_device_names: Optional[Dict[int, str]] = None # Names for 16 output devices (0-15)
|
|
|
|
def get_updates(self):
|
|
"""Get dictionary of updates to apply."""
|
|
return {
|
|
"name": self.name,
|
|
"slot_address": self.slot_address,
|
|
"parent_module": self.parent_module,
|
|
"parent_port_id": self.parent_port_id,
|
|
"output_device_names": self.output_device_names
|
|
}
|
|
|
|
|
|
class OB16EModuleGenerator(BaseBoilerplateGenerator):
|
|
"""Generator for OB16E module L5X files using boilerplate template."""
|
|
|
|
def apply_updates(self):
|
|
"""Apply OB16E-specific updates to the boilerplate."""
|
|
# Update TargetName in root element
|
|
self.root.set("TargetName", self.config.name)
|
|
|
|
# Update Module name and parent info
|
|
module = self.root.find(".//Module[@Use='Target']")
|
|
if module is not None:
|
|
module.set("Name", self.config.name)
|
|
module.set("ParentModule", self.config.parent_module)
|
|
module.set("ParentModPortId", self.config.parent_port_id)
|
|
|
|
# Update slot address (ICP port)
|
|
icp_port = self.root.find(".//Module[@Use='Target']/Ports/Port[@Type='ICP']")
|
|
if icp_port is not None:
|
|
icp_port.set("Address", self.config.slot_address)
|
|
|
|
# Update output device comments
|
|
self.update_comments()
|
|
|
|
def update_comments(self):
|
|
"""Update comments for output devices."""
|
|
# Always clear template comments first
|
|
output_comments = self.root.find(".//Connection[@Name='Fused']/OutputTag/Comments")
|
|
if output_comments is None:
|
|
return
|
|
|
|
output_comments.clear()
|
|
|
|
if not self.config.output_device_names:
|
|
return
|
|
|
|
for index, text in self.config.output_device_names.items():
|
|
comment = ET.SubElement(output_comments, "Comment")
|
|
comment.set("Operand", f".DATA.{index}")
|
|
comment.text = text
|
|
|
|
@classmethod
|
|
def from_mapping(cls, mapping: Dict[str, str], comments: Optional[Dict[int, str]] = None) -> "OB16EModuleGenerator":
|
|
"""Create and fully configure an OB16E generator from the Excel-derived
|
|
`modules` entry (a plain dict). The structure expected is the one
|
|
produced in EnhancedMCMGenerator._organize_modules_by_type().
|
|
|
|
:param comments: Optional dict of output index (0-15) to comment text.
|
|
"""
|
|
|
|
cfg = OB16EModuleConfig(
|
|
name=mapping["name"],
|
|
slot_address=mapping["slot_address"],
|
|
parent_module="Local",
|
|
parent_port_id="1",
|
|
output_device_names=comments,
|
|
)
|
|
|
|
gen = cls(cfg)
|
|
gen.load_boilerplate()
|
|
gen.apply_updates()
|
|
return gen
|
|
|
|
|
|
# Factory function
|
|
def create_ob16e_module(
|
|
name: str,
|
|
slot_address: str = "6",
|
|
parent_module: str = "Local",
|
|
parent_port_id: str = "1",
|
|
output_device_names: Optional[Dict[int, str]] = None
|
|
) -> OB16EModuleConfig:
|
|
"""
|
|
Create an OB16E module configuration.
|
|
|
|
Args:
|
|
name: Module name
|
|
slot_address: Slot number in the chassis
|
|
parent_module: Parent module name
|
|
parent_port_id: Parent module port ID
|
|
output_device_names: Dictionary mapping output index (0-15) to device names
|
|
|
|
Returns:
|
|
OB16EModuleConfig object
|
|
"""
|
|
return OB16EModuleConfig(
|
|
name=name,
|
|
slot_address=slot_address,
|
|
parent_module=parent_module,
|
|
parent_port_id=parent_port_id,
|
|
output_device_names=output_device_names
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Example usage
|
|
config = create_ob16e_module(
|
|
name="SLOT3_OB16E",
|
|
slot_address="3",
|
|
parent_module="Local",
|
|
parent_port_id="1",
|
|
output_device_names={
|
|
0: "Motor 1 Starter",
|
|
1: "Motor 2 Starter",
|
|
2: "Pump Control",
|
|
3: "Valve 1 Open",
|
|
4: "Valve 2 Close",
|
|
5: "Alarm Horn",
|
|
6: "Status Light Red",
|
|
7: "Status Light Green",
|
|
8: "Conveyor Start",
|
|
9: "Fan Control",
|
|
10: "Heater Enable",
|
|
11: "Solenoid 1",
|
|
12: "Solenoid 2",
|
|
13: "Brake Release",
|
|
14: "Emergency Light",
|
|
15: "System Ready Lamp"
|
|
}
|
|
)
|
|
|
|
# Generate the module
|
|
generator = OB16EModuleGenerator(config)
|
|
generator.save_to_file("generated/SLOT3_OB16E.L5X")
|
|
print(f"Generated OB16E module configuration saved to generated/SLOT3_OB16E.L5X") |