Files
energy_storage/src/app/Station.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

#include "Station.h"
#include "database/DAO.h"
#include "database/SQL.h"
2025-08-28 18:42:37 +08:00
#include "common/fields.h"
#include "app/Device.h"
#include "common/Spdlogger.h"
2025-08-28 18:42:37 +08:00
Station::Station() : id(0)
{
}
2025-08-28 18:42:37 +08:00
void Station::setFields(Fields& fields)
{
this->id = 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_ID);
}
void Station::addDevice(int deviceId, std::shared_ptr<Device> device)
{
2025-08-28 18:42:37 +08:00
mapDevice[deviceId] = device;
mapDeviceGroupNum[device->group]++;
}
std::shared_ptr<Device> Station::getDevice(int deviceId)
{
2025-08-28 18:42:37 +08:00
auto iter = mapDevice.find(deviceId);
if (iter!=mapDevice.end())
{
return iter->second;
}
return nullptr;
}
2025-08-28 18:42:37 +08:00
void Station::getDeviceByType(int typeId, std::vector<std::shared_ptr<Device>>& res)
{
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
{
auto device = iter->second;
if (device->type == typeId)
{
res.push_back(device);
}
}
}
int Station::getDeviceNumByGroup(std::string name)
{
return mapDeviceGroupNum[name];
}
void Station::getDeviceByGroup(std::string name, std::vector<std::shared_ptr<Device>>& res)
{
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
{
auto device = iter->second;
if (device->group == name)
{
res.push_back(device);
}
}
}
void Station::setWorkMode(int modeId)
{
2025-08-28 18:42:37 +08:00
this->workModeId = modeId;
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
.update(DMStation::WORK_MODE_ID, std::to_string(modeId))
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
spdlog::error("set station work mode failed.");
}
}
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();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
spdlog::error("set station policy failed.");
}
2025-08-28 18:42:37 +08:00
}