mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
121 lines
3.2 KiB
C++
121 lines
3.2 KiB
C++
#include "Station.h"
|
|
#include "database/DAO.h"
|
|
#include "database/SQL.h"
|
|
#include "common/fields.h"
|
|
#include "app/Device.h"
|
|
#include "common/Spdlogger.h"
|
|
#include "common/Utils.h"
|
|
#include "protocol/MqttEntity.h"
|
|
|
|
Station::Station() : id(0)
|
|
{
|
|
mqttCli = std::make_shared<MqttClient>();
|
|
|
|
// 测试,设置默认值
|
|
for (int i = 1; i<=5; i++) { mapTempHumUnit[i] = TempHumUnit(Utils::random(20, 40), Utils::random(20, 80)); }
|
|
|
|
for (int i = 1; i<=5; i++) { mapFire40Unit[i] = 0; }
|
|
|
|
for (int i = 1; i<=5; i++) {
|
|
auto& unit = mapCoolingUnit[i];
|
|
unit.powerOn = 1;
|
|
unit.mode = i%2;
|
|
}
|
|
|
|
for (int i = 1; i<=5; i++) {
|
|
auto& unit = mapAircUnit[i];
|
|
unit.powerOn = 1;
|
|
unit.temp = Utils::random(20, 40);
|
|
unit.hum = Utils::random(20, 80);
|
|
}
|
|
}
|
|
|
|
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);
|
|
this->code = fields.value(DMStation::CODE);
|
|
}
|
|
|
|
void Station::addDevice(int deviceId, std::shared_ptr<Device> device)
|
|
{
|
|
mapDevice[deviceId] = device;
|
|
mapDeviceGroup[device->category].push_back(device);
|
|
}
|
|
|
|
std::shared_ptr<Device> Station::getDevice(int deviceId)
|
|
{
|
|
auto iter = mapDevice.find(deviceId);
|
|
if (iter!=mapDevice.end())
|
|
{
|
|
return iter->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::shared_ptr<Device> Station::getDeviceByType(int deviceType, std::string code)
|
|
{
|
|
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
|
{
|
|
auto device = iter->second;
|
|
if (device->type == deviceType && device->code == code)
|
|
{
|
|
return device;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Station::getDeviceByType(int deviceType, std::vector<std::shared_ptr<Device>>& res)
|
|
{
|
|
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
|
{
|
|
auto device = iter->second;
|
|
if (device->type == deviceType)
|
|
{
|
|
res.push_back(device);
|
|
}
|
|
}
|
|
}
|
|
|
|
int Station::getDeviceNumByGroup(int category)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
void Station::getDeviceByGroup(int category, std::vector<std::shared_ptr<Device>>& res)
|
|
{
|
|
auto iter = mapDeviceGroup.find(category);
|
|
if (iter != mapDeviceGroup.end())
|
|
{
|
|
res = iter->second;
|
|
}
|
|
}
|
|
|
|
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();
|
|
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.");
|
|
}
|
|
}
|