200 lines
4.3 KiB
C++
200 lines
4.3 KiB
C++
|
|
#include <map>
|
|
#include <iostream>
|
|
#include "F_ReadData.h"
|
|
#include "bizservice.h"
|
|
#include "f-readdata/normalDataDB.h"
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
#include "ToolUtils.h"
|
|
#include "db_api/db_api.h"
|
|
|
|
|
|
REGISTER_BIZSERVICE("F-ReadData",readDataSrv);
|
|
|
|
/**
|
|
* "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)
|
|
{
|
|
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)
|
|
{
|
|
//check data table
|
|
}
|
|
|
|
int readDataSrv::HandleMsg(msg_t msg, msg_t &outMsg, handparam_t *param) {
|
|
//check param table
|
|
json tmjson, repjson, datajson, cdtjson, repjson1;
|
|
int iRet = 0;
|
|
|
|
try {
|
|
tmjson = json::parse(msg.payload);
|
|
|
|
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;
|
|
}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) {
|
|
std::cerr << "parse error at byte " << ex.byte << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
return iRet;
|
|
}
|
|
|
|
std::string readDataSrv::GetFID(void) /* 如"F-ReadData" */
|
|
{
|
|
return std::string("F-ReadData");
|
|
}
|
|
|
|
|
|
/*
|
|
void test_readDataSrv_function(void)
|
|
{
|
|
readDataSrv test_readData;
|
|
msg_t msg_test;
|
|
handparam_t param;
|
|
|
|
msg_test.topic = "/"
|
|
|
|
int iRet = test_readData.HandleMsg(msg_test, ¶m);
|
|
|
|
printf("\niRet test result here %d\n", iRet);
|
|
|
|
}
|
|
*/
|
|
|