talk.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. -- talk.lua
  2. local talk = {}
  3. dnsproxy = require("dnsproxy")
  4. dhcpsrv = require("dhcpsrv")
  5. httpplus = require("httpplus")
  6. local run_state = false
  7. local airaudio = require "airaudio"
  8. local input_method = require "InputMethod"
  9. local input_key = false
  10. -- 初始化fskv
  11. local speech_topic = nil
  12. local mqtt_host = "lbsmqtt.openluat.com"
  13. local mqtt_port = 1886
  14. local mqtt_isssl = false
  15. local client_id = nil
  16. local user_name = "mqtt_hz_test_1"
  17. local password = "Ck8WpNCp"
  18. local mqttc = nil
  19. local message = ""
  20. local event = ""
  21. local talk_state = ""
  22. local mqttc = nil
  23. local function airtalk_event_cb(event, param)
  24. log.info("talk event", event, param)
  25. event = event
  26. end
  27. -- MQTT回调函数
  28. local function mqtt_cb(mqtt_client, event, data, payload)
  29. log.info("mqtt", "event", event, mqtt_client, data, payload)
  30. -- 连接成功时订阅主题
  31. end
  32. local function init_talk()
  33. log.info("init_call")
  34. airaudio.init()
  35. client_id = mobile.imei()
  36. mqttc = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl, {rxSize = 4096})
  37. airtalk.config(airtalk.PROTOCOL_DEMO_MQTT_8K, mqttc, 200) -- 缓冲至少200ms播放
  38. airtalk.on(airtalk_event_cb)
  39. airtalk.start(client_id, speech_topic)
  40. mqttc:auth(client_id,user_name,password) -- client_id必填,其余选填
  41. mqttc:keepalive(240) -- 默认值240s
  42. mqttc:autoreconn(true, 3000) -- 自动重连机制
  43. mqttc:debug(false)
  44. mqttc:on(mqtt_cb)
  45. -- mqttc自动处理重连, 除非自行关闭
  46. mqttc:connect()
  47. end
  48. -- 重新初始化对讲函数
  49. local function reinit_talk()
  50. log.info("talk", "重新初始化对讲")
  51. -- 安全停止对讲
  52. if airtalk and airtalk.stop then
  53. airtalk.stop()
  54. end
  55. if mqttc then
  56. mqttc:close()
  57. end
  58. -- 重新初始化对讲
  59. sys.taskInit(init_talk)
  60. end
  61. -- 输入法回调函数
  62. local function submit_callback(input_text)
  63. if input_text and #input_text > 0 then
  64. speech_topic = input_text
  65. fskv.set("talk_channel", input_text) -- 保存频道名称到fskv
  66. log.info("talk", "使用主题:", fskv.get("talk_channel"))
  67. input_key = false
  68. -- 重新初始化对讲
  69. sys.taskInit(reinit_talk)
  70. end
  71. end
  72. function talk.run()
  73. log.info("talk.run")
  74. lcd.setFont(lcd.font_opposansm12_chinese)
  75. run_state = true
  76. speech_topic = fskv.get("talk_channel")
  77. log.info("get speech_topic",speech_topic)
  78. if speech_topic then
  79. sys.taskInit(init_talk)
  80. end
  81. while run_state do
  82. sys.wait(100)
  83. if input_method.is_active() then
  84. input_method.periodic_refresh()
  85. else
  86. lcd.clear(_G.bkcolor)
  87. if speech_topic == nil then
  88. lcd.drawStr(0, 80, "输入任意手机号,并保证所有终端/平台一致")
  89. lcd.drawStr(0, 100, "方案介绍:airtalk.luatos.com")
  90. lcd.drawStr(0, 120, "平台端网址:airtalk.openluat.com")
  91. lcd.showImage(130, 250, "/luadb/input_topic.jpg")
  92. else
  93. lcd.drawStr(0, 80, "对讲测试,测试topic:"..speech_topic )
  94. lcd.drawStr(0, 100, "方案介绍:airtalk.luatos.com")
  95. lcd.drawStr(0, 120, "平台端网址:airtalk.openluat.com")
  96. lcd.drawStr(0, 140, "所有终端或者网页都要使用同一个topic")
  97. lcd.drawStr(0, 160, talk_state)
  98. lcd.drawStr(0, 180, "事件:" .. event)
  99. -- 显示输入法入口按钮
  100. lcd.showImage(130, 250, "/luadb/input_topic.jpg")
  101. lcd.showImage(130, 350, "/luadb/start.jpg")
  102. lcd.showImage(130, 397, "/luadb/stop.jpg")
  103. lcd.showImage(0, 448, "/luadb/Lbottom.jpg")
  104. -- log.info("flush ui")
  105. end
  106. lcd.showImage(20,360,"/luadb/back.jpg")
  107. lcd.flush()
  108. end
  109. if not run_state then
  110. return true
  111. end
  112. end
  113. end
  114. local function stop_talk()
  115. talk_state = "语音停止采集"
  116. airtalk.uplink(false)
  117. end
  118. local function start_talk()
  119. talk_state = "语音采集上传中"
  120. airtalk.uplink(true)
  121. end
  122. local function start_input()
  123. input_key = true
  124. input_method.init(false, "talk", submit_callback) -- 直接传递函数
  125. end
  126. function talk.tp_handal(x, y, event)
  127. if input_key then
  128. input_method.process_touch(x, y)
  129. else
  130. if x > 20 and x < 100 and y > 360 and y < 440 then
  131. run_state = false
  132. elseif x > 130 and x < 230 and y > 350 and y < 397 then
  133. sysplus.taskInitEx(start_talk, "start_talk")
  134. elseif x > 130 and x < 230 and y > 397 and y < 444 then
  135. sysplus.taskInitEx(stop_talk, "stop_talk")
  136. elseif x > 130 and x < 230 and y > 250 and y < 300 then
  137. sysplus.taskInitEx(start_input,"start_input")
  138. end
  139. end
  140. end
  141. return talk