From 857012ec27e71a837da690eae3fae2fff88c473b Mon Sep 17 00:00:00 2001 From: Henrik Abelsson Date: Tue, 13 Sep 2022 20:44:48 +0200 Subject: [PATCH] Improve implementation of get_management_info function --- pylontech/pylontech.py | 34 +++++++++++++++++++++++----------- tests/test_basic.py | 38 +++++++++++++------------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pylontech/pylontech.py b/pylontech/pylontech.py index 2ef1733..0a5a7a9 100644 --- a/pylontech/pylontech.py +++ b/pylontech/pylontech.py @@ -63,12 +63,25 @@ class Pylontech: ) management_info_fmt = construct.Struct( - "CommandValue" / construct.Byte, - "ChargeVoltageLimit" / construct.Array(2, construct.Byte), - "DischargeVoltageLimit" / construct.Array(2, construct.Byte), - "ChargeCurrentLimit" / construct.Array(2, construct.Byte), - "DishargeCurrentLimit" / construct.Array(2, construct.Byte), - "Status" / construct.Byte, + "ChargeVoltageLimit" / DivideBy1000(construct.Int16sb), + "DischargeVoltageLimit" / DivideBy1000(construct.Int16sb), + "ChargeCurrentLimit" / ToAmp(construct.Int16sb), + "DischargeCurrentLimit" / ToAmp(construct.Int16sb), + "status" + / construct.BitStruct( + "ChargeEnable" / construct.Flag, + "DischargeEnable" / construct.Flag, + "ChargeImmediately2" / construct.Flag, + "ChargeImmediately1" / construct.Flag, + "FullChargeRequest" / construct.Flag, + "ShouldCharge" + / construct.Computed( + lambda this: this.ChargeImmediately2 + | this.ChargeImmediately1 + | this.FullChargeRequest + ), + "_padding" / construct.BitsInteger(3), + ), ) module_serial_number_fmt = construct.Struct( @@ -76,7 +89,6 @@ class Pylontech: "ModuleSerialNumber" / JoinBytes(construct.Array(16, construct.Byte)), ) - get_values_fmt = construct.Struct( "NumberOfModules" / construct.Byte, "Module" / construct.Array(construct.this.NumberOfModules, construct.Struct( @@ -243,14 +255,14 @@ def get_system_parameters(self, dev_id=None): f = self.read_frame() return self.system_parameters_fmt.parse(f.info[1:]) - def get_management_info(self): - raise Exception('Dont touch this for now') - self.send_cmd(2, 0x92) + def get_management_info(self, dev_id): + bdevid = "{:02X}".format(dev_id).encode() + self.send_cmd(dev_id, 0x92, bdevid) f = self.read_frame() print(f.info) print(len(f.info)) - ff = self.management_info_fmt.parse(f.info) + ff = self.management_info_fmt.parse(f.info[1:]) print(ff) return ff diff --git a/tests/test_basic.py b/tests/test_basic.py index f94e20c..0a26da6 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -382,28 +382,16 @@ def test_up2500_1module_status_info_parsing_1(): def test_up2500_management_info(): p = Pylontech([b"~20024600B014026EF05AA0022BFDD5C0F915\r"]) - p.send_cmd(2, 0x92, b"02") - - mgmt = construct.Struct( - "ChargeVoltageLimit" / DivideBy1000(construct.Int16sb), - "DischargeVoltageLimit" / DivideBy1000(construct.Int16sb), - "ChargeCurrentLimit" / ToAmp(construct.Int16sb), - "DischargeCurrentLimit" / ToAmp(construct.Int16sb), - "status" - / construct.BitStruct( - "ChargeEnable" / construct.Flag, - "DischargeEnable" / construct.Flag, - "ChargeImmediately2" / construct.Flag, - "ChargeImmediately1" / construct.Flag, - "FullChargeRequest" / construct.Flag, - "ShouldCharge" - / construct.Computed( - lambda this: this.ChargeImmediately2 - | this.ChargeImmediately1 - | this.FullChargeRequest - ), - "_padding" / construct.BitsInteger(3), - ), - ) - f = p.read_frame() - print(mgmt.parse(f.info[1:])) + + d = p.get_management_info(2) + + assert d.ChargeVoltageLimit == 28.4 + assert d.DischargeVoltageLimit == 23.2 + assert d.ChargeCurrentLimit == 55.5 + assert d.DischargeCurrentLimit == -55.5 + assert d.status.ChargeEnable + assert d.status.DischargeEnable + assert not d.status.ChargeImmediately2 + assert not d.status.ChargeImmediately1 + assert not d.status.FullChargeRequest + assert not d.status.ShouldCharge