|
|
@@ -76,6 +76,24 @@ local function check_param(param, expected_type, name)
|
|
|
return true
|
|
|
end
|
|
|
|
|
|
+-- MQTT消息发布函数,集中处理所有发布操作并打印日志
|
|
|
+local function publish_message(topic, payload)
|
|
|
+ if g_mqttc then
|
|
|
+ log.info("MQTT发布 - 主题:", topic, "内容:", payload)
|
|
|
+ g_mqttc:publish(topic, payload)
|
|
|
+ else
|
|
|
+ log.error("MQTT客户端未初始化,无法发布消息")
|
|
|
+ end
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
+-- 对讲超时处理
|
|
|
+function extalk.wait_speech_to()
|
|
|
+ log.info("主动请求对讲超时无应答")
|
|
|
+ extalk.speech_off(true, false)
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
-- 发送鉴权消息
|
|
|
local function auth()
|
|
|
if g_state == SP_T_NO_READY and g_mqttc then
|
|
|
@@ -84,19 +102,22 @@ local function auth()
|
|
|
["key"] = extalk_configs_local.key,
|
|
|
["device_type"] = 1
|
|
|
})
|
|
|
- g_mqttc:publish(topic, payload)
|
|
|
+ publish_message(topic, payload)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
-- 发送心跳消息
|
|
|
local function heart()
|
|
|
- if g_state == SP_T_CONNECTED and g_mqttc then
|
|
|
+ if g_mqttc then
|
|
|
+ adc.open(adc.CH_VBAT)
|
|
|
+ local vbat = adc.get(adc.CH_VBAT)
|
|
|
+ adc.close(adc.CH_VBAT)
|
|
|
local topic = string.format("ctrl/uplink/%s/0005", g_local_id)
|
|
|
local payload = json.encode({
|
|
|
- ["from"] = g_local_id,
|
|
|
- ["to"] = g_remote_id
|
|
|
+ ["csq"] = mobile.csq(),
|
|
|
+ ["battery"] = vbat
|
|
|
})
|
|
|
- g_mqttc:publish(topic, payload)
|
|
|
+ publish_message(topic, payload)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
@@ -109,12 +130,12 @@ local function speech_on(ssrc, sample)
|
|
|
log.info("对讲模式", g_s_mode)
|
|
|
airtalk.speech(true, g_s_mode, sample)
|
|
|
sys.sendMsg(AIRTALK_TASK_NAME, MSG_SPEECH_ON_IND, true)
|
|
|
- sys.timerLoopStart(heart, extalk_configs_local.heart_break_time * 1000)
|
|
|
- sys.timerStopAll(wait_speech_to)
|
|
|
+ -- sys.timerLoopStart(heart, extalk_configs_local.heart_break_time * 1000)
|
|
|
+ sys.timerStopAll(extalk.wait_speech_to)
|
|
|
end
|
|
|
|
|
|
-- 结束对讲
|
|
|
-local function speech_off(need_upload, need_ind)
|
|
|
+function extalk.speech_off(need_upload, need_ind)
|
|
|
if g_state == SP_T_CONNECTED then
|
|
|
g_mqttc:unsubscribe(g_s_topic)
|
|
|
airtalk.speech(false)
|
|
|
@@ -123,12 +144,12 @@ local function speech_off(need_upload, need_ind)
|
|
|
|
|
|
g_state = SP_T_IDLE
|
|
|
sys.timerStopAll(auth)
|
|
|
- sys.timerStopAll(heart)
|
|
|
- sys.timerStopAll(wait_speech_to)
|
|
|
+
|
|
|
+ sys.timerStopAll(extalk.wait_speech_to)
|
|
|
|
|
|
if need_upload and g_mqttc then
|
|
|
local topic = string.format("ctrl/uplink/%s/0004", g_local_id)
|
|
|
- g_mqttc:publish(topic, json.encode({["to"] = g_remote_id}))
|
|
|
+ publish_message(topic, json.encode({["to"] = g_remote_id}))
|
|
|
end
|
|
|
|
|
|
if need_ind then
|
|
|
@@ -136,11 +157,6 @@ local function speech_off(need_upload, need_ind)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
--- 对讲超时处理
|
|
|
-local function wait_speech_to()
|
|
|
- log.info("主动请求对讲超时无应答")
|
|
|
- speech_off(true, false)
|
|
|
-end
|
|
|
|
|
|
-- 命令处理:请求对讲应答
|
|
|
local function handle_speech_response(obj)
|
|
|
@@ -171,7 +187,7 @@ local function handle_incoming_call(obj)
|
|
|
["topic"] = obj and obj["topic"] or "",
|
|
|
["info"] = "无效的请求参数"
|
|
|
}
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
return
|
|
|
end
|
|
|
|
|
|
@@ -183,7 +199,7 @@ local function handle_incoming_call(obj)
|
|
|
["topic"] = obj["topic"],
|
|
|
["info"] = "device is busy"
|
|
|
}
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
return
|
|
|
end
|
|
|
|
|
|
@@ -197,7 +213,7 @@ local function handle_incoming_call(obj)
|
|
|
["topic"] = obj["topic"],
|
|
|
["info"] = "topic error"
|
|
|
}
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
return
|
|
|
end
|
|
|
|
|
|
@@ -242,7 +258,7 @@ local function handle_incoming_call(obj)
|
|
|
end
|
|
|
|
|
|
-- 发送响应
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8102", g_local_id), json.encode(response))
|
|
|
end
|
|
|
|
|
|
-- 命令处理:对端挂断
|
|
|
@@ -255,13 +271,13 @@ local function handle_remote_hangup(obj)
|
|
|
log.info("0103", obj, obj["type"], g_s_type)
|
|
|
if obj and obj["type"] == g_s_type then
|
|
|
response = {["result"] = SUCC, ["info"] = ""}
|
|
|
- speech_off(false, true)
|
|
|
+ extalk.speech_off(false, true)
|
|
|
else
|
|
|
response = {["result"] = "failed", ["info"] = "type mismatch"}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8103", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8103", g_local_id), json.encode(response))
|
|
|
end
|
|
|
|
|
|
-- 命令处理:更新设备列表
|
|
|
@@ -274,16 +290,18 @@ local function handle_device_list_update(obj)
|
|
|
response = {["result"] = "failed", ["info"] = "json info error"}
|
|
|
end
|
|
|
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/8101", g_local_id), json.encode(response))
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/8101", g_local_id), json.encode(response))
|
|
|
end
|
|
|
|
|
|
-- 命令处理:鉴权结果
|
|
|
local function handle_auth_result(obj)
|
|
|
if obj and obj["result"] == SUCC then
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/0002", g_local_id), "") -- 更新列表
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/0002", g_local_id), "") -- 更新列表
|
|
|
+ sys.timerLoopStart(heart, extalk_configs_local.heart_break_time * 1000) -- 发起心跳
|
|
|
else
|
|
|
sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_IND, false,
|
|
|
"鉴权失败" .. (obj and obj["info"] or ""))
|
|
|
+ log.error("鉴权失败,可能是没有修改PRODUCT_KEY")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
@@ -344,7 +362,7 @@ local function mqtt_cb(mqttc, event, topic, payload)
|
|
|
"订阅失败" .. "ctrl/downlink/" .. g_local_id .. "/#")
|
|
|
end
|
|
|
elseif g_state == SP_T_CONNECTED and not topic then
|
|
|
- speech_off(false, true)
|
|
|
+ extalk.speech_off(false, true)
|
|
|
end
|
|
|
elseif event == "recv" then
|
|
|
local result = string.match(topic, g_dl_topic)
|
|
|
@@ -353,7 +371,7 @@ local function mqtt_cb(mqttc, event, topic, payload)
|
|
|
analyze_v1(result, topic, obj)
|
|
|
end
|
|
|
elseif event == "disconnect" then
|
|
|
- speech_off(false, true)
|
|
|
+ extalk.speech_off(false, true)
|
|
|
g_state = SP_T_NO_READY
|
|
|
elseif event == "error" then
|
|
|
log.error("MQTT错误发生")
|
|
|
@@ -363,7 +381,7 @@ end
|
|
|
-- 任务消息处理
|
|
|
local function task_cb(msg)
|
|
|
if msg[1] == MSG_SPEECH_CONNECT_TO then
|
|
|
- speech_off(true, false)
|
|
|
+ extalk.speech_off(true, false)
|
|
|
else
|
|
|
log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
|
|
|
end
|
|
|
@@ -375,7 +393,7 @@ local function airtalk_event_cb(event, param)
|
|
|
if event == airtalk.EVENT_ERROR then
|
|
|
if param == airtalk.ERROR_NO_DATA then
|
|
|
log.error("长时间没有收到音频数据")
|
|
|
- speech_off(true, true)
|
|
|
+ extalk.speech_off(true, true)
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
@@ -445,7 +463,7 @@ local function airtalk_mqtt_task()
|
|
|
if g_state ~= SP_T_CONNECTING and g_state ~= SP_T_CONNECTED then
|
|
|
log.info("没有对讲", g_state)
|
|
|
else
|
|
|
- speech_off(true, false)
|
|
|
+ extalk.speech_off(true, false)
|
|
|
end
|
|
|
elseif msg[1] == MSG_SPEECH_ON_IND then
|
|
|
if extalk_configs_local.state_cbfnc then
|
|
|
@@ -520,9 +538,9 @@ function extalk.start(id)
|
|
|
g_s_topic = string.format("audio/%s/all/%s",
|
|
|
g_local_id, string.sub(tostring(mcu.ticks()), -4, -1))
|
|
|
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/0003", g_local_id),
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/0003", g_local_id),
|
|
|
json.encode({["topic"] = g_s_topic, ["type"] = g_s_type}))
|
|
|
- sys.timerStart(wait_speech_to, 15000)
|
|
|
+ sys.timerStart(extalk.wait_speech_to, 15000)
|
|
|
else
|
|
|
-- 一对一模式
|
|
|
log.info("向", id, "主动发起对讲")
|
|
|
@@ -538,9 +556,9 @@ function extalk.start(id)
|
|
|
g_s_topic = string.format("audio/%s/%s/%s",
|
|
|
g_local_id, id, string.sub(tostring(mcu.ticks()), -4, -1))
|
|
|
|
|
|
- g_mqttc:publish(string.format("ctrl/uplink/%s/0003", g_local_id),
|
|
|
+ publish_message(string.format("ctrl/uplink/%s/0003", g_local_id),
|
|
|
json.encode({["topic"] = g_s_topic, ["type"] = g_s_type}))
|
|
|
- sys.timerStart(wait_speech_to, 15000)
|
|
|
+ sys.timerStart(extalk.wait_speech_to, 15000)
|
|
|
end
|
|
|
|
|
|
return true
|
|
|
@@ -554,7 +572,7 @@ function extalk.stop()
|
|
|
end
|
|
|
|
|
|
log.info("主动断开对讲")
|
|
|
- speech_off(true, false)
|
|
|
+ extalk.speech_off(true, false)
|
|
|
return true
|
|
|
end
|
|
|
|