实现启动splash画面,实现天历史数据的处理和数据库存贮

This commit is contained in:
lixiaoyuan
2025-09-09 19:26:05 +08:00
parent 5c94e2098a
commit b2338f21b0
29 changed files with 426 additions and 71 deletions

View File

@@ -113,9 +113,9 @@ void AppData::initFromDB()
{
auto station = std::make_shared<Station>();
station->setFields(fields);
this->mapStation[station->id] = station;
mapping.stationName.push_back({std::to_string(station->id), station->name});
str += ("场站: {" + std::to_string(station->id) + ":" + station->name + "},");
this->mapStation[station->stationId] = station;
mapping.stationName.push_back({std::to_string(station->stationId), station->name});
str += ("场站: {" + std::to_string(station->stationId) + ":" + station->name + "},");
}
spdlog::info(str);
}
@@ -210,6 +210,34 @@ void AppData::initFromDB()
}
}
}
{ // 初始化场站设备的历史监测数据
vector<Fields> result;
DAO::queryRuntimeData(dao, Utils::dateStr(), result);
for (auto& item : result)
{
int stationId = item.get<int>("station_id");
int deviceId = item.get<int>("device_id");
auto device = this->getDevice(stationId, deviceId);
if (device)
{
int datatype = item.get<int>("datatype");
std::string value = item.value("value");
njson json;
if (JSON::parse(value, json))
{
std::vector<double> vecVal(json.size());
for (int i=0; i<json.size(); ++i)
{
vecVal[i] = JSON::get<double>(json[i]);
}
device->setCache(datatype, vecVal);
}
}
}
}
}
void AppData::init()

View File

@@ -49,6 +49,9 @@ void Application::init()
// 创建主业务循环线程
std::thread([=]() { runThreadMain(); }).detach();
// 统计分析
std::thread([=]() { runThreadStat(); }).detach();
}
@@ -56,7 +59,7 @@ void Application::runThreadDevice()
{
while (!isQuit)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
@@ -101,4 +104,32 @@ void Application::runThreadMain()
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void Application::runThreadStat()
{
int nCachePos = 0;
while (!isQuit)
{
int64_t tTime = Utils::time();
int64_t tDate = Utils::date();
int64_t delta = tTime-tDate;
int n = delta / 600;
int offset = delta % 600;
if (delta >=0 && delta < 86400 && offset <= 10 && n != nCachePos)
{
nCachePos = n;
std::string dt = Utils::dateStr(tDate);
for (auto item: appdata.mapStation)
{
item.second->writeRuntimeData(dt, nCachePos);
}
}
else
{
spdlog::info("保存历史数据倒计时: {}", 600 - offset);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}

View File

@@ -29,6 +29,8 @@ public:
void runThreadDevice();
void runThreadStat();
public:
bool isQuit = false;

View File

@@ -5,6 +5,13 @@
#include "protocol/CommEntity.h"
#include "common/JsonN.h"
#include <unordered_set>
static std::unordered_set<int> g_setCacheDeviceType = {3, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
static bool CheckCacheType(int type)
{
return g_setCacheDeviceType.find(type) != g_setCacheDeviceType.end();
}
std::shared_ptr<Device> Device::create(Fields& fields)
{
@@ -40,15 +47,15 @@ std::shared_ptr<Device> Device::create(Fields& fields)
}
}
int step = 600;
for (int i = 0; i*600<86400; ++i)
{
double voltage = double(Utils::random(20000, 30000))*0.01;
double current = double(Utils::random(1000, 2000))*0.01;
device->mapCacheVoltage[i*step] = voltage;
device->mapCacheCurrent[i*step] = current;
device->mapCachePower[i*step] = voltage * current;
}
//int step = 600;
//for (int i = 0; i*600<86400; ++i)
//{
// double voltage = double(Utils::random(20000, 30000))*0.01;
// double current = double(Utils::random(1000, 2000))*0.01;
// device->mapCacheVoltage[i*step] = voltage;
// device->mapCacheCurrent[i*step] = current;
// device->mapCachePower[i*step] = voltage * current;
//}
// 启动通讯该函数中会自动判断isOpen状态选择是否进行通讯连接
//device->startComm();
@@ -117,6 +124,64 @@ void Device::getCachePower(std::vector<std::string>& vec)
}
}
int64_t GetCurrentTimePos(int step)
{
auto tp = chrono::system_clock::now();
int64_t tTime = chrono::time_point_cast<chrono::seconds>(tp).time_since_epoch().count();
std::time_t t = chrono::system_clock::to_time_t(tp);
std::tm* tmlocal = localtime(&t);
tmlocal->tm_hour = 0;
tmlocal->tm_min = 0;
tmlocal->tm_sec = 0;
int64_t tDate = chrono::time_point_cast<chrono::seconds>(chrono::system_clock::from_time_t(mktime(tmlocal))).time_since_epoch().count();
return (tTime - tDate) / step;
}
void Device::setCache(int datatype, std::vector<double>& vec)
{
std::map<int, double>* mapptr = NULL;
if (datatype == 1) { mapptr = &mapCacheVoltage; }
else if (datatype == 2) { mapptr = &mapCacheCurrent; }
else if (datatype == 3) { mapptr = &mapCachePower; }
if (mapptr)
{
const int step = 600;
const int N = 86400/step;
int n = GetCurrentTimePos(step);
for (int i = 0; i<N; ++i)
{
if (i < vec.size()) { (*mapptr)[i] = vec[i]; }
else if (i <= n) { (*mapptr)[i] = 0; }
}
}
}
bool Device::cache(int npos)
{
if (!CheckCacheType(this->type))
{
return false;
}
if (npos == 0)
{
mapCacheVoltage.clear();
mapCacheCurrent.clear();
mapCachePower.clear();
}
// 根据设备类型从参数(寄存器地址)中读取实时数据进行保存
mapCacheVoltage[npos] = Utils::random(100, 200);
mapCacheCurrent[npos] = Utils::random(100, 200);
mapCachePower[npos] = Utils::random(100, 200);
return true;
}
void Device::storeDB(int npos)
{
}
void Device::setParam(std::string k, std::string v)
{
mapParams[k] = v;

View File

@@ -4,6 +4,7 @@
#include <map>
#include <vector>
#include <memory>
#include <unordered_map>
#include "common/Fields.h"
@@ -15,18 +16,24 @@ class Device
public:
static std::shared_ptr<Device> create(Fields& fields);
int startComm();
void getCacheVoltage(std::vector<std::string>& vec);
void getCacheCurrent(std::vector<std::string>& vec);
void getCachePower(std::vector<std::string>& vec);
// int datatype: 1: 电压2电流3功率
void setCache(int datatype, std::vector<double>& vec);
bool cache(int npos);
void storeDB(int npos);
void setParam(std::string k, std::string v);
std::string getParam(std::string k, std::string defaultVal = "");
void getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params);
public:
int deviceId = -1;
int type = -1;

View File

@@ -6,8 +6,9 @@
#include "common/Spdlogger.h"
#include "common/Utils.h"
#include "protocol/MqttEntity.h"
#include "common/JsonN.h"
Station::Station() : id(0)
Station::Station() : stationId(0)
{
mqttCli = std::make_shared<MqttClient>();
@@ -32,7 +33,7 @@ Station::Station() : id(0)
void Station::setFields(Fields& fields)
{
this->id = fields.get<int>(DMStation::STATION_ID);
this->stationId = fields.get<int>(DMStation::STATION_ID);
this->name = fields.value(DMStation::NAME);
this->energyCapacity = fields.get<double>(DMStation::CAPACITY);
this->workModeId = fields.get<int>(DMStation::WORK_MODE);
@@ -99,7 +100,7 @@ void Station::setWorkMode(int modeId)
this->workModeId = modeId;
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
.update(DMStation::WORK_MODE, std::to_string(modeId))
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
.where(DMStation::STATION_ID + "=" + std::to_string(stationId)).str();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
@@ -111,10 +112,49 @@ void Station::setPolicy(int policyId)
{
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
.update(DMStation::POLICY_ID, std::to_string(policyId))
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
.where(DMStation::STATION_ID + "=" + std::to_string(stationId)).str();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
spdlog::error("set station policy failed.");
}
}
static std::string MapValueToJson(int npos, std::map<int, double>& mapV)
{
njson jsonarray = njson::array();
for (int i = 0; i<=npos; i++)
{
jsonarray.push_back(mapV[i]);
}
return jsonarray.dump();
}
void Station::writeRuntimeData(std::string dt, int npos)
{
auto dao = DaoEntity::create("history_day");
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
{
auto device = iter->second;
if (device->cache(npos))
{
Fields fields;
fields.set("dt", dt);
fields.set("station_id", this->stationId);
fields.set("device_id", device->deviceId);
fields.set("datatype", 1);
fields.set("value", MapValueToJson(npos, device->mapCacheVoltage));
DAO::insertRuntimeData(dao, fields);
fields.set("datatype", 2);
fields.set("value", MapValueToJson(npos, device->mapCacheCurrent));
DAO::insertRuntimeData(dao, fields);
fields.set("datatype", 3);
fields.set("value", MapValueToJson(npos, device->mapCachePower));
DAO::insertRuntimeData(dao, fields);
spdlog::info("[device] write runtime date to database, deviceId={}", device->deviceId);
}
}
}

View File

@@ -105,10 +105,12 @@ public:
void setWorkMode(int modeId);
void setPolicy(int policyId);
void writeRuntimeData(std::string dt, int npos);
public:
int id {};
int stationId {};
std::string name;
std::string code;
bool isConnected {false};