mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
调试预制舱通讯协议修改数据解析
This commit is contained in:
@@ -12,6 +12,20 @@
|
||||
Station::Station() : stationId(0)
|
||||
{
|
||||
mqttCli = std::make_shared<MqttClient>();
|
||||
|
||||
// 测试,设置默认值
|
||||
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)
|
||||
@@ -19,12 +33,12 @@ void Station::setFields(Fields& fields)
|
||||
this->stationId = fields.get<int>(DMStation::STATION_ID);
|
||||
this->name = fields.value(DMStation::NAME);
|
||||
this->capacity = fields.get<double>(DMStation::CAPACITY);
|
||||
this->workMode = fields.get<int>(DMStation::WORK_MODE);
|
||||
this->workModeId = fields.get<int>(DMStation::WORK_MODE);
|
||||
this->code = fields.value(DMStation::CODE);
|
||||
this->status = fields.get<int>(DMStation::STATUS);
|
||||
this->operationDate = fields.value(DMStation::OPERATION_DATE);
|
||||
this->isOpen = fields.get<int>(DMStation::STATUS);
|
||||
this->launchDate = fields.value("operation_date");
|
||||
|
||||
this->policy.setFields(fields);
|
||||
}
|
||||
|
||||
@@ -120,7 +134,7 @@ void Station::getDeviceByCategory(int category, std::vector<std::shared_ptr<Devi
|
||||
|
||||
void Station::setWorkMode(int modeId)
|
||||
{
|
||||
this->workMode = 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(stationId)).str();
|
||||
@@ -143,6 +157,71 @@ void Station::setPolicy(int policyId)
|
||||
}
|
||||
}
|
||||
|
||||
static std::string MapValueToJson(int npos, std::map<int, double>& mapV)
|
||||
{
|
||||
njson jsonarray = njson::array();
|
||||
for (int i = 0; i<=npos; i++)
|
||||
{
|
||||
jsonarray.push_back(mapV[i]);
|
||||
}
|
||||
return jsonarray.dump();
|
||||
}
|
||||
|
||||
void Station::writeRuntimeData(std::string dt, int npos)
|
||||
{
|
||||
auto dao = DaoEntity::create("history_day");
|
||||
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
||||
{
|
||||
auto device = iter->second;
|
||||
if (device->cache(npos))
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("dt", dt);
|
||||
fields.set("station_id", this->stationId);
|
||||
fields.set("device_id", device->deviceId);
|
||||
fields.set("datatype", 1);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCacheVoltage));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
|
||||
fields.set("datatype", 2);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCacheCurrent));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
|
||||
fields.set("datatype", 3);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCachePower));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
|
||||
spdlog::info("[device] write runtime date to database, deviceId={}", device->deviceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Station::writeStatistic(std::string dt)
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("storage_elect_in", statData.totalElectIn);
|
||||
fields.set("storage_elect_out", statData.totalElectOut);
|
||||
//fields.set("storage_num_in", statData.totalElectIn);
|
||||
//fields.set("storage_num_out", 0);
|
||||
//fields.set("storage_num_err", 0);
|
||||
//fields.set("storage_t_in", 0);
|
||||
//fields.set("storage_t_out", 0);
|
||||
//fields.set("storage_usage", 0);
|
||||
//fields.set("solar_elect_gen", 0);
|
||||
//fields.set("solar_elect_grid", 0);
|
||||
//fields.set("solar_num_err", 0);
|
||||
//fields.set("solar_t", 0);
|
||||
//fields.set("solar_usage", 0);
|
||||
//fields.set("charge_elect", 0);
|
||||
//fields.set("charge_num", 0);
|
||||
//fields.set("charge_num_err", 0);
|
||||
//fields.set("charge_t", 0);
|
||||
//fields.set("charge_usage", 0);
|
||||
fields.set("income_elect", statData.totalIncome);
|
||||
//fields.set("income_charge", 0);
|
||||
//fields.set("usage_rate", 0);
|
||||
}
|
||||
|
||||
void Station::initMqtt()
|
||||
{
|
||||
if (status!=0 && mqttCli)
|
||||
@@ -170,7 +249,7 @@ void Station::setGarewayWorkMode()
|
||||
njson json;
|
||||
json["ts"] = Utils::time();
|
||||
json["no"] = 1; // 设备编号
|
||||
json["40001"] = this->workMode;
|
||||
json["40001"] = this->workModeId;
|
||||
|
||||
if (policy.type == 1)
|
||||
{
|
||||
@@ -188,43 +267,19 @@ void Station::setGarewayWorkMode()
|
||||
mqttCli->publish("Gateway_YT", text);
|
||||
}
|
||||
|
||||
void Station::checkDevice()
|
||||
{
|
||||
for (auto& item: mapDevice)
|
||||
{
|
||||
auto& device = item.second;
|
||||
if (device)
|
||||
{
|
||||
if (Utils::time() - device->ts > 60*6)
|
||||
{
|
||||
device->online = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Station::setRuntimeData(int deviceNo, string addr, int val)
|
||||
void Station::setRuntimeData(string addr, int val)
|
||||
{
|
||||
if (deviceNo == 1)
|
||||
{
|
||||
if (addr == "0x000B") { this->voltage = val; } // A相电压 R uint32 1V 0x000B
|
||||
if (addr == "0x0011") { this->current = val; } // A相电流 R int32 1A 0x0011
|
||||
if (addr == "0x0011") { this->power = val; } // 三相总有功 R int32 1kW 0x0023
|
||||
}
|
||||
else if (deviceNo == 2)
|
||||
{
|
||||
statData.ts = Utils::time();
|
||||
if (addr == "0x002F") { statData.dayElectIn = val; } //日充电电量 R uint32 1kWh 0x002F
|
||||
else if (addr == "0x0031") { statData.dayElectOut = val; } //日放电电量 R uint32 1kWh 0x0031
|
||||
else if (addr == "0x0033") { statData.dayIncomeIn = val; } //日充电费用 R uint32 1RMB 0x0033
|
||||
else if (addr == "0x0035") { statData.dayIncomeOut = val; } //日放电费用 R uint32 1RMB 0x0035
|
||||
else if (addr == "0x0037") { statData.dayIncome = val; } //日收益 R int32 1RMB 0x0037
|
||||
else if (addr == "0x004D") { statData.totalElectIn = val; } //总充电电量 R uint32 1kWh 0x004D
|
||||
else if (addr == "0x004F") { statData.totalElectOut = val; } //总放电电量 R uint32 1kWh 0x004F
|
||||
else if (addr == "0x0051") { statData.totalIncomeIn = val; } //总充电费用 R uint32 1RMB 0x0051
|
||||
else if (addr == "0x0053") { statData.totalIncomeOut = val; } //总放电费用 R uint32 1RMB 0x0053
|
||||
else if (addr == "0x0055") { statData.totalIncome = val; } //总收益 R int32 1RMB 0x0055
|
||||
}
|
||||
if (addr == "0x110E") { statData.dayElectIn = val; } //日充电电量 R uint32 1kWh 0 0x110E
|
||||
else if (addr == "0x1110") { statData.dayElectOut = val; } //日放电电量 R uint32 1kWh 0 0x1110
|
||||
else if (addr == "0x1112") { statData.dayIncomeIn = val; } //日充电费用 R uint32 1RMB 0 0x1112
|
||||
else if (addr == "0x1114") { statData.dayIncomeOut = val; } //日放电费用 R uint32 1RMB 0 0x1114
|
||||
else if (addr == "0x1116") { statData.dayIncome = val; } //日收益 R int32 1RMB 0 0x1116
|
||||
else if (addr == "0x112C") { statData.totalElectIn = val; } //总充电电量 R uint32 1kWh 6659(0x112D) 0x112C
|
||||
else if (addr == "0x112E") { statData.totalElectOut = val; } //总放电电量 R uint32 1kWh 4925(0x112F) 0x112E
|
||||
else if (addr == "0x1130") { statData.totalIncomeIn = val; } //总充电费用 R uint32 1RMB 6605(0x1131) 0x1130
|
||||
else if (addr == "0x1132") { statData.totalIncomeOut = val; } //总放电费用 R uint32 1RMB 4949(0x1133) 0x1132
|
||||
else if (addr == "0x1134") { statData.totalIncome = val; } //总收益 R int32 1RMB -1 0x1134
|
||||
}
|
||||
|
||||
void Station::setTHData(int deviceNo, string addr, int val)
|
||||
@@ -232,16 +287,8 @@ void Station::setTHData(int deviceNo, string addr, int val)
|
||||
auto& unit = mapTempHumUnit[deviceNo];
|
||||
if (addr == "0x0001") { ; } //所属通道号 R uint16 1 0x0001
|
||||
else if (addr == "0x0002") { ; } //所属温湿度号 R uint16 1~10 0x0002
|
||||
else if (addr == "0x0003") //温度 R int16 0.1℃ 0x0003
|
||||
{
|
||||
unit.temp = float(val) * 0.1;
|
||||
if (deviceNo == 1) temperature = unit.temp;
|
||||
}
|
||||
else if (addr == "0x0004") //湿度 R int16 0.1℃ 0x0004
|
||||
{
|
||||
unit.hum = float(val) * 0.1;
|
||||
if (deviceNo == 1) humidity = unit.hum;
|
||||
}
|
||||
else if (addr == "0x0003") { unit.temp = float(val) * 0.1; } //温度 R int16 0.1℃ 0x0003
|
||||
else if (addr == "0x0004") { unit.hum = float(val) * 0.1; } //湿度 R int16 0.1℃ 0x0004
|
||||
}
|
||||
|
||||
void Station::setFire40Data(int deviceNo, string addr, int val)
|
||||
@@ -281,8 +328,8 @@ void Station::setCoolingData(int deviceNo, string addr, int val)
|
||||
|
||||
if (addr == "0x1001") { ; } //所属通道号 R uint16 1 0x1001
|
||||
else if (addr == "0x1002") { ; }// 所属冷机号 R uint16 1~10 0x1002
|
||||
else if (addr == "0x1003") { coolingStatus = unit.powerOn = val; }// 开关 R uint16 0:关机,1:开机 0x1003
|
||||
else if (addr == "0x1004") { unit.mode = val; }// 采样模式 R uint16 0-出水温度 1-电芯温度 0x1004
|
||||
else if (addr == "0x1003") { unit.powerOn = val; }// 开关 R uint16 0:关机,1:开机 0x1003
|
||||
else if (addr == "0x1004") { ; }// 采样模式 R uint16 0-出水温度 1-电芯温度 0x1004
|
||||
else if (addr == "0x1005") { unit.cooling = val; }// 制冷状态 R uint16 0:关闭, 1:启动 0x1005
|
||||
else if (addr == "0x1006") { unit.heating = val; }// 制热状态 R uint16 0:关闭, 1:启动 0x1006
|
||||
else if (addr == "0x1007") { unit.highTemp = val; }// 高温告警 R uint16 0:正常,1:告警 0x1007
|
||||
@@ -293,112 +340,4 @@ void Station::setCoolingData(int deviceNo, string addr, int val)
|
||||
else if (addr == "0x100C") { ; }// 出水温度传感器 R uint16 0:正常,1:告警 0x100C
|
||||
else if (addr == "0x100D") { ; }// 进水压力传感器 R uint16 0:正常,1:告警 0x100D
|
||||
else if (addr == "0x100E") { ; }// 出水压力传感器 R uint16 0:正常,1:告警 0x100E
|
||||
}
|
||||
|
||||
void Station::setWorkModeFromGateway(int mode)
|
||||
{
|
||||
if (mode != this->workMode)
|
||||
{
|
||||
//std::string sql = "update station set work_mode='" + std::to_string(mode) + "'";
|
||||
//auto ret = DaoEntity::execOnce(sql);
|
||||
//if (ret)
|
||||
//{
|
||||
// spdlog::info("[station] wrok_mode is diffent with gateway, update success.[{}]-[{}]", workMode, mode);
|
||||
// this->workMode = mode;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// spdlog::error("[station] wrok_mode is diffent with gateway, update failed.[{}]-[{}]", workMode, mode);
|
||||
//}
|
||||
|
||||
//this->setGarewayWorkMode();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static std::string MapValueToJson(int npos, std::map<int, double>& mapV)
|
||||
{
|
||||
njson jsonarray = njson::array();
|
||||
for (int i = 0; i<=npos; i++)
|
||||
{
|
||||
jsonarray.push_back(mapV[i]);
|
||||
}
|
||||
return jsonarray.dump();
|
||||
}
|
||||
|
||||
void Station::writeStatistic()
|
||||
{
|
||||
auto dao = DaoEntity::create("history_day");
|
||||
|
||||
std::string dt = Utils::dateStr();
|
||||
int64_t tTime = Utils::time();
|
||||
int64_t tDate = Utils::date();
|
||||
int npos = (tTime-tDate) / 600;
|
||||
|
||||
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
||||
{
|
||||
auto device = iter->second;
|
||||
if (device->cache(npos))
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("dt", dt);
|
||||
fields.set("station_id", this->stationId);
|
||||
fields.set("device_id", device->deviceId);
|
||||
fields.set("datatype", 1);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCacheVoltage));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
|
||||
fields.set("datatype", 2);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCacheCurrent));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
|
||||
fields.set("datatype", 3);
|
||||
fields.set("value", MapValueToJson(npos, device->mapCachePower));
|
||||
DAO::insertRuntimeData(dao, fields);
|
||||
//spdlog::info("[device] write runtime date to database, deviceId={}", device->deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
if (statData.ts != 0)
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("dt", Utils::dateStr(statData.ts));
|
||||
fields.set("station_id", this->stationId);
|
||||
fields.set("device_id", 0);
|
||||
fields.set("elect_in", statData.dayElectIn);
|
||||
fields.set("elect_out", statData.dayElectOut);
|
||||
fields.set("income_in", statData.dayIncomeIn);
|
||||
fields.set("income_out", statData.dayIncomeOut);
|
||||
fields.set("income", statData.dayIncome);
|
||||
//fields.set("num_in", "");
|
||||
//fields.set("num_out", "");
|
||||
//fields.set("num_err", "");
|
||||
//fields.set("t_in", "");
|
||||
//fields.set("t_out", "");
|
||||
//fields.set("usage_rate", "");
|
||||
fields.set("elect_in_total", statData.totalElectIn);
|
||||
fields.set("elect_out_total", statData.totalElectOut);
|
||||
fields.set("income_in_total", statData.totalIncomeIn);
|
||||
fields.set("income_out_total", statData.totalIncomeOut);
|
||||
fields.set("income_total", statData.totalIncome);
|
||||
|
||||
dao->setTableName("stat_storage");
|
||||
std::vector<std::string> vecKeys = {
|
||||
"elect_in", "elect_out", "num_in", "num_out", "num_err", "t_in", "t_out", "usage_rate", "income_in", "income_out",
|
||||
"elect_in_total", "elect_out_total", "income_in_total", "income_out_total"
|
||||
};
|
||||
dao->duplicateUpdate(fields, vecKeys);
|
||||
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("dt", Utils::dateStr(statData.ts));
|
||||
fields.set("station_id", this->stationId);
|
||||
fields.set("device_id", 0);
|
||||
fields.set("storage_elect_in", statData.dayElectIn);
|
||||
fields.set("storage_elect_out", statData.dayElectOut);
|
||||
fields.set("income_elect", statData.dayIncome);
|
||||
DAO::insertStatStation(dao, fields);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user