From 9ae239bc18654cfb2671ff1baeccb6c39ab692aa Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Fri, 11 Oct 2024 08:47:23 +1100 Subject: [PATCH] tests actually working --- src/pyfroniusreg/froniusreg.py | 8 ++++---- tests/test_datatype.py | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/pyfroniusreg/froniusreg.py b/src/pyfroniusreg/froniusreg.py index 9c51e0a..e91c4d0 100644 --- a/src/pyfroniusreg/froniusreg.py +++ b/src/pyfroniusreg/froniusreg.py @@ -41,7 +41,7 @@ class DataType(Enum): elif (self == DataType.Int32) or (self == DataType.UInt32) or (self == DataType.Float32): return int(2) - def decode(self, value): + def decode_from_register(self, value): decoder = BinaryPayloadDecoder.fromRegisters(value.registers, byteorder=Endian.BIG, wordorder=Endian.BIG) if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32): @@ -65,7 +65,7 @@ class DataType(Enum): else: return str(decoder.decode_bits()) - def encode(self, value): + def encode_to_buffer(self, value): encoder = BinaryPayloadBuilder(byteorder=Endian.BIG, wordorder=Endian.BIG) if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32): @@ -129,11 +129,11 @@ class FroniusReg: raise registerReadError("Unable to read from Fronius Register: %d, %s" % (self.id, self.description)) if(modbusValue is None): raise registerReadError("It's NONE!") - return self.datatype.decode(modbusValue) + return self.datatype.decode_from_register(modbusValue) def __setRegisterValue(self, modbusClient, value): modbusValue = modbusClient.write_registers(self.address-1, - self.datatype.encode(value), + self.datatype.encode_to_buffer(value), count=self.datatype.getRegisterLength(), slave=self.unit, skip_encode=True) diff --git a/tests/test_datatype.py b/tests/test_datatype.py index 93bad69..7f0e4fd 100644 --- a/tests/test_datatype.py +++ b/tests/test_datatype.py @@ -3,15 +3,11 @@ import unittest import pyfroniusreg.froniusreg as froniusreg -class RegisterValue(): - def __init__(self, value): - self.registers = value - class TestDataTypes(unittest.TestCase): def test_int16(self): - value = RegisterValue(froniusreg.int16.encode(1024)[0]) - assert froniusreg.int16.decode(value) == 1024 + int16_buffer = froniusreg.int16.encode_to_buffer(1024) + assert int16_buffer == [b'\x04\x00'] if __name__ == '__main__':