|
1 | | -# Copyright 2026. ThingsBoard |
2 | | -# # |
3 | | -# Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | -# you may not use this file except in compliance with the License. |
5 | | -# You may obtain a copy of the License at |
6 | | -# # |
7 | | -# http://www.apache.org/licenses/LICENSE-2.0 |
8 | | -# # |
9 | | -# Unless required by applicable law or agreed to in writing, software |
10 | | -# distributed under the License is distributed on an "AS IS" BASIS, |
11 | | -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | -# See the License for the specific language governing permissions and |
13 | | -# limitations under the License. |
14 | | -# |
15 | | - |
16 | | -from ujson import dumps, loads |
17 | 1 | from gc import collect |
18 | 2 |
|
| 3 | +from sdk_core.provision_client import ProvisionClientBase |
19 | 4 | from .umqtt import MQTTClient |
20 | 5 |
|
21 | 6 |
|
22 | | -class ProvisionClient: |
23 | | - PROVISION_REQUEST_TOPIC = b"/provision/request" |
24 | | - PROVISION_RESPONSE_TOPIC = b"/provision/response" |
25 | | - |
| 7 | +class ProvisionClient(ProvisionClientBase): |
26 | 8 | def __init__(self, host, port, provision_request): |
27 | | - self._host = host |
28 | | - self._port = port |
29 | | - self._client_id = b"provision" |
30 | | - self._provision_request = provision_request |
31 | | - self._credentials = None |
| 9 | + super().__init__(host, port, provision_request) |
32 | 10 |
|
33 | | - def _on_message(self, topic, msg): |
34 | | - try: |
35 | | - response = loads(msg) |
36 | | - if response.get("status") == "SUCCESS": |
37 | | - self._credentials = response |
38 | | - else: |
39 | | - print(f"Provisioning failed: {response.get('errorMsg', 'Unknown error')}") |
40 | | - except MemoryError: |
41 | | - print("MemoryError during message processing!") |
| 11 | + mqtt_client = MQTTClient(self._client_id, self._host, self._port, keepalive=10) |
| 12 | + mqtt_client.set_callback(self.on_message_callback) |
| 13 | + self.set_client(mqtt_client) |
42 | 14 |
|
43 | 15 | def provision(self): |
44 | | - mqtt_client = None |
45 | 16 | try: |
46 | | - collect() |
47 | | - |
48 | | - mqtt_client = MQTTClient(self._client_id, self._host, self._port, keepalive=10) |
49 | | - mqtt_client.set_callback(self._on_message) |
50 | | - mqtt_client.connect(clean_session=True) |
51 | | - mqtt_client.subscribe(self.PROVISION_RESPONSE_TOPIC) |
52 | | - collect() |
53 | | - |
54 | | - provision_request_str = dumps(self._provision_request, separators=(',', ':')) |
55 | | - mqtt_client.publish(self.PROVISION_REQUEST_TOPIC, provision_request_str) |
56 | | - del provision_request_str |
57 | | - collect() |
| 17 | + super().provision() |
58 | 18 |
|
59 | | - mqtt_client.wait_msg() |
| 19 | + self._client.wait_msg() |
60 | 20 | finally: |
61 | | - if mqtt_client: |
62 | | - mqtt_client.disconnect() |
63 | | - collect() |
| 21 | + if self._client: |
| 22 | + self._client.disconnect() |
64 | 23 |
|
65 | | - @property |
66 | | - def credentials(self): |
67 | | - return self._credentials |
| 24 | + collect() |
0 commit comments