2025-08-20 19:00:22 +08:00
|
|
|
|
#include "AppData.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
#include "common/Utils.h"
|
2025-08-20 19:00:22 +08:00
|
|
|
|
#include "app/Station.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
#include "app/Device.h"
|
2025-08-26 18:36:25 +08:00
|
|
|
|
#include "app/Policy.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
#include "database/Dao.h"
|
2025-08-26 18:36:25 +08:00
|
|
|
|
#include "common/JsonN.h"
|
2025-08-31 14:38:53 +08:00
|
|
|
|
#include "common/Snowflake.h"
|
2025-09-01 20:08:40 +08:00
|
|
|
|
#include "common/Spdlogger.h"
|
2025-09-04 19:31:04 +08:00
|
|
|
|
#include "protocol/MqttEntity.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
void ElectPeriod::parse(std::string jsonstr)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonroot;
|
|
|
|
|
|
JSON::parse(jsonstr, jsonroot);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
JSON::read(jsonroot, "price_super_peak", this->priceSuperPeak);
|
|
|
|
|
|
JSON::read(jsonroot, "price_peak", this->pricePeak);
|
|
|
|
|
|
JSON::read(jsonroot, "price_shoulder", this->priceShoulder);
|
|
|
|
|
|
JSON::read(jsonroot, "price_off_peak", this->priceOffPeak);
|
|
|
|
|
|
JSON::read<std::vector<std::vector<std::string>>>(jsonroot, "periods", this->vecPeriods);
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::string ElectPeriod::dump()
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonroot;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
jsonroot["price_super_peak"] = this->priceSuperPeak;
|
|
|
|
|
|
jsonroot["price_peak"] = this->pricePeak;
|
|
|
|
|
|
jsonroot["price_shoulder"] = this->priceShoulder;
|
|
|
|
|
|
jsonroot["price_off_peak"] = this->priceOffPeak;
|
|
|
|
|
|
jsonroot["periods"] = this->vecPeriods;
|
|
|
|
|
|
return jsonroot.dump();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-13 17:28:35 +08:00
|
|
|
|
bool AppData::initFromDB()
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
auto dao = DaoEntity::create("");
|
|
|
|
|
|
if (!dao->isConnected())
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-09-05 19:44:26 +08:00
|
|
|
|
spdlog::error("Init app data failed, database connected error.");
|
2025-09-13 17:28:35 +08:00
|
|
|
|
return false;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string str;
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
|
|
|
|
|
|
{ // 数据库读取工作模式定义
|
|
|
|
|
|
str = "", result.clear();
|
|
|
|
|
|
DAO::queryWorkModeDef(dao, result);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
this->mapping.workMode.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields: result)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
int workModeId = fields.get<int>(DMDefWorkMode::WORK_MODE_ID);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::string name = fields.value(DMDefWorkMode::NAME);
|
2025-08-28 18:42:37 +08:00
|
|
|
|
this->mapping.workMode.push_back({std::to_string(workModeId), name});
|
|
|
|
|
|
this->mapWorkMode[workModeId] = name;
|
|
|
|
|
|
str += ("工作模式: {" + std::to_string(workModeId)+":" + name + "},");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info(str);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取策略类型定义
|
|
|
|
|
|
str = "", result.clear();
|
|
|
|
|
|
DAO::queryPolicyTypeDef(dao, result);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
this->mapping.policyType.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields: result)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
int policyTypeId = fields.get<int>(DMDefPolicyType::POLICY_TYPE_ID);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::string name = fields.value(DMDefWorkMode::NAME);
|
2025-08-28 18:42:37 +08:00
|
|
|
|
this->mapping.policyType.push_back({std::to_string(policyTypeId), name});
|
|
|
|
|
|
this->mapPolicyType[policyTypeId] = name;
|
|
|
|
|
|
str += ("策略类型: {" + std::to_string(policyTypeId) + ":" + name + "},");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info(str);
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
{ // 数据库读取设备类型定义
|
|
|
|
|
|
str = "", result.clear();
|
|
|
|
|
|
DAO::queryDeviceTypeDef(dao, result);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
mapping.deviceType.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto item = std::make_shared<DeviceType>();
|
|
|
|
|
|
item->typeId = fields.get<int>(DMDefDeviceType::DEVICE_TYPE_ID);
|
|
|
|
|
|
item->name = fields.value(DMDefDeviceType::NAME);
|
2025-08-28 18:42:37 +08:00
|
|
|
|
item->group = fields.value(DMDefDeviceType::GROUP);
|
|
|
|
|
|
item->attr = fields.value(DMDefDeviceType::ATTRS);
|
|
|
|
|
|
item->fieldsAttr.parseJson(item->attr);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
mapDeviceType[item->typeId] = item;
|
|
|
|
|
|
mapping.deviceType.push_back({std::to_string(item->typeId), item->name});
|
|
|
|
|
|
str += ("设备类型: {" + std::to_string(item->typeId) + ":" + item->name + "},");
|
|
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info(str);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取角色定义
|
|
|
|
|
|
str = "", result.clear();
|
|
|
|
|
|
DAO::queryRoleList(dao, result);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
this->mapping.role.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields : result)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto item = std::make_shared<Role>();
|
|
|
|
|
|
item->roleId = fields.get<int>(DMRole::ROLE_ID);
|
|
|
|
|
|
item->name = fields.value(DMRole::NAME);
|
|
|
|
|
|
item->isOpen = fields.get<int>(DMRole::IS_OPEN);
|
|
|
|
|
|
mapRole[item->roleId] = item;
|
|
|
|
|
|
mapping.role.push_back({std::to_string(item->roleId), item->name});
|
|
|
|
|
|
str += ("角色: {" + std::to_string(item->roleId) + ":" + item->name + "},");
|
|
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info(str);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取场站信息
|
|
|
|
|
|
str = "", result.clear();
|
2025-09-14 16:00:30 +08:00
|
|
|
|
std::string sql = "SELECT s.*, p.name policy_name, p.`type` policy_type, p.value FROM station s LEFT JOIN policy p ON s.policy_id=p.policy_id;";
|
|
|
|
|
|
dao->exec(sql, result);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
this->mapping.stationName.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
auto station = std::make_shared<Station>();
|
|
|
|
|
|
station->setFields(fields);
|
2025-09-09 19:26:05 +08:00
|
|
|
|
this->mapStation[station->stationId] = station;
|
2025-09-16 19:38:46 +08:00
|
|
|
|
this->mapping.stationName.push_back({std::to_string(station->stationId), station->name});
|
2025-09-09 19:26:05 +08:00
|
|
|
|
str += ("场站: {" + std::to_string(station->stationId) + ":" + station->name + "},");
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info(str);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取设备信息
|
|
|
|
|
|
str = "", result.clear();
|
2025-09-16 19:38:46 +08:00
|
|
|
|
DAO::queryDeviceList(dao, result, 1);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& fields: result)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
int deviceId = fields.get<int>(DMDevice::DEVICE_ID);
|
|
|
|
|
|
int stationId = fields.get<int>(DMDevice::STATION_ID);
|
|
|
|
|
|
auto station = this->getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
station->addDevice(fields);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::error("init device error: unknown station_id:, device_id=", stationId, deviceId);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-09-16 19:38:46 +08:00
|
|
|
|
for (auto& item : mapStation)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.second->groupDevice();
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取策略信息
|
|
|
|
|
|
str = "", result.clear();
|
|
|
|
|
|
DAO::queryPolicyList(dao, result);
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
2025-09-14 16:00:30 +08:00
|
|
|
|
auto policy = std::make_shared<SysPolicy>();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
policy->policyId = fields.get<int>(DMPolicy::POLICY_ID);
|
|
|
|
|
|
policy->type = fields.get<int>(DMPolicy::TYPE);
|
|
|
|
|
|
policy->name = fields.value(DMPolicy::NAME);
|
|
|
|
|
|
policy->value = fields.value(DMPolicy::VALUE);
|
|
|
|
|
|
this->mapPolicy[policy->policyId] = policy;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
{ // 数据库读取电价分段信息
|
|
|
|
|
|
result.clear();
|
|
|
|
|
|
vecElectPeriods.resize(12);
|
|
|
|
|
|
DAO::exec(dao, "SELECT * FROM configure;", result);
|
|
|
|
|
|
|
|
|
|
|
|
Fields info;
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto k = fields.value("key");
|
|
|
|
|
|
auto v = fields.value("val");
|
|
|
|
|
|
info.set(k, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int month = 1; month<=12; month++)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
if (month-1 < vecElectPeriods.size())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vecItems = vecElectPeriods[month-1];
|
|
|
|
|
|
std::string str = info.value("period_" + std::to_string(month));
|
|
|
|
|
|
std::vector<std::string> vec;
|
|
|
|
|
|
Utils::split(str, ",", vecItems);
|
|
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
electPriceSuperPeak = info.get<double>("price_super_peak");
|
|
|
|
|
|
electPricePeak = info.get<double>("price_peak");
|
|
|
|
|
|
electPriceShoulder = info.get<double>("price_shoulder");
|
|
|
|
|
|
electPriceOffPeak = info.get<double>("price_off_peak");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
{ // 数据库读取统计数据
|
|
|
|
|
|
vector<Fields> result;
|
|
|
|
|
|
std::string curDate = Utils::dateStr();
|
|
|
|
|
|
DAO::queryStatDataList(dao, curDate, curDate, result);
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string dt = fields.value(DMStatStation::DT);
|
|
|
|
|
|
int stationId = fields.get<int>(DMStatStation::STATION_ID);
|
|
|
|
|
|
auto station = this->getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
station->storageIn = fields.get<double>(DMStatStation::STORAGE_ELECT_IN);
|
|
|
|
|
|
station->storageOut = fields.get<double>(DMStatStation::STORAGE_ELECT_OUT);
|
|
|
|
|
|
//station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM);
|
|
|
|
|
|
//station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM);
|
|
|
|
|
|
station->storageNumErr = fields.get<int>(DMStatStation::STORAGE_NUM_ERR);
|
2025-08-22 19:06:50 +08:00
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
station->solarGen = fields.get<double>(DMStatStation::SOLAR_ELECT_GEN);
|
|
|
|
|
|
station->solarGrid = fields.get<double>(DMStatStation::SOLAR_ELECT_GRID);
|
|
|
|
|
|
station->solarNumErr = fields.get<int>(DMStatStation::SOLAR_NUM_ERR);
|
|
|
|
|
|
|
|
|
|
|
|
station->chargeElect = fields.get<double>(DMStatStation::CHARGE_ELECT);
|
|
|
|
|
|
station->chargeNum = fields.get<int>(DMStatStation::CHARGE_NUM);
|
|
|
|
|
|
station->chargeNumErr = fields.get<int>(DMStatStation::CHARGE_NUM_ERR);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::error("init staticis data error: unknown station_id:{}, dt={}", stationId, dt);
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
2025-09-09 19:26:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{ // 初始化场站设备的历史监测数据
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-13 17:28:35 +08:00
|
|
|
|
return true;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-13 17:28:35 +08:00
|
|
|
|
bool AppData::init()
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-09-13 17:28:35 +08:00
|
|
|
|
bool ret = this->initFromDB();
|
|
|
|
|
|
if (!ret) { return false; }
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
2025-09-10 20:10:51 +08:00
|
|
|
|
this->launchDate = Config::option.lunchDate;
|
2025-09-13 17:28:35 +08:00
|
|
|
|
return true;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Station> AppData::getStation(int stationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapStation.find(stationId);
|
|
|
|
|
|
if (iter!=mapStation.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
int AppData::getStationCount()
|
|
|
|
|
|
{
|
|
|
|
|
|
return mapStation.size();
|
|
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Station> AppData::getStationByName(std::string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second->name == name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
std::shared_ptr<Station> AppData::getStationByCode(std::string code)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second->code == code)
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Device> AppData::getDevice(int stationId, int deviceId)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto station = getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
return station->getDevice(deviceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
std::shared_ptr<Device> AppData::getDeviceByType(int stationId, int deviceType, std::string code)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto station = getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
return station->getDeviceByType(deviceType, code);
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
std::string AppData::getDeviceNameById(int typeId)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
auto iter = mapDeviceType.find(typeId);
|
|
|
|
|
|
if (iter != mapDeviceType.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second->name;
|
|
|
|
|
|
}
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<DeviceType> AppData::getDeviceTypeDef(int typeId)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapDeviceType.find(typeId);
|
|
|
|
|
|
if (iter != mapDeviceType.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
void AppData::loadStatData()
|
|
|
|
|
|
{
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
static std::map<std::string, User> g_mapUser;
|
|
|
|
|
|
std::string AppData::userLogin(std::string userId, std::string account)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-31 14:38:53 +08:00
|
|
|
|
for (auto iter = g_mapUser.begin(); iter!=g_mapUser.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second.userId == userId) // 重复登录
|
|
|
|
|
|
{
|
|
|
|
|
|
g_mapUser.erase(iter);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
User user;
|
|
|
|
|
|
user.userId = userId;
|
|
|
|
|
|
user.account = account;
|
|
|
|
|
|
user.loginTime = Utils::time();
|
|
|
|
|
|
user.token = Snowflake::instance().getIdStr();
|
|
|
|
|
|
g_mapUser[user.token] = user;
|
|
|
|
|
|
return user.token;
|
|
|
|
|
|
}
|
|
|
|
|
|
User AppData::getUser(std::string token)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = g_mapUser.find(token);
|
|
|
|
|
|
if (iter != g_mapUser.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return User();
|
2025-08-28 18:42:37 +08:00
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
int AppData::getWorkModeIdByName(std::string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto iter = mapWorkMode.begin(); iter!=mapWorkMode.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second == name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->first;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getRoleNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapRole.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapRole.begin(); iter!=mapRole.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second->name;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getStationNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapStation.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second->name;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::vector<std::string> AppData::getDeviceTypeNames()
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapping.deviceType.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapping.deviceType.begin(); iter!=mapping.deviceType.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getWorkModes()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapWorkMode.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapWorkMode.begin(); iter!=mapWorkMode.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getPolicyTypeNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapPolicyType.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapPolicyType.begin(); iter!=mapPolicyType.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getPolicyNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec;
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
int AppData::getPolicyTypeId(std::string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto iter = mapPolicyType.begin(); iter != mapPolicyType.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second == name) { return iter->first; }
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::vector<std::string> AppData::getElectPreiodVals(int month)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (month > 0 && month-1 < vecElectPeriods.size())
|
|
|
|
|
|
{
|
|
|
|
|
|
return vecElectPeriods[month-1];
|
|
|
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string AppData::getElectPreiodVal(int month, int hour)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (month > 0 && month-1 < vecElectPeriods.size())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vec = vecElectPeriods[month-1];
|
|
|
|
|
|
if (hour > 0 && hour-1 < vec.size())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& val = vec[hour-1];
|
|
|
|
|
|
if (val == "尖") return "尖峰";
|
|
|
|
|
|
if (val == "峰") return "高峰";
|
|
|
|
|
|
if (val == "平") return "平段";
|
|
|
|
|
|
if (val == "谷") return "低谷";
|
|
|
|
|
|
return val;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return "";
|
2025-09-04 19:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AppData::storeRuntimeDB()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto t = Utils::date();
|
|
|
|
|
|
|
|
|
|
|
|
std::string valStr;
|
|
|
|
|
|
for (auto iter=mapDataDay.begin(); iter!=mapDataDay.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& v = iter->second;
|
|
|
|
|
|
if (v != 0.0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!valStr.empty()) valStr += ",";
|
|
|
|
|
|
valStr += ("[" + std::to_string(iter->first) + "," + Utils::toStr(v, 2) + "]");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
valStr = "[" + valStr + "]";
|
|
|
|
|
|
Fields fields;
|
|
|
|
|
|
fields.set("dt", Utils::dateStr(t));
|
|
|
|
|
|
fields.set("device_id", 1);
|
|
|
|
|
|
fields.set("datatype", 1);
|
|
|
|
|
|
fields.set("value", valStr);
|
|
|
|
|
|
DAO::insertRuntimeData(NULL, fields);
|
2025-08-20 19:00:22 +08:00
|
|
|
|
}
|