extalk.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. --[[
  2. @module extalk
  3. @summary extalk扩展库
  4. @version 1.1.1
  5. @date 2025.09.18
  6. @author 梁健
  7. @usage
  8. local extalk = require "extalk"
  9. -- 配置并初始化
  10. extalk.setup({
  11. key = "your_product_key",
  12. heart_break_time = 30,
  13. contact_list_cbfnc = function(dev_list) end,
  14. state_cbfnc = function(state) end
  15. })
  16. -- 发起对讲
  17. extalk.start("remote_device_id")
  18. -- 结束对讲
  19. extalk.stop()
  20. ]]
  21. local extalk = {}
  22. -- 模块常量(保留原始数据结构)
  23. extalk.START = 1 -- 通话开始
  24. extalk.STOP = 2 -- 通话结束
  25. extalk.UNRESPONSIVE = 3 -- 未响应
  26. extalk.ONE_ON_ONE = 5 -- 一对一来电
  27. extalk.BROADCAST = 6 -- 广播
  28. local AIRTALK_TASK_NAME = "airtalk_task"
  29. -- 消息类型常量(保留原始数据结构)
  30. local MSG_CONNECT_ON_IND = 0
  31. local MSG_CONNECT_OFF_IND = 1
  32. local MSG_AUTH_IND = 2
  33. local MSG_SPEECH_ON_IND = 3
  34. local MSG_SPEECH_OFF_IND = 4
  35. local MSG_SPEECH_CONNECT_TO = 5
  36. local MSG_SPEECH_STOP_TEST_END = 22
  37. -- 设备状态常量(保留原始数据结构)
  38. local SP_T_NO_READY = 0 -- 离线状态无法对讲
  39. local SP_T_IDLE = 1 -- 对讲空闲状态
  40. local SP_T_CONNECTING = 2 -- 主动发起对讲
  41. local SP_T_CONNECTED = 3 -- 对讲中
  42. local SUCC = "success"
  43. -- 全局状态变量(保留原始数据结构)
  44. local g_state = SP_T_NO_READY -- 设备状态
  45. local g_mqttc = nil -- mqtt客户端
  46. local g_local_id -- 本机ID
  47. local g_stask_start = false -- 本机ID
  48. local g_remote_id -- 对端ID
  49. local g_s_type -- 对讲的模式,字符串形式
  50. local g_s_topic -- 对讲用的topic
  51. local g_s_mode -- 对讲的模式
  52. local g_dev_list -- 对讲列表
  53. local g_dl_topic -- 下行消息topic模板
  54. -- 配置参数
  55. local extalk_configs_local = {
  56. key = 0, -- 项目key,一般需要和main的PRODUCT_KEY保持一致
  57. heart_break_time = 0, -- 心跳间隔(单位秒)
  58. contact_list_cbfnc = nil, -- 联系人回调函数,含设备号和昵称
  59. state_cbfnc = nil, -- 状态回调,分为对讲开始,对讲结束,未响应
  60. }
  61. -- 工具函数:参数检查
  62. local function check_param(param, expected_type, name)
  63. if type(param) ~= expected_type then
  64. log.error(string.format("参数错误: %s 应为 %s 类型,实际为 %s",
  65. name, expected_type, type(param)))
  66. return false
  67. end
  68. return true
  69. end
  70. -- MQTT消息发布函数,集中处理所有发布操作并打印日志
  71. local function publish_message(topic, payload)
  72. if g_mqttc then
  73. log.info("MQTT发布 - 主题:", topic, "内容:", payload)
  74. g_mqttc:publish(topic, payload)
  75. else
  76. log.error("MQTT客户端未初始化,无法发布消息")
  77. end
  78. end
  79. -- 对讲超时处理
  80. function extalk.wait_speech_to()
  81. log.info("主动请求对讲超时无应答")
  82. extalk.speech_off(true, false)
  83. end
  84. -- 发送鉴权消息
  85. local function auth()
  86. if g_state == SP_T_NO_READY and g_mqttc then
  87. local topic = string.format("ctrl/uplink/%s/0001", g_local_id)
  88. local payload = json.encode({
  89. ["key"] = extalk_configs_local.key,
  90. ["device_type"] = 1
  91. })
  92. publish_message(topic, payload)
  93. end
  94. end
  95. -- 发送心跳消息
  96. local function heart()
  97. if g_mqttc then
  98. adc.open(adc.CH_VBAT)
  99. local vbat = adc.get(adc.CH_VBAT)
  100. adc.close(adc.CH_VBAT)
  101. local topic = string.format("ctrl/uplink/%s/0005", g_local_id)
  102. local payload = json.encode({
  103. ["csq"] = mobile.csq(),
  104. ["battery"] = vbat
  105. })
  106. publish_message(topic, payload)
  107. end
  108. end
  109. -- 开始对讲
  110. local function speech_on(ssrc, sample)
  111. g_state = SP_T_CONNECTED
  112. g_mqttc:subscribe(g_s_topic)
  113. airtalk.set_topic(g_s_topic)
  114. airtalk.set_ssrc(ssrc)
  115. log.info("对讲模式", g_s_mode)
  116. airtalk.speech(true, g_s_mode, sample)
  117. sys.sendMsg(AIRTALK_TASK_NAME, MSG_SPEECH_ON_IND, true)
  118. -- sys.timerLoopStart(heart, extalk_configs_local.heart_break_time * 1000)
  119. sys.timerStopAll(extalk.wait_speech_to)
  120. end
  121. -- 结束对讲
  122. function extalk.speech_off(need_upload, need_ind)
  123. if g_state == SP_T_CONNECTED then
  124. g_mqttc:unsubscribe(g_s_topic)
  125. airtalk.speech(false)
  126. g_s_topic = nil
  127. end
  128. g_state = SP_T_IDLE
  129. sys.timerStopAll(auth)
  130. sys.timerStopAll(extalk.wait_speech_to)
  131. if need_upload and g_mqttc then
  132. local topic = string.format("ctrl/uplink/%s/0004", g_local_id)
  133. publish_message(topic, json.encode({["to"] = g_remote_id}))
  134. end
  135. if need_ind then
  136. sys.sendMsg(AIRTALK_TASK_NAME, MSG_SPEECH_OFF_IND, true)
  137. end
  138. end
  139. -- 命令处理:请求对讲应答
  140. local function handle_speech_response(obj)
  141. if g_state ~= SP_T_CONNECTING then
  142. log.error("state", g_state, "need", SP_T_CONNECTING)
  143. return
  144. end
  145. if obj and obj["result"] == SUCC and g_s_topic == obj["topic"] then
  146. -- 开始对讲
  147. local sample_rate = obj["audio_code"] == "amr-nb" and 8000 or 16000
  148. speech_on(obj["ssrc"], sample_rate)
  149. return
  150. else
  151. log.info(obj["result"], obj["topic"], g_s_topic)
  152. sys.sendMsg(AIRTALK_TASK_NAME, MSG_SPEECH_ON_IND, false)
  153. end
  154. g_s_topic = nil
  155. g_state = SP_T_IDLE
  156. end
  157. -- 命令处理:对端来电
  158. local function handle_incoming_call(obj)
  159. if not obj or not obj["topic"] or not obj["ssrc"] or not obj["audio_code"] or not obj["type"] then
  160. local response = {
  161. ["result"] = "failed",
  162. ["topic"] = obj and obj["topic"] or "",
  163. ["info"] = "无效的请求参数"
  164. }
  165. publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
  166. return
  167. end
  168. -- 非空闲状态无法接收来电
  169. if g_state ~= SP_T_IDLE then
  170. log.error("state", g_state, "need", SP_T_IDLE)
  171. local response = {
  172. ["result"] = "failed",
  173. ["topic"] = obj["topic"],
  174. ["info"] = "device is busy"
  175. }
  176. publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
  177. return
  178. end
  179. local response, from = {}, nil
  180. -- 提取对端ID
  181. from = string.match(obj["topic"], "audio/(.*)/.*/.*")
  182. if not from then
  183. response = {
  184. ["result"] = "failed",
  185. ["topic"] = obj["topic"],
  186. ["info"] = "topic error"
  187. }
  188. publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
  189. return
  190. end
  191. -- 处理一对一通话
  192. if obj["type"] == "one-on-one" then
  193. g_s_topic = obj["topic"]
  194. g_remote_id = from
  195. g_s_type = "one-on-one"
  196. g_s_mode = airtalk.MODE_PERSON
  197. -- 触发回调
  198. if extalk_configs_local.state_cbfnc then
  199. extalk_configs_local.state_cbfnc({
  200. state = extalk.ONE_ON_ONE,
  201. id = from
  202. })
  203. end
  204. response = {["result"] = SUCC, ["topic"] = obj["topic"], ["info"] = ""}
  205. local sample_rate = obj["audio_code"] == "amr-nb" and 8000 or 16000
  206. speech_on(obj["ssrc"], sample_rate)
  207. end
  208. -- 处理广播
  209. if obj["type"] == "broadcast" then
  210. g_s_topic = obj["topic"]
  211. g_remote_id = from
  212. g_s_mode = airtalk.MODE_GROUP_LISTENER
  213. g_s_type = "broadcast"
  214. -- 触发回调
  215. if extalk_configs_local.state_cbfnc then
  216. extalk_configs_local.state_cbfnc({
  217. state = extalk.BROADCAST,
  218. id = from
  219. })
  220. end
  221. response = {["result"] = SUCC, ["topic"] = obj["topic"], ["info"] = ""}
  222. local sample_rate = obj["audio_code"] == "amr-nb" and 8000 or 16000
  223. speech_on(obj["ssrc"], sample_rate)
  224. end
  225. -- 发送响应
  226. publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
  227. end
  228. -- 命令处理:对端挂断
  229. local function handle_remote_hangup(obj)
  230. local response = {}
  231. if g_state == SP_T_IDLE then
  232. response = {["result"] = "failed", ["info"] = "no speech"}
  233. else
  234. log.info("0103", obj, obj["type"], g_s_type)
  235. if obj and obj["type"] == g_s_type then
  236. response = {["result"] = SUCC, ["info"] = ""}
  237. extalk.speech_off(false, true)
  238. else
  239. response = {["result"] = "failed", ["info"] = "type mismatch"}
  240. end
  241. end
  242. publish_message(string.format("ctrl/uplink/%s/8103", g_local_id), json.encode(response))
  243. end
  244. -- 命令处理:更新设备列表
  245. local function handle_device_list_update(obj)
  246. local response = {}
  247. if obj then
  248. g_dev_list = obj["dev_list"]
  249. response = {["result"] = SUCC, ["info"] = ""}
  250. else
  251. response = {["result"] = "failed", ["info"] = "json info error"}
  252. end
  253. publish_message(string.format("ctrl/uplink/%s/8101", g_local_id), json.encode(response))
  254. end
  255. -- 命令处理:鉴权结果
  256. local function handle_auth_result(obj)
  257. if obj and obj["result"] == SUCC then
  258. publish_message(string.format("ctrl/uplink/%s/0002", g_local_id), "") -- 更新列表
  259. sys.timerLoopStart(heart, extalk_configs_local.heart_break_time * 1000) -- 发起心跳
  260. else
  261. sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, false,
  262. "鉴权失败" .. (obj and obj["info"] or ""))
  263. log.error("鉴权失败,可能是没有修改PRODUCT_KEY")
  264. end
  265. end
  266. -- 命令处理:设备列表更新应答
  267. local function handle_device_list_response(obj)
  268. if obj and obj["result"] == SUCC then
  269. g_dev_list = obj["dev_list"]
  270. if extalk_configs_local.contact_list_cbfnc then
  271. extalk_configs_local.contact_list_cbfnc(g_dev_list)
  272. end
  273. g_state = SP_T_IDLE
  274. sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, true) -- 完整登录流程结束
  275. else
  276. sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, false, "更新设备列表失败")
  277. end
  278. end
  279. -- 命令解析路由表
  280. local cmd_handlers = {
  281. ["8003"] = handle_speech_response, -- 请求对讲应答
  282. ["0102"] = handle_incoming_call, -- 平台通知对端对讲开始
  283. ["0103"] = handle_remote_hangup, -- 平台通知终端对讲结束
  284. ["0101"] = handle_device_list_update,-- 平台通知终端更新对讲设备列表
  285. ["8001"] = handle_auth_result, -- 平台对鉴权应答
  286. ["8002"] = handle_device_list_response -- 平台对终端获取终端列表应答
  287. }
  288. -- 解析接收到的消息
  289. local function analyze_v1(cmd, topic, obj)
  290. -- 忽略心跳和结束对讲的应答
  291. if cmd == "8005" or cmd == "8004" then
  292. return
  293. end
  294. -- 查找并执行对应的命令处理器
  295. local handler = cmd_handlers[cmd]
  296. if handler then
  297. handler(obj)
  298. else
  299. log.warn("未处理的命令", cmd)
  300. end
  301. end
  302. -- MQTT回调处理
  303. local function mqtt_cb(mqttc, event, topic, payload)
  304. log.info(event, topic or "")
  305. if event == "conack" then
  306. -- MQTT连接成功,开始自定义鉴权流程
  307. sys.sendMsg(AIRTALK_TASK_NAME, MSG_CONNECT_ON_IND)
  308. g_mqttc:subscribe("ctrl/downlink/" .. g_local_id .. "/#")
  309. elseif event == "suback" then
  310. if g_state == SP_T_NO_READY then
  311. if topic then
  312. auth()
  313. else
  314. sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, false,
  315. "订阅失败" .. "ctrl/downlink/" .. g_local_id .. "/#")
  316. end
  317. elseif g_state == SP_T_CONNECTED and not topic then
  318. extalk.speech_off(false, true)
  319. end
  320. elseif event == "recv" then
  321. local result = string.match(topic, g_dl_topic)
  322. if result then
  323. local obj = json.decode(payload)
  324. analyze_v1(result, topic, obj)
  325. end
  326. elseif event == "disconnect" then
  327. extalk.speech_off(false, true)
  328. g_state = SP_T_NO_READY
  329. elseif event == "error" then
  330. log.error("MQTT错误发生",topic,payload)
  331. end
  332. end
  333. -- 任务消息处理
  334. local function task_cb(msg)
  335. if msg[1] == MSG_SPEECH_CONNECT_TO then
  336. extalk.speech_off(true, false)
  337. else
  338. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  339. end
  340. end
  341. -- 对讲事件回调
  342. local function airtalk_event_cb(event, param)
  343. log.info("airtalk event", event, param)
  344. if event == airtalk.EVENT_ERROR then
  345. if param == airtalk.ERROR_NO_DATA and g_s_mode == airtalk.MODE_PERSON then
  346. log.error("长时间没有收到音频数据")
  347. extalk.speech_off(true, true)
  348. end
  349. end
  350. end
  351. -- MQTT任务主循环
  352. local function airtalk_mqtt_task()
  353. if g_stask_start then
  354. log.info("airtalk task 已经初始化了")
  355. return true
  356. end
  357. g_stask_start = true
  358. local msg, online = nil, false
  359. -- 初始化本地ID
  360. g_local_id = mobile.imei()
  361. g_dl_topic = "ctrl/downlink/" .. g_local_id .. "/(%w%w%w%w)"
  362. -- 创建MQTT客户端
  363. g_mqttc = mqtt.create(nil, "mqtt.airtalk.luatos.com", 1883, false, {rxSize = 32768})
  364. -- 配置对讲参数
  365. airtalk.config(airtalk.PROTOCOL_MQTT, g_mqttc, 200) -- 缓冲至少200ms播放
  366. airtalk.on(airtalk_event_cb)
  367. airtalk.start()
  368. -- 配置MQTT客户端
  369. g_mqttc:auth(g_local_id, g_local_id, mobile.muid())
  370. g_mqttc:keepalive(240) -- 默认值240s
  371. g_mqttc:autoreconn(true, 15000) -- 自动重连机制
  372. g_mqttc:debug(false)
  373. g_mqttc:on(mqtt_cb)
  374. log.info("设备信息", g_local_id, mobile.muid())
  375. -- 开始连接
  376. g_mqttc:connect()
  377. online = false
  378. while true do
  379. -- 等待MQTT连接成功
  380. msg = sys.waitMsg(AIRTALK_TASK_NAME, MSG_CONNECT_ON_IND)
  381. log.info("connected")
  382. -- 处理登录流程
  383. while not online do
  384. msg = sys.waitMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, 30000) -- 30秒超时
  385. if type(msg) == 'table' then
  386. online = msg[2]
  387. if online then
  388. -- 鉴权通过,60分钟后重新鉴权
  389. sys.timerLoopStart(auth, 3600000)
  390. else
  391. log.info(msg[3])
  392. -- 鉴权失败,5分钟后重试
  393. sys.timerLoopStart(auth, 300000)
  394. end
  395. else
  396. -- 超时未收到鉴权结果,重新发送
  397. auth()
  398. end
  399. end
  400. log.info("对讲管理平台已连接")
  401. -- 处理在线状态下的消息
  402. while online do
  403. msg = sys.waitMsg(AIRTALK_TASK_NAME)
  404. if type(msg) == 'table' and type(msg[1]) == "number" then
  405. if msg[1] == MSG_SPEECH_STOP_TEST_END then
  406. if g_state ~= SP_T_CONNECTING and g_state ~= SP_T_CONNECTED then
  407. log.info("没有对讲", g_state)
  408. else
  409. extalk.speech_off(true, false)
  410. end
  411. elseif msg[1] == MSG_SPEECH_ON_IND then
  412. if extalk_configs_local.state_cbfnc then
  413. local state = msg[2] and extalk.START or extalk.UNRESPONSIVE
  414. extalk_configs_local.state_cbfnc({state = state})
  415. end
  416. elseif msg[1] == MSG_SPEECH_OFF_IND then
  417. if extalk_configs_local.state_cbfnc then
  418. extalk_configs_local.state_cbfnc({state = extalk.STOP})
  419. end
  420. elseif msg[1] == MSG_CONNECT_OFF_IND then
  421. log.info("connect", msg[2])
  422. online = msg[2]
  423. end
  424. else
  425. log.info(type(msg), type(msg and msg[1]))
  426. end
  427. msg = nil -- 清理引用
  428. end
  429. online = false -- 重置在线状态
  430. end
  431. end
  432. -- 模块初始化
  433. function extalk.setup(extalk_configs)
  434. if not extalk_configs or type(extalk_configs) ~= "table" then
  435. log.error("AirTalk配置必须为table类型")
  436. return false
  437. end
  438. -- 检查配置参数
  439. if not check_param(extalk_configs.key, "string", "key") then
  440. return false
  441. end
  442. extalk_configs_local.key = extalk_configs.key
  443. if not check_param(extalk_configs.heart_break_time, "number", "heart_break_time") then
  444. return false
  445. end
  446. extalk_configs_local.heart_break_time = extalk_configs.heart_break_time
  447. if not check_param(extalk_configs.contact_list_cbfnc, "function", "contact_list_cbfnc") then
  448. return false
  449. end
  450. extalk_configs_local.contact_list_cbfnc = extalk_configs.contact_list_cbfnc
  451. if not check_param(extalk_configs.state_cbfnc, "function", "state_cbfnc") then
  452. return false
  453. end
  454. extalk_configs_local.state_cbfnc = extalk_configs.state_cbfnc
  455. -- 启动任务
  456. sys.taskInitEx(airtalk_mqtt_task, AIRTALK_TASK_NAME, task_cb)
  457. return true
  458. end
  459. -- 开始对讲
  460. function extalk.start(id)
  461. if g_state ~= SP_T_IDLE then
  462. log.warn("正在对讲无法开始,当前状态:", g_state)
  463. return false
  464. end
  465. if id == nil then
  466. -- 广播模式
  467. g_remote_id = "all"
  468. g_state = SP_T_CONNECTING
  469. g_s_mode = airtalk.MODE_GROUP_SPEAKER
  470. g_s_type = "broadcast"
  471. g_s_topic = string.format("audio/%s/all/%s",
  472. g_local_id, string.sub(tostring(mcu.ticks()), -4, -1))
  473. publish_message(string.format("ctrl/uplink/%s/0003", g_local_id),
  474. json.encode({["topic"] = g_s_topic, ["type"] = g_s_type}))
  475. sys.timerStart(extalk.wait_speech_to, 15000)
  476. else
  477. -- 一对一模式
  478. log.info("向", id, "主动发起对讲")
  479. if id == g_local_id then
  480. log.error("不允许本机给本机拨打电话")
  481. return false
  482. end
  483. g_state = SP_T_CONNECTING
  484. g_remote_id = id
  485. g_s_mode = airtalk.MODE_PERSON
  486. g_s_type = "one-on-one"
  487. g_s_topic = string.format("audio/%s/%s/%s",
  488. g_local_id, id, string.sub(tostring(mcu.ticks()), -4, -1))
  489. publish_message(string.format("ctrl/uplink/%s/0003", g_local_id),
  490. json.encode({["topic"] = g_s_topic, ["type"] = g_s_type}))
  491. sys.timerStart(extalk.wait_speech_to, 15000)
  492. end
  493. return true
  494. end
  495. -- 结束对讲
  496. function extalk.stop()
  497. if g_state ~= SP_T_CONNECTING and g_state ~= SP_T_CONNECTED then
  498. log.info("没有对讲,当前状态:", g_state)
  499. return false
  500. end
  501. log.info("主动断开对讲")
  502. extalk.speech_off(true, false)
  503. return true
  504. end
  505. return extalk