All Products
Search
Document Center

:SDK for Python

Last Updated:May 13, 2024

Link IoT Edge provides the SDK for Python. The SDK is named lethingaccesssdk. This topic describes how to use the SDK for Python.

For more information about the source code of the SDK, visit Open-source SDK for Python.

Installation and usage

  1. Run the following command to install the SDK:
    pip3 install lethingaccesssdk
  2. After the SDK is installed, you can develop drivers based on the SDK interfaces.
    Important After the driver development is completed, running the driver directly will generate an error. You must deploy the driver to the gateway on the IoT Platform console before you run the driver. For more information about how to deploy the driver to the gateway, see Driver development.

    The following is an example of how to use the SDK to develop a driver:

    # -*- coding: utf-8 -*-
    import logging
    import time
    import lethingaccesssdk
    from threading import Timer
    # Base on device, User need to implement the getProperties, setProperties and callService function.
    class Temperature_device(lethingaccesssdk.ThingCallback):
        def __init__(self):
            self.temperature = 41
            self.humidity = 80
        def getProperties(self, input_value):
            '''
            Get properties from the physical thing and return the result.
            :param input_value:
            :return:
            '''
            retDict = {
                "temperature": 41,
                "humidity": 80
            }
            return 0, retDict
        def setProperties(self, input_value):
            '''
            Set properties to the physical thing and return the result.
            :param input_value:
            :return:
            '''
            return 0, {}
        def callService(self, name, input_value):
            '''
            Call services on the physical thing and return the result.
            :param name:
            :param input_value:
            :return:
            '''
            return 0, {}
    def thing_behavior(client, device):
        while True:
            properties = {"temperature": device.temperature,
                          "humidity": device.humidity}
            client.reportProperties(properties)
            client.reportEvent("high_temperature", {"temperature": 41})
            time.sleep(2)
    try:
        thing_config = lethingaccesssdk.Config().getThingInfos()
        for config in thing_config:
            device = Temperature_device()
            client = lethingaccesssdk.ThingAccessClient(config)
            client.registerAndonline(device)
            t = Timer(2, thing_behavior, (client, device))
            t.start()
    except Exception as e:
        logging.error(e)
    # don't remove this function
    def handler(event, context):
        return 'hello world'

Config

The information related to driver configurations.

  • Config()

    Constructs a new Config object based on a configuration string.

  • getThingInfos()

    Returns all device-related information.

    The return values are described as follows:

    Returns the encapsulated configuration information that the device uses to connect to Link IoT Edge.

    Table 1. Response Parameters
    ParameterTypeDescription
    ThingInfoListThe device information.
    Table 2. ThingInfo description
    ParameterTypeDescription
    productKeyStringThe unique identifier of the product.
    deviceNameStringThe name of the device.
    customObjectThe custom device configurations.
  • getDriverInfo()

    Returns the driver information.

    Response:
    dict

ThingCallback

Create a class (for example, Demo_device) that inherits ThingCallback based on the physical device. Then implement the setProperties, getProperties, and callService functions in the class (Demo_device).

  • setProperties

    Sets device properties.

    Table 3. Request Parameters
    ParameterTypeDescription
    propertiesDictThe property object. The format of the property value is as follows:
    {
        "property1": "value1", 
        "property2": "value2"
    }
    Table 4. Response Parameters
    ParameterTypeDescription
    codeIntegerIf the request is successful, 0 is returned. If the request fails, a non-zero error code is returned.
    outputDictThe returned data. You can customize the content. Example:
    {
        "key1": xxx,
        "key2": yyy,
        ...
    }

    If no data is returned, {} is returned.

  • getProperties

    Obtains device properties.

    Table 5. Request Parameters
    ParameterTypeDescription
    keysListThe names of the properties. The format is as follows:
    ['key1', 'key2']
    Table 6. Response Parameters
    ParameterTypeDescription
    codeIntegerIf the request is successful, 0 is returned. If the request fails, a non-zero error code is returned.
    outputDictThe return value. Example:
    {
        'property1': xxx,
        'property2': yyy,
        ..}.
    }
  • callService

    Calls a device service.

    Table 7. Request Parameters
    ParameterTypeDescription
    nameStringThe name of the device service.
    argsDictThe list of service input parameters. The format is as follows:
    {
        "key1": "value1", 
        "key2": "value2"
    }
    Table 8. Response Parameters
    ParameterTypeDescription
    codeIntegerIf the request is successful, 0 is returned. If the request fails, a non-zero error code is returned.
    outputDictThe returned data. You can customize the content. Example:
    {
        "key1": xxx,
        "key2": yyy,
        ...
    }

    If no data is returned, {} is returned.

ThingAccessClient(config)

The client that allows devices to connect to IoT Platform. You can use the client to report properties and events. You can also use the client to send commands from the cloud to the devices.

Table 9. Request Parameters
ParameterTypeDescription
configDictThe productKey and deviceName assigned by IoT Platform.
Example:
{
    "productKey": "xxx",
    "deviceName": "yyy"
}
  • registerAndOnline(ThingCallback)

    Registers the device on the gateway and notifies the gateway to connect the device to IoT Platform. You must register and connect the devices to IoT platform before they can receive commands from or send data to IoT platform.

    Table 10. Request Parameters
    ParameterTypeDescription
    ThingCallbackObjectThe callback object of the device.
  • reportProperties(properties)
    Reports device properties to IoT Platform.
    Table 11. Request Parameters
    ParameterTypeDescription
    propertiesDictThe keys and values in the properties. The format is as follows:
    {
        "key1": "value1", 
        "key2": "value2"
    }
  • reportEvent(eventName, args)
    Reports device events to IoT Platform.
    Table 12. Request Parameters
    ParameterTypeDescription
    eventNameStringThe event name, which is the same as the name of the event you specified in the product definition.
    argsDictThe keys and values in the event. The format is as follows:
    {
        "key1": "value1", 
        "key2": "value2"
    }
  • getTsl()

    Returns the TSL string of the device. The data format is the same as in IoT Platform.

    Response:
    A TSL string
  • getTslExtInfo()

    Returns the extended information of the TSL.

    Response:
    The extended information of the TSL.
  • online()

    Notifies the gateway that the device is online. The interface is used when the device is offline and then is online again.

  • offline()

    Notifies the gateway that the device is offline.

  • cleanup()

    Recycles resources. You can use this interface to free up resources.

  • unregister()

    Unbinds a device from a gateway. Use this interface with caution.

getConfig()

Obtains information related to driver configurations.

The return value is a driver configuration string.