Add Kepserver CSV processing functionality.
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
"""
|
||||
AuDaGe Kepserver CSV module
|
||||
|
||||
name: csv.py
|
||||
by: Josh Lucas
|
||||
date: 2026-10-12
|
||||
|
||||
desc: AuDaGe module for handling Kepserver CSV files.
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
import pandas as pd
|
||||
from numpy import nan
|
||||
|
||||
from AuDaGe import frame, iec
|
||||
from AuDaGe.better_enum import BetterStrEnum
|
||||
|
||||
AC = frame.AuDaGeColumns
|
||||
|
||||
|
||||
class Columns(BetterStrEnum):
|
||||
TAG_NAME = "Tag Name"
|
||||
ADDRESS = "Address"
|
||||
DATA_TYPE = "Data Type"
|
||||
RESPECT_DATA_TYPE = "Respect Data Type"
|
||||
CLIENT_ACCESS = "Client Access"
|
||||
SCAN_RATE = "Scan Rate"
|
||||
SCALING = "Scaling"
|
||||
RAW_LOW = "Raw Low"
|
||||
RAW_HIGH = "Raw High"
|
||||
SCALED_LOW = "Scaled Low"
|
||||
SCALED_HIGH = "Scaled High"
|
||||
SCALED_DATA_TYPE = "Scaled Data Type"
|
||||
CLAMP_LOW = "Clamp Low"
|
||||
CLAMP_HIGH = "Clamp High"
|
||||
ENG_UNITS = "Eng Units"
|
||||
DESCRIPTION = "Description"
|
||||
|
||||
|
||||
KC = Columns
|
||||
|
||||
|
||||
class ClientAccess(BetterStrEnum):
|
||||
"""Allowable values for the `Client Access` column of a Kepserver CSV file."""
|
||||
|
||||
R = "R"
|
||||
W = "W"
|
||||
RW = "R/W"
|
||||
|
||||
|
||||
class OPCUAType(BetterStrEnum):
|
||||
"""Allowable values for the `Data Type` and `Scaled Type` columns of a
|
||||
Kepserver CSV file.
|
||||
"""
|
||||
|
||||
NONE = ""
|
||||
BOOLEAN = "Boolean"
|
||||
CHAR = "Char"
|
||||
BYTE = "Byte"
|
||||
SHORT = "Short"
|
||||
WORD = "Word"
|
||||
LONG = "Long"
|
||||
DWORD = "DWord"
|
||||
LONGLONG = "LongLong"
|
||||
QWORD = "QWord"
|
||||
FLOAT = "Float"
|
||||
DOUBLE = "Double"
|
||||
STRING = "String"
|
||||
|
||||
|
||||
class Scaling(BetterStrEnum):
|
||||
"""Allowable values for the `Scaling` column of a Kepserver CSV file."""
|
||||
|
||||
NONE = ""
|
||||
LINEAR = "Linear"
|
||||
SQUARE_ROOT = "Square Root"
|
||||
|
||||
|
||||
class TranslatorsFromAF:
|
||||
"""Class for organizing static methods for converting from an AuDaGeFrame to
|
||||
a Kepserver CSV frame.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def tag_name(row: pd.Series) -> str:
|
||||
name = row[AC.NAME]
|
||||
# Remove brackets, then replace characters other than letters +
|
||||
# underscore (\w), numbers (\d), and period (\.) with "_".
|
||||
return re.sub(r"[^\w\d\.]", "_", name.replace("[", "").replace("]", ""))
|
||||
|
||||
@staticmethod
|
||||
def address(row: pd.Series) -> str:
|
||||
address = row[AC.ADDRESS]
|
||||
try:
|
||||
return iec.Address.from_string(address).to_modbus()
|
||||
except iec.InvalidSpecError:
|
||||
return address
|
||||
except ValueError:
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def data_type(row: pd.Series) -> str:
|
||||
return iec.type_iec_to_opcua[row[AC.TYPE]]
|
||||
|
||||
|
||||
class TranslatorsToAF:
|
||||
"""Class for organizing static methods for converting from a Kepserver CSV
|
||||
frame to an AuDaGeFrame.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def name(row: pd.Series) -> str:
|
||||
return row[KC.TAG_NAME]
|
||||
|
||||
@staticmethod
|
||||
def type(row: pd.Series) -> str:
|
||||
return iec.type_opcua_to_iec[row[KC.DATA_TYPE]]
|
||||
|
||||
@staticmethod
|
||||
def address(row: pd.Series) -> str:
|
||||
address = row[KC.ADDRESS]
|
||||
try:
|
||||
return iec.Address.from_modbus(address).to_string()
|
||||
except iec.InvalidSpecError:
|
||||
return address
|
||||
|
||||
|
||||
translators_from_af = {
|
||||
KC.TAG_NAME: TranslatorsFromAF.tag_name,
|
||||
KC.ADDRESS: TranslatorsFromAF.address,
|
||||
KC.DATA_TYPE: TranslatorsFromAF.data_type,
|
||||
KC.RESPECT_DATA_TYPE: 1,
|
||||
KC.CLIENT_ACCESS: ClientAccess.RW,
|
||||
KC.SCAN_RATE: 1000,
|
||||
KC.SCALING: Scaling.NONE,
|
||||
KC.RAW_LOW: nan,
|
||||
KC.RAW_HIGH: nan,
|
||||
KC.SCALED_LOW: nan,
|
||||
KC.SCALED_HIGH: nan,
|
||||
KC.SCALED_DATA_TYPE: OPCUAType.NONE,
|
||||
KC.CLAMP_LOW: nan,
|
||||
KC.CLAMP_HIGH: nan,
|
||||
KC.ENG_UNITS: "",
|
||||
KC.DESCRIPTION: AC.COMMENT,
|
||||
}
|
||||
|
||||
translators_to_af = {
|
||||
AC.NAME: TranslatorsToAF.name,
|
||||
AC.TYPE: TranslatorsToAF.type,
|
||||
AC.ADDRESS: TranslatorsToAF.address,
|
||||
AC.COMMENT: KC.DESCRIPTION,
|
||||
}
|
||||
|
||||
|
||||
class Frame(
|
||||
frame.Frame,
|
||||
mandatory_columns=Columns.values(),
|
||||
translators_from_af=translators_from_af,
|
||||
translators_to_af=translators_to_af,
|
||||
):
|
||||
"""Dataframe for Kepserver CSV."""
|
||||
|
||||
20000
tests/assets/WA3251_PLC_001_1.csv
Normal file
20000
tests/assets/WA3251_PLC_001_1.csv
Normal file
File diff suppressed because it is too large
Load Diff
70
tests/test_kepserver.py
Normal file
70
tests/test_kepserver.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
AuDaGe Kepserver tests
|
||||
|
||||
name: test_kepserver.py
|
||||
by: Josh Lucas
|
||||
date: 2026-10-13
|
||||
|
||||
desc: Unit tests for the AuDaGe kepserver.csv module.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from importlib import resources
|
||||
|
||||
from AuDaGe.debug_tools import MeasureTime
|
||||
from AuDaGe.formats.kepserver import csv
|
||||
from AuDaGe.frame import AuDaGeFrame
|
||||
|
||||
try:
|
||||
from tests import assets, output
|
||||
except ImportError: # pragma: no cover
|
||||
import assets # noqa: I001
|
||||
import output
|
||||
|
||||
|
||||
class TestCSV(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.input_file = resources.files(assets).joinpath(
|
||||
"WA3251_PLC_001_1.csv"
|
||||
)
|
||||
self.output_folder = resources.files(output)
|
||||
with MeasureTime(
|
||||
do_print=True,
|
||||
message="Kepserver CSV import completed in {time:.3f} {unit}",
|
||||
):
|
||||
self.csv_frame = csv.Frame.from_file(self.input_file)
|
||||
with MeasureTime(
|
||||
do_print=True,
|
||||
message=(
|
||||
"Kepserver CSV frame translation to AuDaGeFrame completed in "
|
||||
"{time:.3f} {unit}"
|
||||
),
|
||||
):
|
||||
self.af = self.csv_frame.to_AF()
|
||||
|
||||
def test_read(self):
|
||||
self.assertIsInstance(self.csv_frame, csv.Frame)
|
||||
|
||||
def test_from_af(self):
|
||||
with MeasureTime(
|
||||
do_print=True,
|
||||
message=(
|
||||
"Kepserver CSV frame translation from AuDaGeFrame completed in "
|
||||
"{time:.3f} {unit}"
|
||||
),
|
||||
):
|
||||
translate_from_af = csv.Frame.from_AF(self.af)
|
||||
self.assertIsInstance(translate_from_af, csv.Frame)
|
||||
translate_from_af.to_file(
|
||||
self.output_folder.joinpath("kep_from_af.csv")
|
||||
)
|
||||
|
||||
def test_to_af(self):
|
||||
self.assertIsInstance(self.af, AuDaGeFrame)
|
||||
|
||||
def test_write(self):
|
||||
self.csv_frame.to_file(self.output_folder.joinpath("kepserver.csv"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(failfast=True)
|
||||
Reference in New Issue
Block a user