Skip to main content

LSY IoT Adapter Hub SDK - 用于与 Adapter Hub RPC/API 服务进行通信的 Python SDK

Project description

lsyiot_adapter_hub_sdk

LSY IoT Adapter Hub SDK - 用于与 Adapter Hub RPC 服务进行通信的 Python SDK。

安装

pip install lsyiot-adapter-hub-sdk

快速开始

基本用法

from lsyiot_adapter_hub_sdk import AdapterHubRpcClient, AdapterHubRpcError

# 创建客户端
client = AdapterHubRpcClient("http://localhost:8080/rpc")

# 发送主题消息
try:
    result = client.topic_message("sensor/temperature", {"value": 25.5})
    
    if result.is_success:
        print(f"发送成功: {result.message}")
    else:
        print(f"发送失败: {result.message}")
        
except AdapterHubRpcError as e:
    print(f"RPC 错误: {e}")

发送不同类型的消息

from lsyiot_adapter_hub_sdk import AdapterHubRpcClient

client = AdapterHubRpcClient("http://localhost:8080/rpc")

# 发送字符串消息
result = client.topic_message("sensor/temperature", "25.5")

# 发送字典消息
result = client.topic_message("device/status", {
    "device_id": "001",
    "status": "online",
    "timestamp": 1732780800
})

# 发送列表消息
result = client.topic_message("batch/data", [
    {"id": 1, "value": 10},
    {"id": 2, "value": 20}
])

使用响应结果

from lsyiot_adapter_hub_sdk import AdapterHubRpcClient

client = AdapterHubRpcClient("http://localhost:8080/rpc")
result = client.topic_message("sensor/temperature", {"value": 25.5})

# 访问响应属性
print(result.code)        # 状态码,200 表示成功
print(result.message)     # 状态消息
print(result.data)        # 返回数据
print(result.error)       # 是否有错误
print(result.is_success)  # 是否成功

# 转换为字典
result_dict = result.to_dict()

# 获取原始 JSON 字符串
raw_json = result.raw

异常处理

from lsyiot_adapter_hub_sdk import AdapterHubRpcClient, AdapterHubRpcError

client = AdapterHubRpcClient("http://localhost:8080/rpc")

try:
    result = client.topic_message("sensor/temperature", {"value": 25.5})
except AdapterHubRpcError as e:
    print(f"错误码: {e.code}")
    print(f"错误消息: {e.message}")
    print(f"附加数据: {e.data}")
    
    # 根据错误码处理不同类型的错误
    if e.code == -1001:
        print("连接失败,请检查服务器地址")
    elif e.code == -1002:
        print("连接超时")
    elif e.code == -1003:
        print("响应解析失败")

API 参考

AdapterHubRpcClient

RPC 客户端类,用于与 Adapter Hub 服务通信。

构造函数

AdapterHubRpcClient(rpc_server_url: str)
  • rpc_server_url: RPC 服务器地址,例如 http://localhost:8080/rpc

方法

topic_message
topic_message(topic: str, data: Union[str, Dict, List]) -> AdapterHubRpcResult

发送主题消息到 Adapter Hub。

  • topic: 主题名称,用于匹配规则中配置的 topic
  • data: 消息数据,支持字符串、字典或列表

AdapterHubRpcResult

RPC 响应结果类。

属性

属性 类型 说明
code int 状态码,200 表示成功
message str 状态消息
data Any 返回数据
error bool 是否有错误
is_success bool 是否成功
raw str 原始 JSON 字符串

方法

方法 说明
get(key, default) 从结果字典中获取值
to_dict() 转换为字典

AdapterHubRpcError

RPC 调用异常类。

属性

属性 类型 说明
code int 错误码
message str 错误消息
data Any 附加数据

错误码说明

错误码 说明
200 成功
-1 业务逻辑错误
-1000 HTTP 协议错误
-1001 连接失败
-1002 连接超时
-1003 JSON 解析失败
-1999 未知错误

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lsyiot_adapter_hub_sdk-0.0.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lsyiot_adapter_hub_sdk-0.0.2-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file lsyiot_adapter_hub_sdk-0.0.2.tar.gz.

File metadata

  • Download URL: lsyiot_adapter_hub_sdk-0.0.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for lsyiot_adapter_hub_sdk-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e4ddcfb9ef5302ce2f72b112f468b46f0239ddf76338f5402b204789227595b4
MD5 c7969b3fbf8080ffcfc52ae2af4de90e
BLAKE2b-256 321cb9d9e49bdf86ba24dddbc638e539e1549432b477777d41545030b27bc792

See more details on using hashes here.

File details

Details for the file lsyiot_adapter_hub_sdk-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for lsyiot_adapter_hub_sdk-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c4c6e5b2e5874363c60ec2a42c1753d2f16e8e981dc606c25cd9ebb890de8ad4
MD5 ee26642c9fbd00c3b6fa9c06c4f2f40d
BLAKE2b-256 7dbc765b3ddc9895c20b42ce998ccb6ff3bbdcbff9463eda9708455f9cf538e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page