Skip to content

Commit

Permalink
优化超时
Browse files Browse the repository at this point in the history
  • Loading branch information
qlwz committed Feb 1, 2020
1 parent 3551f88 commit 3d94d7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string.h>
#include "cJSON.h"

#define VERSION "2020.01.04.1100"
#define VERSION "2020.02.01.2000"

extern char httpPrefix[100];
extern char xiaoaiSN[20];
Expand Down
13 changes: 12 additions & 1 deletion src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ void http_ev_handler(struct mg_connection *nc, int ev, void *ev_data)
switch (ev)
{
case MG_EV_CONNECT:
mg_set_timer(nc, 0); // Clear connect timer
if (*(int *)ev_data != 0)
{
printf("connect() failed: %s\n", (*(int *)ev_data));
printf("Error connecting to server, error code: %d\n", (*(int *)ev_data));
state->exitFlag = 1;
}
mg_set_timer(nc, mg_time() + 4);
break;
case MG_EV_HTTP_REPLY:
mg_set_timer(nc, 0); // Clear connect timer
state->httpCode = hm->resp_code;
nc->flags |= MG_F_CLOSE_IMMEDIATELY;

Expand All @@ -22,7 +25,14 @@ void http_ev_handler(struct mg_connection *nc, int ev, void *ev_data)
state->data[hm->body.len] = 0;
state->exitFlag = 1;
break;
case MG_EV_TIMER:
printf("Connect timeout\n");
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
state->httpCode = 504;
state->exitFlag = 1;
break;
case MG_EV_CLOSE:
mg_set_timer(nc, 0); // Clear connect timer
if (state->exitFlag == 0)
{
printf("Server closed connection\n");
Expand All @@ -41,6 +51,7 @@ void connectHhttp(struct httpState *state, const char *url, const char *contentT

mg_mgr_init(&mgr, NULL);
struct mg_connection *nc = mg_connect_http(&mgr, http_ev_handler, url, contentType, params); //POST模拟表单提交
mg_set_timer(nc, mg_time() + 2);
nc->user_data = state;
while (state->exitFlag == 0)
{
Expand Down
43 changes: 22 additions & 21 deletions src/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int doW()
char *resBuffer = NULL, *asrBuffer = NULL;

resBuffer = readFile("/tmp/mipns/mibrain/mibrain_txt_RESULT_NLP.log");
if (resBuffer == NULL)
if (resBuffer == NULL || strlen(resBuffer) == 0)
{
return -1;
}
Expand Down Expand Up @@ -89,15 +89,15 @@ int doW()
uint8_t result;
size_t i;
// 若干循环,直到成功一次直接跳出
for (i = 0; i < 200; i++)
for (i = 0; i < 300; i++)
{
result = playerPlayOperation(strcmp(domain, "scenes") == 0 ? "stop" : "resume");
if (result == 0)
{
printf("== 停止成功\n");
break;
}
usleep(50);
usleep(10);
}

doStatus();
Expand All @@ -118,9 +118,16 @@ int doW()
url_encode(&mb, resBuffer);
mbuf_append(&mb, "&asr=", 5);
url_encode(&mb, asrBuffer);
mb.buf[mb.len] = '\0';

DEL(resBuffer);
DEL(asrBuffer);

if (playStatus == 1)
{
shellcmdNoResult("mphelper pause > /dev/null 2>&1");
}

// 转发asr和res给服务端接口,远端可以处理控制逻辑完成后返回需要播报的TTS文本
// 2秒连接超时,4秒传输超时

Expand All @@ -133,39 +140,33 @@ int doW()
// 如果远端返回内容不为空则用TTS播报之
if (strlen(state->data) > 0 && strlen(state->data) < 450)
{
if (playStatus == 1)
{
shellcmdNoResult("mphelper pause > /dev/null 2>&1");
usleep(200 * 1000);
}

printf("== 播报TTS | TTS内容: %s\n", state->data);
char resBuf[512];
sprintf(resBuf, "ubus call mibrain text_to_speech \"{\\\"text\\\":\\\"%s\\\",\\\"save\\\":0}\" > /dev/null 2>&1", state->data);
shellcmdNoResult(resBuf);
usleep(200 * 1000);
//usleep(200 * 1000);
// 最长20秒TTS播报时间,20秒内如果播报完成跳出
for (i = 0; i < 20; i++)
for (i = 0; i < 100; i++)
{
doStatus();
if (lastPlayerType != 1)
{
printf("== 播报TTS结束\n");
break;
}
usleep(1000 * 1000);
}

//如果之前音乐是播放的则接着播放
if (playStatus == 1)
{
//这里延迟一秒是因为前面处理如果太快,可能引起恢复播放不成功
usleep(1000 * 1000);
printf("== 继续播放音乐\n");
shellcmdNoResult("mphelper play > /dev/null 2>&1");
usleep(200 * 1000);
}
}
}

//如果之前音乐是播放的则接着播放
if (playStatus == 1)
{
//这里延迟一秒是因为前面处理如果太快,可能引起恢复播放不成功
usleep(500 * 1000);
printf("== 继续播放音乐\n");
shellcmdNoResult("mphelper play > /dev/null 2>&1");
}
DEL(state->data);
DEL(state);

Expand Down

0 comments on commit 3d94d7b

Please sign in to comment.