Skip to content

Commit

Permalink
chore(plc4py): Update the generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
hutcheb committed Nov 9, 2023
1 parent 2330c04 commit 8c47fa6
Show file tree
Hide file tree
Showing 53 changed files with 1,463 additions and 1,808 deletions.
778 changes: 270 additions & 508 deletions sandbox/plc4py/plc4py/protocols/modbus/readwrite/DataItem.py

Large diffs are not rendered by default.

76 changes: 40 additions & 36 deletions sandbox/plc4py/plc4py/protocols/modbus/readwrite/ModbusADU.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

from abc import ABC
from abc import abstractmethod
from plc4py.api.exceptions.exceptions import ParseException
from plc4py.api.exceptions.exceptions import PlcRuntimeException
from plc4py.api.exceptions.exceptions import SerializationException
from plc4py.api.messages.PlcMessage import PlcMessage
from plc4py.protocols.modbus.readwrite.DriverType import DriverType
from plc4py.spi.generation.ReadBuffer import ReadBuffer
Expand All @@ -33,10 +35,6 @@ class ModbusADU(ABC, PlcMessage):
# Arguments.
response: bool


def __post_init__(self):
super().__init__( )

# Abstract accessors for discriminator values.
@property
@abstractmethod
Expand All @@ -58,9 +56,9 @@ def serialize(self, write_buffer: WriteBuffer):


def length_in_bytes(self) -> int:
return int(math.ceil(float(self.get_length_in_bits() / 8.0)))
return int(math.ceil(float(self.length_in_bits() / 8.0)))

def get_length_in_bits(self) -> int:
def length_in_bits(self) -> int:
length_in_bits: int = 0
_value: ModbusADU = self

Expand All @@ -69,29 +67,31 @@ def get_length_in_bits(self) -> int:
return length_in_bits


def static_parse(self, read_buffer: ReadBuffer , args):
if args is None:
@staticmethod
def static_parse(read_buffer: ReadBuffer, **kwargs):

if kwargs is None:
raise PlcRuntimeException("Wrong number of arguments, expected 2, but got None")
elif args.length != 2:
raise PlcRuntimeException("Wrong number of arguments, expected 2, but got " + str(len(args)))

driverType: DriverType = 0
if isinstance(args[0], DriverType):
driverType = DriverType(args[0])
elif isinstance(args[0], str):
driverType = DriverType(str(args[0]))
elif len(kwargs) == 2:
raise PlcRuntimeException("Wrong number of arguments, expected 2, but got " + str(len(kwargs)))

driver_type: DriverType = 0
if isinstance(kwargs.get("driverType"), DriverType):
driver_type = DriverType(kwargs.get("driverType"))
elif isinstance(kwargs.get("driverType"), str):
driver_type = DriverType(str(kwargs.get("driverType")))
else:
raise PlcRuntimeException("Argument 0 expected to be of type DriverType or a string which is parseable but was " + args[0].getClass().getName())
raise PlcRuntimeException("Argument 0 expected to be of type DriverType or a string which is parseable but was " + kwargs.get("driverType").getClass().getName())

response: bool = False
if isinstance(args[1], bool):
response = bool(args[1])
elif isinstance(args[1], str):
response = bool(str(args[1]))
if isinstance(kwargs.get("response"), bool):
response = bool(kwargs.get("response"))
elif isinstance(kwargs.get("response"), str):
response = bool(str(kwargs.get("response")))
else:
raise PlcRuntimeException("Argument 1 expected to be of type bool or a string which is parseable but was " + args[1].getClass().getName())
raise PlcRuntimeException("Argument 1 expected to be of type bool or a string which is parseable but was " + kwargs.get("response").getClass().getName())

return self.static_parse_context(read_buffer, driverType, response)
return ModbusADU.static_parse_context(read_buffer, driver_type, response)


@staticmethod
Expand All @@ -100,17 +100,20 @@ def static_parse_context(read_buffer: ReadBuffer, driver_type: DriverType, respo

# Switch Field (Depending on the discriminator values, passes the instantiation to a sub-type)
builder: ModbusADUBuilder = None
if EvaluationHelper.equals( driverType, DriverType.get_modbu_s__tcp() ):
from plc4py.protocols.modbus.readwrite.ModbusTcpADU import ModbusTcpADU
if driver_type == DriverType.MODBUS_TCP :

builder = ModbusTcpADU.staticParseBuilder(read_buffer, driverType, response)
if EvaluationHelper.equals( driverType, DriverType.get_modbu_s__rtu() ):
builder = ModbusTcpADU.static_parse_builder(read_buffer, driver_type, response)
from plc4py.protocols.modbus.readwrite.ModbusRtuADU import ModbusRtuADU
if driver_type == DriverType.MODBUS_RTU :

builder = ModbusRtuADU.staticParseBuilder(read_buffer, driverType, response)
if EvaluationHelper.equals( driverType, DriverType.get_modbu_s__ascii() ):
builder = ModbusRtuADU.static_parse_builder(read_buffer, driver_type, response)
from plc4py.protocols.modbus.readwrite.ModbusAsciiADU import ModbusAsciiADU
if driver_type == DriverType.MODBUS_ASCII :

builder = ModbusAsciiADU.staticParseBuilder(read_buffer, driverType, response)
builder = ModbusAsciiADU.static_parse_builder(read_buffer, driver_type, response)
if builder is None:
raise ParseException("Unsupported case for discriminated type"+" parameters ["+"driverType="+driverType+"]")
raise ParseException("Unsupported case for discriminated type"+" parameters ["+"driverType="+str(driver_type)+"]")


read_buffer.pop_context("ModbusADU")
Expand All @@ -133,13 +136,14 @@ def hash_code(self) -> int:
return hash(self)

def __str__(self) -> str:
write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
try:
write_buffer_box_based.writeSerializable(self)
except SerializationException as e:
raise RuntimeException(e)
pass
#write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
#try:
# write_buffer_box_based.writeSerializable(self)
#except SerializationException as e:
# raise PlcRuntimeException(e)

return "\n" + str(write_buffer_box_based.get_box()) + "\n"
#return "\n" + str(write_buffer_box_based.get_box()) + "\n"

class ModbusADUBuilder:
def build(self, response: bool ) -> ModbusADU:
Expand Down
46 changes: 21 additions & 25 deletions sandbox/plc4py/plc4py/protocols/modbus/readwrite/ModbusAsciiADU.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from dataclasses import dataclass

from plc4py.api.exceptions.exceptions import PlcRuntimeException
from plc4py.api.exceptions.exceptions import SerializationException
from plc4py.api.messages.PlcMessage import PlcMessage
from plc4py.protocols.modbus import StaticHelper
from plc4py.protocols.modbus.readwrite.DriverType import DriverType
Expand All @@ -27,10 +29,11 @@
from plc4py.protocols.modbus.readwrite.ModbusPDU import ModbusPDU
from plc4py.spi.generation.ReadBuffer import ReadBuffer
from plc4py.spi.generation.WriteBuffer import WriteBuffer
from plc4py.utils.GenericTypes import ByteOrder
import math

@dataclass
class ModbusAsciiADU(PlcMessage,ModbusADU):
class ModbusAsciiADU(ModbusADU):
address: int
pdu: ModbusPDU
# Arguments.
Expand All @@ -39,10 +42,6 @@ class ModbusAsciiADU(PlcMessage,ModbusADU):
driver_type: DriverType = DriverType.MODBUS_ASCII


def __post_init__(self):
super().__init__( self.response )



def serialize_modbus_adu_child(self, write_buffer: WriteBuffer):
write_buffer.push_context("ModbusAsciiADU")
Expand All @@ -54,23 +53,23 @@ def serialize_modbus_adu_child(self, write_buffer: WriteBuffer):
write_buffer.write_serializable(self.pdu, logical_name="pdu")

# Checksum Field (checksum) (Calculated)
write_buffer.write_unsigned_byte(int(StaticHelper.ascii_lrc_check(self.address, self.pdu)), logical_name="crc")
write_buffer.write_unsigned_byte(int(StaticHelper.ascii_lrc_check(address, pdu)), logical_name="crc")

write_buffer.pop_context("ModbusAsciiADU")


def length_in_bytes(self) -> int:
return int(math.ceil(float(self.get_length_in_bits() / 8.0)))
return int(math.ceil(float(self.length_in_bits() / 8.0)))

def get_length_in_bits(self) -> int:
length_in_bits: int = super().get_length_in_bits()
def length_in_bits(self) -> int:
length_in_bits: int = super().length_in_bits()
_value: ModbusAsciiADU = self

# Simple field (address)
length_in_bits += 8

# Simple field (pdu)
length_in_bits += self.pdu.get_length_in_bits()
length_in_bits += self.pdu.length_in_bits()

# Checksum Field (checksum)
length_in_bits += 8
Expand All @@ -82,11 +81,11 @@ def get_length_in_bits(self) -> int:
def static_parse_builder(read_buffer: ReadBuffer, driver_type: DriverType, response: bool):
read_buffer.push_context("ModbusAsciiADU")

self.address= read_simple_field("address", read_unsigned_short, WithOption.WithByteOrder(get_bi_g__endian()))
address: int = read_buffer.read_unsigned_short(logical_name="address", byte_order=ByteOrder.BIG_ENDIAN)

self.pdu= read_simple_field("pdu", DataReaderComplexDefault(ModbusPDU.static_parse(read_buffer, bool(response)), read_buffer), WithOption.WithByteOrder(get_bi_g__endian()))
pdu: ModbusPDU = read_buffer.read_complex(read_function=ModbusPDU.static_parse, logical_name="pdu", byte_order=ByteOrder.BIG_ENDIAN)

crc: int = read_checksum_field("crc", read_unsigned_short, (int) (ascii_lrc_check(self.address, self.pdu)), WithOption.WithByteOrder(get_bi_g__endian()))
crc: int = read_buffer.read_unsigned_short(logical_name="crc", byte_order=ByteOrder.BIG_ENDIAN)

read_buffer.pop_context("ModbusAsciiADU")
# Create the instance
Expand All @@ -107,26 +106,23 @@ def hash_code(self) -> int:
return hash(self)

def __str__(self) -> str:
write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
try:
write_buffer_box_based.writeSerializable(self)
except SerializationException as e:
raise RuntimeException(e)
pass
#write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
#try:
# write_buffer_box_based.writeSerializable(self)
#except SerializationException as e:
# raise PlcRuntimeException(e)

return "\n" + str(write_buffer_box_based.get_box()) + "\n"
#return "\n" + str(write_buffer_box_based.get_box()) + "\n"


@dataclass
class ModbusAsciiADUBuilder(ModbusADUBuilder):
address: int
pdu: ModbusPDU
response: bool

def __post_init__(self):
pass

def build(self,response: bool ) -> ModbusAsciiADU:
modbus_ascii_adu: ModbusAsciiADU = ModbusAsciiADU(self.address, self.pdu , response )
def build(self,response: bool , ) -> ModbusAsciiADU:
modbus_ascii_adu: ModbusAsciiADU = ModbusAsciiADU(response , self.address, self.pdu )
return modbus_ascii_adu


Expand Down
38 changes: 19 additions & 19 deletions sandbox/plc4py/plc4py/protocols/modbus/readwrite/ModbusConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,32 @@

from dataclasses import dataclass

from plc4py.api.exceptions.exceptions import PlcRuntimeException
from plc4py.api.exceptions.exceptions import SerializationException
from plc4py.api.messages.PlcMessage import PlcMessage
from plc4py.spi.generation.ReadBuffer import ReadBuffer
from plc4py.spi.generation.WriteBuffer import WriteBuffer
import math

@dataclass
class ModbusConstants(PlcMessage):
MODBUSTCPDEFAULTPORT: int = int(502)


def __post_init__(self):
super().__init__( )
class ModbusConstants():
MODBUS_TCP_DEFAULT_PORT: int = int(502)



def serialize(self, write_buffer: WriteBuffer):
write_buffer.push_context("ModbusConstants")

# Const Field (modbusTcpDefaultPort)
write_buffer.write_unsigned_short(self.modbus_tcp_default_port.value, logical_name="modbusTcpDefaultPort")
write_buffer.write_unsigned_short(self.MODBUS_TCP_DEFAULT_PORT, logical_name="modbusTcpDefaultPort")

write_buffer.pop_context("ModbusConstants")


def length_in_bytes(self) -> int:
return int(math.ceil(float(self.get_length_in_bits() / 8.0)))
return int(math.ceil(float(self.length_in_bits() / 8.0)))

def get_length_in_bits(self) -> int:
def length_in_bits(self) -> int:
length_in_bits: int = 0
_value: ModbusConstants = self

Expand All @@ -56,15 +54,16 @@ def get_length_in_bits(self) -> int:
return length_in_bits


def static_parse(self, read_buffer: ReadBuffer , args):
return self.static_parse_context(read_buffer)
@staticmethod
def static_parse(read_buffer: ReadBuffer, **kwargs):
return ModbusConstants.static_parse_context(read_buffer)


@staticmethod
def static_parse_context(read_buffer: ReadBuffer):
read_buffer.push_context("ModbusConstants")

self.modbus_tcp_default_port: int = read_const_field("modbusTcpDefaultPort", read_unsigned_int, ModbusConstants.MODBUSTCPDEFAULTPORT)
MODBUS_TCP_DEFAULT_PORT: int = read_buffer.read_unsigned_int(logical_name="modbusTcpDefaultPort")

read_buffer.pop_context("ModbusConstants")
# Create the instance
Expand All @@ -86,13 +85,14 @@ def hash_code(self) -> int:
return hash(self)

def __str__(self) -> str:
write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
try:
write_buffer_box_based.writeSerializable(self)
except SerializationException as e:
raise RuntimeException(e)

return "\n" + str(write_buffer_box_based.get_box()) + "\n"
pass
#write_buffer_box_based: WriteBufferBoxBased = WriteBufferBoxBased(True, True)
#try:
# write_buffer_box_based.writeSerializable(self)
#except SerializationException as e:
# raise PlcRuntimeException(e)

#return "\n" + str(write_buffer_box_based.get_box()) + "\n"



Expand Down
Loading

0 comments on commit 8c47fa6

Please sign in to comment.