全部產品
Search
文件中心

:Python版本SDK

更新時間:Jul 13, 2024

Link IoT Edge提供Python版本的SDK,名稱為lethingaccesssdk。本章為您介紹Python版本的SDK使用方法及相關SDK。

Python版本開源的SDK源碼請見開源的Python庫

安裝和使用

  1. 您可以通過如下命令來安裝SDK。
    pip3 install lethingaccesssdk
  2. 安裝完成後,您可以根據SDK介面進行驅動開發。
    重要 完成驅動開發後,直接運行會提示錯誤,必須通過物聯網平台控制台,將已開發的驅動部署到網關中方可執行。部署驅動到網關的操作請參考驅動開發

    使用SDK開發驅動的範例程式碼片段如下所示。

    # -*- 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

驅動相關配置資訊。

  • Config()

    基於當前驅動配置字串構造新的Config對象。

  • getThingInfos()

    返回所有裝置相關資訊。

    傳回值說明如下:

    返回裝置用於串連Link IoT Edge的配置資訊的封裝。

    表 1. 返回參數
    名稱類型描述
    ThingInfoList裝置資訊。
    表 2. ThingInfo參數說明
    名稱類型描述
    productKeyString產品唯一標識。
    deviceNameString裝置名稱。
    customObject裝置自訂配置。
  • getDriverInfo()

    返回驅動相關資訊。

    傳回值:
    dict

ThingCallback

首先根據真實裝置,命名一個類(如Demo_device)繼承ThingCallback,然後在該類(Demo_device)中實現setPropertiesgetPropertiescallService三個函數。

  • setProperties

    設定具體裝置屬性函數。

    表 3. 請求參數
    名稱類型描述
    propertiesDict設定屬性,取值格式為:
    {
        "property1": "value1", 
        "property2": "value2"
    }
    表 4. 返回參數
    名稱類型描述
    codeInteger若擷取成功則返回0,失敗則返回非0錯誤碼。
    outputDict資料內容自訂,例如:
    {
        "key1": xxx,
        "key2": yyy,
        ...
    }

    若無返回資料,則值為空白{}

  • getProperties

    擷取具體裝置屬性的函數。

    表 5. 請求參數
    名稱類型描述
    keysList擷取屬性對應的名稱,取值格式為:
    ['key1', 'key2']
    表 6. 返回參數
    名稱類型描述
    codeInteger若擷取成功則返回0,失敗則返回非0錯誤碼。
    outputDict傳回值,例如:
    {
        'property1': xxx,
        'property2': yyy,
        ..}.
    }
  • callService

    調用裝置服務函數。

    表 7. 請求參數
    名稱類型描述
    nameString裝置服務名稱。
    argsDict服務入參列表,取值格式為:
    {
        "key1": "value1", 
        "key2": "value2"
    }
    表 8. 返回參數
    名稱類型描述
    codeInteger若擷取成功則返回0,失敗則返回非0錯誤碼。
    outputDict資料內容自訂,例如:
    {
        "key1": xxx,
        "key2": yyy,
        ...
    }

    若無返回資料,則值為空白{}

ThingAccessClient(config)

裝置接入用戶端,您可以通過該用戶端來主動上報裝置屬性或事件,也可被動接受雲端下發的指令。

表 9. 請求參數
名稱類型描述
configDict包括雲端分配的ProductKey和deviceName。
例如,
{
    "productKey": "xxx",
    "deviceName": "yyy"
}
  • registerAndOnline(ThingCallback)

    將裝置註冊到網關中並通知網關上線裝置。裝置需要註冊並上線後,裝置端才能收到雲端下發的指令或者發送資料到雲端。

    表 10. 請求參數
    名稱類型描述
    ThingCallbackObject裝置的callback對象。
  • reportProperties(properties)
    主動上報裝置屬性。
    表 11. 請求參數
    名稱類型描述
    propertiesDict屬性中包含的屬性key與value,取值格式為:
    {
        "key1": "value1", 
        "key2": "value2"
    }
  • reportEvent(eventName, args)
    主動上報裝置事件。
    表 12. 請求參數
    名稱類型描述
    eventNameString事件對應的名稱,與您在產品定義中建立事件的名稱一致。
    argsDict事件中包含的屬性key與value,取值格式為:
    {
        "key1": "value1", 
        "key2": "value2"
    }
  • getTsl()

    返回TSL字串,資料格式與雲端一致。

    傳回值:
    TSL字串
  • getTslExtInfo()

    返回TSL擴充資訊字串。

    傳回值:
    TSL擴充資訊字串
  • online()

    通知網關裝置上線,該介面一般在裝置離線後再次上線時使用。

  • offline()

    通知網關裝置已離線。

  • cleanup()

    資源回收介面,您可以使用該介面回收您的資源。

  • unregister()

    移除裝置和網關的綁定關係。請謹慎使用該介面。

getConfig()

擷取驅動相關配置資訊。

傳回值為驅動配置資訊字串。