1578 lines
61 KiB
C++
1578 lines
61 KiB
C++
#include "SH_SqlDB.hpp"
|
|
#include "../API_log/SH_log.h"
|
|
#include "../common/SH_global.h"
|
|
#include <unistd.h>
|
|
#include "../calculation/Calculation.hpp"
|
|
|
|
bool SqlSwitch()
|
|
{
|
|
if (access("./sql", 0) >= 0) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
SqliteDB *sql_ctl = SqliteDB::instance();
|
|
Calculation *pCalculation = Calculation::instance();
|
|
|
|
SqliteDB::SqliteDB()
|
|
{
|
|
}
|
|
|
|
SqliteDB::~SqliteDB()
|
|
{
|
|
}
|
|
|
|
void SqliteDB::SqliteInit(const char *pDbName, bool isDB2)
|
|
{
|
|
char sql_exec[2048];
|
|
memset(sql_exec, 0, 2048);
|
|
|
|
//创建传感器信息存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
|
T_SENSOR_INFO(TNAME),
|
|
T_SENSOR_INFO(DATANODENO),
|
|
T_SENSOR_INFO(DATANODENAME),
|
|
T_SENSOR_INFO(INITFLAG),
|
|
T_SENSOR_INFO(ACCFLAG),
|
|
T_SENSOR_INFO(ZIGBEEFLAG),
|
|
T_SENSOR_INFO(TEMTOPFLAG),
|
|
T_SENSOR_INFO(TEMBOTFLAG),
|
|
T_SENSOR_INFO(EQUIPSTA),
|
|
T_SENSOR_INFO(HARDVERSION),
|
|
T_SENSOR_INFO(SOFTVERSION),
|
|
T_SENSOR_INFO(BPNO),
|
|
T_SENSOR_INFO(SERIALNO),
|
|
T_SENSOR_INFO(FIRSTPOWERNO),
|
|
"WakeupTime",
|
|
"StaticTime",
|
|
"WaveTime",
|
|
"BateryV",
|
|
"ProductNo",
|
|
T_SENSOR_INFO(CONFIGFLAG),
|
|
T_SENSOR_INFO(STARTBRANDS),
|
|
T_SENSOR_INFO(STOPBRANDS),
|
|
T_SENSOR_INFO(FEATUREINTERVAL),
|
|
T_SENSOR_INFO(WAVEINTERVAL),
|
|
T_SENSOR_INFO(SAMPLINGRATE),
|
|
T_SENSOR_INFO(SCOPE),
|
|
T_SENSOR_INFO(RANGE),
|
|
T_SENSOR_INFO(ENVELOPEBANDPASS),
|
|
T_SENSOR_INFO(FAULTFREQUENCY),
|
|
T_SENSOR_INFO(ZIGBEEPANID),
|
|
T_SENSOR_INFO(ZIGBEECHANNEL),
|
|
T_SENSOR_INFO(ZIGBEESHORTADDR),
|
|
T_SENSOR_INFO(ZIGBEELONGADDR),
|
|
T_SENSOR_INFO(ZIGBEEDESADDR),
|
|
"ZigbeePower",
|
|
"ZigbeeRetry",
|
|
"ZigbeeRetryGap",
|
|
"ACCSampleTime",
|
|
T_SENSOR_INFO(STATUS),
|
|
T_SENSOR_INFO(TIMESTAMP),
|
|
T_SENSOR_INFO(VIFF),
|
|
T_SENSOR_INFO(RSSI),
|
|
"UpdateFlag",
|
|
"LooseValue",
|
|
"batteryPower"),
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
int iRet = GetTableRows(" sqlite_master "," name = 't_sensor_info' and sql LIKE '%LooseValue%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_sensor_info ADD COLUMN 'LooseValue'");
|
|
}
|
|
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_sensor_info' and sql LIKE '%batteryPower%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_sensor_info ADD COLUMN 'batteryPower'");
|
|
}
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_sensor_info' and sql LIKE '%MeasurementID%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_sensor_info ADD COLUMN 'MeasurementID'");
|
|
}
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_sensor_info' and sql LIKE '%NodeWaveSend%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_sensor_info ADD COLUMN 'NodeWaveSend'");
|
|
}
|
|
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec,"update t_sensor_info set MeasurementID = dataNodeNo where MeasurementID IS NULL ;");
|
|
UpdateTableData(sql_exec,isDB2);
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec,"update t_sensor_info set MeasurementID = dataNodeNo where MeasurementID = '' ;");
|
|
UpdateTableData(sql_exec,isDB2);
|
|
|
|
//创建传感器数据存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s);",
|
|
T_DATA_INFO(TNAME),
|
|
T_DATA_INFO(DATANODENO),
|
|
T_DATA_INFO(CHANNELID),
|
|
T_DATA_INFO(DIAGNOSISEAK),
|
|
T_DATA_INFO(INTEGRATPK),
|
|
T_DATA_INFO(INTEGRATRMS),
|
|
T_DATA_INFO(RMSVALUES),
|
|
T_DATA_INFO(ENVELOPNERGY),
|
|
T_DATA_INFO(AMP1),
|
|
T_DATA_INFO(AMP2),
|
|
T_DATA_INFO(AMP3),
|
|
T_DATA_INFO(AMP4),
|
|
T_DATA_INFO(AMP5),
|
|
T_DATA_INFO(PHASE1),
|
|
T_DATA_INFO(PHASE2),
|
|
T_DATA_INFO(PHASE3),
|
|
T_DATA_INFO(PHASE4),
|
|
T_DATA_INFO(STATICINDEX),
|
|
T_DATA_INFO(TIMESTAMP),
|
|
T_DATA_INFO(SENDMSG),
|
|
T_DATA_INFO(NODERESEND));
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_data_info' and sql LIKE '%nodeResend%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_data_info ADD COLUMN 'nodeResend'");
|
|
}
|
|
|
|
//创建传感器静态数据存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s);",
|
|
T_DATASTATIC_INFO(TNAME),
|
|
T_DATASTATIC_INFO(DATANODENO),
|
|
T_DATASTATIC_INFO(CHANNELID),
|
|
T_DATASTATIC_INFO(TEMTOP),
|
|
T_DATASTATIC_INFO(TEMBOT),
|
|
T_DATASTATIC_INFO(DIP),
|
|
T_DATASTATIC_INFO(VOLTAGE),
|
|
"Battery",
|
|
T_DATASTATIC_INFO(STATICINDEX),
|
|
T_DATASTATIC_INFO(TIMESTAMP),
|
|
T_DATASTATIC_INFO(SENDMSG),
|
|
T_DATASTATIC_INFO(NODERESEND));
|
|
CreateTable(sql_exec, isDB2);
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_datastatic_info' and sql LIKE '%nodeResend%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_datastatic_info ADD COLUMN 'nodeResend'");
|
|
}
|
|
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
|
T_GATEWAY_INFO(TNAME),
|
|
T_GATEWAY_INFO(GATEWAYMAC),
|
|
T_GATEWAY_INFO(SENSORVER),
|
|
T_GATEWAY_INFO(GATEWAYLOCATION),
|
|
T_GATEWAY_INFO(ZIGBEEPANID),
|
|
T_GATEWAY_INFO(ZIGBEECHANNEL),
|
|
T_GATEWAY_INFO(COMMUNICATIONTYPE),
|
|
T_GATEWAY_INFO(SIGNAL),
|
|
T_GATEWAY_INFO(LOCALIP),
|
|
T_GATEWAY_INFO(SYSTEMVERSION),
|
|
T_GATEWAY_INFO(PROGRAMVERSION),
|
|
T_GATEWAY_INFO(WEBVERSION),
|
|
T_GATEWAY_INFO(SERVERIP),
|
|
T_GATEWAY_INFO(SERVERPORT),
|
|
T_GATEWAY_INFO(STATUS),
|
|
T_GATEWAY_INFO(GATEWAYUPDATE),
|
|
T_GATEWAY_INFO(MAC2));
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s);",
|
|
"t_data_waveSend","channelID","waveName","timeStamp","sendMsg","save");
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_data_waveSend' and sql LIKE '%sendMsg%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_data_waveSend ADD COLUMN 'sendMsg'");
|
|
}
|
|
iRet = GetTableRows(" sqlite_master "," name = 't_data_waveSend' and sql LIKE '%save%' ");
|
|
if(iRet == 0){
|
|
CreateTable("ALTER TABLE t_data_waveSend ADD COLUMN 'save'");
|
|
}
|
|
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
|
T_BATTERY_INFO(TNAME),
|
|
T_BATTERY_INFO(DATANODENO),
|
|
T_BATTERY_INFO(DIP),
|
|
T_BATTERY_INFO(TEMBOT),
|
|
T_BATTERY_INFO(NODEWORKTIME),
|
|
T_BATTERY_INFO(NODESENDTIME),
|
|
T_BATTERY_INFO(BATTERYVOLTAGE),
|
|
T_BATTERY_INFO(BATTERYUSAGE),
|
|
T_BATTERY_INFO(BATTERYREMAIN),
|
|
T_BATTERY_INFO(TIMESTAMP));
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s);",
|
|
"t_battery_history",
|
|
T_BATTERY_INFO(DATANODENO),
|
|
T_BATTERY_INFO(DIP),
|
|
T_BATTERY_INFO(TEMBOT),
|
|
T_BATTERY_INFO(NODEWORKTIME),
|
|
T_BATTERY_INFO(NODESENDTIME),
|
|
T_BATTERY_INFO(BATTERYVOLTAGE),
|
|
T_BATTERY_INFO(BATTERYUSAGE),
|
|
T_BATTERY_INFO(BATTERYREMAIN),
|
|
T_BATTERY_INFO(TIMESTAMP));
|
|
CreateTable(sql_exec, isDB2);
|
|
|
|
/*memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s integer,%s integer ,%s integer ,%s integer,%s integer);",
|
|
T_DATANODE_TIME(TNAME),
|
|
T_DATANODE_TIME(DATANODENO),
|
|
T_DATANODE_TIME(SHORTADDR),
|
|
T_DATANODE_TIME(STATICCYCLE),
|
|
T_DATANODE_TIME(WAVECYCLE),
|
|
T_DATANODE_TIME(NODEGROUP),
|
|
T_DATANODE_TIME(NODEINDEX),
|
|
T_DATANODE_TIME(NODEWAVEINDEX),
|
|
T_DATANODE_TIME(STATICTIME),
|
|
T_DATANODE_TIME(STATICSTARTTIME));
|
|
CreateTable(sql_exec, isDB2);*/
|
|
|
|
}
|
|
void SqliteDB::Createtable(const char *ptableName)
|
|
{
|
|
char sql_exec[2048];
|
|
//创建传感器数据存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s,%s);",
|
|
ptableName,
|
|
T_DATA_INFO(DATANODENO),
|
|
T_DATA_INFO(CHANNELID),
|
|
T_DATA_INFO(DIAGNOSISEAK),
|
|
T_DATA_INFO(INTEGRATPK),
|
|
T_DATA_INFO(INTEGRATRMS),
|
|
T_DATA_INFO(RMSVALUES),
|
|
T_DATA_INFO(ENVELOPNERGY),
|
|
T_DATA_INFO(AMP1),
|
|
T_DATA_INFO(AMP2),
|
|
T_DATA_INFO(AMP3),
|
|
T_DATA_INFO(AMP4),
|
|
T_DATA_INFO(AMP5),
|
|
T_DATA_INFO(PHASE1),
|
|
T_DATA_INFO(PHASE2),
|
|
T_DATA_INFO(PHASE3),
|
|
T_DATA_INFO(PHASE4),
|
|
"StaticIndex",
|
|
T_DATA_INFO(TIMESTAMP),
|
|
"sendMsg",
|
|
"nodeResend");
|
|
CreateTable(sql_exec, 0);
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec,"CREATE INDEX %s_1 \
|
|
ON %s (%s)",ptableName,ptableName,T_DATA_INFO(DATANODENO));
|
|
CreateTable(sql_exec, 0);
|
|
}
|
|
|
|
void SqliteDB::SqliteInitDel(const char *pDbName)
|
|
{
|
|
Deletetable(T_DATA_INFO(TNAME));
|
|
Deletetable(T_DATASTATIC_INFO(TNAME));
|
|
vec_t vecResult = GetDataMultiLineOfOneColumn(T_SENSOR_INFO(TNAME),T_SENSOR_INFO(ZIGBEELONGADDR),NULL);
|
|
for(int i = 0; i < vecResult.size() && vecResult.size() > 0;i++)
|
|
{
|
|
char sztableName[100]={0x00};
|
|
sprintf(sztableName,"t_data_%s",vecResult[i].c_str());
|
|
Deletetable(sztableName);
|
|
sprintf(sztableName,"t_dataStatic_%s",vecResult[i].c_str());
|
|
Deletetable(sztableName);
|
|
}
|
|
}
|
|
void SqliteDB::Deletetable(const char *ptableName)
|
|
{
|
|
char sql_exec[2048];
|
|
//创建传感器数据存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec,"DROP TABLE %s ;",ptableName);
|
|
int iRet = sqlite3_exec(GetDbHandle(false), sql_exec, 0, 0, NULL);
|
|
if (iRet != SQLITE_OK) {
|
|
perror_info("sqlite3_exec");
|
|
}
|
|
|
|
}
|
|
void SqliteDB::CreatedataStatictable(const char *ptableName)
|
|
{
|
|
char sql_exec[2048];
|
|
//创建传感器数据存储表
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec, "create table if not exists %s(%s,%s,%s,%s,%s,%s,%s,%s integer,%s,%s,%s,%s integer,%s,%s);",
|
|
ptableName,
|
|
T_DATASTATIC_INFO(DATANODENO),
|
|
T_DATASTATIC_INFO(CHANNELID),
|
|
T_DATASTATIC_INFO(TEMTOP),
|
|
T_DATASTATIC_INFO(TEMBOT),
|
|
T_DATASTATIC_INFO(DIP),
|
|
T_DATASTATIC_INFO(VOLTAGE),
|
|
"zigbeeSignal",
|
|
"StaticIndex",
|
|
T_DATASTATIC_INFO(TIMESTAMP),
|
|
"sendMsg",
|
|
"nodeResend",
|
|
"zigbeeSignalNode",
|
|
"statisticType",
|
|
"timing"
|
|
);
|
|
CreateTable(sql_exec, 0);
|
|
memset(sql_exec, 0, 2048);
|
|
sprintf(sql_exec,"CREATE INDEX %s_1 \
|
|
ON %s (%s)",ptableName,ptableName,T_DATA_INFO(DATANODENO));
|
|
CreateTable(sql_exec, 0);
|
|
}
|
|
bool SqliteDB::OpenDB(const char *pDbName, bool isDB2)
|
|
{
|
|
if (isDB2 == false) {
|
|
int ret = 0;
|
|
ret = sqlite3_open(pDbName, &mDBAcess);
|
|
if (ret == SQLITE_OK) {
|
|
sqlite3_exec(mDBAcess, "PRAGMA synchronous = NORMAL;", 0, 0, 0);
|
|
sqlite3_exec(mDBAcess, "PRAGMA cache_size=8000;", 0, 0, 0);
|
|
sqlite3_exec(mDBAcess, "PRAGMA temp_store = MEMORY;", 0, 0, 0);
|
|
sqlite3_exec(mDBAcess, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
|
print_info("Success To Open DataBase!\n");
|
|
}
|
|
else {
|
|
print_error("Fail To Open DataBase!\n");
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
int ret = 0;
|
|
ret = sqlite3_open(pDbName, &mDb2);
|
|
if (ret == SQLITE_OK) {
|
|
sqlite3_exec(mDb2, "PRAGMA synchronous = NORMAL;", 0, 0, 0);
|
|
sqlite3_exec(mDb2, "PRAGMA cache_size=8000;", 0, 0, 0);
|
|
sqlite3_exec(mDb2, "PRAGMA temp_store = MEMORY;", 0, 0, 0);
|
|
sqlite3_exec(mDb2, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
|
print_info("Success To Open DataBase!\n");
|
|
}
|
|
else {
|
|
print_error("Fail To Open DataBase!\n");
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int SqliteDB::CloseDB()
|
|
{
|
|
return sqlite3_close(mDBAcess);
|
|
}
|
|
|
|
sqlite3 *SqliteDB::GetDbHandle(bool isDB2)
|
|
{
|
|
if (isDB2 == false) {
|
|
return mDBAcess;
|
|
}
|
|
else {
|
|
return mDb2;
|
|
}
|
|
}
|
|
|
|
int SqliteDB::CreateTable(const char *sql, bool isDB2)
|
|
{
|
|
print_light_green("%s\n", sql);
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", sql);
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), sql, 0, 0, &msg);
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::ExeSql(const char *sql, bool isDB2)
|
|
{
|
|
print_light_green("%s\n", sql);
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), sql, 0, 0, &msg);
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::GetTableColumnCount(const char *tablename, bool isDB2)
|
|
{
|
|
std::string strSql = "select * from ";
|
|
int count = 0;
|
|
sqlite3_stmt *stmt;
|
|
strSql = strSql + tablename + ";";
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_prepare_v2(GetDbHandle(isDB2), strSql.c_str(), -1, &stmt, 0);
|
|
count = sqlite3_column_count(stmt);
|
|
sqlite3_finalize(stmt);
|
|
return count;
|
|
}
|
|
|
|
int SqliteDB::GetTableRows(const char *tablename, const char *whereCon)
|
|
{
|
|
int nRow = 0;
|
|
std::string strSql = "select count(*) from ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n",sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
return -1;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
if (retStep == SQLITE_ROW) {
|
|
nRow = sqlite3_column_int(stmt, 0);
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
return nRow;
|
|
}
|
|
|
|
int SqliteDB::AlterTable(const char *tablename, const char *column, bool isAdd, bool isDB2)
|
|
{
|
|
int iRet = SQLITE_ERROR;
|
|
if (isAdd) {
|
|
std::string strSql = "alter table ";
|
|
strSql = strSql + tablename + " add " + column + ";";
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, NULL);
|
|
if (iRet != SQLITE_OK) {
|
|
perror_info("sqlite3_exec");
|
|
}
|
|
}
|
|
else {
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
vec_t SqliteDB::GetDataSingleLine(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
vec_t vecResult;
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
int column_count = sqlite3_column_count(stmt);
|
|
if (retStep == SQLITE_ROW) {
|
|
for (int iCol = 0; iCol < column_count; iCol++) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, iCol);
|
|
std::string str = columninfo != NULL ? columninfo : "";
|
|
vecResult.push_back(str);
|
|
}
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
|
|
char* SqliteDB::GetDataChar(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
char szRes[100]={0x00};
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
g_tDbMutex.Lock();
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return NULL;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
if (retStep == SQLITE_ROW) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, 0);
|
|
memcpy(szRes,columninfo,sizeof(szRes));
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return szRes;
|
|
}
|
|
|
|
std::string SqliteDB::GetData(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
std::string strRes = "";
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
g_tDbMutex.Lock();
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return strRes;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
if (retStep == SQLITE_ROW) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, 0);
|
|
strRes = columninfo != NULL ? columninfo : "";
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return strRes;
|
|
}
|
|
|
|
array_t SqliteDB::GetDataMultiLine(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
array_t arrResult;
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
g_tDbMutex.Lock();
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return arrResult;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
int column_count = sqlite3_column_count(stmt);
|
|
while (retStep == SQLITE_ROW) {
|
|
vec_t vecResult;
|
|
for (int iCol = 0; iCol < column_count; iCol++) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, iCol);
|
|
std::string str = columninfo != NULL ? columninfo : "";
|
|
vecResult.push_back(str);
|
|
}
|
|
arrResult.push_back(vecResult);
|
|
retStep = sqlite3_step(stmt);
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
g_tDbMutex.UnLock();
|
|
return arrResult;
|
|
}
|
|
array_t SqliteDB::GetDataMultiLineTransaction(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
array_t arrResult;
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_exec(mDBAcess, "BEGIN", 0, 0, NULL);
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
return arrResult;
|
|
}
|
|
sqlite3_reset(stmt);
|
|
sqlite3_bind_int(stmt, 1, 0);
|
|
int retStep = sqlite3_step(stmt);
|
|
int column_count = sqlite3_column_count(stmt);
|
|
while (retStep == SQLITE_ROW) {
|
|
vec_t vecResult;
|
|
for (int iCol = 0; iCol < column_count; iCol++) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, iCol);
|
|
std::string str = columninfo != NULL ? columninfo : "";
|
|
vecResult.push_back(str);
|
|
}
|
|
arrResult.push_back(vecResult);
|
|
retStep = sqlite3_step(stmt);
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
sqlite3_exec(mDBAcess, "COMMIT", 0, 0, NULL);
|
|
return arrResult;
|
|
}
|
|
vec_t SqliteDB::GetDataMultiLineOfOneColumn(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
vec_t vecResult;
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
while (retStep == SQLITE_ROW) {
|
|
char *columninfo = (char*)sqlite3_column_text(stmt, 0);
|
|
std::string str = columninfo != NULL ? columninfo : "";
|
|
vecResult.push_back(str);
|
|
retStep = sqlite3_step(stmt);
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
vec_Value SqliteDB::GetDataMultiLineOfOneColumnDouble(const char *tablename, const char *column, const char *whereCon)
|
|
{
|
|
vec_Value vecResult;
|
|
std::string strSql = "select ";
|
|
if (whereCon != NULL) {
|
|
strSql = strSql + column + " from " + tablename + " where " + whereCon + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + column + " from " + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
|
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
int retStep = sqlite3_step(stmt);
|
|
while (retStep == SQLITE_ROW) {
|
|
double columninfo = sqlite3_column_double(stmt, 0);
|
|
vecResult.push_back(columninfo);
|
|
retStep = sqlite3_step(stmt);
|
|
}
|
|
sqlite3_finalize(stmt);
|
|
return vecResult;
|
|
}
|
|
|
|
|
|
int SqliteDB::DeleteTableData(const char* tablename, const char* whereCond, bool isDB2)
|
|
{
|
|
std::string strSql = "delete from ";
|
|
if (whereCond != NULL) {
|
|
strSql = strSql + tablename + " where " + whereCond + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + ";";
|
|
}
|
|
// LOG_INFO("strSql = %s",strSql.c_str());
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
|
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
int SqliteDB::DeleteTableDataOneConditon(const char* tablename, const char* condColumnName, const char * condColumnValue, bool isDB2)
|
|
{
|
|
std::string strSql = "delete from ";
|
|
if (condColumnName != NULL) {
|
|
strSql = strSql + tablename + " where " + condColumnName + "='" + condColumnValue + "';";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
|
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::UpdateNodeNameData(const char* tablename, const char* updateColumn, const char* whereCond, bool isDB2)
|
|
{
|
|
std::string strSql = "update ";
|
|
char szSql[1024] = {0x00};
|
|
strcat(szSql,"update ");
|
|
if (whereCond != NULL) {
|
|
strcat(szSql,tablename);
|
|
strcat(szSql," set ");
|
|
strcat(szSql,updateColumn);
|
|
strcat(szSql," where ");
|
|
strcat(szSql,whereCond);
|
|
strcat(szSql,";");
|
|
strSql = strSql + tablename + " set " + updateColumn + " where " + whereCond + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + " set " + updateColumn + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), szSql, 0, 0, &msg);
|
|
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::UpdateTableData(const char* tablename, const char* updateColumn, const char* whereCond, bool isDB2)
|
|
{
|
|
std::string strSql = "update ";
|
|
if (whereCond != NULL) {
|
|
strSql = strSql + tablename + " set " + updateColumn + " where " + whereCond + ";";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + " set " + updateColumn + ";";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
char *msg;
|
|
g_tDbMutex.Lock();
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
|
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
g_tDbMutex.UnLock();
|
|
return iRet;
|
|
}
|
|
|
|
int SqliteDB::UpdateTableData(const char* directSql, bool isDB2)
|
|
{
|
|
print_light_green("%s\n", directSql);
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", directSql);
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), directSql, 0, 0, &msg);
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
int SqliteDB::UpdateTableDataOneColumn(const char* tablename, const char* columnName, const char* columnValue, const char* whereColName, const char * whereColValue, bool isDB2)
|
|
{
|
|
std::string strSql = "update ";
|
|
if (whereColName != NULL) {
|
|
strSql = strSql + tablename + " set " + columnName + "='" + columnValue + "'" + " where " + whereColName + "='" + whereColValue + "';";
|
|
}
|
|
else {
|
|
strSql = strSql + tablename + " set " + columnName + "='" + columnValue + "';";
|
|
}
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
int SqliteDB::InsertData(const char* tablename, const char* insertValues, int replace, bool expandable, bool isDB2)
|
|
{
|
|
char *msg;
|
|
int iRet = 0;
|
|
if (expandable) {
|
|
char *strSql = (char *)malloc(4096);
|
|
if (strSql == NULL) {
|
|
print_error("malloc error\n");
|
|
abort();
|
|
}
|
|
char strReplace[] = "replace into ";
|
|
memset(strSql, 0, 4096);
|
|
if (replace == 0) {
|
|
memset(strReplace, 0, sizeof(strSql));
|
|
}
|
|
sprintf(strSql, "%s %s%s %s %s %s", "insert into", strReplace, tablename, "values(", insertValues, ");");
|
|
print_light_green("%s\n", strSql);
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql);
|
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql, 0, 0, &msg);
|
|
|
|
free(strSql);
|
|
} else {
|
|
std::string strSql = "insert into ";
|
|
if (replace != 0) {
|
|
strSql = "replace into ";
|
|
}
|
|
strSql = strSql + tablename + " values(" + insertValues + ");";
|
|
print_light_green("%s\n", strSql.c_str());
|
|
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
|
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
|
}
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::InsertData(const char* insertSql)
|
|
{
|
|
char *msg;
|
|
int iRet = sqlite3_exec(GetDbHandle(false), insertSql, 0, 0, &msg);
|
|
if (iRet != SQLITE_OK) {
|
|
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
|
sqlite3_free(msg);
|
|
}
|
|
return iRet;
|
|
}
|
|
int SqliteDB::CalculateBattery()
|
|
{
|
|
LOG_INFO("CalculateBattery start\n");
|
|
char whereCon[1024] = {0};
|
|
char selectSql[1024] = { 0 };
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(selectSql,0x00,sizeof(selectSql));
|
|
char updateSql[1024] = { 0 };
|
|
sprintf(selectSql," dataNodeNo,StaticTime,WaveTime,featureInterVal,waveInterVal,samplingRate,batteryPower ");
|
|
array_t vecRes = GetDataMultiLine(T_SENSOR_INFO(TNAME), selectSql, NULL);
|
|
print_info("res = %d\n",vecRes.size());
|
|
if(vecRes.size() > 0){
|
|
for(int i = 0; i < vecRes.size(); i++){
|
|
float capacity = 0.0,startCapacity = 0.0;
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(selectSql,0x00,sizeof(selectSql));
|
|
sprintf(whereCon," dataNodeNo = '%s' and batteryRemain <> '' order by timeStamp desc limit 0,1 ",vecRes[i][0].c_str());
|
|
vec_t vecResSig = sql_ctl->GetDataSingleLine(T_BATTERY_INFO(TNAME)," * ",whereCon);
|
|
vector<string> vParam;
|
|
boost::split( vParam, vecRes[i][6], boost::is_any_of( "," ), boost::token_compress_on );
|
|
if(vParam.size() <= 0 || vecResSig.size() <= 0){//第一次计算
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon,"dataNodeNo = '%s' ",vecRes[i][0].c_str());
|
|
string Dip = sql_ctl->GetData(T_DATASTATIC_INFO(TNAME)," dip ",whereCon);
|
|
if(Dip == ""){
|
|
continue;
|
|
}
|
|
capacity = (0.9+0.1*(90-atoi(Dip.c_str()))/90)*19000;//mAh //电池总量
|
|
startCapacity = capacity;
|
|
|
|
sprintf(updateSql,"batteryPower = '%f,%f' ",startCapacity,startCapacity);
|
|
|
|
int iRet = UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon," dataNodeNo = '%s' order by timeStamp asc limit 0,1 ",vecRes[i][0].c_str());
|
|
vecResSig = sql_ctl->GetDataSingleLine(T_BATTERY_INFO(TNAME)," * ",whereCon);
|
|
if(vecResSig.size() <= 0){//一条数据都没有
|
|
|
|
continue;
|
|
}
|
|
|
|
}else{
|
|
capacity = atof(vecResSig[7].c_str());
|
|
}
|
|
|
|
print_info("dip = %d\n",atoi(vecResSig[1].c_str()));
|
|
|
|
print_info("capacity = %f\n",capacity);
|
|
sprintf(whereCon," dataNodeNo = '%s' and timeStamp > '%s'",vecRes[i][0].c_str(),vecResSig[8].c_str());
|
|
array_t vecResbattery = GetDataMultiLine(T_BATTERY_INFO(TNAME), " * ", whereCon);
|
|
print_info("vecResbattery size = %d\n",vecResbattery.size());
|
|
if(vecResbattery.size() <= 0){
|
|
continue;
|
|
}
|
|
int y10_mins = 10 * 365 * 24 * 60;
|
|
int d200_mins = 200 * 24 * 60;
|
|
int x1 = 25,x2 = 60;
|
|
int y1 = 2;
|
|
float y2 = (float)y10_mins/(float)d200_mins;
|
|
|
|
float k = (y2-y1)/(x2-x1);
|
|
vector<float> vecb;
|
|
vector<long> vecworkTime;
|
|
vector<long> vecsendTime;
|
|
float to_math = 0.0;
|
|
print_info("vecResbattery = %d,temp = %s\n",vecResbattery.size(),vecResbattery[0][2].c_str());
|
|
//LOG_INFO("vecResbattery = %d,temp = %s\n",vecResbattery.size(),vecResbattery[0][2].c_str());
|
|
for (size_t j = 0; j < vecResbattery.size(); j++)
|
|
{
|
|
float b = 2 - 25 * k;
|
|
float dpm = k * atoi(vecResbattery[j][2].c_str()) + b;//温度
|
|
float cost_e = dpm * atoi(vecRes[i][3].c_str());//特征值时间间隔
|
|
float cost_h = cost_e / 60;
|
|
vecworkTime.push_back(atol(vecResbattery[j][3].c_str()));
|
|
vecsendTime.push_back(atol(vecResbattery[j][4].c_str()));
|
|
to_math += cost_h;
|
|
}
|
|
|
|
print_info("自放电 = %f\n",to_math);
|
|
//LOG_INFO("自放电 = %f\n",to_math);
|
|
long sumworkTime = 0.0,sumsendTime = 0.0;
|
|
for(size_t j = 0 ; j < vecworkTime.size();j++)
|
|
{
|
|
sumworkTime += vecworkTime[j];
|
|
}
|
|
for(size_t j = 0 ; j < vecsendTime.size();j++)
|
|
{
|
|
sumsendTime += vecsendTime[j];
|
|
}
|
|
print_info("sumworkTime = %ld,sumsendTime = %ld\n",sumworkTime,sumsendTime);
|
|
//LOG_INFO("sumworkTime = %ld,sumsendTime = %ld\n",sumworkTime,sumsendTime);
|
|
float usageworkTime = ((float)sumworkTime/3600000) * 4;
|
|
float usagesendTime = ((float)sumsendTime/3600000) * 39;
|
|
print_info("work = %f,send = %f\n",usageworkTime,usagesendTime);
|
|
//LOG_INFO("work = %f,send = %f\n",usageworkTime,usagesendTime);
|
|
if(to_math < 0)
|
|
to_math = 0;
|
|
float usageBattery = usageworkTime + usagesendTime + to_math;
|
|
print_info("已经使用 = %f\n",atof(vecResSig[6].c_str()));
|
|
|
|
float remainBattery = capacity - usageBattery * 0.2;
|
|
if (remainBattery < 10)
|
|
{
|
|
remainBattery = 10;
|
|
}
|
|
|
|
LOG_INFO("dataNodeNo = %s,batteryUsage = %f,batteryRemain = %f\n",vecRes[i][0].c_str(),atof(vecResSig[6].c_str()),remainBattery);
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon," dataNodeNo = '%s' order by timeStamp desc limit 0,1 ",vecRes[i][0].c_str());
|
|
string strtimeStamp = sql_ctl->GetData(T_BATTERY_INFO(TNAME)," timeStamp ",whereCon);
|
|
|
|
sprintf(updateSql, "batteryUsage='%f',batteryRemain='%f'",usageBattery,remainBattery);
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon, "dataNodeNo ='%s' and timeStamp = '%s'", vecRes[i][0].c_str(),strtimeStamp.c_str());
|
|
sql_ctl->UpdateTableData(T_BATTERY_INFO(TNAME), updateSql, whereCon);
|
|
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(updateSql,0x00,sizeof(updateSql));
|
|
|
|
char insertSql[1024]={0x00},deleteSql[1024]={0x00};
|
|
sprintf(insertSql,"insert into t_battery_history select * from t_battery_info where timeStamp < '%s' and dataNodeNo = '%s'",strtimeStamp.c_str(),vecRes[i][0].c_str());
|
|
ExeSql(insertSql);
|
|
|
|
sprintf(deleteSql,"delete from t_battery_info where timeStamp < '%s' and dataNodeNo = '%s'",strtimeStamp.c_str(),vecRes[i][0].c_str());
|
|
ExeSql(deleteSql);
|
|
|
|
sprintf(whereCon,"dataNodeNo = '%s' ",vecRes[i][0].c_str());
|
|
if(startCapacity > 0){
|
|
sprintf(updateSql,"batteryPower = '%f,%f' ",startCapacity,remainBattery);
|
|
}else{
|
|
sprintf(updateSql,"batteryPower = '%s,%f' ",vParam[0].c_str(),remainBattery);
|
|
}
|
|
|
|
int iRet = UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
|
string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
|
|
data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
|
}
|
|
}
|
|
LOG_INFO("CalculateBattery end\n");
|
|
|
|
|
|
|
|
|
|
/*float dayStaticPower = 6.5,dayWavePower = 237;//
|
|
char whereCon[1024] = {0};
|
|
char selectSql[1024] = { 0 };
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(selectSql,0x00,sizeof(selectSql));
|
|
sprintf(selectSql," DataNodeNo,StaticTime,WaveTime,featureInterVal,waveInterVal,samplingRate ");
|
|
array_t vecRes ;
|
|
vecRes = GetDataMultiLine(T_SENSOR_INFO(TNAME), selectSql, NULL);
|
|
if(vecRes.size() > 0){
|
|
for(int i = 0; i < vecRes.size(); i++){
|
|
if(vecRes[i][5] == "12800"){
|
|
|
|
}else if(vecRes[i][5] == "3200"){
|
|
|
|
}
|
|
float dayStaticTime = 0.0;
|
|
float dayWaveTime = 0.0;
|
|
if(atoi(vecRes[i][3].c_str()) == 0){
|
|
dayStaticTime = 0;
|
|
}else{
|
|
dayStaticTime = 24*60/atoi(vecRes[i][3].c_str());
|
|
}
|
|
if(atoi(vecRes[i][4].c_str()) == 0){
|
|
dayWaveTime = 0;
|
|
}else{
|
|
dayWaveTime = 24*60/atoi(vecRes[i][4].c_str());
|
|
}
|
|
|
|
float dayPower = dayStaticTime * dayStaticPower + dayWaveTime * dayWavePower + 24*3.26;//static + wave + sleep
|
|
float usePower = atoi(vecRes[i][1].c_str()) * dayStaticPower + atoi(vecRes[i][2].c_str()) * dayWavePower;
|
|
if(dayPower == 0)
|
|
dayPower = 1;
|
|
float remainDay = (19000000*0.5 - usePower)/dayPower;
|
|
float bateryProportion = remainDay/(19000000*0.5/dayPower);
|
|
char updateSql[1024] = { 0x00 };
|
|
char tmpParam[1024] = {0x00};
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(updateSql,0x00,sizeof(updateSql));
|
|
sprintf(whereCon,"dataNodeNo = '%s' ",vecRes[i][0].c_str());
|
|
sprintf(updateSql,"batteryPower = '%f,%f' ",bateryProportion,remainDay);
|
|
int iRet = UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
|
print_info("iRet = %d\n",iRet);
|
|
string strData = sql_ctl->GetNodeConfigureInfor(whereCon);
|
|
data_publish(strData.c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
|
}
|
|
}*/
|
|
|
|
print_info("CalculateBattery \n");
|
|
return 0;
|
|
}
|
|
int SqliteDB::CalculateDip()
|
|
{
|
|
char whereCon[1024] = {0};
|
|
char selectSql[1024] = { 0 };
|
|
char updateSql[1024] = { 0 };
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(selectSql,0x00,sizeof(selectSql));
|
|
sprintf(selectSql," DataNodeNo");
|
|
array_t vecRes ;
|
|
char looseValue[10]={0x00};
|
|
char localtimestamp[32] = { 0 };
|
|
GetTimeNet(localtimestamp, 1);
|
|
readStringValue("config", "loose",looseValue,(char*)GlobalConfig::Config_G.c_str());
|
|
vecRes = GetDataMultiLine(T_SENSOR_INFO(TNAME), " * ", NULL);
|
|
print_info("vecRes111 = %d\n",vecRes.size());
|
|
for(int i = 0 ; i < vecRes.size(); i++){
|
|
vector<string> vParam;
|
|
print_info("vecRes =%s\n",vecRes[i][42].c_str());
|
|
boost::split( vParam, vecRes[i][42], boost::is_any_of( "," ), boost::token_compress_on );
|
|
print_info("vParam size = %d\n",vParam.size());
|
|
if(vParam.size() < 2)
|
|
{
|
|
sprintf(updateSql,"LooseValue = '%f,0' ",atof(vParam[0].c_str()));
|
|
sprintf(whereCon,"dataNodeNo = '%s' ",vecRes[i][0].c_str());
|
|
UpdateTableData(T_SENSOR_INFO(TNAME),updateSql,whereCon);
|
|
}else{
|
|
char szTablename[32]={0x00};
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
if(vParam[1] == "2"){//人工干预
|
|
sprintf(whereCon," timeStamp > '%ld' ",atol(vParam[2].c_str()));
|
|
}else if(vParam[1] == "0"){//正常状态
|
|
sprintf(whereCon," timeStamp > '%ld' ",atol(localtimestamp) - 86400);//一天数据
|
|
}else if(vParam[1] == "1")//松动状态
|
|
{
|
|
continue;
|
|
}
|
|
print_info("vParam[0]= %s,vParam[1]=%s\n",vParam[0].c_str(),vParam[1].c_str());
|
|
|
|
sprintf(szTablename,"t_dataStatic_%s",vecRes[i][0].c_str());
|
|
vec_Value vecResDip = GetDataMultiLineOfOneColumnDouble(szTablename," Dip ", whereCon);
|
|
float sample_variance = pCalculation->getSample_variance(vecResDip);
|
|
print_info("sample_variance = %f\n",sample_variance);
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
memset(updateSql,0x00,sizeof(updateSql));
|
|
sprintf(whereCon,"dataNodeNo = '%s' ",vecRes[i][0].c_str());
|
|
if(vParam[1] == "0"){
|
|
if(sample_variance > atol(looseValue)){
|
|
sprintf(updateSql,"LooseValue = '%f,1' ",sample_variance);
|
|
}else{
|
|
sprintf(updateSql,"LooseValue = '%f,0' ",sample_variance);
|
|
}
|
|
}else if(vParam[1] == "1"){
|
|
|
|
}else if(vParam[1] == "2"){
|
|
print_info("localtimestamp = %ld,vParam[2]=%d,%ld\n",atol(localtimestamp),atol(vParam[2].c_str()),atol(localtimestamp) - atol(vParam[2].c_str()));
|
|
if(atol(localtimestamp) - atol(vParam[2].c_str()) > 86400){
|
|
sprintf(updateSql,"LooseValue = '%f,0' ",atof(vParam[0].c_str()));
|
|
}else{
|
|
sprintf(updateSql,"LooseValue = '%f,2,",atof(vParam[0].c_str()));
|
|
string strUpdateSql = string(updateSql) + vParam[2] + "' " ;
|
|
memset(updateSql,0x00,sizeof(updateSql));
|
|
memcpy(updateSql,strUpdateSql.c_str(),sizeof(updateSql));
|
|
}
|
|
if(sample_variance > atol(looseValue))
|
|
{
|
|
sprintf(updateSql,"LooseValue = '%f,1' ",sample_variance);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
UpdateTableData(T_SENSOR_INFO(TNAME),updateSql,whereCon);
|
|
}
|
|
print_info("CalculateDip \n");
|
|
}
|
|
int SqliteDB::InintGateway()
|
|
{
|
|
//更新网关配置表
|
|
string strIP = GetGwIp_("eth0");
|
|
string strServerIP = ReadStrByOpt(SERVERCONFIG, "Server", "localServerIpAddress");
|
|
string strServerPort = ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort");
|
|
string strwebVersion = ReadStrByOpt(SYSTEMINFOFILE, "Version", "WebVersion");
|
|
string strsystemVersion = ReadStrByOpt(SYSTEMINFOFILE, "Version", "SystemVersion");
|
|
string strGatewayVersion = ReadStrByOpt(SYSTEMINFOFILE, "Version", "GateWayVersion");
|
|
std::string strchan = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "channel");
|
|
std::string strPanID = ReadStrByOpt(ZIGBEECONFIG, "Zigbee", "PanID");
|
|
if(strPanID == ""){
|
|
strPanID = GlobalConfig::MacAddr_G.substr(8);
|
|
}
|
|
if(0 == sql_ctl->GetTableRows(T_GATEWAY_INFO(TNAME), NULL)){
|
|
|
|
char strSql[1024] = { 0 };
|
|
sprintf(strSql,"insert into t_gateway_info(gatewayMAC,zigbeePanID,zigbeeChannel,\
|
|
localIP,systemVersion,programVersion,webVersion,serverIP,serverPort,MAC2)\
|
|
values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');",GlobalConfig::MacAddr_G.c_str(),strPanID.c_str(),strchan.c_str(),strIP.c_str(),\
|
|
strsystemVersion.c_str(),strGatewayVersion.c_str(),strwebVersion.c_str(),strServerIP.c_str(),strServerPort.c_str(),GlobalConfig::MacAddr_G2.c_str());
|
|
sql_ctl->InsertData(strSql);
|
|
print_info("strSql = %s\n",strSql);
|
|
}else{
|
|
char whereCon[1024] = {0};
|
|
char updateSql[1024] = { 0 };
|
|
sprintf(updateSql, "zigbeePanID = '%s',zigbeeChannel = '%s',localIP = '%s',systemVersion='%s',programVersion='%s',webVersion='%s',serverIP='%s',serverPort='%s'",\
|
|
strPanID.c_str(),strchan.c_str(),strIP.c_str(),\
|
|
strsystemVersion.c_str(),strGatewayVersion.c_str(),strwebVersion.c_str(),strServerIP.c_str(),strServerPort.c_str());
|
|
sprintf(whereCon, "gatewayMAC='%s'", GlobalConfig::MacAddr_G.c_str());
|
|
sql_ctl->UpdateTableData(T_GATEWAY_INFO(TNAME), updateSql, whereCon);
|
|
}
|
|
|
|
//上传网关配置到MQTT
|
|
Json::Value jsSystemSetting;
|
|
Json::Value jsBody;
|
|
Json::FastWriter showValue;
|
|
Json::Value jsonVal;
|
|
jsonVal.clear();
|
|
|
|
jsonVal["cmd"] = "23";
|
|
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
|
jsSystemSetting["WebVersion"] = strwebVersion;
|
|
jsSystemSetting["SystemVersion"] = strsystemVersion;
|
|
jsSystemSetting["GateWayVersion"] = strGatewayVersion;
|
|
jsBody["localServerIpAddress"] = ReadStrByOpt(SERVERCONFIG, "Server", "localServerIpAddress");
|
|
jsBody["localServerPort"] = atoi(ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort").c_str());
|
|
jsBody["CommMode"] = atoi(ReadStrByOpt(SERVERCONFIG, "Server", "CommMode").c_str());
|
|
jsBody["Password"] = (ReadStrByOpt(SERVERCONFIG, "Server", "Password"));
|
|
jsBody["UserName"] = (ReadStrByOpt(SERVERCONFIG, "Server", "UserName"));
|
|
std::string dataBody = showValue.write(jsBody);
|
|
jsonVal["cmdBody"] = dataBody;
|
|
data_publish(showValue.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
|
|
|
jsBody.clear();
|
|
jsonVal["cmd"] = "25";
|
|
jsBody["dnsName"] = ReadStrByOpt(NETWORKCONFIG, "Net", "dnsName");
|
|
jsBody["networkPortStatus"] = ReadStrByOpt(NETWORKCONFIG, "Net", "networkPortStatus");
|
|
jsBody["gateway"] = ReadStrByOpt(NETWORKCONFIG, "Net", "gateway");
|
|
jsBody["subnetMask"] = ReadStrByOpt(NETWORKCONFIG, "Net", "subnetMask");
|
|
jsBody["dataWatchIpAddress"] = ReadStrByOpt(NETWORKCONFIG, "Net", "ipAddress");
|
|
jsBody["hostName"] = ReadStrByOpt(NETWORKCONFIG, "Net", "hostName");
|
|
|
|
dataBody = showValue.write(jsBody);
|
|
jsonVal["cmdBody"] = dataBody;
|
|
data_publish(showValue.write(jsonVal).c_str(), GlobalConfig::Topic_G.mPubConfig.c_str());
|
|
}
|
|
|
|
std::string SqliteDB::GetNodeConfigureInfor(const char* whereCon)
|
|
{
|
|
Json::Value jsonVal;
|
|
Json::FastWriter showValue;
|
|
jsonVal.clear();
|
|
jsonVal["cmd"] = "26";
|
|
jsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
|
jsonVal["success"] = true;
|
|
jsonVal["message"] = "查询成功";
|
|
Json::Value jsArray;
|
|
array_t arrRes;
|
|
arrRes = sql_ctl->GetDataMultiLine(T_SENSOR_INFO(TNAME), "*", whereCon);
|
|
int iResult = arrRes.size();
|
|
if (iResult > 0) {
|
|
for (int j = 0; j < iResult; j++) {
|
|
Json::Value jsSensorData;
|
|
jsSensorData["dataNodeNo"] = arrRes[j][44];
|
|
jsSensorData["dataNodeName"] = arrRes[j][1];
|
|
jsSensorData["initFlag"] = atoi(arrRes[j][2].c_str());
|
|
jsSensorData["accFlag"] = atoi(arrRes[j][3].c_str());
|
|
jsSensorData["zigbeeFlag"] = atoi(arrRes[j][4].c_str());
|
|
jsSensorData["temTopFlag"] = atoi(arrRes[j][5].c_str());
|
|
jsSensorData["temBotFlag"] = atoi(arrRes[j][6].c_str());
|
|
jsSensorData["equipsta"] = atoi(arrRes[j][7].c_str());
|
|
jsSensorData["hardVersion"] = arrRes[j][8];
|
|
jsSensorData["softVersion"] = arrRes[j][9];
|
|
jsSensorData["bpNo"] = arrRes[j][10];
|
|
jsSensorData["serialNo"] = arrRes[j][11];
|
|
jsSensorData["firstPowerTime"] = arrRes[j][12];
|
|
jsSensorData["WakeupTime"] = atoi(arrRes[j][13].c_str());
|
|
jsSensorData["StaticTime"] = atoi(arrRes[j][14].c_str());
|
|
jsSensorData["WaveTime"] = atoi(arrRes[j][15].c_str());
|
|
jsSensorData["BateryV"] = arrRes[j][16];
|
|
jsSensorData["ProductNo"] = arrRes[j][17];
|
|
jsSensorData["configFlag"] = atoi(arrRes[j][18].c_str());
|
|
jsSensorData["startBrands"] = arrRes[j][19];
|
|
jsSensorData["stopBrands"] = arrRes[j][20];
|
|
jsSensorData["featureInterVal"] = (arrRes[j][21]);
|
|
jsSensorData["waveInterVal"] = atoi(arrRes[j][22].c_str());
|
|
jsSensorData["samplingRate"] = atoi(arrRes[j][23].c_str());
|
|
jsSensorData["range"] = atoi(arrRes[j][25].c_str());
|
|
jsSensorData["envelopeBandPass"] = arrRes[j][26];
|
|
jsSensorData["faultFrequency"] = arrRes[j][27];
|
|
jsSensorData["zigbeePanId"] = arrRes[j][28];
|
|
jsSensorData["zigbeeChannel"] = (arrRes[j][29]);
|
|
jsSensorData["zigbeeAddr"] = arrRes[j][30];
|
|
jsSensorData["zigbeeLongAddr"] = arrRes[j][31];
|
|
jsSensorData["zigbeeDesAddr"] = arrRes[j][32];
|
|
jsSensorData["ZigbeePower"] = atoi(arrRes[j][33].c_str());
|
|
jsSensorData["ZigbeeRetry"] = atoi(arrRes[j][34].c_str());
|
|
jsSensorData["ZigbeeRetryGap"] = atoi(arrRes[j][35].c_str());
|
|
jsSensorData["ACCSampleTime"] = atoi(arrRes[j][36].c_str());
|
|
jsSensorData["status"] = atoi(arrRes[j][37].c_str());
|
|
jsSensorData["timeStamp"] = arrRes[j][38];
|
|
jsSensorData["viff"] = atoi(arrRes[j][39].c_str());
|
|
jsSensorData["RSSI"] = arrRes[j][40];
|
|
jsSensorData["Update"] = atoi(arrRes[j][41].c_str());
|
|
jsSensorData["looseValue"] = arrRes[j][42];
|
|
jsSensorData["battery"] = arrRes[j][43];
|
|
jsSensorData["MeasurementID"] = arrRes[j][44];
|
|
jsSensorData["nodeWaveSend"] = arrRes[j][45];
|
|
jsArray.append(jsSensorData);
|
|
}
|
|
} else {
|
|
jsArray.resize(0);
|
|
jsonVal["success"] = false;
|
|
jsonVal["message"] = "查询失败";
|
|
}
|
|
|
|
Json::Value jsBody;
|
|
jsBody["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
|
jsBody["dataNodeArray"] = jsArray;
|
|
std::string dataBody = showValue.write(jsBody);
|
|
jsonVal["cmdBody"] = dataBody;
|
|
return showValue.write(jsonVal);
|
|
}
|
|
|
|
int SqliteDB::QueryofflineData()
|
|
{
|
|
array_t arrRetdataNode = GetDataMultiLine(T_SENSOR_INFO(TNAME)," dataNodeNo,MeasurementID ",NULL);
|
|
for(int i = 0; i < arrRetdataNode.size();i++){
|
|
char StaticTableName[50]={0x00};
|
|
sprintf(StaticTableName,"t_dataStatic_%s",arrRetdataNode[i][0].c_str());
|
|
|
|
array_t arrRetData = GetDataMultiLine(StaticTableName, "*","sendMsg = '0' order by timeStamp asc");
|
|
LOG_INFO("mqttresend check datanodeNo %s,data count %d\n",arrRetdataNode[i][0].c_str(),arrRetData.size());
|
|
if(arrRetData.size() < 1)
|
|
continue;
|
|
for(int j = 0; j < arrRetData.size();j++){
|
|
char dataTableName[50]={0x00};
|
|
sprintf(dataTableName,"t_data_%s",arrRetdataNode[i][0].c_str());
|
|
char tmpWhere[128]={0x00};
|
|
sprintf(tmpWhere,"sendMsg = '0' and timeStamp = '%s'",arrRetData[j][8].c_str());
|
|
array_t arrRet = GetDataMultiLine(dataTableName, "*","sendMsg = '0'");
|
|
if(arrRet.size() > 0){
|
|
Json::Value valNodeData;
|
|
Json::Value valNodeFeature;
|
|
for(int k = 0; k < arrRet.size();k++){
|
|
|
|
valNodeFeature["dataNodeNo"] = arrRetdataNode[i][1].c_str();
|
|
valNodeFeature["ChannelId"] = arrRet[k][1].c_str();
|
|
valNodeFeature["diagnosisPk"] = atof(arrRet[k][2].c_str());
|
|
valNodeFeature["integratPk"] = atof(arrRet[k][3].c_str());
|
|
valNodeFeature["integratRMS"] = atof(arrRet[k][4].c_str());
|
|
valNodeFeature["rmsValues"] = atof(arrRet[k][5].c_str());
|
|
valNodeFeature["envelopEnergy"] = atof(arrRet[k][6].c_str());
|
|
valNodeFeature["Amp1"] = atof(arrRet[k][7].c_str());
|
|
valNodeFeature["Amp2"] = atof(arrRet[k][8].c_str());
|
|
valNodeFeature["Amp3"] = atof(arrRet[k][9].c_str());
|
|
valNodeFeature["Amp4"] = atof(arrRet[k][10].c_str());
|
|
valNodeFeature["Amp5"] = atof(arrRet[k][1].c_str());
|
|
valNodeFeature["Phase1"] = atof(arrRet[k][12].c_str());
|
|
valNodeFeature["Phase2"] = atof(arrRet[k][13].c_str());
|
|
valNodeFeature["Phase3"] = atof(arrRet[k][14].c_str());
|
|
valNodeFeature["Phase4"] = atof(arrRet[k][15].c_str());
|
|
valNodeFeature["timeStamp"] = atoi(arrRet[k][17].c_str());
|
|
valNodeData.append(valNodeFeature);
|
|
}
|
|
}
|
|
//无线传感器信息
|
|
Json::Value root;
|
|
Json::Value valdatastatic;
|
|
Json::Value valNodeData;
|
|
valdatastatic["TemperatureTop"] = atof(arrRetData[j][2].c_str());
|
|
valdatastatic["TemperatureBot"] = atof(arrRetData[j][3].c_str());
|
|
valdatastatic["Dip"] = atof(arrRetData[j][4].c_str());
|
|
valdatastatic["Voltage"] = atof(arrRetData[j][5].c_str());
|
|
valdatastatic["ChannelType"] = "STATUS";
|
|
valdatastatic["ChannelId"] = (arrRetData[j][1].c_str());
|
|
valdatastatic["TimeStamp"] = atoi(arrRetData[j][8].c_str());
|
|
|
|
valdatastatic["dataNodeNo"] = arrRetData[j][0].c_str();
|
|
valNodeData.append(valdatastatic);
|
|
|
|
root["data"] = valNodeData;
|
|
root["TimeStamp"] = atoi(arrRetData[j][8].c_str());
|
|
root["dataNodeNo"] = arrRetdataNode[i][1].c_str();
|
|
root["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
|
root["status"] = "resend";
|
|
Json::FastWriter featureValue;
|
|
std::string strstatisticData = featureValue.write(root);
|
|
|
|
int iRet = data_publish(strstatisticData.c_str(), GlobalConfig::Topic_G.mPubData.c_str());
|
|
//print_info("dataNodeNo = '%s' and TimeStamp = '%s',MQTT ret = %d\n",arrRetData[j][0].c_str(),nowTimetamp.c_str(),iRet);
|
|
if(iRet == 0){
|
|
char updateSql[1024] = { 0 };
|
|
char whereCon[64] = { 0 };
|
|
memset(whereCon, 0, 64);
|
|
sprintf(whereCon, "dataNodeNo = '%s' and TimeStamp = '%s'", arrRetData[j][0].c_str(),arrRetData[j][8].c_str());
|
|
memcpy(updateSql, "sendMsg='1'",sizeof(updateSql));
|
|
sql_ctl->UpdateTableData(StaticTableName, updateSql, whereCon);
|
|
sql_ctl->UpdateTableData(dataTableName, updateSql, whereCon);
|
|
//LOG_INFO("resend success dataNodeNo = %s,TimeStamp = '%s'\n",arrRetData[j][0].c_str(),arrRetData[j][8].c_str());
|
|
}
|
|
}
|
|
}
|
|
char whereCon[1024] = {0x00};
|
|
sprintf(whereCon, " SendMsg = 0 ");
|
|
array_t arrRetData = GetDataMultiLine("t_data_waveSend", "*",whereCon);
|
|
LOG_INFO("mqttresend check wave count %d\n",arrRetData.size());
|
|
if(arrRetData.size() > 0){
|
|
for(int i = 0; i < arrRetData.size();i++){
|
|
std::string strWaveData = "";
|
|
if (access(arrRetData[i][1].c_str(), 0) >= 0)
|
|
{
|
|
std::vector<float> vecWave;
|
|
char localtimestamp[32] = { 0 };
|
|
float fTemp = 0;
|
|
std::ifstream inFile(arrRetData[i][1].c_str(),ios::in|ios::binary);
|
|
inFile.read((char *)localtimestamp,sizeof(localtimestamp));
|
|
while(inFile.read((char *)&fTemp,sizeof(fTemp))){
|
|
vecWave.push_back(fTemp);
|
|
}
|
|
|
|
for (int i = 0; i < vecWave.size(); i++) {
|
|
char buf[33]={0x00};
|
|
memset(buf,0x00,sizeof(buf));
|
|
sprintf(buf, "%.2f", vecWave[i]);
|
|
std::string waveTemp(buf);
|
|
if(i == 0){
|
|
strWaveData = waveTemp;
|
|
}
|
|
strWaveData = strWaveData + "," + waveTemp;
|
|
}
|
|
inFile.close();
|
|
}
|
|
Json::Value valWaveData;
|
|
int length = strWaveData.length();
|
|
valWaveData["number"] = length;
|
|
valWaveData["channelId"] = arrRetData[i][0];
|
|
valWaveData["dataNodeNo"] = arrRetData[i][0].substr(0,arrRetData[i][0].length()-2);
|
|
valWaveData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G;
|
|
valWaveData["SensorEngineeringUnit"] = "";
|
|
valWaveData["timeStamp"] = arrRetData[i][2];
|
|
valWaveData["waveData"] = strWaveData;
|
|
Json::FastWriter WaveValue;
|
|
std::string WaveData = WaveValue.write(valWaveData);
|
|
int iRet = data_publish(WaveData.c_str(), GlobalConfig::Topic_G.mPubWaveData.c_str());
|
|
if(iRet == 0){
|
|
char whereCon[1024] = {0x00};
|
|
char updateSql[1024] = {0x00};
|
|
sprintf(whereCon, "channelID='%s' and timeStamp = '%s' ", arrRetData[i][0].c_str(),arrRetData[i][2].c_str());
|
|
sprintf(updateSql, "SendMsg = 1");
|
|
sql_ctl->UpdateTableData("t_data_waveSend", updateSql, whereCon);
|
|
//char whereCon[64]={0x00};
|
|
//sprintf(whereCon,"waveName = '%s'",arrRetData[i][1].c_str());
|
|
//DeleteTableData("t_data_waveSend", whereCon);
|
|
std::string strCmd = "rm " + arrRetData[i][1];
|
|
system(strCmd.c_str());
|
|
//LOG_INFO("resend success waveName = %s\n",arrRetData[i][1].c_str());
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
int SqliteDB::CalculateData()
|
|
{
|
|
array_t vecRet = GetDataMultiLine(T_SENSOR_INFO(TNAME)," dataNodeNo,MeasurementID,NodeWaveSend,featureInterVal,waveInterVal,ZigbeePower ",NULL);
|
|
if (vecRet.size() > 0)
|
|
{
|
|
|
|
int planCount = 0,planCountStatistic = 0,StatisticCountTotal = 0,SixCountTotal = 0,TimingCountTotal = 0,CountWaveXTotal = 0,waveInterVal = 0,featureInterVal = 0;
|
|
int CountWaveYTotal = 0 , CountWaveZTotal = 0;
|
|
for (size_t i = 0; i < vecRet.size(); i++)
|
|
{
|
|
planCount = 1440 / atoi(vecRet[i][4].c_str()) ;
|
|
planCountStatistic = 1440 / atoi(vecRet[i][3].c_str()) ;
|
|
int rate = readIntValue( "config", "waveRate",(char*)GlobalConfig::Config_G.c_str());
|
|
char tableName[50]={0x00};
|
|
char whereCon[128]={0x00};
|
|
sprintf(tableName,"t_dataStatic_%s",vecRet[i][0].c_str());
|
|
const char *sql =
|
|
" timestamp >= strftime('%s', 'now', '-1 day', 'start of day','utc') "
|
|
"AND timestamp < strftime('%s', 'now', '-1 day','start of day','utc','+24 hours') ";
|
|
int StatisticCount = GetTableRows(tableName,sql);
|
|
|
|
sql = "timeStamp >= strftime('%s', 'now', '-1 day', 'start of day','utc') AND timeStamp < strftime('%s', 'now', '-1 day','start of day','utc','+24 hours') and statisticType = '1' ";
|
|
int SixCount = GetTableRows(tableName,sql);
|
|
|
|
sql = "timeStamp >= strftime('%s', 'now', '-1 day', 'start of day','utc') AND timeStamp < strftime('%s', 'now', '-1 day','start of day','utc','+24 hours') and statisticType = '1' and timing = '0'";
|
|
int TimingCount = GetTableRows(tableName,sql);
|
|
print_info("nodeWaveSend = %s,waveInterVal = %s\n",vecRet[i][2].c_str(),vecRet[i][4].c_str());
|
|
if(vecRet[i][2] == "")
|
|
continue;
|
|
vector<string> nodeWaveSend ;
|
|
boost::split( nodeWaveSend, vecRet[i][2], boost::is_any_of( "," ), boost::token_compress_on );
|
|
int CountWaveX = 0,CountWaveY = 0,CountWaveZ = 0;
|
|
int CountWaveX2 = 0,CountWaveY2 = 0,CountWaveZ2 = 0;
|
|
int CountWaveX3 = 0,CountWaveY3 = 0,CountWaveZ3 = 0;
|
|
int rateX = 0,rateY = 0,rateZ = 0;
|
|
if (nodeWaveSend.size() < 3)
|
|
{
|
|
continue;
|
|
}
|
|
sprintf(tableName,"t_data_waveSend");
|
|
const char *sql1 =
|
|
"timeStamp >= strftime('%s', 'now', '-1 day', 'start of day','utc')"
|
|
" AND timeStamp < strftime('%s', 'now','-1 day', 'start of day','utc','+24 hours')";
|
|
|
|
const char *sql2 =
|
|
"timeStamp >= strftime('%s', 'now', '-2 day', 'start of day','utc')"
|
|
" AND timeStamp < strftime('%s', 'now','-2 day', 'start of day','utc','+24 hours')";
|
|
|
|
const char *sql3 =
|
|
"timeStamp >= strftime('%s', 'now', '-3 day', 'start of day','utc')"
|
|
" AND timeStamp < strftime('%s', 'now','-3 day', 'start of day','utc','+24 hours')";
|
|
if (nodeWaveSend[0] == "0")
|
|
{
|
|
sprintf(whereCon," and channelID = '%s-X'",vecRet[i][1].c_str());
|
|
string strsql = string(sql1) + string(whereCon);
|
|
printf("sql = %s\n",strsql.c_str());
|
|
CountWaveX = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-X'",vecRet[i][1].c_str());
|
|
strsql = string(sql2) + string(whereCon);
|
|
CountWaveX2 = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-X'",vecRet[i][1].c_str());
|
|
strsql = string(sql3) + string(whereCon);
|
|
CountWaveX3 = GetTableRows(tableName,strsql.c_str());
|
|
if ((CountWaveX > 0 && (CountWaveX / planCount > (rate/100))) && (CountWaveX2 > 0 && (CountWaveX2 / planCount > (rate/100))) && (CountWaveX3 > 0 && (CountWaveX3 / planCount > (rate/100))))
|
|
{
|
|
rateX = 1;
|
|
}else{
|
|
rateX = -1;
|
|
}
|
|
|
|
}
|
|
if(nodeWaveSend[1] == "0"){
|
|
|
|
sprintf(whereCon," and channelID = '%s-Y'",vecRet[i][1].c_str());
|
|
string strsql = string(sql1) + string(whereCon);
|
|
CountWaveY = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-Y'",vecRet[i][1].c_str());
|
|
strsql = string(sql2) + string(whereCon);
|
|
CountWaveY2 = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-Y'",vecRet[i][1].c_str());
|
|
strsql = string(sql3) + string(whereCon);
|
|
CountWaveY3 = GetTableRows(tableName,strsql.c_str());
|
|
|
|
if ((CountWaveY > 0 && (CountWaveY / planCount > (rate/100))) && (CountWaveY2 > 0 && (CountWaveY2 / planCount > (rate/100))) && (CountWaveY3 > 0 && (CountWaveY3 / planCount > (rate/100))))
|
|
{
|
|
rateY = 1;
|
|
}else{
|
|
rateY = -1;
|
|
}
|
|
|
|
}
|
|
if(nodeWaveSend[2] == "0"){
|
|
|
|
sprintf(whereCon," and channelID = '%s-Z'",vecRet[i][1].c_str());
|
|
string strsql = string(sql1) + string(whereCon);
|
|
CountWaveZ = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-Z'",vecRet[i][1].c_str());
|
|
strsql = string(sql2) + string(whereCon);
|
|
CountWaveZ2 = GetTableRows(tableName,strsql.c_str());
|
|
sprintf(whereCon," and channelID = '%s-Z'",vecRet[i][1].c_str());
|
|
strsql = string(sql3) + string(whereCon);
|
|
CountWaveZ3 = GetTableRows(tableName,strsql.c_str());
|
|
|
|
if ((CountWaveZ > 0 && (CountWaveZ / planCount > (rate/100))) && (CountWaveZ2 > 0 && (CountWaveZ2 / planCount > (rate/100))) && (CountWaveZ3 > 0 && (CountWaveZ3 / planCount > (rate/100))))
|
|
{
|
|
rateZ = 1;
|
|
}else{
|
|
rateZ = -1;
|
|
}
|
|
}
|
|
StatisticCountTotal += StatisticCount;
|
|
SixCountTotal += SixCount;
|
|
TimingCountTotal += TimingCount;
|
|
CountWaveXTotal += CountWaveX;
|
|
CountWaveYTotal += CountWaveY;
|
|
CountWaveZTotal += CountWaveZ;
|
|
LOG_INFO("dataNodeNo = %s , Statistic Count = %d , 06 Count = %d , Timing Count = %d , CountWaveX = %d , CountWaveY = %d , CountWaveZ = %d , featureInterVal = %s , waveInterVal = %s , NodeWaveSend = %s\n"\
|
|
,vecRet[i][0].c_str(),StatisticCount,SixCount,TimingCount,CountWaveX,CountWaveY,CountWaveZ,vecRet[i][3].c_str(),vecRet[i][4].c_str(),vecRet[i][2].c_str());
|
|
|
|
int zigbeepowerEnable = readIntValue( "config", "zigbeepowerEnable",(char*)GlobalConfig::Config_G.c_str());
|
|
if(zigbeepowerEnable){
|
|
LOG_INFO("rateX = %d,rateY = %d,rateZ = %d,zigbeepower = %s \n",rateX,rateY,rateZ,vecRet[i][5].c_str());
|
|
if (rateX != -1 && rateY != -1 && rateZ != -1 && vecRet[i][5] == "3")
|
|
{
|
|
char updateSql[50] = {0x00};
|
|
sprintf(updateSql,"ZigbeePower = '2',UpdateFlag = 0 ");
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon," dataNodeNo = '%s'",vecRet[i][0].c_str());
|
|
UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
|
LOG_INFO("2 update ZigbeePower ");
|
|
}
|
|
|
|
if ((rateX == -1 || rateY == -1 || rateZ == -1 ) && vecRet[i][5] == "2")
|
|
{
|
|
char updateSql[50] = {0x00};
|
|
sprintf(updateSql,"ZigbeePower = '3',UpdateFlag = 0 ");
|
|
memset(whereCon,0x00,sizeof(whereCon));
|
|
sprintf(whereCon," dataNodeNo = '%s'",vecRet[i][0].c_str());
|
|
UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
|
|
LOG_INFO("3 update ZigbeePower ");
|
|
}
|
|
}
|
|
featureInterVal = atoi(vecRet[i][3].c_str());
|
|
waveInterVal = atoi(vecRet[i][4].c_str());
|
|
}
|
|
LOG_INFO("Node Count = %d , featureInterVal = %d , waveInterVal = %d\n",vecRet.size(),featureInterVal,waveInterVal);
|
|
LOG_INFO("plan Statistic Count = %d , 06 Count = %d , Timing Count = %d , CountWaveX = %d ",planCountStatistic * vecRet.size(),planCount * vecRet.size(),planCount * vecRet.size(),planCount * vecRet.size());
|
|
LOG_INFO("reality Statistic Count = %d , 06 Count = %d , Timing Count = %d , CountWaveX = %d ,CountWaveY = %d ,CountWaveZ = %d ",StatisticCountTotal,SixCountTotal,TimingCountTotal,CountWaveXTotal,CountWaveYTotal,CountWaveZTotal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int SqliteDB::TransBegin()
|
|
{
|
|
return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0);
|
|
}
|
|
|
|
int SqliteDB::TransRollback()
|
|
{
|
|
return sqlite3_exec(mDBAcess, "rollback;", 0, 0, 0);
|
|
}
|
|
|
|
int SqliteDB::TransCommit()
|
|
{
|
|
return sqlite3_exec(mDBAcess, "commit;", 0, 0, 0);
|
|
}
|
|
|
|
void SqliteDB::HandError(int code)
|
|
{
|
|
if (code == SQLITE_CORRUPT) {
|
|
/*
|
|
* 用system 删除数据库
|
|
*/
|
|
exit(1);
|
|
}
|
|
}
|