1. 修改读写参数接口代码
This commit is contained in:
@@ -7,13 +7,119 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include "ToolUtils.h"
|
||||
#include "db_api/db_api.h"
|
||||
|
||||
|
||||
REGISTER_BIZSERVICE("F-ReadData",readDataSrv);
|
||||
|
||||
static bool readDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
/**
|
||||
* "reqdata":[
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000100"
|
||||
},
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000101"
|
||||
}
|
||||
]
|
||||
|
||||
"dataarray":[
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000100",
|
||||
"createdtime":"2021-05-10T08:18:28",
|
||||
"source":"app1",
|
||||
"databuff":"000102030405060708090A0B0C0D"
|
||||
},
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000101",
|
||||
"createdtime":"",
|
||||
"source":"",
|
||||
"databuff":""
|
||||
}
|
||||
]
|
||||
*
|
||||
*/
|
||||
static bool readDataDb(void *db_handle, const json &reqdata, json &dataarray)
|
||||
{
|
||||
return true;
|
||||
bool fRet=false;
|
||||
std::vector<std::string> sqllist;
|
||||
db_info_t db_info;
|
||||
json dataarray0;
|
||||
std::vector<db_result_t> results;
|
||||
|
||||
for (auto data:reqdata){
|
||||
db_info.tablename.clear();
|
||||
db_info.db=NULL;
|
||||
//fRet = find_db_api_from_handle(db_handle, afn, dataid, db_info);
|
||||
if (fRet){
|
||||
std::string sqlcmd;
|
||||
sqlcmd = "select ";
|
||||
sqlcmd += " metid, dataid, createdtime, source, databuff from ";
|
||||
sqlcmd += db_info.tablename;
|
||||
sqlcmd += " where ";
|
||||
sqlcmd += " metid=='";
|
||||
sqlcmd += data["metid"].get<std::string>();
|
||||
sqlcmd += "' and dataid=='";
|
||||
sqlcmd += data["dataid"].get<std::string>();
|
||||
sqlcmd += "'; ";
|
||||
sqllist.push_back(sqlcmd);
|
||||
fRet=true;
|
||||
|
||||
json value;
|
||||
value["metid"] = data["metid"];
|
||||
value["afn"] = data["afn"];
|
||||
value["dataid"] = data["dataid"];
|
||||
value["createdtime"] = "";
|
||||
value["source"] = "";
|
||||
value["databuff"] = "";
|
||||
dataarray0.push_back(value);
|
||||
|
||||
}else{
|
||||
json value;
|
||||
value["metid"] = data["metid"];
|
||||
value["afn"] = data["afn"];
|
||||
value["dataid"] = data["dataid"];
|
||||
value["createdtime"] = "";
|
||||
value["source"] = "";
|
||||
value["databuff"] = "";
|
||||
dataarray.push_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
if (fRet){
|
||||
if (db_info.db->batch_exec(sqllist, results)){
|
||||
json value;
|
||||
int i=0;
|
||||
for (auto r:results){
|
||||
value["metid"] = dataarray0[i]["metid"];
|
||||
value["afn"] = dataarray0[i]["afn"];
|
||||
value["dataid"] = dataarray0[i]["dataid"];
|
||||
if (r.result.status==0){
|
||||
value["createdtime"] = ToolUtils::getTimeStringWithFomat(r.row[0].stime) ;
|
||||
value["source"] = r.row[0].source ;
|
||||
value["databuff"] = r.row[0].data ;
|
||||
}else{
|
||||
value["createdtime"] = "";
|
||||
value["source"] = "";
|
||||
value["databuff"] = "";
|
||||
}
|
||||
dataarray.push_back(value);
|
||||
i++;
|
||||
}
|
||||
}else{
|
||||
dataarray += dataarray0;
|
||||
}
|
||||
fRet=true;
|
||||
}
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
void readDataSrv::Init(initparam_t *param)
|
||||
@@ -23,49 +129,42 @@ void readDataSrv::Init(initparam_t *param)
|
||||
|
||||
int readDataSrv::HandleMsg(msg_t msg, msg_t &outMsg, handparam_t *param) {
|
||||
//check param table
|
||||
json tmjson, repjson, datajson, dataarray, cdtjson, repjson1;
|
||||
json tmjson, repjson, datajson, cdtjson, repjson1;
|
||||
int iRet = 0;
|
||||
|
||||
try {
|
||||
tmjson = json::parse(msg.payload);
|
||||
|
||||
cdtjson = tmjson["data"]["reqdata"];
|
||||
bool fRet = readDataDb(param->pDbHandle, cdtjson, datajson);
|
||||
if (fRet) {
|
||||
for (auto vljson:datajson) {
|
||||
vljson["metid"] = tmjson["metid"];
|
||||
vljson["afn"] = "04";
|
||||
vljson["dataid"] = tmjson["dataid"];
|
||||
vljson["createdtime"] = ToolUtils::getTimeStringWithFomat(datajson["ctime"]);
|
||||
vljson["source"] = datajson["source"];
|
||||
vljson["databuff"] = datajson["databuff"];
|
||||
dataarray.push_back(vljson);
|
||||
}
|
||||
repjson["dataarray"] = dataarray;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
} else {
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
if(tmjson.contains("data") && tmjson["data"].contains("reqdata")){
|
||||
auto reqdata = tmjson["data"]["reqdata"];
|
||||
if (reqdata.is_array()){
|
||||
bool fRet = readDataDb(param->pDbHandle, reqdata, datajson);
|
||||
if (fRet) {
|
||||
repjson["dataarray"] = datajson;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
} else {
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
|
||||
msg_t msg_rep;
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
msg_rep.payload = repjson1.dump();
|
||||
outMsg = msg_rep;
|
||||
iRet = 1;
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
}else{
|
||||
iRet=1;
|
||||
}*/
|
||||
msg_t msg_rep;
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
msg_rep.payload = repjson1.dump();
|
||||
outMsg = msg_rep;
|
||||
iRet = 1;
|
||||
}else{
|
||||
std::cerr << "json not contain reqdata is not array !!! error " << std::endl;
|
||||
}
|
||||
|
||||
}else{
|
||||
std::cerr << "json not contain reqdata error %s" << msg.payload << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
catch (json::parse_error &ex) {
|
||||
|
||||
@@ -5,12 +5,100 @@
|
||||
#include "bizservice.h"
|
||||
#include "f-readdata/normalDataDB.h"
|
||||
#include "ToolUtils.h"
|
||||
#include "db_api/db_api.h"
|
||||
|
||||
|
||||
static bool writeDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
/**
|
||||
* "dataarray":
|
||||
[
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000100",
|
||||
"databuff":"000102030405060708090A0B0C0D"
|
||||
},
|
||||
{
|
||||
"metid":"0",
|
||||
"afn":"04",
|
||||
"dataid":"E0000101",
|
||||
"databuff":"000102030405060708090A0B0C0D"
|
||||
}
|
||||
]
|
||||
|
||||
"rtnmsg": [
|
||||
{"metid":"0", "afn":"04", "dataid":"E0000100", "errmsg":"0" },
|
||||
{"metid":"0", "afn":"04", "dataid":"E0000101", "errmsg":"1" }
|
||||
]
|
||||
*
|
||||
*/
|
||||
static bool writeDataDb(void *db_handle, std::string src, const json &dataarray, json &rtnmsg)
|
||||
{
|
||||
bool fRet=false;
|
||||
std::vector<std::string> sqllist;
|
||||
db_info_t db_info;
|
||||
json rtnmsg0;
|
||||
std::vector<db_result_t> results;
|
||||
|
||||
return true;
|
||||
for (auto data:dataarray){
|
||||
db_info.tablename.clear();
|
||||
db_info.db=NULL;
|
||||
//fRet = find_db_api_from_handle(db_handle, afn, dataid, db_info);
|
||||
if (fRet){
|
||||
std::string sqlcmd;
|
||||
sqlcmd = "insert into ";
|
||||
sqlcmd += db_info.tablename;
|
||||
sqlcmd += " (metid, dataid, source,ctime, databuff )" ;
|
||||
sqlcmd += " values ";
|
||||
sqlcmd += "( " ;
|
||||
sqlcmd += data["metid"].get<std::string>();
|
||||
sqlcmd += ", ";
|
||||
sqlcmd += data["dataid"].get<std::string>();
|
||||
sqlcmd += ", ";
|
||||
sqlcmd += src;
|
||||
sqlcmd += ", ";
|
||||
sqlcmd += std::to_string(time(NULL));
|
||||
sqlcmd += ", ";
|
||||
sqlcmd += data["databuff"].get<std::string>();
|
||||
sqlcmd += " ); ";
|
||||
sqllist.push_back(sqlcmd);
|
||||
fRet=true;
|
||||
|
||||
json value;
|
||||
value["metid"] = data["metid"];
|
||||
value["afn"] = data["afn"];
|
||||
value["dataid"] = data["dataid"];
|
||||
value["errmsg"] = "1";
|
||||
rtnmsg0.push_back(value);
|
||||
}else{
|
||||
//to do packet fail result;
|
||||
json value;
|
||||
value["metid"] = data["metid"];
|
||||
value["afn"] = data["afn"];
|
||||
value["dataid"] = data["dataid"];
|
||||
value["errmsg"] = "1";
|
||||
rtnmsg.push_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
if (fRet){
|
||||
if (db_info.db->batch_exec(sqllist, results)){
|
||||
json value;
|
||||
int i=0;
|
||||
for (auto r:results){
|
||||
value["metid"] = rtnmsg0[i]["metid"];
|
||||
value["afn"] = rtnmsg0[i]["afn"];
|
||||
value["dataid"] = rtnmsg0[i]["dataid"];
|
||||
value["errmsg"] = std::to_string(r.result.status);
|
||||
rtnmsg.push_back(value);
|
||||
i++;
|
||||
}
|
||||
}else{
|
||||
rtnmsg += rtnmsg0;
|
||||
}
|
||||
fRet = true;
|
||||
}
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
void writeDataSrv::Init(initparam_t *param)
|
||||
@@ -21,50 +109,44 @@ void writeDataSrv::Init(initparam_t *param)
|
||||
int writeDataSrv::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param)
|
||||
{
|
||||
//check param table
|
||||
json tmjson, repjson, repjson1,datajson, dataarray;
|
||||
json tmjson, repjson, repjson1,datajson, rtnmsg;
|
||||
int iRet=0;
|
||||
std::string src;
|
||||
|
||||
try {
|
||||
tmjson = json::parse(msg.payload);
|
||||
src = msg.topic;
|
||||
|
||||
auto cdtjson = tmjson["data"]["reqdata"] ;
|
||||
bool fRet = writeDataDb(param->pDbHandle, cdtjson, datajson);
|
||||
if (fRet){
|
||||
for (auto vljson:datajson){
|
||||
vljson["metid"]="0";
|
||||
vljson["afn"]="04";
|
||||
vljson["dataid"]="E0000100";
|
||||
vljson["createdtime"] = "2025";
|
||||
vljson["source"] = "sys.dbc";
|
||||
vljson["databuff"] = "010202";
|
||||
dataarray.push_back(vljson);
|
||||
if(tmjson.contains("data") && tmjson["data"].contains("dataarray")){
|
||||
auto wdataarray = tmjson["data"]["dataarray"] ;
|
||||
if (wdataarray.is_array()){
|
||||
bool fRet = writeDataDb(param->pDbHandle, src, wdataarray, rtnmsg);
|
||||
if (fRet){
|
||||
repjson["rtnmsg"] = rtnmsg;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}else{
|
||||
repjson["rtnmsg"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
msg_t msg_rep;
|
||||
msg_rep.payload = repjson1.dump();
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
}else{
|
||||
std::cerr << "json dataarray is not array error !!!" << std::endl;
|
||||
}
|
||||
repjson["dataarray"] = dataarray;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = "2025";
|
||||
repjson1["token"] = "11223344";
|
||||
}else{
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = "2025";
|
||||
repjson1["token"] = "11223344";
|
||||
}
|
||||
msg_t msg_rep;
|
||||
msg_rep.payload = repjson1.dump();
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
|
||||
}else{
|
||||
iRet=1;
|
||||
std::cerr << "json not contain dataarray error %s" << msg.payload << std::endl;
|
||||
}
|
||||
*/
|
||||
}
|
||||
catch(json::parse_error& ex){
|
||||
std::cerr << "parse error at byte " << ex.byte << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user