File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+ # SPDX-FileCopyrightText: Copyright (c) 2026 ThingsBoard Inc.
3+ #
4+ # SPDX-License-Identifier: Unlicense
5+
6+ import sys
7+
8+
9+ class NetworkAdapter :
10+ def get_socket_pool (self ):
11+ pass
12+
13+ def get_ssl_context (self ):
14+ pass
15+
16+
17+ class CircuitPythonNetworkAdapter (NetworkAdapter ):
18+ def __init__ (self ):
19+ import socketpool
20+ import wifi
21+
22+ self ._pool = socketpool .SocketPool (wifi .radio )
23+
24+ def get_socket_pool (self ):
25+ return self ._pool
26+
27+ def get_ssl_context (self ):
28+ return None
29+
30+
31+ class CPythonNetworkAdapter (NetworkAdapter ):
32+ def __init__ (self ):
33+ import adafruit_connection_manager
34+ from adafruit_connection_manager import CPythonNetwork
35+
36+ radio = CPythonNetwork ()
37+ self ._pool = adafruit_connection_manager .get_radio_socketpool (radio )
38+ self ._ssl_context = adafruit_connection_manager .get_radio_ssl_context (radio )
39+
40+ def get_socket_pool (self ):
41+ return self ._pool
42+
43+ def get_ssl_context (self ):
44+ return self ._ssl_context
45+
46+
47+ class NetworkAdapterFactory :
48+ @staticmethod
49+ def create ():
50+ if sys .implementation .name == "circuitpython" :
51+ print ("Using CircuitPython network adapter" )
52+ return CircuitPythonNetworkAdapter ()
53+ print ("Using CPython network adapter" )
54+ return CPythonNetworkAdapter ()
Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ ignore = [
9494 " PLR0913" , # too-many-arguments
9595 " PLR0917" , # too-many-positional-arguments
9696 " PLC0415" , # top-level-import
97+ " PLR6301" , # no-self-use
9798 " UP031" ,
9899 " UP032" ,
99100 " PLR0912" ,
Original file line number Diff line number Diff line change 2323import gc
2424from json import dumps , loads
2525
26- import socketpool
27- import wifi
26+ from network_adapter import NetworkAdapterFactory
2827
2928__version__ = "0.0.1"
3029__repo__ = "https://github.com/samson0v/CircuitPython_thingsboard-client-sdk.git"
@@ -45,6 +44,7 @@ def __init__(
4544 access_token = None ,
4645 quality_of_service = None ,
4746 client_id = None ,
47+ network_adapter = None ,
4848 ):
4949 from adafruit_minimqtt .adafruit_minimqtt import MQTT
5050
@@ -70,17 +70,21 @@ def __init__(
7070 if not client_id :
7171 client_id = "sdk-client"
7272 self ._client_id = client_id
73- self ._pool = socketpool .SocketPool (wifi .radio )
74-
75- self ._client = MQTT (
76- broker = self ._host ,
77- port = self ._port ,
78- client_id = self ._client_id ,
79- username = self ._access_token ,
80- password = "pswd" ,
81- keep_alive = 120 ,
82- socket_pool = self ._pool ,
83- )
73+ self ._adapter = network_adapter or NetworkAdapterFactory .create ()
74+ mqtt_kwargs = {
75+ "broker" : self ._host ,
76+ "port" : self ._port ,
77+ "client_id" : self ._client_id ,
78+ "username" : self ._access_token ,
79+ "password" : "pswd" ,
80+ "keep_alive" : 120 ,
81+ "socket_pool" : self ._adapter .get_socket_pool (),
82+ }
83+ ssl_context = self ._adapter .get_ssl_context ()
84+ if ssl_context is not None :
85+ mqtt_kwargs ["ssl_context" ] = ssl_context
86+
87+ self ._client = MQTT (** mqtt_kwargs )
8488
8589 def connect (self ):
8690 try :
You can’t perform that action at this time.
0 commit comments