This commit is contained in:
joy,zhou 2021-02-02 14:37:37 +08:00
parent 083675759b
commit a3a7cf4676
2 changed files with 94 additions and 0 deletions

81
examples/rrpc/index.py Normal file
View File

@ -0,0 +1,81 @@
import json
import logging
import time
from iotedgedriverlinksdk import getLogger
from iotedgedriverlinksdk.client import Config, SubDevice
from iotedgedriverlinksdk.exception import BaseEdgeException
# 配置log
log = getLogger()
log.setLevel(logging.DEBUG)
# 主函数
if __name__ == "__main__":
try:
# 获取驱动及子设备配置信息
driverConfig = Config().getDriverInfo()
log.info('driver config:{}'.format(driverConfig))
# 从驱动配置获取设备数据上报周期
uploadPeriod = 5
if "period" in driverConfig.keys() and isinstance(driverConfig['period'], int):
uploadPeriod = int(driverConfig['period'])
deviceInfoList = Config().getDeviceInfos()
log.info('device list config:{}'.format(deviceInfoList))
except Exception as e:
log.error('load driver config error: {}'.format(str(e)))
exit(1)
try:
# 判断是否绑定子设备
if len(deviceInfoList) < 1:
log.error(
'subdevice null, please bind sub device for driver')
while True:
time.sleep(60)
# 取其中一个子设备
subDeviceInfo = deviceInfoList[0]
# 获取子设备的ProductSN key值为 productSN
productSN = subDeviceInfo['productSN']
# 获取子设备的DeviceSN key值为 deviceSN
deviceSN = subDeviceInfo['deviceSN']
def callback(topic: str, payload: b''):
log.info("recv message from {} : {}".format(topic, str(payload)))
def rrpc_callback(topic: str, payload: b''):
return b'{"a":1}'
# 初始化一个子设备对象
subDevice = SubDevice(product_sn=productSN,
device_sn=deviceSN, on_msg_callback=callback)
subDevice.set_rrpc_callback(rrpc_callback)
# 子设备上线
subDevice.login()
# 获取当前子设备的配置
deviceConfig = subDeviceInfo['config']
log.info('sub device config:{}'.format(deviceConfig))
# 从子设备配置获取子设备上报topic定义
topic = "/{}/{}/upload".format(productSN, deviceSN) # 此处为默认topic
if 'topic' in deviceConfig and isinstance(deviceConfig['topic'], str):
topic = deviceConfig['topic'].format(productSN, deviceSN)
# 从子设备配置获取子设备上报参数名称
param = 'RelayStatus' # 此处定义默认属性名称: RelayStatus
if 'paramName' in deviceConfig and isinstance(deviceConfig['paramName'], str):
param = deviceConfig['paramName']
while True:
time.sleep(uploadPeriod)
except BaseEdgeException:
log.error('Edge Exception: {}'.format(str(e)))
except Exception as e:
log.error('Exception error: {}'.format(str(e)))

13
examples/rrpc/pack.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash -e
if [ -e "./demo.zip" ] ; then
rm ./demo.zip
fi
mkdir tmp
cp index.py tmp/
cd tmp
pip3 install -t . iotedge_driver_link_sdk==0.0.2.beta #打包驱动SDK
zip -r demo.zip .
cd ..
cp tmp/demo.zip .
rm -rf tmp