Feat: add bms_cooldown_on_error option and clear raw BLE stream buffer during JK-BMS AT-command flood - #392
Open
aka-raveren wants to merge 5 commits into
Open
Feat: add bms_cooldown_on_error option and clear raw BLE stream buffer during JK-BMS AT-command flood#392aka-raveren wants to merge 5 commits into
aka-raveren wants to merge 5 commits into
Conversation
…r during JK-BMS AT-command flood
…dalone mode in _create_client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Problem:
Some JK BMS (Jikong) models running specific inverter-interfacing firmware versions actively broadcast non-protocol telemetry text messages starting with
AT\r\n(AT-command flood) onto the shared BLE/UART bus (as referenced in issue #370).When heavy load, solar inverter EMI, or prolonged connection sessions cause data packet fragmentation, these junk bytes accumulate exponentially inside
BleakClientincoming streams. Under the original code logic,bmslib/models/jikong.pycounts these bytes (discarded junk bytes) but leaves them insideself._buffer. Within a few poll cycles, this unmanaged binary noise chokes theasyncioloop queue, leading to continuousTimeoutErrorspikes. Worse, the Linux host Bluetooth daemon (BlueZ) eventually drops into a critical deadlock state (org.bluez.Error.InProgress / Operation already in progress), which completely freezes data fetching for all other healthy, connected BMS units (e.g., JBD).The Solution:
This PR introduces a two-tier escalation recovery strategy controlled via
options.json / config.yaml:Low-Level Junk Buffer Suppression (
bmslib/models/jikong.py):Directly inside the
if dropped:routine, we execute.clear()onself._bufferon the fly. This instantaneously vaporizes theAT\r\ntext flood out of the active Python RAM before it can bottleneck the async parser. Crucially, the valid static device configuration mappings inside_resp_table(such asnum_cellsindex mapping) are completely preserved, preventingKeyErrororIndexErrorcode crashes.First Tier: Software Isolation (
bms_cooldown_on_error):If a sampling timeout still slips through, the main loop catches the error, forces a programmatic
.close()sequence on the troubled client, and wipes the static Bleak GATT descriptors memory cache usingBleakClient._gatt_cache.clear(). This isolates the faulty battery, giving its internal MCU buffer exactly one polling period of silent cooldown time to auto-reset, while allowing other adjacent healthy BMS nodes to continue reporting data to Home Assistant without a single dropped packet.Second Tier: Hardware Remediation (
bt_power_cycle_on_error):If the software cache flush fails to bypass a severe OS-level kernel freeze, this option executes a hot power cycle of the
hci0radio controller interface utilizinghciconfig hci0 down/uphooks with proper hardware delay sleep constraints (3 seconds), safely reviving the host adapter state without restarting the daemon container.Testing:
Tested in a real-world multi-BMS ecosystem (JK BMS + JBD BMS) running continuously for hours under volatile grid load. Programmatic cache and stream clearing successfully bypasses 3800+ junk byte/sec streams on the fly, reducing host CPU overhead and achieving unbroken long-term data collection stability in Home Assistant Core.