|
@@ -1,7 +1,7 @@
|
|
|
-require "audio_config"
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-local mqttc = nil
|
|
|
|
|
|
|
+local speech_state = SP_T_IDLE
|
|
|
|
|
+local speech_mdoe
|
|
|
|
|
+local g_mqttc = nil
|
|
|
local client_id
|
|
local client_id
|
|
|
local downlink_topic
|
|
local downlink_topic
|
|
|
local topic_auth
|
|
local topic_auth
|
|
@@ -11,6 +11,8 @@ local topic_talk_stop
|
|
|
local topic_list_update_ack
|
|
local topic_list_update_ack
|
|
|
local topic_talk_start_ack
|
|
local topic_talk_start_ack
|
|
|
local topic_talk_stop_ack
|
|
local topic_talk_stop_ack
|
|
|
|
|
+local speech_topic
|
|
|
|
|
+local speech_sample
|
|
|
local dev_list
|
|
local dev_list
|
|
|
|
|
|
|
|
local function task_cb(msg)
|
|
local function task_cb(msg)
|
|
@@ -22,30 +24,103 @@ local function airtalk_event_cb(event, param)
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
local function next_auth()
|
|
local function next_auth()
|
|
|
- sys.sendMsg(AIRTALK_TASK_NAME, MSG_NET_READY)
|
|
|
|
|
|
|
+ if speech_state == SP_T_IDLE then
|
|
|
|
|
+ obj = {["key"] = PRODUCT_KEY, ["device_type"] = 1}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_auth, data)
|
|
|
|
|
+ mqttc:publish(topic_auth, data)
|
|
|
|
|
+ end
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
-local function mqtt_cb(mqtt_client, event, data, payload)
|
|
|
|
|
- log.info(event)
|
|
|
|
|
|
|
+local function mqtt_cb(mqttc, event, topic, payload)
|
|
|
|
|
+ -- log.info(event)
|
|
|
|
|
+ local msg,data,obj,speech_topic,speech_sample
|
|
|
if event == "conack" then
|
|
if event == "conack" then
|
|
|
-- 联上了
|
|
-- 联上了
|
|
|
- mqtt_client:subscribe("ctrl/downlink/" .. client_id .. "/#")--单主题订阅
|
|
|
|
|
|
|
+ mqttc:subscribe("ctrl/downlink/" .. client_id .. "/#")--单主题订阅
|
|
|
elseif event == "suback" then
|
|
elseif event == "suback" then
|
|
|
- sys.sendMsg(AIRTALK_TASK_NAME, MSG_NET_READY)
|
|
|
|
|
|
|
+ if speech_state == SP_T_IDLE then
|
|
|
|
|
+ obj = {["key"] = PRODUCT_KEY, ["device_type"] = 1}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_auth, data)
|
|
|
|
|
+ mqttc:publish(topic_auth, data)
|
|
|
|
|
+ sys.timerLoopStart(next_auth, 60000) --1分钟就尝试重新鉴权
|
|
|
|
|
+ elseif speech_state == SP_T_CONNECTING then
|
|
|
|
|
+ speech_state = SP_T_CONNECTED
|
|
|
|
|
+ obj = {["result"] = SUCC, ["topic"] = msg[2], ["info"] = ""}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_talk_start_ack, data)
|
|
|
|
|
+ mqttc:publish(topic_talk_start_ack, data)
|
|
|
|
|
+ airtalk.speech(true, speech_mdoe, speech_sample)
|
|
|
|
|
+ else
|
|
|
|
|
+
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
elseif event == "recv" then
|
|
elseif event == "recv" then
|
|
|
- local result = string.match(data, downlink_topic)
|
|
|
|
|
|
|
+ local result = string.match(topic, downlink_topic)
|
|
|
if result then
|
|
if result then
|
|
|
- log.info(data, payload)
|
|
|
|
|
|
|
+ log.info(topic, payload)
|
|
|
local obj,res,err = json.decode(payload)
|
|
local obj,res,err = json.decode(payload)
|
|
|
if result == "0102" then
|
|
if result == "0102" then
|
|
|
-
|
|
|
|
|
|
|
+ if res and obj["topic"] and obj["ssrc"] and obj["audio_code"] then
|
|
|
|
|
+ if speech_state == SP_T_CONNECTED then
|
|
|
|
|
+ obj = {["result"] = "failed", ["topic"] = msg[2], ["info"] = "last speech is running"}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_talk_start_ack, data)
|
|
|
|
|
+ mqttc:publish(topic_talk_start_ack, data)
|
|
|
|
|
+ airtalk.speech(true, speech_mdoe, speech_sample)
|
|
|
|
|
+ elseif speech_state == SP_T_DISCONNECTING then
|
|
|
|
|
+ obj = {["result"] = "failed", ["topic"] = msg[2], ["info"] = "last speech not stop"}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_talk_start_ack, data)
|
|
|
|
|
+ mqttc:publish(topic_talk_start_ack, data)
|
|
|
|
|
+ airtalk.speech(true, speech_mdoe, speech_sample)
|
|
|
|
|
+ else
|
|
|
|
|
+ speech_state = SP_T_CONNECTING
|
|
|
|
|
+ speech_topic = msg[2]
|
|
|
|
|
+ speech_sample = msg[4] == "amr-nb" and 8000 or 16000
|
|
|
|
|
+ mqttc:subscribe(speech_topic)
|
|
|
|
|
+ airtalk.set_topic(speech_topic)
|
|
|
|
|
+ airtalk.set_ssrc(msg[3])
|
|
|
|
|
+ speech_mdoe = airtalk.MODE_PERSON
|
|
|
|
|
+ end
|
|
|
|
|
+ else
|
|
|
|
|
+ obj = {["result"] = "failed", ["info"] = "json info error"}
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_talk_start_ack, data)
|
|
|
|
|
+ mqttc:publish(topic_talk_start_ack, data)
|
|
|
|
|
+ end
|
|
|
elseif result == "0103" then
|
|
elseif result == "0103" then
|
|
|
|
|
+ if speech_state == SP_T_IDLE then
|
|
|
|
|
+ obj = {["result"] = "failed", ["info"] = "no speech"}
|
|
|
|
|
+ else
|
|
|
|
|
+ obj = {["result"] = SUCC, ["info"] = ""}
|
|
|
|
|
+ speech_state = SP_T_IDLE
|
|
|
|
|
+ mqttc:unsubscribe(speech_topic)
|
|
|
|
|
+ airtalk.speech(false)
|
|
|
|
|
+ speech_topic = nil
|
|
|
|
|
+ end
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ mqttc:publish(topic_talk_stop_ack, data)
|
|
|
elseif result == "0101" then
|
|
elseif result == "0101" then
|
|
|
|
|
+ if res then
|
|
|
|
|
+ dev_list = obj["dev_list"]
|
|
|
|
|
+ for i=1,#dev_list do
|
|
|
|
|
+ log.info(dev_list[i]["id"],dev_list[i]["name"])
|
|
|
|
|
+ end
|
|
|
|
|
+ obj = {["result"] = SUCC, ["info"] = ""}
|
|
|
|
|
+ else
|
|
|
|
|
+ obj = {["result"] = "failed", ["info"] = "json info error"}
|
|
|
|
|
+ end
|
|
|
|
|
+ data = json.encode(obj)
|
|
|
|
|
+ log.info(topic_list_update_ack, data)
|
|
|
|
|
+ mqttc:publish(topic_list_update_ack, data)
|
|
|
elseif result == "8003" then
|
|
elseif result == "8003" then
|
|
|
elseif result == "8004" then
|
|
elseif result == "8004" then
|
|
|
elseif result == "8001" then
|
|
elseif result == "8001" then
|
|
|
if res and obj["result"] then
|
|
if res and obj["result"] then
|
|
|
- sys.sendMsg(AIRTALK_TASK_NAME, MSG_AUTH_ACK, obj["result"], obj["version"])
|
|
|
|
|
|
|
+ sys.timerLoopStart(next_auth, 3600000) --鉴权通过则60分钟后尝试重新鉴权
|
|
|
|
|
+ mqttc:publish(topic_list_update, "")
|
|
|
end
|
|
end
|
|
|
elseif result == "8002" then
|
|
elseif result == "8002" then
|
|
|
if res and obj["result"] == SUCC then
|
|
if res and obj["result"] == SUCC then
|
|
@@ -53,24 +128,28 @@ local function mqtt_cb(mqtt_client, event, data, payload)
|
|
|
for i=1,#dev_list do
|
|
for i=1,#dev_list do
|
|
|
log.info(dev_list[i]["id"],dev_list[i]["name"])
|
|
log.info(dev_list[i]["id"],dev_list[i]["name"])
|
|
|
end
|
|
end
|
|
|
|
|
+ sys.sendMsg(USER_TASK_NAME, MSG_READY)
|
|
|
end
|
|
end
|
|
|
else
|
|
else
|
|
|
end
|
|
end
|
|
|
decode_data = nil
|
|
decode_data = nil
|
|
|
end
|
|
end
|
|
|
result = nil
|
|
result = nil
|
|
|
|
|
+ data = nil
|
|
|
|
|
+ obj = nil
|
|
|
|
|
|
|
|
elseif event == "sent" then
|
|
elseif event == "sent" then
|
|
|
-- log.info("mqtt", "sent", "pkgid", data)
|
|
-- log.info("mqtt", "sent", "pkgid", data)
|
|
|
- -- elseif event == "disconnect" then
|
|
|
|
|
|
|
+ elseif event == "disconnect" then
|
|
|
|
|
+ speech_state = SP_T_IDLE
|
|
|
-- 非自动重连时,按需重启mqttc
|
|
-- 非自动重连时,按需重启mqttc
|
|
|
- -- mqtt_client:connect()
|
|
|
|
|
|
|
+ -- mqttc:connect()
|
|
|
else
|
|
else
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
function airtalk_mqtt_task()
|
|
function airtalk_mqtt_task()
|
|
|
- local msg,data,obj
|
|
|
|
|
|
|
+ local msg,data,obj,speech_topic,speech_sample
|
|
|
--client_id也可以自己设置
|
|
--client_id也可以自己设置
|
|
|
client_id = mobile.imei()
|
|
client_id = mobile.imei()
|
|
|
downlink_topic = "ctrl/downlink/" .. client_id .. "/(%w%w%w%w)"
|
|
downlink_topic = "ctrl/downlink/" .. client_id .. "/(%w%w%w%w)"
|
|
@@ -83,40 +162,43 @@ function airtalk_mqtt_task()
|
|
|
topic_talk_stop_ack = "ctrl/uplink/" .. client_id .."/8103"
|
|
topic_talk_stop_ack = "ctrl/uplink/" .. client_id .."/8103"
|
|
|
sys.timerLoopStart(next_auth, 900000)
|
|
sys.timerLoopStart(next_auth, 900000)
|
|
|
|
|
|
|
|
- audio_init()
|
|
|
|
|
-
|
|
|
|
|
- mqttc = mqtt.create(nil, "mqtt.airtalk.luatos.com", 1883, false, {rxSize = 32768})
|
|
|
|
|
- airtalk.config(airtalk.PROTOCOL_MQTT, mqttc, 200) -- 缓冲至少200ms播放
|
|
|
|
|
|
|
+ g_mqttc = mqtt.create(nil, "mqtt.airtalk.luatos.com", 1883, false, {rxSize = 32768})
|
|
|
|
|
+ airtalk.config(airtalk.PROTOCOL_MQTT, g_mqttc, 200) -- 缓冲至少200ms播放
|
|
|
airtalk.on(airtalk_event_cb)
|
|
airtalk.on(airtalk_event_cb)
|
|
|
airtalk.start()
|
|
airtalk.start()
|
|
|
|
|
|
|
|
- mqttc:auth(client_id,client_id,mobile.muid()) -- client_id必填,其余选填
|
|
|
|
|
- mqttc:keepalive(240) -- 默认值240s
|
|
|
|
|
- mqttc:autoreconn(true, 3000) -- 自动重连机制
|
|
|
|
|
- mqttc:debug(false)
|
|
|
|
|
- mqttc:on(mqtt_cb)
|
|
|
|
|
|
|
+ g_mqttc:auth(client_id,client_id,mobile.muid()) -- client_id必填,其余选填
|
|
|
|
|
+ g_mqttc:keepalive(240) -- 默认值240s
|
|
|
|
|
+ g_mqttc:autoreconn(true, 3000) -- 自动重连机制
|
|
|
|
|
+ g_mqttc:debug(false)
|
|
|
|
|
+ g_mqttc:on(mqtt_cb)
|
|
|
|
|
|
|
|
-- mqttc自动处理重连, 除非自行关闭
|
|
-- mqttc自动处理重连, 除非自行关闭
|
|
|
- mqttc:connect()
|
|
|
|
|
|
|
+ g_mqttc:connect()
|
|
|
while true do
|
|
while true do
|
|
|
msg = sys.waitMsg(AIRTALK_TASK_NAME)
|
|
msg = sys.waitMsg(AIRTALK_TASK_NAME)
|
|
|
- if type(msg) == 'table' and type(msg[1]) == "number" and msg[1] < MSG_QTY then
|
|
|
|
|
- if msg[1] == MSG_NET_READY then
|
|
|
|
|
- obj = {["key"] = PRODUCT_KEY, ["device_type"] = 1}
|
|
|
|
|
|
|
+ if type(msg) == 'table' and type(msg[1]) == "number" then
|
|
|
|
|
+ if msg[1] == MSG_PERSON_SPEECH_REQ then
|
|
|
|
|
+ -- if speech_state =
|
|
|
|
|
+ -- speech_state = SP_T_CONNECTING
|
|
|
|
|
+ -- obj = {["to"] = msg[2]}
|
|
|
|
|
+ -- data = json.encode(obj)
|
|
|
|
|
+ -- log.info(topic_talk_start, data)
|
|
|
|
|
+ -- mqttc:publish(topic_talk_start, data)
|
|
|
|
|
+ elseif msg[1] == MSG_SPEECH_STOP_REQ then
|
|
|
|
|
+ speech_state = SP_T_IDLE
|
|
|
|
|
+ mqttc:unsubscribe(speech_topic)
|
|
|
|
|
+ airtalk.speech(false)
|
|
|
|
|
+ speech_topic = nil
|
|
|
data = json.encode(obj)
|
|
data = json.encode(obj)
|
|
|
- log.info(topic_auth, data)
|
|
|
|
|
- mqttc:publish(topic_auth, data)
|
|
|
|
|
- sys.timerLoopStart(next_auth, 60000) --1分钟就尝试重新鉴权
|
|
|
|
|
- elseif msg[1] == MSG_AUTH_ACK then
|
|
|
|
|
- if msg[2] == SUCC then
|
|
|
|
|
- sys.timerLoopStart(next_auth, 3600000) --鉴权通过则60分钟后尝试重新鉴权
|
|
|
|
|
- mqttc:publish(topic_list_update, "")
|
|
|
|
|
- else
|
|
|
|
|
- log.info("auth", msg[2])
|
|
|
|
|
- end
|
|
|
|
|
|
|
+ log.info(topic_talk_stop, data)
|
|
|
|
|
+ mqttc:publish(topic_talk_stop, data)
|
|
|
end
|
|
end
|
|
|
obj = nil
|
|
obj = nil
|
|
|
|
|
+ else
|
|
|
|
|
+ log.info(type(msg), type(msg[1]))
|
|
|
end
|
|
end
|
|
|
|
|
+ msg = nil
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
|
|
|