add: libfota需要兼容老的hmeta库, 没有hwver函数
[LuatOS.git] / script / libs / onenetcoap.lua
blob711ad029c8e4792cd528d002fc84d1f4e0cb3476
1 --[[
2 @module onenetcoap
3 @summary 中移动OneNet平台COAP接入
4 @version 1.0
5 @date 2023.11.17
6 @author wendal
7 @demo onenet/coap
8 @tag LUAT_USE_NETWORK
9 @usage
10 -- 使用方法请查阅demo
14 local onenet = {}
15 local TAG = "onenet.coap"
17 --[[
18 初始化onenet coap库
19 @onenet.setup(conf)
20 @table 配置项信息,必须有
21 @return boolean 成功返回true,否则返回nil
22 @usage
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)
36 if not conf then
37 return
38 end
39 if not conf.product_id then
40 log.error(TAG, "配置信息缺product_id")
41 return
42 end
43 if not conf.device_name then
44 log.error(TAG, "配置信息缺device_name")
45 return
46 end
47 if not conf.device_key then
48 log.error(TAG, "配置信息缺device_key")
49 return
50 end
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 {
63 params = {}
65 onenet.debug = conf.debug
66 onenet.thing_id = 1
67 onenet.state = 0
68 onenet.adapter = conf.adapter
69 onenet.timeout = conf.timeout or 3000
70 onenet.rx_buff = zbuff.create(1500)
71 onenet.callback = conf.callback
72 return true
73 end
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)
80 if ok then
81 local data = rxbuff:query()
82 rxbuff:del()
83 log.info(TAG, "读到数据", data:toHex())
84 ercoap.print(data)
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
90 onenet.state = 2
91 sys.publish(onenet.topic)
92 end
93 if resp then
94 if onenet.callback then
95 onenet.callback(resp)
96 else
97 sys.publish(onenet.topic, resp)
98 end
99 end
100 else
101 log.info(TAG, "服务器断开了连接")
102 onenet.state = 0
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已准备就绪,可以上行")
109 -- 上行登陆包
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())
113 socket.tx(sc, data)
114 else
115 log.info(TAG, "其他事件", event)
119 function onenet.main_task()
120 onenet.state = 1
121 while onenet.state ~= 0 do
122 local ok = onenet.main_loop()
123 if not ok then
124 sys.wait(500)
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秒后重试")
135 return
137 local netc = onenet.netc
138 if onenet.state ~= 2 then
139 socket.config(netc, nil, true)
140 if onenet.debug then
141 socket.debug(netc, true)
143 socket.connect(netc, onenet.host, onenet.port)
144 local result, resp = sys.waitUntil(onenet.topic, onenet.timeout)
145 if not result then
146 log.info(TAG, "等待底层连接成功超时了")
147 return
150 sys.waitUntil(onenet.topic, 3000)
151 return
154 function onenet.start()
155 if onenet.state ~= 0 then
156 log.info("onenet", "coap", "已经在启动状态,不需要再启动")
157 return
159 sys.taskInit(onenet.main_task)
160 return true
163 function onenet.uplink(tp, payload)
164 if not tp then
165 return
167 if payload and payload["id"] == nil then
168 payload["id"] = "1"
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)
177 return true
180 return onenet