Commit 5f2c1796 by guanghui.cui

1、增加判断预警类型,用于处理非法请求 2、ods长连接socket对象定义为全局,解决设置pos优先级的问题

parent 13eea5e2
...@@ -919,7 +919,9 @@ bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result ...@@ -919,7 +919,9 @@ bool JsonModule::convertDataOds2Pos(const std::string &data, std::string &result
result = _convertToNewOrderJson(order); result = _convertToNewOrderJson(order);
} }
} }
else {
rlt = false; //如果推送报文不是以上类型,则返回失败
}
return rlt; return rlt;
} }
...@@ -1717,7 +1719,7 @@ void JsonModule::_getStockWarnObj(IN const char* json,OUT stockWarnObj &warn_obj ...@@ -1717,7 +1719,7 @@ void JsonModule::_getStockWarnObj(IN const char* json,OUT stockWarnObj &warn_obj
GetJsonStringSafe(document, "barCounter"); GetJsonStringSafe(document, "barCounter");
detail.source = GetJsonStringSafe(document, "channel"); detail.source = GetJsonStringSafe(document, "channel");
GetJsonIntSafe(document, "id"); GetJsonStringSafe(document, "id");
GetJsonStringSafe(document, "memo"); GetJsonStringSafe(document, "memo");
GetJsonStringSafe(document, "pcId"); GetJsonStringSafe(document, "pcId");
GetJsonStringSafe(document, "posId"); GetJsonStringSafe(document, "posId");
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.6 beta 3" //版本号 #define VERSION "1.0.6 beta 5" //版本号
std::string g_init_data; std::string g_init_data;
std::string g_init_data_ods_back; std::string g_init_data_ods_back;
...@@ -36,6 +36,7 @@ int pos_listen_port; ...@@ -36,6 +36,7 @@ int pos_listen_port;
bool bInitDone=false; //初始化完成 bool bInitDone=false; //初始化完成
bool bPriorityDone=true; //设置POS优先级 bool bPriorityDone=true; //设置POS优先级
TCPClient longConnectionOds; //长连接tcp对象
//函数,订单信息发送给pos //函数,订单信息发送给pos
bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json); bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json);
...@@ -100,6 +101,7 @@ void* listen_pos_func(void* arg) ...@@ -100,6 +101,7 @@ void* listen_pos_func(void* arg)
jsonTool.convertInitDataPos2Ods(posRequestData,g_init_data); jsonTool.convertInitDataPos2Ods(posRequestData,g_init_data);
//jsonTool.getPosResponseData(100, "successful!", responseData); //jsonTool.getPosResponseData(100, "successful!", responseData);
LOG(INFO) <<"INIT PLUGIN ===>> ODS:"<<g_init_data.c_str(); LOG(INFO) <<"INIT PLUGIN ===>> ODS:"<<g_init_data.c_str();
longConnectionOds.close(); //关闭长连接,等待重连
//等待ods返回初始化结果 //等待ods返回初始化结果
while(true){ while(true){
if(!g_init_data_ods_back.empty()){ if(!g_init_data_ods_back.empty()){
...@@ -236,8 +238,9 @@ int main(int argc,char *argv[]) ...@@ -236,8 +238,9 @@ int main(int argc,char *argv[])
////--------test------------ ////--------test------------
//JsonModule jsonTest; //JsonModule jsonTest;
//std::string json = "{\"alertTime\":1534416551000,\"channel\":\"201\",\"id\":117049102732151083,\"pcId\":\"10\",\"qty\":0,\"saftQty\":1,\"selfMacId\":\"2027490121\",\"shelfNo\":\"20274901210405\",\"sku\":\"20627836\",\"storeId\":\"202749\",\"pk_id\":1}"; //std::string json = "{\"alertTime\":1534416551000,\"channel\":\"201\",\"id\":117049102732151083,\"pcId\":\"10\",\"qty\":0,\"saftQty\":1,\"selfMacId\":\"2027490121\",\"shelfNo\":\"20274901210405\",\"sku\":\"20627836\",\"storeId\":\"202749\",\"pk_id\":1}";
//std::string json = "{\"key\":\"网络故障\",\"notifyDate\":1535240399875,\"notifyDesc\":\"网络故障\",\"notifyType\":0,\"storeId\":\"403219\",\"pk_id\":415,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018,\"fm_cmd\":1018}";
//std::string rltTest; //std::string rltTest;
//jsonTest.convertDataOds2Pos(json, rltTest); //bool bTest = jsonTest.convertDataOds2Pos(json, rltTest);
////------------------------- ////-------------------------
if (2 == argc && 0 == strcmp("-v", argv[1])) { if (2 == argc && 0 == strcmp("-v", argv[1])) {
printf("version:%s\n", VERSION); printf("version:%s\n", VERSION);
...@@ -307,7 +310,7 @@ int main(int argc,char *argv[]) ...@@ -307,7 +310,7 @@ int main(int argc,char *argv[])
#endif // WIN32 #endif // WIN32
TCPClient ods; //TCPClient ods;
JsonModule jsonTool; JsonModule jsonTool;
while(true) while(true)
...@@ -317,12 +320,12 @@ int main(int argc,char *argv[]) ...@@ -317,12 +320,12 @@ int main(int argc,char *argv[])
std::string responseData; std::string responseData;
// 判断是否成功连接ODS // 判断是否成功连接ODS
if( !ods.isValid() ) if( !longConnectionOds.isValid() )
{ {
if( ods.doConnect(ods_push_port, ods_ip.c_str()) ) if(longConnectionOds.doConnect(ods_push_port, ods_ip.c_str()) )
{ {
LOG(INFO) << "connect ODS successful"; LOG(INFO) << "connect ODS successful";
ods.setSocketTimeout(ods_socket_timeout); //设置超时时间10s longConnectionOds.setSocketTimeout(ods_socket_timeout); //设置超时时间10s
// 注册socket信息 // 注册socket信息
while(true) while(true)
{ {
...@@ -330,20 +333,20 @@ int main(int argc,char *argv[]) ...@@ -330,20 +333,20 @@ int main(int argc,char *argv[])
//g_init_data = "{\"fm_cmd\":1000,\"listen_port\":24445,\"storeId\":\"202557\",\"pos_id\":\"1\",\"operator_id\":\"17076352\",\"is_master\":true,\"ver\":1}"; //g_init_data = "{\"fm_cmd\":1000,\"listen_port\":24445,\"storeId\":\"202557\",\"pos_id\":\"1\",\"operator_id\":\"17076352\",\"is_master\":true,\"ver\":1}";
if(!g_init_data.empty()) if(!g_init_data.empty())
{ {
if( ods.send(g_init_data) ) if(longConnectionOds.send(g_init_data) )
{ {
ods.receive(odsPushData); longConnectionOds.receive(odsPushData);
g_init_data_ods_back=odsPushData; g_init_data_ods_back=odsPushData;
LOG(INFO)<<"INIT ODS ===>> PLUGIN:"<<odsPushData.data(); LOG(INFO)<<"INIT ODS ===>> PLUGIN:"<<odsPushData.data();
if (odsPushData.empty()|| !jsonTool.isInitBackValid(odsPushData.data())) { if (odsPushData.empty()|| !jsonTool.isInitBackValid(odsPushData.data())) {
LOG(INFO) << "invalid data,close connect"; LOG(INFO) << "invalid data,close connect";
ods.close(); //当连接到负载均衡,并没有和ods建立连接的时候。返回值为空,所以应该关闭重新连接 longConnectionOds.close(); //当连接到负载均衡,并没有和ods建立连接的时候。返回值为空,所以应该关闭重新连接
os_sleep(10); os_sleep(10);
} }
int count=0; int count=0;
while(!bInitDone){ while(!bInitDone){
LOG(INFO)<<"wait for init done"; LOG(INFO)<<"wait for init done";
if( !ods.isValid() ){ if( !longConnectionOds.isValid() ){
LOG(INFO)<<"ods is not valid"; LOG(INFO)<<"ods is not valid";
os_sleep(1); //防止在ods挂掉后狂刷日志 os_sleep(1); //防止在ods挂掉后狂刷日志
break; break;
...@@ -352,7 +355,7 @@ int main(int argc,char *argv[]) ...@@ -352,7 +355,7 @@ int main(int argc,char *argv[])
count++; count++;
//如果10s没有初始化完成,则跳出循环,重新连接 //如果10s没有初始化完成,则跳出循环,重新连接
if(10==count){ if(10==count){
ods.close(); longConnectionOds.close();
break; break;
} }
} }
...@@ -375,15 +378,15 @@ int main(int argc,char *argv[]) ...@@ -375,15 +378,15 @@ int main(int argc,char *argv[])
if(!bPriorityDone){ if(!bPriorityDone){
LOG(INFO)<<"ODS set priority send:"<<g_set_pos_priority.data(); LOG(INFO)<<"ODS set priority send:"<<g_set_pos_priority.data();
if( ods.send(g_set_pos_priority) ) if(longConnectionOds.send(g_set_pos_priority) )
{ {
ods.receive(odsPushData); longConnectionOds.receive(odsPushData);
g_set_pos_priority_ods_back=odsPushData; g_set_pos_priority_ods_back=odsPushData;
LOG(INFO)<<"ODS set priority back:"<<odsPushData.data(); LOG(INFO)<<"ODS set priority back:"<<odsPushData.data();
} }
} }
if( ods.receive(odsPushData) ) if(longConnectionOds.receive(odsPushData) )
{ {
LOG(INFO) << "---------------- ODS ===>> PLUGIN ---------------"; LOG(INFO) << "---------------- ODS ===>> PLUGIN ---------------";
LOG(INFO) << odsPushData; LOG(INFO) << odsPushData;
...@@ -398,6 +401,9 @@ int main(int argc,char *argv[]) ...@@ -398,6 +401,9 @@ int main(int argc,char *argv[])
jsonTool.getOdsResponseData(101, "send to pos failed or receive data illegal!", odsPushData, responseData); jsonTool.getOdsResponseData(101, "send to pos failed or receive data illegal!", odsPushData, responseData);
} }
} }
else {
jsonTool.getOdsResponseData(101, "convert data to [POS] format failed,empty json!", odsPushData, responseData);
}
}else }else
{ {
...@@ -407,12 +413,12 @@ int main(int argc,char *argv[]) ...@@ -407,12 +413,12 @@ int main(int argc,char *argv[])
//test //test
//responseData="{\"fm_cmd\": 2,\"order_id\" : \"1234\",\"status\" : 1,\"channel\" : \"1234\",\"status_code\" : 100,\"msg\" : \"\"}"; //responseData="{\"fm_cmd\": 2,\"order_id\" : \"1234\",\"status\" : 1,\"channel\" : \"1234\",\"status_code\" : 100,\"msg\" : \"\"}";
bool rlt = ods.send(responseData); bool rlt = longConnectionOds.send(responseData);
LOG(INFO)<<"send to ods result:"<<rlt; LOG(INFO)<<"send to ods result:"<<rlt;
} }
else if(1==dataType){ else if(1==dataType){
if(!bInitDone){ if(!bInitDone){
ods.setValid(false); longConnectionOds.setValid(false);
LOG(INFO)<<"maybe pos restart,reconnect!!!"; LOG(INFO)<<"maybe pos restart,reconnect!!!";
} }
} }
...@@ -422,7 +428,7 @@ int main(int argc,char *argv[]) ...@@ -422,7 +428,7 @@ int main(int argc,char *argv[])
}else }else
{ {
ods.close(); longConnectionOds.close();
os_sleep(2); //防止在ods挂掉后狂刷日志 os_sleep(2); //防止在ods挂掉后狂刷日志
LOG(INFO) << "recv ODS pushData failed or timeout"; LOG(INFO) << "recv ODS pushData failed or timeout";
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment