LSY IoT Adapter Hub SDK - 用于与 Adapter Hub RPC 服务进行通信的 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: 主题名称,用于匹配规则中配置的 topicdata: 消息数据,支持字符串、字典或列表
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lsyiot_adapter_hub_sdk-0.0.1.tar.gz.
File metadata
- Download URL: lsyiot_adapter_hub_sdk-0.0.1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01211fbdb890619b42257908d2f40fbe76bb9c0d228f8b189bb21d4e7b7c5a13
|
|
| MD5 |
aedb0bb02887962ed5204ada23218ea1
|
|
| BLAKE2b-256 |
083722faac8f30bf5dec5dbfcf3cf74b79fed731baa688b36d5a64fabd799ea6
|
File details
Details for the file lsyiot_adapter_hub_sdk-0.0.1-py3-none-any.whl.
File metadata
- Download URL: lsyiot_adapter_hub_sdk-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d448819b63762a6030db671049e66be5619ea96b837c45c95fb837a64e73030
|
|
| MD5 |
dec518f7f64c6ad060331536286254a4
|
|
| BLAKE2b-256 |
c4559df191e046ef40ff792206a91b13e8a0f9f4e8d5a819679b598671c8f062
|