thingsboard-endpoint 0.2.2__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. {thingsboard_endpoint-0.2.2/src/thingsboard_endpoint.egg-info → thingsboard_endpoint-0.3.0}/PKG-INFO +1 -1
  2. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/endpoint.py +49 -0
  3. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/mqtt/client.py +16 -2
  4. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/version.py +1 -1
  5. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0/src/thingsboard_endpoint.egg-info}/PKG-INFO +1 -1
  6. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/LICENSE +0 -0
  7. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/README.md +0 -0
  8. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/pyproject.toml +0 -0
  9. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/setup.cfg +0 -0
  10. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/setup.py +0 -0
  11. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/__init__.py +0 -0
  12. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/__init__.py +0 -0
  13. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/attribute/__init__.py +0 -0
  14. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/attribute/attribute.py +0 -0
  15. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/attribute/constraint.py +0 -0
  16. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/attribute/decorator.py +0 -0
  17. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/attribute/type.py +0 -0
  18. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/connector/__init__.py +0 -0
  19. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/connector/model_to_cloud.py +0 -0
  20. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/exception/__init__.py +0 -0
  21. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/exception/invalid_attribute_exception.py +0 -0
  22. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/exception/invalid_attribute_type_exception.py +0 -0
  23. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/exception/invalid_property_exception.py +0 -0
  24. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/exception/modification_exception.py +0 -0
  25. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/factory/__init__.py +0 -0
  26. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/factory/xml.py +0 -0
  27. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/__init__.py +0 -0
  28. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/attribute_container.py +0 -0
  29. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/attribute_listener.py +0 -0
  30. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/message_format.py +0 -0
  31. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/named_item.py +0 -0
  32. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/node_container.py +0 -0
  33. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/object_container.py +0 -0
  34. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/unique_identifiable.py +0 -0
  35. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/interface/uuid.py +0 -0
  36. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/message_format/__init__.py +0 -0
  37. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/message_format/factory.py +0 -0
  38. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/message_format/json_format.py +0 -0
  39. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/message_format/jsonzip_format.py +0 -0
  40. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/message_format/thingsboard_format.py +0 -0
  41. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/mqtt/__init__.py +0 -0
  42. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/mqtt/pending_update.py +0 -0
  43. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/mqtt/persistence.py +0 -0
  44. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/node/__init__.py +0 -0
  45. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/node/node.py +0 -0
  46. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/object/__init__.py +0 -0
  47. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/object/object.py +0 -0
  48. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/properties_endpoint_configuration.py +0 -0
  49. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/runtime/__init__.py +0 -0
  50. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/runtime/node.py +0 -0
  51. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/runtime/object.py +0 -0
  52. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard/endpoint/topicuuid.py +0 -0
  53. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard_endpoint.egg-info/SOURCES.txt +0 -0
  54. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard_endpoint.egg-info/dependency_links.txt +0 -0
  55. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard_endpoint.egg-info/requires.txt +0 -0
  56. {thingsboard_endpoint-0.2.2 → thingsboard_endpoint-0.3.0}/src/thingsboard_endpoint.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: thingsboard-endpoint
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: ThingsBoard endpoint package
5
5
  Home-page: https://gitlab.com/things-board
6
6
  Author: HES-SO Valais, School of Engineering, Sion
@@ -69,6 +69,10 @@ class Endpoint(Threaded, NodeContainer):
69
69
  ENDPOINT_IDENTITY_CERT_FILE_PROPERTY = 'thingsboard-endpoint-tls-cert' # (*.pem)
70
70
  ENDPOINT_IDENTITY_KEY_FILE_PROPERTY = 'thingsboard-endpoint-tls-key' # (*.pem)
71
71
 
72
+ # Reconnection supervisor tuning (Solution A)
73
+ RECONNECT_MIN_DELAY = 1 # Seconds before the first supervised reconnect attempt
74
+ RECONNECT_MAX_DELAY = 120 # Upper bound for the exponential backoff between attempts
75
+
72
76
  log = logging.getLogger(__name__)
73
77
 
74
78
  def __init__(self, uuid, configuration=None, locations: str or list = None):
@@ -79,6 +83,10 @@ class Endpoint(Threaded, NodeContainer):
79
83
 
80
84
  self._end_point_is_ready = False # Set to true after connection and subscription
81
85
 
86
+ # Reconnection supervisor state
87
+ self._reconnect_attempt = 0 # Number of consecutive supervised reconnect attempts
88
+ self._last_reconnect_attempt = 0.0 # Monotonic timestamp of the last supervised reconnect attempt
89
+
82
90
  self.uuid = uuid # type: str
83
91
  self.nodes = {} # type: dict[str, Node]
84
92
  self.clean_session = True
@@ -188,6 +196,7 @@ class Endpoint(Threaded, NodeContainer):
188
196
 
189
197
  self._check_published_not_acknowledged_container()
190
198
  self._check_persistent_data_store()
199
+ self._check_and_reconnect()
191
200
 
192
201
  # Wait until next interval begins
193
202
  if self._thread_should_run:
@@ -524,6 +533,10 @@ class Endpoint(Threaded, NodeContainer):
524
533
 
525
534
  self._end_point_is_ready = True
526
535
 
536
+ # A fresh successful connection means the reconnection supervisor is done
537
+ self._reconnect_attempt = 0
538
+ self._last_reconnect_attempt = 0.0
539
+
527
540
  # time.sleep(4) # Give the clients time to connect to broker and to set up the mqtt queue
528
541
 
529
542
  def _on_connection_thread_finished(self):
@@ -581,6 +594,42 @@ class Endpoint(Threaded, NodeContainer):
581
594
  # Try to send stored messages to cloud
582
595
  self._purge_persistent_data_store()
583
596
 
597
+ def _check_and_reconnect(self):
598
+ """Supervises the MQTT connection and re-establishes it when lost.
599
+
600
+ The underlying paho/tb_mqtt stack does not reliably auto-reconnect
601
+ when the broker ends the session with a clean DISCONNECT packet
602
+ (e.g. 'Normal disconnection', reason code 0). This supervisor polls
603
+ the connection state each _run() cycle and actively rebuilds the
604
+ client with an exponential backoff so the endpoint recovers without
605
+ a manual restart.
606
+
607
+ :return: None
608
+ """
609
+ if self._client.is_connected():
610
+ # Connection is up (or was just re-established): reset the supervisor
611
+ self._reconnect_attempt = 0
612
+ self._last_reconnect_attempt = 0.0
613
+ return
614
+
615
+ # Endpoint is offline - make sure is_online() reflects that accurately
616
+ self._end_point_is_ready = False
617
+
618
+ # Exponential backoff to avoid hammering the broker while it is unavailable
619
+ backoff_delay = min(self.RECONNECT_MIN_DELAY * (2 ** self._reconnect_attempt),
620
+ self.RECONNECT_MAX_DELAY)
621
+ if time.time() - self._last_reconnect_attempt < backoff_delay:
622
+ return
623
+
624
+ attempt = self._reconnect_attempt + 1
625
+ self.log.warning('MQTT connection lost, attempting to reconnect (attempt %d)...' % attempt)
626
+ try:
627
+ self._client.reconnect()
628
+ except Exception as exception:
629
+ self.log.error('Reconnect attempt failed: %s' % exception, exc_info=True)
630
+ self._reconnect_attempt += 1
631
+ self._last_reconnect_attempt = time.time()
632
+
584
633
  def _put_persistent_data_store(self, topic, payload, timestamp):
585
634
  # If the message could not be sent for any reason, add the message to the pending
586
635
  # updates persistence if available.
@@ -60,6 +60,20 @@ class MqttClient(object):
60
60
  def start(self):
61
61
  self.connect(self._options)
62
62
 
63
+ def reconnect(self):
64
+ """Tear down and re-establish the MQTT client connection.
65
+
66
+ Re-running connect() also re-triggers the subscription of shared
67
+ attribute changes, mirroring the initial start. Safe to call
68
+ repeatedly (e.g. from a reconnection supervisor).
69
+ """
70
+ with self._client_lock:
71
+ if self._client is not None:
72
+ self._client.stop()
73
+ self._client = None
74
+ # Re-create and reconnect the MQTT client
75
+ self.start()
76
+
63
77
  def connect(self, options):
64
78
  if options.ca_file:
65
79
  # Check if file exists
@@ -114,7 +128,7 @@ class MqttClient(object):
114
128
  In case to let MQTT client die silently, call force_client_disconnect parameter with
115
129
  'false' value. In this case no disconnect callback method is called.
116
130
 
117
- ::param force_client_disconnect Set to true to call also MQTT clients disconnect method. Default: true
131
+ :param force_client_disconnect Set to true to call also MQTT clients disconnect method. Default: true
118
132
  :type force_client_disconnect bool
119
133
  :return None
120
134
  :rtype: None
@@ -123,7 +137,7 @@ class MqttClient(object):
123
137
  # Stop MQTT client if still running
124
138
  if self._client:
125
139
  if force_client_disconnect:
126
- self._client.on_disconnect = None # Want get a disconnect callback call
140
+ self._client.on_disconnect = None # Don't want to get a disconnect callback call
127
141
  self._client.disconnect()
128
142
  self._client.stop()
129
143
  self._client = None
@@ -3,7 +3,7 @@
3
3
 
4
4
  __author__ = 'Thomas Sterren'
5
5
  __contributors__ = []
6
- __version__ = '0.2.2'
6
+ __version__ = '0.3.0'
7
7
 
8
8
 
9
9
  def main():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: thingsboard-endpoint
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: ThingsBoard endpoint package
5
5
  Home-page: https://gitlab.com/things-board
6
6
  Author: HES-SO Valais, School of Engineering, Sion