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.3.tar.gz (13.8 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.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lsyiot_adapter_hub_sdk-0.0.3.tar.gz
  • Upload date:
  • Size: 13.8 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.3.tar.gz
Algorithm Hash digest
SHA256 812523aec107060e99fa7d0754f39184bf8912cba6fab79125fff7189d5f4124
MD5 40304ce30d170c9b81cecac794dbf06c
BLAKE2b-256 5ab5f955ab3c0ee3be151e72265e5ceea563f98b821565e4a2d042d8a717fe45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lsyiot_adapter_hub_sdk-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ed2ad8506cdd757e06f47fcebb881a469e4c69b7aa7650fdbdd201f28eab3a9b
MD5 22ac802d1a731e80f48fadc39d2ead5e
BLAKE2b-256 ea93df1b1ef41e1829913b8340f36263adf05073aaa5aaed1529eb576f7a40e7

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