Get XSY module mostly feature-complete.

This commit is contained in:
Silcantar
2026-10-02 16:34:43 -05:00
parent c59544e6a6
commit 304c21b478
4 changed files with 892 additions and 88 deletions

View File

@@ -386,6 +386,479 @@
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="Para_RAMP">
<structure>
<variables name="inc_rate" typeName="REAL">
</variables>
<variables name="dec_rate" typeName="REAL">
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="ADDM_TYPE">
<structure>
<variables name="array" typeName="ARRAY [0..7] of INT">
<comment>Common array for communication EF Mid-Range</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="ADDMX_TYPE">
<structure>
<variables name="array" typeName="ARRAY [0..15] of INT">
<comment>Common array for EIO communication function blocks</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="ADDR_TYPE">
<structure>
<variables name="array" typeName="ARRAY [0..5] of INT">
<comment>Common array for communication EF</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="MSTR_CTL_EXT">
<comment>Datatype extended for master control block MSTR_CTL</comment>
<structure>
<variables name="operation" typeName="UINT"></variables>
<comment>operation code</comment>
<variables name="status" typeName="UINT"></variables>
<comment>error status</comment>
<variables name="length" typeName="UINT"></variables>
<comment>number of data units transferred</comment>
<variables name="reference" typeName="UINT"></variables>
<comment>operation-dependent information</comment>
<variables name="route" typeName="ARRAY [0..4] of UINT"></variables>
<comment>routing address</comment>
<variables name="ext" typeName="ARRAY [1..5] of UINT"></variables>
<comment>operation-dependent information</comment>
</structure>
</DDTSource>
<DDTSource DDTName="WordArr5">
<structure>
<variables name="array" typeName="ARRAY [0..5] of WORD"></variables>
</structure>
</DDTSource>
<DDTSource DDTName="XMIT_SET">
<comment>Data structure for the XMIT configuration</comment>
<structure>
<variables name="BaudRate" typeName="INT">
<comment>Data Rate</comment>
</variables>
<variables name="DataBits" typeName="WORD">
<comment>Data bits</comment>
</variables>
<variables name="Parity" typeName="BOOL">
<comment>Parity</comment>
</variables>
<variables name="StopBits" typeName="WORD">
<comment>Stop bits</comment>
</variables>
<variables name="CommandWord" typeName="WORD">
<comment>Command word</comment>
</variables>
<variables name="MessageLen" typeName="INT">
<comment>Message Length</comment>
</variables>
<variables name="RetryLimit" typeName="INT">
<comment>Retry limit</comment>
</variables>
<variables name="XmStartDelay" typeName="INT">
<comment>Start of transmission delay (ms)</comment>
</variables>
<variables name="XmEndDelay" typeName="INT">
<comment>End of transmission delay (ms)</comment>
</variables>
<variables name="RespTimeOut" typeName="INT">
<comment>Response time-out (ms)</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="T_M_CRA_EXT_IN">
<structure>
<variables name="IO_HEALTH_RACK0" typeName="WORD">
<comment>Health bits of rack 0: slots 0 to 15</comment>
</variables>
<variables name="IO_HEALTH_RACK1" typeName="WORD">
<comment>Health bits of rack 1: slots 0 to 15</comment>
</variables>
<variables name="DEVICE_NAME" typeName="STRING[16]">
<comment>Device name of the CRA drop</comment>
</variables>
<variables name="VERSION" typeName="WORD">
<comment>Firmware version of the CRA</comment>
</variables>
<variables name="ROTARY_SWITCHES" typeName="BYTE">
<comment>Rotary switches value at power up</comment>
</variables>
<variables name="CRA_STATE" typeName="BYTE">
<comment>State of the CRA module</comment>
</variables>
<variables name="CRA_DIAGNOSTIC" typeName="WORD">
<comment>CRA diagnostic information</comment>
</variables>
<variables name="GLOBAL_IO_HEALTH" typeName="BOOL">
<comment>At least one IO error in drop when 0</comment>
</variables>
<variables name="CCOTF_IN_PROGRESS" typeName="BOOL">
<comment>CCOTF in progress</comment>
</variables>
<variables name="CCOTF_INVALID_CONF" typeName="BOOL">
<comment>CCOTF error</comment>
</variables>
<variables name="IOPL_MISMATCH" typeName="BOOL">
<comment>IOPL mismatch</comment>
</variables>
<variables name="SWITCH_CHANGE" typeName="BOOL">
<comment>Rotary switches have changed since last power up</comment>
</variables>
<variables name="DROP_COM_HEALTH" typeName="BOOL">
<comment>Drop communication health</comment>
</variables>
<variables name="REDUNDANT_POWER_SUPPLY_STATUS" typeName="BOOL">
<comment>Redundant power supply status</comment>
</variables>
<variables name="CYCLE_CURR_TIME" typeName="UINT">
<comment>Execution time of the last CRA cycle (unit: 0.01 ms)</comment>
</variables>
<variables name="CYCLE_MAX_TIME" typeName="UINT">
<comment>Longes CRA cycle execution time (unit: 0.01 ms)</comment>
</variables>
<variables name="CYCLE_MIN_TIME" typeName="UINT">
<comment>Shortest CRA cycle execution time (uint: 0.01 ms)</comment>
</variables>
<variables name="TIME_STAMP_RECORDS" typeName="UINT">
<comment>Number of time stamp records available in the drop local buffer</comment>
</variables>
<variables name="TS_DIAGNOSTIC_FLAGS" typeName="WORD">
<comment>Diagnostic information about the source time stamped events service</comment>
</variables>
<variables name="TIME_VALID" typeName="BOOL">
<comment>Time valid and synchronized</comment>
</variables>
<variables name="CLOCK_FAILURE" typeName="BOOL">
<comment>Clock failure</comment>
</variables>
<variables name="CLOCK_NOT_SYNC" typeName="BOOL">
<comment>Clock not synchronized</comment>
</variables>
<variables name="BUFF_FULL" typeName="BOOL">
<comment>Buffer full</comment>
</variables>
<variables name="TS_BUF_FILLED_PCTAGE" typeName="BYTE">
<comment>Percentage of the buffer filled [0..100]</comment>
</variables>
<variables name="TS_EVENTS_STATE" typeName="BYTE">
<comment>Main state of the TS events service</comment>
</variables>
<variables name="ETH_STATUS" typeName="BYTE">
<comment>Ethernet status</comment>
</variables>
<variables name="PORT1_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 1</comment>
</variables>
<variables name="PORT2_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 2</comment>
</variables>
<variables name="PORT3_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 3</comment>
</variables>
<variables name="ETH_BKP_PORT_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet backplane port</comment>
</variables>
<variables name="RPI_CHANGE" typeName="BOOL">
<comment>Ethernet/IP RPI change in progress</comment>
</variables>
<variables name="REDUNDANCY_OWNER" typeName="BOOL">
<comment>Redundant owner is present</comment>
</variables>
<variables name="GLOBAL_STATUS" typeName="BOOL">
<comment>0: one or more services not operating normally / 1: all operational</comment>
</variables>
<variables name="SERVICE_STATUS" typeName="BYTE">
<comment>One bit for each user-observable feature</comment>
</variables>
<variables name="RSTP_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="SNTP_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="PORT502_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="SNMP_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="ETH_BKP_FAILURE" typeName="BOOL">
<comment>Ethernet backplane status (0: failure, 1: OK)</comment>
</variables>
<variables name="ETH_BKP_ERROR" typeName="BOOL">
<comment>Ethernet backplane error (0: error, 1: OK)</comment>
</variables>
<variables name="ETH_PORT_STATUS" typeName="WORD">
<comment>Ethernet port status</comment>
</variables>
<variables name="NTP_UPDATE" typeName="UINT">
<comment>Time elapsed since last update from NTP server (unit: 100 ms)</comment>
</variables>
<variables name="MAX_PACKET_INTERVAL" typeName="UINT">
<comment>Max packet interval for output packets (unit: 1 ms)</comment>
</variables>
<variables name="IN_BYTES" typeName="UINT">
<comment>Number of bytes received on interface</comment>
</variables>
<variables name="IN_ERRORS" typeName="UINT">
<comment>Numberof inbound packets that contain errors</comment>
</variables>
<variables name="OUT_BYTES" typeName="UINT">
<comment>Number of bytes sent on interface</comment>
</variables>
<variables name="OUT_ERRORS" typeName="UINT">
<comment>Number of outbound packets that contain errors</comment>
</variables>
<variables name="SOE_UNCERTAIN" typeName="BOOL">
<comment>SOE uncertain (in timestamping system only)</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="T_BMENOC0301_4">
<structure>
<variables name="ETH_STATUS" typeName="WORD">
<comment>Ethernet status</comment>
</variables>
<variables name="PORT1_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 1</comment>
</variables>
<variables name="PORT2_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 2</comment>
</variables>
<variables name="PORT3_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet port 3</comment>
</variables>
<variables name="ETH_BKP_PORT_LINK" typeName="BOOL">
<comment>Link up/down for Ethernet backplane port</comment>
</variables>
<variables name="SCANNER_OK" typeName="BOOL">
<comment>Scanner OK and scanning at least 1 equipment (if at least 1 equipment configured)</comment>
</variables>
<variables name="GLOBAL_STATUS" typeName="BOOL">
<comment>0: one or more services not operating normally / 1: all operational</comment>
</variables>
<variables name="NETWORK_HEALTH" typeName="BOOL">
<comment>1: No traffic overload detected / 0: Traffic overload detected</comment>
</variables>
<variables name="SERVICE_STATUS" typeName="WORD">
<comment>One bit for each user-observable feature</comment>
</variables>
<variables name="RSTP_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="PORT502_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="SNMP_SERVICE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="MAIN_IP_ADDRESS_STATUS" typeName="BOOL">
<comment>Main IP address status (0 in caes of duplicate IP or no IP assigned)</comment>
</variables>
<variables name="EIP_SCANNER" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="MODBUS_SCANNER" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="SNTP_CLIENT" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="WEB_SERVER" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="FIRMWARE_UPGRADE" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="FTP" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="FDR_SERVER" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="EIP_ADAPTER" typeName="BOOL">
<comment>0: service not operating normally / 1: service operating normally or disabled</comment>
</variables>
<variables name="SERVICE_STATUS2" typeName="WORD">
<comment>One bit for each user-observable feature</comment>
</variables>
<variables name="LLDP_SERVICE" typeName="BOOL">
<comment>LLDP service status</comment>
</variables>
<variables name="EVENT_LOG_STATUS" typeName="BOOL">
<comment>0: event log service not operating normally / 1: event log service operating normally</comment>
</variables>
<variables name="LOG_SERVER_NOT_REACHABLE" typeName="BOOL">
<comment>1: No acknowledgement received from the syslog server / 0: otherwise</comment>
</variables>
<variables name="ETH_PORT_1_2_STATUS" typeName="BYTE">
<comment>Ethernet port 1 and 2 status</comment>
</variables>
<variables name="ETH_PORT3_BKP_STATUS" typeName="BYTE">
<comment>Ethernet port 3 and backplane port status</comment>
</variables>
<variables name="FIRMWARE_VERSION" typeName="WORD">
<comment>MSB=Major revision, LSB=Minor revision</comment>
</variables>
<variables name="FDR_USAGE" typeName="BYTE">
<comment>% of FDR server usage</comment>
</variables>
<variables name="IN_PACKETS" typeName="UINT">
<comment>Number of packets received on interface</comment>
</variables>
<variables name="IN_ERRORS" typeName="UINT">
<comment>Number of inbound packets that contain errors</comment>
</variables>
<variables name="OUT_PACKETS" typeName="UINT">
<comment>Number of packets sent on interface</comment>
</variables>
<variables name="OUT_ERRORS" typeName="UINT">
<comment>Number of outbound packets that contain errors</comment>
</variables>
<variables name="CONF_SIG" typeName="UDINT">
<comment>Signature of all files on local module FDR server</comment>
</variables>
<variables name="LS_HEALTH" typeName="ARRAY [1..16] of BOOL">
<comment>Local Slave health bits (local slave 1 to 16)</comment>
</variables>
<variables name="DIO_HEALTH" typeName="ARRAY [0..127] of BOOL">
<comment>DIO health bits (1 bit per DIO up to 128 DIOs. 112-127 are reserved for future extension)</comment>
</variables>
<variables name="DIO_CTRL" typeName="ARRAY[0..127] OF BOOL">
<comment>DIO control bits (1 bit per DIO up to 128 DIOs. 112-127 are reserved for future extension)</comment>
</variables>
</structure>
</DDTSource>
<DDTSource DDTName="T_M_ECPU_HSBY">
<structure>
<variables name="REMOTE_STS_VALID" typeName="BOOL">
<comment>Status of the remote PLC</comment>
</variables>
<variables name="APP_MISMATCH" typeName="BOOL">
<comment>Applications of the primary and the standby are different from the origin</comment>
</variables>
<variables name="LOGIC_MISMATCH_ALLOWED" typeName="BOOL">
<comment>Command that allows logic mismatch between the primary and the standby</comment>
</variables>
<variables name="LOGIC_MISMATCH" typeName="BOOL">
<comment>Applications of the primary and the standby are running with different step of online modification</comment>
</variables>
<variables name="SFC_MISMATCH" typeName="BOOL">
<comment>Applications of the primary and the standby are running with difference at least one SFC section</comment>
</variables>
<variables name="OFFLINE_BUILD_MISMATCH" typeName="BOOL">
<comment>Applications of the primary and the standby are running with different steps of offline modification</comment>
</variables>
<variables name="APP_BUILDCHANGE_DIFF" typeName="UINT">
<comment>Number of differences between the application of the primary and application of the secondary</comment>
</variables>
<variables name="MAX_APP_BUILDCHANGE_DIFF" typeName="UINT">
<comment>Number maximum of differences allowed between application of the primary and the application of the secondary</comment>
</variables>
<variables name="FW_MISMATCH_ALLOWED" typeName="BOOL">
<comment>Command that allows firmware mismatch between the primary and the standby</comment>
</variables>
<variables name="FW_MISMATCH" typeName="BOOL">
<comment>Firmware of the primary is different from the firmware of the standby</comment>
</variables>
<variables name="DATA_LAYOUT_MISMATCH" typeName="BOOL">
<comment>Data layout of the primary is different from the data layout of the standby</comment>
</variables>
<variables name="DATA_DISCARDED" typeName="UINT">
<comment>Amount of data in KByte sent by the primary and discarded by the standby</comment>
</variables>
<variables name="DATA_NOT_UPDATED" typeName="UINT">
<comment>Amount of data in KByte not updated by the standby (rounded to the upper)</comment>
</variables>
<variables name="BACKUP_APP_MISMATCH" typeName="BOOL">
<comment>Backup application of the primary and backup application fo the standby</comment>
</variables>
<variables name="PLCA_ONLINE" typeName="BOOL">
<comment>Command that allows PLC A to go promary or standby (modification allowed only on primary)</comment>
</variables>
<variables name="PLCB_ONLINE" typeName="BOOL">
<comment>Command that allows PLC B to go primary or standby (modification allowed only on primary)</comment>
</variables>
<variables name="CMD_SWAP" typeName="BOOL">
<comment>Swap primary and standby command (modification allowed only on primary)</comment>
</variables>
<variables name="CMD_APP_TRANSFER" typeName="BOOL">
<comment>Transfer application from the primary to the standby command (modification allowed only on primary)</comment>
</variables>
<variables name="CMD_RUN_AFTER_TRANSFER" typeName="BOOL">
<comment>Command that automatically start the application in Run after transfer (modification allowed only on primary)</comment>
</variables>
<variables name="CMD_RUN_REMOTE" typeName="BOOL">
<comment>Run remote controller command (modification allowed only on primary)</comment>
</variables>
<variables name="CMD_STOP_REMOTE" typeName="BOOL">
<comment>Stop remote controller command (modification allowed on primary, on stop or on wait)</comment>
</variables>
<variables name="CMD_COMPARE_INITIAL_VALUE" typeName="BOOL">
<comment>Command that start a comparison of the variable initial values (modification allowed only on primary)</comment>
</variables>
<variables name="INITIAL_VALUE_MISMATCH" typeName="BOOL">
<comment>Initial values comparision status</comment>
</variables>
<variables name="MAST_SYNCHRONIZED" typeName="BOOL">
<comment>Exchanged data from the last MAST cycle standby receipt status</comment>
</variables>
<variables name="FAST_SYNCHRONIZED" typeName="BOOL">
<comment>Exchanged data from the last FAST cycle standby receipt status</comment>
</variables>
<variables name="SAFE_SYNCHRONIZED" typeName="BOOL">
<comment>Exchanged data from the last SAFE cycle standby receipt status</comment>
</variables>
<variables name="LOCAL_HSBY_STS" typeName="T_M_ECPU_HSBY_STS"></variables>
<variables name="REMOTE_HSBY_STS" typeName="T_M_ECPU_HSBY_STS"></variables>
</structure>
</DDTSource>
<DDTSource DDTName="T_M_ECPU_HSBY_STS">
<structure>
<variables name="HSBY_LINK_ERROR" typeName="BOOL">
<comment>Hot standby link error (no heartbeat connection on hot standby link)</comment>
</variables>
<variables name="HSBY_SUPPLEMENTARY_LINK_ERROR" typeName="BOOL">
<comment>Hot standby supplementary link error (no heartbeat connection on supplementary link)</comment>
</variables>
<variables name="WAIT" typeName="BOOL">
<comment>The PLC is in Run but is waiting to be part of the hot standby system</comment>
</variables>
<variables name="RUN_PRIMARY" typeName="BOOL">
<comment>The PLC is in Run Primary state</comment>
</variables>
<variables name="RUN_STANDBY" typeName="BOOL">
<comment>The PLC is in Run Standby state</comment>
</variables>
<variables name="STOP" typeName="BOOL">
<comment>The PLC is in Stop</comment>
</variables>
<variables name="PLC_A" typeName="BOOL">
<comment>The PLC switch is on A position</comment>
</variables>
<variables name="PLC_B" typeName="BOOL">
<comment>The PLC switch is on B position</comment>
</variables>
<variables name="EIO_ERROR" typeName="BOOL">
<comment>The PLC does not see any of the configured EIO drops</comment>
</variables>
<variables name="SD_CARD_PRESENT" typeName="BOOL">
<comment>An SD Card is inserted</comment>
</variables>
<variables name="LOCAL_RACK_STS" typeName="BOOL">
<comment>True: the local rack is OK / False: the local rack is not ok (module missing)</comment>
</variables>
<variables name="REGISTER" typeName="ARRAY [0..63] of WORD">
<comment>Free space to exchange information between primary and standby PLCs.</comment>
</variables>
</structure>
</DDTSource>
<FBSource nameOfFBType="ACI030"></FBSource>
<FBSource nameOfFBType="ACI040"></FBSource>
<FBSource nameOfFBType="ACO020"></FBSource>

View File

@@ -4,11 +4,16 @@
Copyright 2026 Prime Controls. All rights reserved.
-->
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:output encoding="utf8" method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:template match="/VariablesExchangeFile">
<xsl:copy>
<xsl:apply-templates select="DDTSource"/>
<xsl:apply-templates select="FBSource"/>
</xsl:copy>
</xsl:template>
@@ -19,8 +24,8 @@
<xsl:template match="structure/variables">
<xsl:copy>
<name><xsl:value-of select="@name"/></name>
<parent><xsl:value-of select="../../@DDTName"/></parent>
<type><xsl:value-of select="@typeName"/></type>
<parent><xsl:value-of select="translate(../../@DDTName, $lowercase, $uppercase)"/></parent>
<type><xsl:value-of select="translate(@typeName, $lowercase, $uppercase)"/></type>
<address/>
<bitrank>
<xsl:for-each select="attribute">
@@ -34,4 +39,12 @@
</xsl:copy>
</xsl:template>
<xsl:template match="FBSource">
<xsl:copy>
<name>NULL</name>
<parent><xsl:value-of select="translate(@nameOfFBType, $lowercase, $uppercase)"/></parent>
<type>DFB</type>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

View File

@@ -1,28 +1,34 @@
"""
AuDaGe XSY module
name: test_xsy.py
name: xsy.py
by: Josh Lucas
date: 2026-09-14
desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
"""
import logging
import os
import warnings
from collections.abc import Iterable
from dataclasses import dataclass
from enum import IntEnum, StrEnum, auto
from importlib import resources
import pandas as pd
from pandas import DataFrame
from AuDaGe import iec
from AuDaGe.audage_frame import AuDaGeFrame
from AuDaGe.debug_tools import MeasureTime, TimeUnit
from AuDaGe.format import Format
from AuDaGe.iec import *
ASSETS_PATH = "AuDaGe.formats.controlexpert.assets"
MAX_RECURSION = 5
ASSETS_PATH = "AuDaGe.formats.controlexpert.assets"
module_logger = logging.getLogger(__name__)
class XSYBlock(AuDaGeFrame):
@@ -31,15 +37,17 @@ class XSYBlock(AuDaGeFrame):
from_file class method.
"""
_stylesheet_path = None
_xsy_path = None
# _stylesheet_path: str # = None
# _xsy_path: str # = None
_name: str
@classmethod
def from_file(
cls,
xsy_path: os.PathLike,
stylesheet_path: os.PathLike,
) -> XSYBlock:
name_ext: str = "",
) -> XSYBlock: # noqa: F821
"""Load an XSY file into a dataframe using the specified stylesheet."""
cls.stylesheet_path = stylesheet_path
cls.xsy_path = xsy_path
@@ -47,34 +55,41 @@ class XSYBlock(AuDaGeFrame):
resources.as_file(cls.xsy_path) as xsy_file,
resources.as_file(cls.stylesheet_path) as stylesheet_file,
):
return cls(pd.read_xml(xsy_file, stylesheet=stylesheet_file))
# frame = AuDaGeFrame(pd.read_xml(xsy_file, stylesheet=stylesheet_file))
return cls(
pd.read_xml(xsy_file, stylesheet=stylesheet_file),
".".join(
(os.path.splitext(os.path.basename(xsy_path))[0], name_ext)
),
)
class DataBlock(XSYBlock):
"""Read and parse the dataBlock node of an XSY file."""
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath("flatten_datablock.xslt")
udt_lengths: dict[str, int]
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath(
"flatten_datablock.xslt"
)
@classmethod
def from_file(
cls, xsy_path: os.PathLike, stylesheet_path: os.PathLike | None = None
) -> DataBlock:
) -> DataBlock: # noqa: F821
"""Read the dataBlock of an XSY file into a DataFrame."""
if stylesheet_path is None:
stylesheet_path = cls.DEFAULT_SS_PATH
return cls(XSYBlock.from_file(xsy_path, stylesheet_path))
def calculate_all_offsets(self, mwvoea: bool = True) -> DataFrame:
"""Calculate the memory offset for all UDT members in the datablock."""
return cls(XSYBlock.from_file(xsy_path, stylesheet_path, "DataBlock"))
class Definitions(XSYBlock):
"""Read and parse the DDTSource nodes of an XSY file."""
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath("flatten_ddtsource.xslt")
DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath("builtins.xsy")
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath(
"flatten_ddtsource.xslt"
)
DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath(
"builtins.xsy"
)
@classmethod
def from_file(
@@ -83,7 +98,7 @@ class Definitions(XSYBlock):
stylesheet_path: os.path | None = None,
definition_paths: Iterable[os.path] | None = None,
include_builtins: bool = True,
):
) -> Definitions: # noqa: F821
"""Read the DDTSource nodes of an XSY file into a DataFrame."""
cls.stylesheet_path = (
cls.DEFAULT_SS_PATH if stylesheet_path is None else stylesheet_path
@@ -95,80 +110,370 @@ class Definitions(XSYBlock):
cls.definition_paths.append(cls.DEFAULT_BUILTINS_PATH)
# Get the definitions from the main XSY file and put it in the list of
# definitions to be concatenated.
cls.definitions = [XSYBlock.from_file(xsy_path, cls.stylesheet_path)]
cls.definitions_list = [
XSYBlock.from_file(xsy_path, cls.stylesheet_path)
]
# Read the definitions from the other definition files (including the
# builtins file) and add then to the list.
cls.definitions.extend(
XSYBlock.from_file(path, cls.stylesheet_path)
cls.definitions_list.extend(
XSYBlock.from_file(path, cls.stylesheet_path, "Definitions")
for path in cls.definition_paths
)
# Concatenate the dataframes in the list.
return cls(pd.concat(cls.definitions))
_name = ".".join(
(os.path.splitext(os.path.basename(xsy_path))[0], "Definitions")
)
with MeasureTime(
TimeUnit.MS,
True,
"{_name} offset calculation completed in {time} {unit}.".format(
_name=_name, time="{time:.3f}", unit="{unit}"
),
):
offsets = cls._calculate_offsets(
pd.concat(cls.definitions_list),
_name=_name,
)
return cls(offsets)
def _get_length(self, row, udt_lengths: dict[str, int]) -> int:
# Find packed-bit words by checking whether...
if (
# ...the first member is a 16-bit binary or integer type...
row["type"] in ("WORD", "INT", "UINT")
# ...and that the second member is a boolean...
and row["next_type"] == "BOOL"
# ...and that the second member has a bitrank.
and row["next_bitrank"].notna()
):
# We don't increase the offset for packed-bit words because the
# following booleans overlap with them.
return 0
else:
return iec.get_data_length(
row["type"],
udt_lengths=udt_lengths,
unknown_length=pd.NA,
)
@classmethod
def _calculate_offsets(
cls,
definitions: XSYBlock | None = None,
udt_lengths: dict[str, int] | None = None,
max_recursion: int = MAX_RECURSION,
_current_recursion: int = 0,
_name: str = "",
) -> Definitions: # noqa: F821
"""Recursively calculate the memory offset for all UDT members in the
definitions.
"""
with MeasureTime(
TimeUnit.MS,
True,
"{_name} offset calculation recursion {recursion} completed in "
"{time} {unit}.".format(
_name=_name,
recursion=_current_recursion,
time="{time:.3f}",
unit="{unit}",
),
):
# Set parameters to default values.
definitions = cls if definitions is None else definitions
udt_lengths = {} if udt_lengths is None else udt_lengths
# We don't care about DFBs so set their length at zero.
udt_lengths["DFB"] = 0
definitions_joined = definitions.shift((0, -1), axis=0)
definitions_joined.to_csv("definitions_joined.csv")
# Create a column with the length of each member.
definitions["length"] = definitions_joined.apply(
lambda row: (
0
if (
row["type_0"] in ("WORD", "INT", "UINT")
and row["type_-1"] == "BOOL"
and pd.notna(row["bitrank_-1"])
)
else iec.get_data_length(
row["type_0"],
udt_lengths=udt_lengths,
unknown_length=pd.NA,
)
),
axis=1,
).shift(1, axis=0, fill_value=0)
definitions.to_csv("definitions.csv")
# Calculate how many bits need to be skipped before each member so
# that addressing rules are followed (e.g. word-length types on
# integer addresses, multi-word types on even addresses).
# definitions["padding"] = definitions.apply()
# Set a flag high for checking whether all offsets have been found.
# If this isn't set False by anything later then we're done.
all_offsets_found = True
# Calculate the cumulative sum of the data lengths for each UDT
# definition. This is the offset of the subsequent member.
definitions_calculated_list = []
for name, group in definitions.groupby("parent"):
group["offset"] = group["length"].cumsum(skipna=False)
# Check if all offsets for this UDT has been found. If so, we
# know its length so we save it to the `udt_lengths` dictionary
# for future reference.
if group["offset"].notna().all():
udt_lengths[name] = group["offset"].tail(1).values[0]
else:
# This UDT's offsets aren't finished so set the flag False.
all_offsets_found = False
definitions_calculated_list.append(group)
# Re-combine all the UDT definitions.
definitions_calculated = pd.concat(definitions_calculated_list)
if all_offsets_found:
# We're done! Log the recursion depth we reached and return our
# results.
print(
f"{_name} offset calculation recursion depth reached: "
f"{_current_recursion}."
)
definitions_finished = definitions_calculated
elif _current_recursion >= max_recursion:
# We've reached the maximum recursion depth so return now
# without finishing.
warnings.warn(
"Maximum recursion depth for offset calculation reached."
"Returning now."
)
definitions_finished = definitions_calculated
else:
# Recurse to find the offsets for nested UDTs.
definitions_finished = cls._calculate_offsets(
definitions_calculated,
udt_lengths,
max_recursion,
_current_recursion + 1,
_name,
)
return definitions_finished
@dataclass
class XSY:
"""Object representing a ControlExpert variable exchange (XSY) file."""
# Dataclass attributes:
datablock: DataBlock
definitions: Definitions
mwvoea: bool
even_addresses: bool
name: str
def compile_datablock(self, datablock: DataFrame | None = None) -> DataFrame:
"""Compile a cross-joined and queried DataBlock by combining the columns
from the datablock with the columns from the definitions."""
datablock_compiled = DataFrame()
datablock_compiled["name"] = datablock.apply(
lambda row: f"{row.name_def}.{row.name}", axis=1
)
datablock_compiled["parent"] = ""
datablock_compiled["type"] = datablock.type_def
datablock_compiled["address"] = datablock.address
datablock_compiled["bitrank"] = datablock.bitrank_def
datablock_compiled["save"] = datablock.save
datablock_compiled["comment"] = datablock.apply(
lambda row: f"{row.comment_def}: {row.comment}", axis=1
)
return datablock_compiled
class DebugStep(IntEnum):
MAKE_ARRAY_DEFS = auto() # 1
GROUP = auto() # 2
JOIN = auto() # 3
COMPILE = auto() # 4
NONE = auto() # 5
class IgnoredDataTypes(StrEnum):
DFB = auto()
def expand_datablock(
self,
datablock: DataBlock | None = None,
definitions: Definitions | None = None,
ignored_data_types: StrEnum = IgnoredDataTypes,
debug_step: DebugStep | int = DebugStep.NONE,
max_recursion: int = MAX_RECURSION,
current_recursion: int = 0,
) -> DataFrame:
) -> DataBlock:
"""Recursively expand the UDTs in `datablock` based on the UDT
definitions in `definitions` and return the resulting DataFrame.
Parameters
----------
datablock : DataBlock | None
DataBlock to expand. By default use self.datablock.
definitions : Definitions | None
Definitions to use for expansion. By default use self.definitions.
ignored_data_types : StrEnum
Data types to ignore. Defaults to self.IgnoredDataTypes.
debug_step : DebugStep | int
If set, return early at the specified step with an intermediate
version of the datablock. Default DebugStep.NONE.
max_recursion : int
The maximum number of times to recursively expand UDTs. Default 5.
"""
datablock = self.datablock if datablock is None else datablock
definitions = self.definitions if definitions is None else definitions
datablock_joined = datablock.join(
definitions, how="cross", rsuffix="_def"
)
datablock_queried = datablock_joined[
datablock_joined.type == datablock_joined.parent_def
]
datablock_compiled = self.compile_datablock(datablock_queried)
if all(
get_data_length(data_type, DataUnit.WORD) != -1
for data_type in datablock_compiled.type
datablock_expanded: DataBlock
with MeasureTime(
TimeUnit.MS,
True,
"{name} datablock expansion completed in {time} {unit}.".format(
name=self.name, time="{time:.3f}", unit="{unit}"
),
):
datablock_finished = datablock_compiled
elif current_recursion >= max_recursion:
warnings.warn("Maximum recursion depth reached. Returning now.")
datablock_finished = datablock_compiled
else:
datablock_finished = self.expand_datablock(
datablock_compiled, definitions, max_recursion, current_recursion + 1
datablock = self.datablock if datablock is None else datablock
definitions = (
self.definitions if definitions is None else definitions
)
return datablock_finished
# Make UDT array definitions.
with MeasureTime(
TimeUnit.MS,
True,
"{name} array definition completed in {time} {unit}.".format(
name=self.name,
time="{time:.3f}",
unit="{unit}",
),
):
array_definitions = self._make_array_definitions(
datablock, definitions
)
definitions_extended = pd.concat(
(definitions, array_definitions)
)
if debug_step == self.DebugStep.MAKE_ARRAY_DEFS:
return definitions_extended
datablock_expanded = self._expand_udts(
datablock,
definitions_extended,
ignored_data_types,
debug_step,
max_recursion,
)
return datablock_expanded
def _compile_datablock(self, datablock: DataBlock) -> DataBlock:
"""Compile a cross-joined and filtered DataBlock by combining the
columns from the datablock with the columns from the definitions."""
datablock_compiled = pd.DataFrame()
datablock_compiled["name"] = datablock.apply(
lambda row: f"{row['name']}.{row['name_def']}", axis=1
)
datablock_compiled["parent"] = ""
datablock_compiled["type"] = datablock["type_def"]
datablock_compiled["address"] = datablock["address"]
datablock_compiled["bitrank"] = datablock["bitrank_def"]
datablock_compiled["save"] = datablock["save"]
datablock_compiled["comment"] = datablock.apply(
lambda row: f"{row['comment']}: {row['comment_def']}", axis=1
)
return DataBlock(datablock_compiled)
def _expand_udts(
self,
datablock: DataBlock,
definitions: Definitions,
ignored_data_types: StrEnum,
debug_step: DebugStep,
max_recursion: int,
_current_recursion: int = 0,
) -> DataBlock:
"""Helper function for expand_datablock that does the recursive
expansion of UDTs.
Parameters
----------
udts: DataBlock | None
UDTs to expand. By default use self.datablock.
definitions: Definitions | None
Definitions to use for expansion. By default use self.definitions.
debug_step: int
If in DebugStep, return early with an intermediate version of the
datablock. Default -1.
max_recursion: int
Maximum recursion depth. Default 5.
_current_recursion: int
Current recursion depth. Not to be used except within this method.
"""
datablock_grouped = datablock.groupby("type")
edts = pd.concat(
group
for (name, group) in datablock_grouped
if name in iec.ElementaryTypes
)
udts = pd.concat(
group
for (name, group) in datablock_grouped
if name not in iec.ElementaryTypes
)
if debug_step == self.DebugStep.GROUP:
return pd.concat((edts, udts))
definitions_grouped = definitions.groupby("parent")
udts_grouped = udts.groupby("type")
group_list = []
# Cross-join each type group in udts_grouped with its corresponding
# group from definitions_grouped.
with MeasureTime(
TimeUnit.MS,
True,
"{name} datablock expansion recursion {recursion} completed in "
"{time} {unit}.".format(
name=self.name,
recursion=_current_recursion,
time="{time:.3f}",
unit="{unit}",
),
):
for name, group in udts_grouped:
if name.lower() not in ignored_data_types:
group_list.append(
group.join(
pd.DataFrame(
definitions_grouped.get_group(name.upper())
),
how="cross",
rsuffix="_def",
)
)
udts_joined = pd.concat(group_list)
if debug_step == self.DebugStep.JOIN:
return udts_joined
udts_compiled = self._compile_datablock(udts_joined)
if debug_step == self.DebugStep.COMPILE:
return udts_compiled
if all(
udts_compiled.apply(
lambda row: row["type"] in iec.ElementaryTypes, axis=1
)
):
print(
f"{self.name} datablock expansion recursion depth reached: "
f"{_current_recursion}."
)
udts_finished = udts_compiled
elif _current_recursion >= max_recursion:
warnings.warn(
"Maximum recursion depth for DataBlock expansion "
"reached. Returning now."
)
udts_finished = udts_compiled
else:
udts_finished = self._expand_udts(
udts_compiled,
definitions,
ignored_data_types,
debug_step,
max_recursion,
_current_recursion + 1,
)
return pd.concat((edts, udts_finished))
def _make_array_definitions(
self, datablock: DataBlock, definitions: Definitions
) -> Definitions:
"""Make definitions for the expansion of arrays of UDTs."""
everything_grouped = pd.concat((datablock, definitions)).groupby("type")
array_definitions_list = []
for name, group in everything_grouped:
try:
array = iec.Array(name)
if not array.is_elementary():
array_definitions_list.append(array.make_definition(""))
except iec.InvalidSpecError:
continue
return pd.concat(array_definitions_list)
class XSYReader(Format):
"""Class for manipulating Schneider Electric EcoStruxure Control Expert
variable exchange (XSY) files. Can be re-used to read multiple XSY files
using common stylesheets and UDT definitions.
"""Class for reading and writing Schneider Electric EcoStruxure Control
Expert variable exchange (XSY) files. Can be re-used to read multiple XSY
files using common stylesheets and UDT definitions.
"""
def __init__(
@@ -176,7 +481,7 @@ class XSYReader(Format):
definition_paths: Iterable[os.path] | None = None,
datablock_ss_path: os.path | None = None,
definitions_ss_path: os.path | None = None,
mwvoea: bool = True,
even_addresses: bool = True,
):
"""Initialize XSYReader object.
@@ -184,28 +489,37 @@ class XSYReader(Format):
----------
definition_paths : iterable of pathlike (optional)
List of additional XSY files to read UDT (DDT) definitions from.
db_ss_path: pathlike (optional)
datablock_ss_path: pathlike (optional)
Path to XSLT stylesheet to flatten the XSY file and extract the
dataBlock node before importing into pandas.
ddt_ss_path: pathlike (optional)
definitions_ss_path: pathlike (optional)
Path to XSLT stylesheet to flatten the XSY file and extract the
DDTSource nodes before importing into pandas.
even_addresses: bool
Boolean indicating whether multi-word variables must be located on
even-numbered addresses. Default True.
"""
self.definition_paths = [] if definition_paths is None else definition_paths
self.definition_paths = (
[] if definition_paths is None else definition_paths
)
self.datablock_ss_path = datablock_ss_path
self.definitions_ss_path = definitions_ss_path
self.mwvoea = mwvoea
"""Dict for storing the length of all the UDTs included in the
definitions.
"""
self.even_addresses = even_addresses
def read(self, xsy_path: os.path, expand: bool = True) -> XSY:
"""Read the dataBlock and definitions of an XSY file into an XSY object."""
"""Read the dataBlock and definitions of an XSY file into an XSY
object.
"""
self.datablock = DataBlock.from_file(xsy_path, self.datablock_ss_path)
self.definitions = Definitions.from_file(
xsy_path, self.definitions_ss_path, self.definition_paths
)
return XSY(self.datablock, self.definitions, self.mwvoea)
return XSY(
datablock=self.datablock,
definitions=self.definitions,
even_addresses=self.even_addresses,
name=os.path.basename(xsy_path),
)
def write(self):
"""Write XSY object to file."""

View File

@@ -9,6 +9,8 @@ desc: Unit tests for the AuDaGe XSY module.
"""
import os
# import time
import unittest
from importlib import resources
@@ -17,9 +19,6 @@ import output
import assets
from AuDaGe.formats.controlexpert.xsy import *
ASSETS_PATH = "assets"
OUTPUT_PATH = "output"
class TestXSY(unittest.TestCase):
"""This test case tests the XSYBlock, DataBlock, and Definitions classes in
@@ -69,17 +68,22 @@ class TestXSYReader(unittest.TestCase):
file_name = os.path.splitext(os.path.split(path)[-1])[0]
xsy = self.xsy_reader.read(path, expand=False)
xsy.datablock.to_csv(
self.out_path.joinpath(f"{file_name}_datablock.csv"), index=False
self.out_path.joinpath(f"{file_name}_datablock.csv"),
index=False,
)
xsy.definitions.to_csv(
self.out_path.joinpath(f"{file_name}_definitions.csv"), index=False
)
xsy.datablock.update(xsy.expand_datablock())
xsy.datablock.to_csv(
self.out_path.joinpath(f"{file_name}_expanded.csv"), index=False
self.out_path.joinpath(f"{file_name}_definitions.csv"),
index=False,
)
debug = False
for debug_step in XSY.DebugStep if debug else [XSY.DebugStep.NONE]:
xsy.expand_datablock(debug_step=debug_step).to_csv(
self.out_path.joinpath(
f"{file_name}_debug_{debug_step.name}.csv"
),
index=False,
)
if __name__ == "__main__":
__package__ = "test_xsy"
unittest.main(failfast=True)