152 lines
5.3 KiB
Python
152 lines
5.3 KiB
Python
"""JK02_32S profile qualified against BD6A17S6P / 11.47.
|
|
|
|
Only read requests are constructed here. Operating switches have a separate
|
|
strict encoder in settings.py; protection/calibration writes are absent.
|
|
Never expose the raw identity frame: it contains unrelated credential fields.
|
|
"""
|
|
|
|
import hashlib
|
|
import re
|
|
|
|
SERVICE = "0000ffe0-0000-1000-8000-00805f9b34fb"
|
|
CHARACTERISTIC = "0000ffe1-0000-1000-8000-00805f9b34fb"
|
|
HEADER = bytes.fromhex("55aaeb90")
|
|
|
|
|
|
def query(command):
|
|
if type(command) is not int or command not in (0x97, 0x96):
|
|
raise ValueError("Only the two reviewed read requests are supported")
|
|
data = bytes.fromhex("aa5590eb") + bytes([command, 0]) + bytes(13)
|
|
return data + bytes([sum(data) & 255])
|
|
|
|
|
|
def device_id(address):
|
|
if not re.fullmatch(r"(?:[0-9A-F]{2}:){5}[0-9A-F]{2}", address):
|
|
raise ValueError("Invalid public Bluetooth identity")
|
|
return "jkbms_" + hashlib.sha256(("public:" + address).encode()).hexdigest()[:32]
|
|
|
|
|
|
def candidate(properties):
|
|
"""Advertisement is a candidate, never a verified model or ownership claim."""
|
|
address = str(properties.get("Address", "")).upper()
|
|
if properties.get("AddressType") != "public" or not re.fullmatch(
|
|
r"(?:[0-9A-F]{2}:){5}[0-9A-F]{2}", address
|
|
):
|
|
return False
|
|
manufacturer = properties.get("ManufacturerData", {})
|
|
data = bytes(manufacturer.get(0x0B65, b""))
|
|
return (
|
|
str(properties.get("Name", "")).startswith("JK_")
|
|
and len(data) == 8
|
|
and data[:2] == b"\x88\xa0"
|
|
and data[2:] == bytes.fromhex(address.replace(":", ""))
|
|
)
|
|
|
|
|
|
class Frames:
|
|
def __init__(self):
|
|
self.buffer = bytearray()
|
|
self.rejected = 0
|
|
|
|
def feed(self, data):
|
|
if len(data) > 512:
|
|
self.buffer.clear()
|
|
self.rejected += 1
|
|
return []
|
|
self.buffer.extend(data)
|
|
result = []
|
|
while len(self.buffer) >= 4:
|
|
offset = self.buffer.find(HEADER)
|
|
if offset < 0:
|
|
del self.buffer[:-3]
|
|
break
|
|
del self.buffer[:offset]
|
|
if len(self.buffer) < 300:
|
|
break
|
|
frame = bytes(self.buffer[:300])
|
|
if frame[4] not in (1, 2, 3) or sum(frame[:-1]) & 255 != frame[-1]:
|
|
del self.buffer[0]
|
|
self.rejected += 1
|
|
continue
|
|
del self.buffer[:300]
|
|
result.append(frame)
|
|
return result
|
|
|
|
|
|
def identity(frame):
|
|
if len(frame) != 300 or frame[:5] != HEADER + b"\x03":
|
|
raise ValueError("Not a JK identity frame")
|
|
|
|
def field(start, end):
|
|
return frame[start:end].split(b"\0", 1)[0].decode("ascii", "replace")
|
|
|
|
return {
|
|
"model": field(6, 22),
|
|
"hardware": field(22, 30),
|
|
"firmware": field(30, 38),
|
|
"name": field(46, 62),
|
|
"manufactured_raw": field(78, 84),
|
|
"serial_number": field(86, 97),
|
|
}
|
|
|
|
|
|
def supported(info):
|
|
return info.get("model") == "JK_BD6A17S6P" and info.get("firmware") == "11.47"
|
|
|
|
|
|
def telemetry(frame):
|
|
if len(frame) != 300 or frame[:5] != HEADER + b"\x02":
|
|
raise ValueError("Not a JK telemetry frame")
|
|
|
|
def u(offset, size=4, signed=False):
|
|
return int.from_bytes(frame[offset : offset + size], "little", signed=signed)
|
|
|
|
mask = u(70)
|
|
count = mask.bit_count()
|
|
# Non-contiguous channel maps are not qualified by this profile.
|
|
if not 1 <= count <= 32 or mask != (1 << count) - 1:
|
|
raise ValueError("Unsupported cell map")
|
|
cells = [u(6 + i * 2, 2) / 1000 for i in range(count)]
|
|
voltage, soc = u(150) / 1000, u(173, 1)
|
|
if soc > 100 or any(not 0.5 < v < 5 for v in cells) or abs(sum(cells) - voltage) > 0.1:
|
|
raise ValueError("Inconsistent battery measurements")
|
|
|
|
def temperature(offset, bit):
|
|
value = u(offset, 2, True)
|
|
return value / 10 if u(214, 1) & (1 << bit) and value != -2000 else None
|
|
|
|
current = u(158, signed=True) / 1000
|
|
return {
|
|
"voltage_v": voltage,
|
|
"current_a": current,
|
|
"soc_percent": soc,
|
|
"power_w": round(voltage * current, 3),
|
|
"power_source": "voltage_times_current",
|
|
"remaining_ah": u(174) / 1000,
|
|
"configured_capacity_ah": None,
|
|
"effective_capacity_ah": u(178) / 1000,
|
|
"reported_health_percent": u(190, 1) if u(190, 1) <= 100 else None,
|
|
"cycle_count": u(182),
|
|
"cycle_capacity_ah": u(186) / 1000,
|
|
"cell_count": count,
|
|
"cell_voltages_v": cells,
|
|
"cell_delta_v": u(76, 2) / 1000,
|
|
"cell_min_v": min(cells),
|
|
"cell_max_v": max(cells),
|
|
"cell_average_v": round(sum(cells) / count, 4),
|
|
"cell_min_index": cells.index(min(cells)) + 1,
|
|
"cell_max_index": cells.index(max(cells)) + 1,
|
|
"wire_resistances_ohm": [u(80 + i * 2, 2) / 1000 for i in range(count)],
|
|
"wire_warning_cells": [i + 1 for i in range(count) if u(146) & (1 << i)],
|
|
"protection_mask": u(166),
|
|
"charge_mos_on": bool(u(198, 1)),
|
|
"discharge_mos_on": bool(u(199, 1)),
|
|
"runtime_s": u(194),
|
|
"balance_current_a": u(170, 2, True) / 1000,
|
|
"balance_action_raw": u(172, 1),
|
|
"protection_flags_raw": frame[166:170].hex(),
|
|
"temperature_mos_c": temperature(144, 0),
|
|
"temperature_1_c": temperature(162, 1),
|
|
"temperature_2_c": temperature(164, 2),
|
|
}
|