1. 增加读写普通数据服务源文件
This commit is contained in:
+5
-2
@@ -23,11 +23,14 @@ add_executable(dcapp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/mqttClient.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/ToolUtils.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_InsertRecord.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_UpdateRecord.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_UpdateRecord.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetSaveCount.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_Tool.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetSupportNum.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_SetSupportNum.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_ReadData.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_WriteData.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/f-readdata/normalDataDB.cpp
|
||||
${DC_SRC} ${DS_SRC} ${DQ_SRC} ${DS_BIZSRC})
|
||||
|
||||
target_link_libraries(dcapp
|
||||
@@ -35,4 +38,4 @@ target_link_libraries(dcapp
|
||||
sqlite3
|
||||
pthread
|
||||
dl
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include "F_ReadData.h"
|
||||
#include "bizservice.h"
|
||||
#include "f-readdata/normalDataDB.h"
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
REGISTER_BIZSERVICE("F-ReadData",readDataSrv);
|
||||
|
||||
//时间格式转换,时间戳转成 "2021-05-10T08:18:28.666+0800"
|
||||
static std::string time2jsontime(time_t timestamp)
|
||||
{
|
||||
struct timeval tmptime;
|
||||
struct tm tm_time;
|
||||
char buf[50]={0};
|
||||
char tempbuf[200]={0};
|
||||
gettimeofday(&tmptime, NULL);
|
||||
localtime_r(&tmptime.tv_sec, &tm_time);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.:03d%z", &tm_time);
|
||||
char *ptr = strstr(buf, ":03d");
|
||||
if (ptr){
|
||||
ptr[0] = '%';
|
||||
}
|
||||
sprintf(tempbuf, buf, tmptime.tv_usec/1000);
|
||||
std::string timestr = tempbuf;
|
||||
return timestr;
|
||||
}
|
||||
|
||||
static bool parse_topic(const std::string &topic, std::vector<std::string> &strarray)
|
||||
{
|
||||
std::size_t pos_h, pos_t;
|
||||
bool fRet=false;
|
||||
|
||||
for (int i=0; i<4; i++){
|
||||
pos_h = topic.find("/");
|
||||
pos_t = topic.find("/", pos_h+1);
|
||||
if (pos_h!=std::string::npos && pos_t!=std::string::npos){
|
||||
strarray.push_back(topic.substr(pos_h,pos_t));
|
||||
fRet=true;
|
||||
}else{
|
||||
fRet=false;
|
||||
}
|
||||
}
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
static bool readDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
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, dataarray, 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"] = time2jsontime(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"] = time2jsontime(time(NULL));
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}else{
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = time2jsontime(time(NULL));
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
|
||||
std::vector<std::string> str_array;
|
||||
if (parse_topic(msg.topic, str_array)){
|
||||
msg_t msg_rep;
|
||||
msg_rep.payload = repjson1.dump();
|
||||
std::string tempstr = str_array[0];
|
||||
str_array[0] = str_array[1];
|
||||
str_array[1] = tempstr;
|
||||
for (auto j:str_array){
|
||||
msg_rep.topic += "/";
|
||||
msg_rep.topic += j;
|
||||
}
|
||||
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
}else{
|
||||
iRet=1;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @brief 读普通数据
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _READ_DATA_SRV_H_
|
||||
#define _READ_DATA_SRV_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
|
||||
class readDataSrv : public IBizService{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
virtual void Init(initparam_t *param) ;
|
||||
|
||||
/**
|
||||
* @brief 处理读普通数据
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为1 失败为0
|
||||
*/
|
||||
virtual int HandleMsg(const msg_t msg, msg_t &outMsg, handparam_t *param) ; // 业务处理
|
||||
|
||||
/**
|
||||
* @brief 获取方法名称
|
||||
*
|
||||
* @return const char*
|
||||
*/
|
||||
virtual std::string GetFID(void) ; /* 如"F-ReadData" */
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <sqlite3.h>
|
||||
#include "F_WriteData.h"
|
||||
#include "bizservice.h"
|
||||
#include "f-readdata/normalDataDB.h"
|
||||
|
||||
|
||||
static bool writeDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void writeDataSrv::Init(initparam_t *param)
|
||||
{
|
||||
//check data table
|
||||
}
|
||||
|
||||
int writeDataSrv::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param)
|
||||
{
|
||||
//check param table
|
||||
json tmjson, repjson, repjson1,datajson, dataarray;
|
||||
int iRet=0;
|
||||
|
||||
try {
|
||||
tmjson = json::parse(msg.payload);
|
||||
|
||||
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);
|
||||
}
|
||||
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 = msg.topic;
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
}else{
|
||||
iRet=1;
|
||||
}
|
||||
*/
|
||||
}
|
||||
catch(json::parse_error& ex){
|
||||
std::cerr << "parse error at byte " << ex.byte << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return iRet;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string writeDataSrv::GetFID(void)
|
||||
{
|
||||
return std::string("F-WriteData");
|
||||
}
|
||||
|
||||
|
||||
REGISTER_BIZSERVICE("F-WriteData", writeDataSrv);
|
||||
@@ -0,0 +1,44 @@
|
||||
|
||||
|
||||
/**
|
||||
* @brief 写普通数据
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _WRITE_DATA_SRV_H_
|
||||
#define _WRITE_DATA_SRV_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "bizservice.h"
|
||||
|
||||
class writeDataSrv : public IBizService{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
virtual void Init(initparam_t *param) ;
|
||||
|
||||
/**
|
||||
* @brief 处理写普通数据
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为1 失败为0
|
||||
*/
|
||||
virtual int HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param) ; // 业务处理
|
||||
|
||||
/**
|
||||
* @brief 获取方法名称
|
||||
*
|
||||
* @return const char*
|
||||
*/
|
||||
virtual std::string GetFID(void) ;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* 普通数据库表结构 normal_data_table
|
||||
* pn | id | stime | source | crc | class_id | value
|
||||
*/
|
||||
|
||||
#include "normalDataDB.h"
|
||||
|
||||
|
||||
CNormalDB::CNormalDB(void* handle)
|
||||
{
|
||||
m_db = handle;
|
||||
}
|
||||
bool CNormalDB::createDataTable(void)
|
||||
{
|
||||
|
||||
bool fRet=false;
|
||||
/*
|
||||
std::string sql = "create table " ;
|
||||
std::string tableName = "normal_data";
|
||||
std::string columnStr = " (pn int, id char(8), stime int, dtime int, source char(32), value blob, primary key(pn, id)); ";
|
||||
sql = sql+tableName+columnStr;
|
||||
|
||||
sqlite3_stmt *stmt=NULL;
|
||||
|
||||
if (sqlite3_exec((sqlite3*)m_db, sql.c_str(), NULL, 0, NULL)==SQLITE_OK){
|
||||
|
||||
}else{
|
||||
printf("create file data table fail error %s\n", sqlite3_errmsg(m_sdb));
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
|
||||
bool CNormalDB::readData(const json &cdt, json &outjv)
|
||||
{
|
||||
/*
|
||||
std::string sql = "select " ;
|
||||
std::string columnStr = "(pn , id , stime , source , value) from ";
|
||||
std::string tableName = "normal_data";
|
||||
sql = sql+tableName+columnStr;
|
||||
|
||||
sqlite3_stmt *stmt=NULL;
|
||||
|
||||
if (sqlite3_exec((sqlite3*)m_db, sql.c_str(), NULL, 0, NULL)==SQLITE_OK){
|
||||
|
||||
}else{
|
||||
printf("create file data table fail error %s\n", sqlite3_errmsg(m_sdb));
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool CNormalDB::writeData(const json &wjv, json &outjv)
|
||||
{
|
||||
/*
|
||||
std::string sql = "insert into " ;
|
||||
std::string tableName = " normal_data ";
|
||||
std::string columnStr = " (pn , id , stime , dtime , source , value ) ";
|
||||
std::string valueStr = "(" + wjv["pn"].get<std::string>() + wjv["id"].get<std::string>()
|
||||
+ wjv["stime"].get<std::string>() + wjv["dtime"].get<std::string>() + wjv["source"].get<std::string>() +
|
||||
wjv["value"].get<std::string>() + ")" ;
|
||||
sql = sql + tableName + columnStr + " values " + valueStr;
|
||||
|
||||
sqlite3_stmt *stmt=NULL;
|
||||
|
||||
if (sqlite3_exec((sqlite3*)m_db, sql.c_str(), NULL, 0, NULL)==SQLITE_OK){
|
||||
|
||||
}else{
|
||||
printf("create file data table fail error %s\n", sqlite3_errmsg(m_sdb));
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
|
||||
/**
|
||||
* @brief 普通数据数据库操作类
|
||||
*
|
||||
*/
|
||||
#ifndef _NORMAL_DATA_DB_H_
|
||||
#define _NORMAL_DATA_DB_H_
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
class CNormalDB{
|
||||
|
||||
public:
|
||||
CNormalDB(void* handle);
|
||||
~CNormalDB();
|
||||
|
||||
bool createDataTable(void);
|
||||
/**
|
||||
* @param cdt - 读数据检索条件,根据台区终端接口文件F-ReadData定义
|
||||
*
|
||||
* @param outjv - 读数据返回结果, 格式根据台区终端接口文件F-ReadData定义
|
||||
*
|
||||
*/
|
||||
bool readData(const json &cdt, json &outjv);
|
||||
/**
|
||||
* @param cdt - 写数据检索条件,根据台区终端接口文件F-WriteData定义
|
||||
*
|
||||
* @param outjv - 写数据返回结果, 格式根据台区终端接口文件F-WriteData定义
|
||||
*
|
||||
*/
|
||||
bool writeData(const json &wjv, json &outjv);
|
||||
|
||||
private:
|
||||
void *m_db;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user