添加QT的webengineview开发者调试功能

This commit is contained in:
lixiaoyuan
2025-09-13 17:28:35 +08:00
parent 7f23138d9c
commit d1a8fb0665
18 changed files with 653 additions and 218 deletions

View File

@@ -33,13 +33,13 @@ std::string ElectPeriod::dump()
}
void AppData::initFromDB()
bool AppData::initFromDB()
{
auto dao = DaoEntity::create("");
if (!dao->isConnected())
{
spdlog::error("Init app data failed, database connected error.");
return;
return false;
}
std::string str;
@@ -235,26 +235,17 @@ void AppData::initFromDB()
}
}
}
return true;
}
void AppData::init()
bool AppData::init()
{
this->initFromDB();
bool ret = this->initFromDB();
if (!ret) { return false; }
auto& optionMqtt = Config::option.mqtt;
if (!optionMqtt.host.empty())
{
for (auto& item : mapStation)
{
auto& station = item.second;
if (station->status == 1)
{
// "tcp://localhost:1883"
station->mqttCli->init(optionMqtt.host, station->code, optionMqtt.username, optionMqtt.password);
}
}
}
this->launchDate = Config::option.lunchDate;
return true;
}
std::shared_ptr<Station> AppData::getStation(int stationId)

View File

@@ -57,8 +57,8 @@ public:
class AppData
{
public:
void init();
void initFromDB();
bool init();
bool initFromDB();
// 读取统计数据: 今日统计数据,累计统计数据
void loadStatData();

View File

@@ -18,6 +18,8 @@ void Application::init()
// MQTT 数据结构
MqttClient::loadDataStruct("assets/config/registeraddr.json");
// 设备读取寄存器的地址定义
Device::loadParamAddr("assets/config/monitoraddr.json");
// 设置数据库配置
DaoEntity::setOption(Config::option.database.host,
@@ -34,7 +36,7 @@ void Application::init()
// 连接数据库,读取基础信息
// 初始化系统基础数据
appdata.init();
this->isInit = appdata.init();
// 创建设备处理线程
std::thread([=]() { runThreadDevice(); }).detach();
@@ -47,11 +49,11 @@ void Application::init()
}
}).detach();
// 创建主业务循环线程
std::thread([=]() { runThreadMain(); }).detach();
// 统计分析
std::thread([=]() { runThreadStat(); }).detach();
// 创建主业务循环线程
std::thread([=]() { runThreadMain(); }).detach();
}
@@ -71,47 +73,37 @@ void Application::runThreadMain()
while (!isQuit)
{
//// 连接场站
//static TimeTick ttStation;
//if (ttStation.elapse(10000))
//{
// if (!mqttCli->isConnected)
// {
// }
// else
// {
// for (auto& item: appdata.mapStation)
// {
// auto station = item.second;
// if (station && !station->isConnected)
// {
// std::string stationCode = station->code;
// std::vector<std::string> vecTopics = {
// "up/json" + stationCode + "/EMS_YX",
// "up/json" + stationCode + "/EMS_YC",
// "up/json" + stationCode + "/PCU_YX",
// "up/json" + stationCode + "/PCU_YC",
// };
// mqttCli->subscribe(vecTopics, [=](int id)
// {
// station->isConnected = (id == 0);
// });
// }
// break;
// }
// }
//}
if (!this->isInit) // 初始化失败
{
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
this->isInit = appdata.init();
if (!this->isInit) { continue; }
}
static TimeTick ttMqtt;
// 检查 场站的 MQTT 连接
if (ttMqtt.elapse(10))
{
auto& optionMqtt = Config::option.mqtt;
if (!optionMqtt.host.empty())
{
for (auto& item : appdata.mapStation)
{
if (item.second)
{
item.second->initMqtt();
//item.second->polling();
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
/// 召测
static TimeTick tt1;
if (tt1.elapse(10))
{
for (auto& item: appdata.mapStation)
{
auto& station = item.second;
station->polling();
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

View File

@@ -33,6 +33,7 @@ public:
public:
bool isQuit = false;
bool isInit = false;
// 登录的管理员信息
Operator op;

View File

@@ -7,6 +7,19 @@
#include <unordered_set>
std::map<int, std::vector<DeviceParamAddr>> Device::s_mapDeviceParamAddr;
static std::vector<DeviceParamAddr>& GetDeviceParamAddrs(int deviceType)
{
static std::vector<DeviceParamAddr> vecAddrs = {};
auto iter = Device::s_mapDeviceParamAddr.find(deviceType);
if (iter != Device::s_mapDeviceParamAddr.end())
{
return iter->second;
}
return vecAddrs;
}
static std::unordered_set<int> g_setCacheDeviceType = {3, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
static bool CheckCacheType(int type)
{
@@ -20,6 +33,41 @@ std::shared_ptr<Device> Device::create(Fields& fields)
return device;
}
void Device::loadParamAddr(std::string filename)
{
try
{
njson json;
if (!JSON::load(filename, json))
{
spdlog::error("[device] json load param addr error, filename={}", filename);
}
for (auto& jsonitem : json.items())
{
std::string key = jsonitem.key();
auto& jsonnodeItem = jsonitem.value();
spdlog::info(jsonnodeItem.dump());
int type = jsonnodeItem["deviceType"];
auto& vec = s_mapDeviceParamAddr[type];
for (auto& v : jsonnodeItem["addr_YC"])
{
std::string name = JSON::get<std::string>(v[0]);
std::string addr = JSON::get<std::string>(v[1]);
std::string defaultVal = JSON::get<std::string>(v[2]);
std::string unit = JSON::get<std::string>(v[3]);
float ratio = Utils::toFloat(JSON::get<string>(v[4]));
vec.push_back(DeviceParamAddr(name, addr, defaultVal, unit, ratio));
}
}
}
catch (nlohmann::json::parse_error& e)
{
spdlog::error("[device] parse [{}] error: ", filename, e.what());
}
}
void Device::setFields(Fields& fields)
{
fields.get("device_id", this->deviceId);
@@ -53,6 +101,12 @@ void Device::setFields(Fields& fields)
}
}
}
auto& vecAddrs = GetDeviceParamAddrs(this->type);
for (auto& item: vecAddrs)
{
this->mapMyParams[item.addr] = &item;
}
}
int Device::startComm()
@@ -174,42 +228,47 @@ void Device::storeDB(int npos)
{
}
void Device::setParam(std::string k, std::string v)
void Device::setParam(std::string k, int v)
{
mapParams[k] = v;
float ratio = 1.0;
auto iter = mapMyParams.find(k);
if (iter != mapMyParams.end())
{
ratio = iter->second->ratio;
spdlog::info("[device] set param: {} {}={}, ratio={}", iter->second->name, k, v, ratio);
}
int precision = (ratio != 1.0f) ? 2 : 0;
mapParams[k] = Utils::toStr(v*ratio, precision);
if (type == 3 ) // 电表
{
if (k == "") this->err = Utils::toInt(v);
if (k == "") this->err = v;
}
else if (type == 101) // EMS
{
}
else if (type == 102) // PCS
{
if (k == "0x1003") err = Utils::toInt(v); // 故障状态 R uint16 1故障0正常 0 0x1003
if (k == "0x1005") online = Utils::toInt(v); // 设备在线 R uint16 1在线0无效 1 0x1005
if (k == "0x1009") running = (v=="1" || v=="2"); //充放状态 R uint16 0待机, 1充电, 2放电, 3搁置 0 0x1009
if (k == "0x1003") err = v; // 故障状态 R uint16 1故障0正常 0 0x1003
if (k == "0x1005") online = v; // 设备在线 R uint16 1在线0无效 1 0x1005
if (k == "0x1009") running = (v==1 || v==2); //充放状态 R uint16 0待机, 1充电, 2放电, 3搁置 0 0x1009
}
else if (type == 103) // PCU
{
if (k == "0x1002") err = Utils::toInt(v); //故障状态 R uint16 1故障0正常 0 0x1002
if (k == "0x1004") online = Utils::toInt(v); //设备在线 R uint16 1在线0无效 1 0x1004
if (k == "0x1006") running = Utils::toInt(v); //启停状态 R uint16 1开机0关机 1 0x1006
if (k == "0x1002") err = v; //故障状态 R uint16 1故障0正常 0 0x1002
if (k == "0x1004") online = v; //设备在线 R uint16 1在线0无效 1 0x1004
if (k == "0x1006") running = v; //启停状态 R uint16 1开机0关机 1 0x1006
}
else if (type == 104) // BMS
{
if (k == "0x004A") { err = (v=="1"); online = 1; } //运行状态 R uint16 0 运行状态 0-正常 1-告警 2-保护 0x004A
if (k == "0x004B") running = (v=="1" || v=="2"); //充放电状态 R uint16 0 0-待机 1-充电 2-放电 0x004B
if (k == "0x004A") { err = (v==1); online = 1; } //运行状态 R uint16 0 运行状态 0-正常 1-告警 2-保护 0x004A
if (k == "0x004B") running = (v==1 || v==2); //充放电状态 R uint16 0 0-待机 1-充电 2-放电 0x004B
}
else if (type == 105) // BCU
{
if (k == "0xA003") running = (v=="51" || v=="68"); //蓄电池充放电状态 R uint16 "0x11开路,0x22待机,0x33充电,0x44放电" 34 0xA003
if (k == "0xA004") err = (v=="85"); online=1; //电池组运行状态 R uint16 "0x11跳机,0x22待机,0x33放空,0x44充满,0x55预警,0x66正常" 102 0xA004
if (k == "0xA003") running = (v==0x33 || v==0x44); //蓄电池充放电状态 R uint16 "0x11开路,0x22待机,0x33充电,0x44放电" 34 0xA003
if (k == "0xA004") err = (v==0x55); online=1; //电池组运行状态 R uint16 "0x11跳机,0x22待机,0x33放空,0x44充满,0x55预警,0x66正常" 102 0xA004
}
else if (type == 106) // 充电桩
{
@@ -229,7 +288,6 @@ std::string Device::getParam(std::string k, std::string defaultVal)
return defaultVal;
}
void Device::getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params)
{
// 3 电表
@@ -240,71 +298,23 @@ void Device::getRuntimeParams(std::vector<std::pair<std::string, std::string>>&
// 105 BCU
// 106 充电桩
// 109 光伏板
if (this->type == 3)
auto& vecAddr = s_mapDeviceParamAddr[this->type];
for (auto& itemAddr: vecAddr)
{
params.push_back({"A相电压", getParam("0x000B", "0.0") + " V"});
params.push_back({"A相电流", getParam("0x000D", "0.0") + " A"});
params.push_back({"B相电压", getParam("0x000F", "0.0") + " V"});
params.push_back({"B相电流", getParam("0x0011", "0.0") + " A"});
params.push_back({"C相电压", getParam("0x0013", "0.0") + " V"});
params.push_back({"C相电流", getParam("0x0015", "0.0") + " A"});
}
else if (this->type == 101) // EMS
{
params.push_back({"A相电压", getParam("0x107E", "0.0") + " V"});
params.push_back({"A相电流", getParam("0x1084", "0.0") + " A"});
params.push_back({"B相电压", getParam("0x1080", "0.0") + " V"});
params.push_back({"B相电流", getParam("0x1086", "0.0") + " A"});
params.push_back({"C相电压", getParam("0x1082", "0.0") + " V"});
params.push_back({"C相电流", getParam("0x1088", "0.0") + " A"});
}
else if (this->type == 102) // PCS
{
params.push_back({"A相电压", getParam("0x0010", "0.0") + " V"});
params.push_back({"A相电流", getParam("0x0019", "0.0") + " A"});
params.push_back({"B相电压", getParam("0x0011", "0.0") + " V"});
params.push_back({"B相电流", getParam("0x001A", "0.0") + " A"});
params.push_back({"C相电压", getParam("0x0011", "0.0") + " V"});
params.push_back({"C相电流", getParam("0x001B", "0.0") + " A"});
}
else if (this->type == 103) // PCU
{
params.push_back({"A相电压", getParam("0x0013", "0.0") + " V"});
params.push_back({"A相电流", getParam("0x001C", "0.0") + " A"});
params.push_back({"B相电压", getParam("0x0014", "0.0") + " V"});
params.push_back({"B相电流", getParam("0x001D", "0.0") + " A"});
params.push_back({"C相电压", getParam("0x0015", "0.0") + " V"});
params.push_back({"C相电流", getParam("0x001E", "0.0") + " A"});
}
else if (this->type == 104) // BMS
{
params.push_back({"SOC", getParam("0x0001", "0") + " %"});
params.push_back({"SOH", getParam("0x0002", "0") + " %"});
params.push_back({"电压", getParam("0x0003", "0.0") + " V"});
params.push_back({"电流", getParam("0x0005", "0.0") + " A"});
params.push_back({"单体最大电压", getParam("0x0021", "0.0") + " V"});
params.push_back({"单体最小电压", getParam("0x0024", "0.0") + " V"});
params.push_back({"单体最大温度", getParam("0x0029", "0.0") + ""});
params.push_back({"单体最小温度", getParam("0x002C", "0.0") + ""});
}
else if (this->type == 105) // BCU
{
params.push_back({"簇电压", getParam("0x0003", "0.0") + " V"});
params.push_back({"簇电流", getParam("0x0005", "0") + " A"});
params.push_back({"簇温度", getParam("0x0007", "0.0") + ""});
params.push_back({"簇电阻", getParam("0x0009", "0.0") + " Ω"});
params.push_back({"簇SOC", getParam("0x000B", "0") + " %"});
params.push_back({"簇SOH", getParam("0x000C", "0") + " %"});
}
else
{
params.push_back({"额定电压", getParam("0x0001", "0.0") + " V"});
params.push_back({"实时电压", getParam("0x0001", "0.0") + " V"});
params.push_back({"额定电流", getParam("0x0001", "0.0") + " A"});
params.push_back({"实时电流", getParam("0x0001", "0.0") + " A"});
params.push_back({"额定功率", getParam("0x0001", "0.0") + " W"});
params.push_back({"实时功率", getParam("0x0001", "0.0") + " W"});
params.push_back({itemAddr.name, getParam(itemAddr.addr, itemAddr.defaultVal) + itemAddr.unit});
}
}
void Device::getRuntimeParams1(std::vector<std::pair<std::string, std::string>>& params)
{
if (type == 106)
{
params.push_back({"需求电压", getParam("31072", "0.0") + " V"});
params.push_back({"需求电流", getParam("31074", "0.0") + " A"});
params.push_back({"需求功率", getParam("31076", "0.0") + " kW"});
params.push_back({"功率限值", getParam("31078", "0.0") + " kW"});
params.push_back({"输出电压", getParam("31080", "0.0") + " V"});
params.push_back({"输出电流", getParam("31082", "0.0") + " A"});
params.push_back({"输出功率", getParam("31084", "0.0") + " kW"});
}
}

View File

@@ -1,20 +1,39 @@
#pragma once
#include <string>
#include <set>
#include <map>
#include <vector>
#include <memory>
#include <string>
#include <unordered_map>
#include "common/Fields.h"
class CommEntity;
struct DeviceParamAddr
{
std::string name;
std::string addr;
std::string defaultVal;
std::string unit;
float ratio {1.0};
DeviceParamAddr() {};
DeviceParamAddr(std::string name, std::string addr, std::string defaultVal, std::string unit, float ratio=1.0f)
: name(name), addr(addr), defaultVal(defaultVal), unit(unit), ratio(ratio)
{
if (this->ratio == 0.0)
{
this->ratio = 1.0f;
}
};
};
class Device
{
public:
static std::shared_ptr<Device> create(Fields& fields);
static void loadParamAddr(std::string filename);
void setFields(Fields& fields);
@@ -30,13 +49,15 @@ public:
bool cache(int npos);
void storeDB(int npos);
void setParam(std::string k, std::string v);
void setParam(std::string k, int v);
std::string getParam(std::string k, std::string defaultVal = "");
void getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params);
void getRuntimeParams1(std::vector<std::pair<std::string, std::string>>& params);
public:
static std::map<int, std::vector<DeviceParamAddr>> s_mapDeviceParamAddr;
int deviceId = -1;
int type = -1;
std::string name;
@@ -60,4 +81,8 @@ public:
std::map<int, double> mapCacheCurrent;
std::map<int, double> mapCachePower;
std::map<std::string, std::string> mapParams;
std::map<std::string, DeviceParamAddr*> mapMyParams;
};

View File

@@ -7,6 +7,7 @@
#include "common/Utils.h"
#include "protocol/MqttEntity.h"
#include "common/JsonN.h"
#include "app/Config.h"
Station::Station() : stationId(0)
{
@@ -181,6 +182,14 @@ void Station::writeRuntimeData(std::string dt, int npos)
}
}
void Station::initMqtt()
{
if (status!=0 && mqttCli)
{
auto& optionMqtt = Config::option.mqtt;
mqttCli->init(optionMqtt.host, code, optionMqtt.username, optionMqtt.password);
}
}
void Station::polling()
{
@@ -188,4 +197,20 @@ void Station::polling()
{
mqttCli->polling();
}
}
void Station::setGarewayWorkMode()
{
if (!mqttCli)
{
return;
}
njson json;
json["ts"] = Utils::time();
json["no"] = 1; // 设备编号
json["40001"] = this->workModeId;
std::string text = json.dump();
mqttCli->publish("Gateway_YT", text);
}

View File

@@ -109,12 +109,15 @@ public:
void writeRuntimeData(std::string dt, int npos);
void initMqtt();
void polling();
void setGarewayWorkMode();
public:
int stationId {};
std::string name;
std::string code;
bool isOpen {false};
int status {0};
bool isConnected {false};