|
| 1 | +# Copyright 2025 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 | +# Example script to device provisioning using the DeviceClient |
| 16 | + |
| 17 | +import asyncio |
| 18 | +from random import randint |
| 19 | + |
| 20 | +from tb_mqtt_client.entities.data.provisioning_request import AccessTokenProvisioningCredentials, ProvisioningRequest |
| 21 | +from tb_mqtt_client.entities.data.timeseries_entry import TimeseriesEntry |
| 22 | +from tb_mqtt_client.service.device.client import DeviceClient |
| 23 | + |
| 24 | + |
| 25 | +async def main(): |
| 26 | + provisioning_credentials = AccessTokenProvisioningCredentials( |
| 27 | + provision_device_key='YOUR_PROVISION_DEVICE_KEY', |
| 28 | + provision_device_secret='YOUR_PROVISION_DEVICE_SECRET', |
| 29 | + ) |
| 30 | + provisioning_request = ProvisioningRequest('localhost', credentials=provisioning_credentials) |
| 31 | + provisioning_response = await DeviceClient.provision(provisioning_request) |
| 32 | + |
| 33 | + if provisioning_response.error is not None: |
| 34 | + print(f"Provisioning failed: {provisioning_response.error}") |
| 35 | + return |
| 36 | + |
| 37 | + print('Provisined device config: ', provisioning_response) |
| 38 | + |
| 39 | + # Create a DeviceClient instance with the provisioned device config |
| 40 | + client = DeviceClient(provisioning_response.result) |
| 41 | + await client.connect() |
| 42 | + |
| 43 | + # Send single telemetry entry to provisioned device |
| 44 | + await client.send_telemetry(TimeseriesEntry("batteryLevel", randint(0, 100))) |
| 45 | + |
| 46 | + await client.stop() |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + asyncio.run(main()) |
0 commit comments