2025-08-20 19:00:22 +08:00
|
|
|
|
#include "Station.h"
|
2025-08-26 18:36:25 +08:00
|
|
|
|
#include "database/DAO.h"
|
|
|
|
|
|
#include "database/SQL.h"
|
2025-08-28 18:42:37 +08:00
|
|
|
|
#include "common/fields.h"
|
|
|
|
|
|
#include "app/Device.h"
|
2025-09-01 20:08:40 +08:00
|
|
|
|
#include "common/Spdlogger.h"
|
2025-09-04 19:31:04 +08:00
|
|
|
|
#include "common/Utils.h"
|
|
|
|
|
|
#include "protocol/MqttEntity.h"
|
2025-08-28 18:42:37 +08:00
|
|
|
|
|
|
|
|
|
|
Station::Station() : id(0)
|
2025-08-20 19:00:22 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
2025-09-04 19:31:04 +08:00
|
|
|
|
this->workModeId = fields.get<int>(DMStation::WORK_MODE);
|
|
|
|
|
|
this->code = fields.value(DMStation::CODE);
|
2025-08-28 18:42:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
void Station::addDevice(int deviceId, std::shared_ptr<Device> device)
|
|
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
mapDevice[deviceId] = device;
|
|
|
|
|
|
mapDeviceGroupNum[device->group]++;
|
2025-08-20 19:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
2025-08-20 19:00:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
void Station::setWorkMode(int modeId)
|
|
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
this->workModeId = modeId;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
.update(DMStation::WORK_MODE, std::to_string(modeId))
|
2025-08-26 18:36:25 +08:00
|
|
|
|
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
|
|
|
|
|
|
Errcode err = DAO::exec(NULL, sql);
|
|
|
|
|
|
if (err != Errcode::OK)
|
|
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::error("set station work mode failed.");
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::error("set station policy failed.");
|
2025-08-26 18:36:25 +08:00
|
|
|
|
}
|
2025-08-28 18:42:37 +08:00
|
|
|
|
}
|