ThingsBoard Python Client SDK 2.0 – Complete Architectural Rewrite Based on gmqtt - #99
Open
imbeacon wants to merge 78 commits into
Open
ThingsBoard Python Client SDK 2.0 – Complete Architectural Rewrite Based on gmqtt#99imbeacon wants to merge 78 commits into
imbeacon wants to merge 78 commits into
Conversation
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.
Description:
This PR introduces a complete rewrite of the Python Client SDK using an asynchronous, event‑driven architecture built on top of gmqtt.
The new implementation is optimized for high‑throughput telemetry and attribute publishing, strict ordering, advanced delivery tracking, and robust gateway/multi‑device operation.
Tested throughput: ~500,000 uplink datapoints/second under optimal conditions.
⸻
Architectural Changes
Core MQTT Layer
• New MQTTManager built on gmqtt with:
• Patched CONNACK, DISCONNECT, and PUBACK handlers for MQTT 5.0 reason codes/properties.
• QoS 1 persistent inflight tracking and retransmission.
• Publish/subscribe acknowledgment tracking via asyncio.Future.
• Integrated BackpressureController for automatic publish pause/resume.
Message Dispatching
• Unified MessageDispatcher for telemetry, attributes, and RPC publishing.
• JSON serialization with payload size and datapoint limit enforcement.
• Configurable batching and splitting via MessageSplitter.
Rate Limit Enforcement
• Supports ThingsBoard‑style multi‑window limits (10:1,300:60).
• Separate telemetry message and datapoint quotas.
• Head‑blocking queue logic ensures in‑order sending when rate limits are hit.
Message Queue
• Asynchronous deque with:
• Head‑first batch formation (no skipping blocked messages).
• Deduplication for re‑queued messages.
• Priority retry queue for QoS 1 retransmissions.
Structured Uplink Messages
• DeviceUplinkMessage immutable dataclass + builder:
• Predictable size tracking before publish.
• Shared delivery future for all split/batched payloads from a single source message.
• Reduced per‑datapoint overhead, efficient allocation.
Gateway Support
• GatewayClient manages connected devices via SubDeviceManager.
• Handles:
• Sub‑device attribute updates.
• RPC requests.
• Attribute responses.
• Same batching/splitting logic for main and sub‑devices.
Provisioning & Firmware
• ProvisioningClient supports token/basic/X.509 provisioning.
• FirmwareUpdater supports streaming download, checksum verification, and persistent storage.
⸻
Performance Notes
• Throughput: ~500k uplink datapoints/sec in optimal lab conditions.
• Latency: Low per‑message processing overhead.
• Ordering: Guaranteed strict ordering in the send queue.
• Delivery Tracking: PUBACK futures for precise delivery confirmation.
⸻
Testing
• Coverage includes:
• MQTT lifecycle (connect/reconnect/disconnect).
• PUBACK and QoS 1 retransmission handling.
• Rate limit exhaustion and recovery.
• Message batching/splitting edge cases.
• Gateway sub‑device lifecycle and routing.