3 @summary 中移动OneNet平台COAP接入
15 local TAG
= "onenet.coap"
21 @return boolean 成功返回true,否则返回nil
23 -- 配置信息是一个table,具体键值如下:
24 -- product_id 产品id,字符串,必须有
25 -- device_name 设备名称,字符串,必须有
26 -- device_key 设备密钥,字符串,必须有
27 -- host coap服务器地址,字符串,默认"183.230.102.122"
28 -- port coap服务器端口,整数,默认5683
29 -- auto_reply 自动回复thing获取,布尔值,默认关闭false
30 -- thing 物模型,类型是table,默认 {params={}}
31 -- debug 调试开关,默认关闭false
32 -- adapter 适配器编号,默认是最后一个注册的适配器
33 -- callback 收到合法coap数据时的用户回调
35 function onenet
.setup(conf
)
39 if not conf
.product_id
then
40 log.error(TAG
, "配置信息缺product_id")
43 if not conf
.device_name
then
44 log.error(TAG
, "配置信息缺device_name")
47 if not conf
.device_key
then
48 log.error(TAG
, "配置信息缺device_key")
51 onenet
.product_id
= conf
.product_id
52 onenet
.device_name
= conf
.device_name
53 -- log.info(">>", onenet.product_id, onenet.device_name, conf.device_key)
54 _
, _
, onenet
.login_token
= iotauth
.onenet(onenet
.product_id
, onenet
.device_name
, conf
.device_key
, "sha1")
55 -- log.info("onenet.login_token", onenet.login_token)
57 onenet
.host
= conf
.host
or "183.230.102.122"
58 onenet
.port
= conf
.port
or 5683
60 onenet
.auto_reply
= conf
.auto_reply
61 onenet
.topic
= conf
.topic
or "onenet_udp_inc"
62 onenet
.thing
= conf
.thing
or {
65 onenet
.debug
= conf
.debug
68 onenet
.adapter
= conf
.adapter
69 onenet
.timeout
= conf
.timeout
or 3000
70 onenet
.rx_buff
= zbuff
.create(1500)
71 onenet
.callback
= conf
.callback
75 function onenet
.netc_cb(sc
, event
)
76 -- log.info("udp", sc, string.format("%08X", event))
77 local rxbuff
= onenet
.rx_buff
78 if event
== socket
.EVENT
then
79 local ok
, len
, remote_ip
, remote_port
= socket
.rx(sc
, rxbuff
)
81 local data
= rxbuff
:query()
83 log.info(TAG
, "读到数据", data
:toHex())
85 local resp
= ercoap
.parse(data
)
86 if resp
and resp
.code
== 201 then
87 log.info(TAG
, "login success", resp
.code
, resp
.payload
:toHex())
88 -- 这里非常重要, 获取其他请求所需要的token值
89 onenet
.post_token
= resp
.payload
91 sys
.publish(onenet
.topic
)
94 if onenet
.callback
then
97 sys
.publish(onenet
.topic
, resp
)
101 log.info(TAG
, "服务器断开了连接")
103 sys
.publish(onenet
.topic
)
105 elseif event
== socket
.TX_OK
then
106 log.info(TAG
, "上行完成")
107 elseif event
== socket
.ON_LINE
then
108 log.info(TAG
, "UDP已准备就绪,可以上行")
110 -- log.info("登陆参数", onenet.product_id, onenet.device_name, onenet.login_token)
111 local data
= ercoap
.onenet("login", onenet
.product_id
, onenet
.device_name
, onenet
.login_token
)
112 -- log.info("上行登陆包", data:toHex())
115 log.info(TAG
, "其他事件", event
)
119 function onenet
.main_task()
121 while onenet
.state
~= 0 do
122 local ok
= onenet
.main_loop()
129 function onenet
.main_loop()
130 if onenet
.netc
== nil then
131 onenet
.netc
= socket
.create(onenet
.adapter
, onenet
.netc_cb
)
133 if onenet
.netc
== nil then
134 log.info(TAG
, "创建socket失败!!! 3秒后重试")
137 local netc
= onenet
.netc
138 if onenet
.state
~= 2 then
139 socket
.config(netc
, nil, true)
141 socket
.debug(netc
, true)
143 socket
.connect(netc
, onenet
.host
, onenet
.port
)
144 local result
, resp
= sys
.waitUntil(onenet
.topic
, onenet
.timeout
)
146 log.info(TAG
, "等待底层连接成功超时了")
150 sys
.waitUntil(onenet
.topic
, 3000)
154 function onenet
.start()
155 if onenet
.state
~= 0 then
156 log.info("onenet", "coap", "已经在启动状态,不需要再启动")
159 sys
.taskInit(onenet
.main_task
)
163 function onenet
.uplink(tp
, payload
)
167 if payload
and payload
["id"] == nil then
170 if type(payload
) == "table" then
171 payload
= json
.encode(payload
, "7f")
173 -- log.info("uplink", onenet.product_id, onenet.device_name, onenet.post_token:toHex(), payload)
174 local tmp
= ercoap
.onenet(tp
, onenet
.product_id
, onenet
.device_name
, onenet
.post_token
, payload
)
175 -- log.info("uplink", tp, tmp:toHex())
176 socket
.tx(onenet
.netc
, tmp
)