Module aLiYun
模块功能:阿里云物联网套件客户端功能.
目前的产品节点类型仅支持“设备”,设备认证方式支持“一机一密和“一型一密”
Info:
- Copyright: openLuat
- Release: 2018.04.16
- License: MIT
- Author: openLuat
Functions
setup (productKey[, productSecret=nil], getDeviceNameFnc, getDeviceSecretFnc[, setDeviceSecretFnc=nil]) | 配置阿里云物联网套件的产品信息和设备信息 |
setMqtt ([cleanSession=1][, will=nil][, keepAlive=240]) | 设置MQTT数据通道的参数 |
subscribe (topic, qos) | 订阅主题 |
publish (topic, payload[, qos=0][, cbFnc=nil][, cbPara=nil]) | 发布一条消息 |
on (evt, cbFnc) | 注册事件的处理函数 |
setErrHandle (cbFnc[, tmout=150]) | 设置阿里云task连续一段时间工作异常的处理程序 |
Functions
- setup (productKey[, productSecret=nil], getDeviceNameFnc, getDeviceSecretFnc[, setDeviceSecretFnc=nil])
-
配置阿里云物联网套件的产品信息和设备信息
Parameters:
- productKey
string
产品标识
- productSecret
string
[此参数可选,默认值为: nil]
产品密钥 一机一密认证方案时,此参数传入nil 一型一密认证方案时,此参数传入真实的产品密钥
- getDeviceNameFnc
function
获取设备名称的函数
- getDeviceSecretFnc
function
获取设备密钥的函数
- setDeviceSecretFnc
function
[此参数可选,默认值为: nil]
设置设备密钥的函数,一型一密认证方案才需要此参数
Returns:
-
nil
Usage:
aLiYun.setup("b0FMK1Ga5cp",nil,getDeviceNameFnc,getDeviceSecretFnc) aLiYun.setup("a1AoVqkCIbG","7eCdPyR6fYPntFcM",getDeviceNameFnc,getDeviceSecretFnc,setDeviceSecretFnc)
- productKey
string
- setMqtt ([cleanSession=1][, will=nil][, keepAlive=240])
-
设置MQTT数据通道的参数
Parameters:
- cleanSession
number
[此参数可选,默认值为: 1]
1/0
- will
table
[此参数可选,默认值为: nil]
遗嘱参数,格式为{qos=, retain=, topic=, payload=}
- keepAlive
number
[此参数可选,默认值为: 240]
单位秒
Returns:
-
nil
Usage:
aLiYun.setMqtt(0) aLiYun.setMqtt(1,{qos=0,retain=1,topic="/willTopic",payload="will payload"}) aLiYun.setMqtt(1,{qos=0,retain=1,topic="/willTopic",payload="will payload"},120)
- cleanSession
number
[此参数可选,默认值为: 1]
- subscribe (topic, qos)
-
订阅主题
Parameters:
- topic
string或者table类型,一个主题时为string类型,多个主题时为table类型,主题内容为UTF8编码
- qos
number或者nil,topic为一个主题时,qos为number类型(0/1/2,默认0);topic为多个主题时,qos为nil
Returns:
-
nil
Usage:
aLiYun.subscribe("/b0FMK1Ga5cp/862991234567890/get", 0) aLiYun.subscribe({["/b0FMK1Ga5cp/862991234567890/get"] = 0, ["/b0FMK1Ga5cp/862991234567890/get"] = 1})
- topic
- publish (topic, payload[, qos=0][, cbFnc=nil][, cbPara=nil])
-
发布一条消息
Parameters:
- topic
string
UTF8编码的主题
- payload
string
负载
- qos
number
[此参数可选,默认值为: 0]
质量等级,0/1/2,默认0
- cbFnc
function
[此参数可选,默认值为: nil]
消息发布结果的回调函数 回调函数的调用形式为:cbFnc(result,cbPara)。result为true表示发布成功,false或者nil表示订阅失败;cbPara为本接口中的第5个参数
- cbPara
[此参数可选,默认值为: nil]
消息发布结果回调函数的回调参数
Returns:
-
nil
Usage:
aLiYun.publish("/b0FMK1Ga5cp/862991234567890/update","test",0) aLiYun.publish("/b0FMK1Ga5cp/862991234567890/update","test",1,cbFnc,"cbFncPara")
- topic
string
- on (evt, cbFnc)
-
注册事件的处理函数
Parameters:
- evt
string
事件 "auth"表示鉴权服务器认证结果事件 "connect"表示接入服务器连接结果事件 "receive"表示接收到接入服务器的消息事件
- cbFnc
function
事件的处理函数 当evt为"auth"时,cbFnc的调用形式为:cbFnc(result),result为true表示认证成功,false或者nil表示认证失败 当evt为"connect"时,cbFnc的调用形式为:cbFnc(result),result为true表示连接成功,false或者nil表示连接失败 当evt为"receive"时,cbFnc的调用形式为:cbFnc(topic,qos,payload),topic为UTF8编码的主题(string类型),qos为质量等级(number类型),payload为原始编码的负载(string类型)
Returns:
-
nil
Usage:
aLiYun.on("b0FMK1Ga5cp",nil,getDeviceNameFnc,getDeviceSecretFnc)
- evt
string
- setErrHandle (cbFnc[, tmout=150])
-
设置阿里云task连续一段时间工作异常的处理程序
Parameters:
- cbFnc
function
异常处理函数,cbFnc的调用形式为:cbFnc()
- tmout
number
[此参数可选,默认值为: 150]
连续工作异常的时间,当连续异常到达这个时间之后,会调用cbFnc()
Returns:
-
nil
Usage:
aLiYun.setErrHandle(function() sys.restart("ALIYUN_TASK_INACTIVE") end, 300)
- cbFnc
function