Skip to content

Commit

Permalink
Merge pull request #20 from abelsson/improve_management_info
Browse files Browse the repository at this point in the history
Improve implementation of get_management_info function
  • Loading branch information
Frankkkkk authored Jun 16, 2023
2 parents dee5098 + 857012e commit 6255788
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
34 changes: 23 additions & 11 deletions pylontech/pylontech.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,32 @@ 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(
"CommandValue" / construct.Byte,
"ModuleSerialNumber" / JoinBytes(construct.Array(16, construct.Byte)),
)


get_values_fmt = construct.Struct(
"NumberOfModules" / construct.Byte,
"Module" / construct.Array(construct.this.NumberOfModules, construct.Struct(
Expand Down Expand Up @@ -246,14 +258,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

Expand Down
38 changes: 13 additions & 25 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6255788

Please sign in to comment.