From 6e0a00f271428d8636d734b18efcc13982f44297 Mon Sep 17 00:00:00 2001 From: lixiaoyuan Date: Tue, 3 Mar 2026 15:51:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=AD=96=E7=95=A5=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=95=88=E7=9B=8A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/AppData.h | 5 ++ src/app/Station.cpp | 25 ++++++-- src/app/Station.h | 4 +- src/protocol/HttpEntity.cpp | 124 +++++++++++++++++++++++++++++++++++- src/protocol/HttpEntity.h | 5 ++ src/protocol/MqttEntity.cpp | 2 + 6 files changed, 158 insertions(+), 7 deletions(-) diff --git a/src/app/AppData.h b/src/app/AppData.h index 0ef3ae6..e4b4f12 100644 --- a/src/app/AppData.h +++ b/src/app/AppData.h @@ -54,6 +54,11 @@ public: std::string dump(); }; +class StateGrid +{ +public: +}; + class AppData { public: diff --git a/src/app/Station.cpp b/src/app/Station.cpp index 39b2f9f..a51e031 100644 --- a/src/app/Station.cpp +++ b/src/app/Station.cpp @@ -377,11 +377,8 @@ void Station::readEnergyData(int deviceNo, string addr, int val) { statData.ts = Utils::time(); - if (addr == "0x000B") { this->storage.voltage = val; } // A相电压 R uint32 1V 0x000B - else if (addr == "0x0011") { this->storage.current = val; } // A相电流 R int32 1A 0x0011 - else if (addr == "0x0023") { this->storage.power = val; } // 三相总有功 R int32 1kW 0x0023 // 功率因数 ?? 电表没有功率因数数据 - else if (addr == "0x002F") { statData.dayElectIn = val; } // 日充电电量 R uint32 1kWh 0x002F + 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.dayFeeIn = val; } //日充电费用 R uint32 1RMB 0x0033 else if (addr == "0x0035") { statData.dayFeeOut = val; } //日放电费用 R uint32 1RMB 0x0035 @@ -416,6 +413,21 @@ void Station::readEnergyData(int deviceNo, string addr, int val) else if (addr == "0x0069") { statData.totalElectOut_Total = val; } //总反向总有功电能 R uint32 1kWh 0x0069 } } + +void Station::readEMSData(int deviceNo, string addr, int val) +{ + // ["A相电压", "0x107E", "0.0", " V", "1"] , + // ["A相电流", "0x1084", "0.0", " A"], + // ["总有功功率", "0x1096", "0.0", " kW"] +} + +void Station::readBMSData(int deviceNo, string addr, int val) +{ + if (addr == "0x0003") { this->storage.voltage = val * 0.1; } // ["电压", "0x0003", "0.0", " V", "0.1"], + else if (addr == "0x0005") { this->storage.current = val * 0.1; } // ["电流", "0x0005", "0.0", " A", "0.1"] , + else if (addr == "0x000F") { this->storage.power = val; } // ["堆功率", "0x000F", "0.0", " kW"] , +} + // 充电桩 void Station::readChargeData(int deviceNo, string addr, int val) { @@ -745,6 +757,7 @@ void Station::writeStatistic() } { + // stat_total Fields fields; fields.set("dt", Utils::dateStr(statData.ts)); fields.set("station_id", this->stationId); @@ -771,7 +784,8 @@ void Station::writeStatistic() } if (statData.ts > 0) { - { // stat_day + { + // stat_day Fields fields; fields.set("dt", Utils::dateStr(statData.ts)); fields.set("station_id", this->stationId); @@ -783,6 +797,7 @@ void Station::writeStatistic() if (statData.dayElectIn > 0.0f || statData.dayElectOut > 0.0f) { + //stat_total_day Fields fields; fields.set("dt", Utils::dateStr(statData.ts)); fields.set("station_id", this->stationId); diff --git a/src/app/Station.h b/src/app/Station.h index 3379354..e324baf 100644 --- a/src/app/Station.h +++ b/src/app/Station.h @@ -119,7 +119,9 @@ public: string getGatewayParam(); void readAlert(std::shared_ptr device, std::string addr, int v, std::string text); - void readEnergyData(int deviceNo, string addr, int val); + void readEnergyData(int deviceNo, string addr, int val); // 电表 + void readEMSData(int deviceNo, string addr, int val); + void readBMSData(int deviceNo, string addr, int val); void readChargeData(int deviceNo, string addr, int val); void readTHData(int deviceNo, string addr, int val); void readFire40Data(int deviceNo, string addr, int val); diff --git a/src/protocol/HttpEntity.cpp b/src/protocol/HttpEntity.cpp index aa5779d..dc25137 100644 --- a/src/protocol/HttpEntity.cpp +++ b/src/protocol/HttpEntity.cpp @@ -193,7 +193,9 @@ static std::map g_mapHttpHandlerGet = {"/queryServiceApiList", HandlerOptions(&HttpEntity::queryServiceApiList, {})}, {"/deleteServiceApi", HandlerOptions(&HttpEntity::deleteServiceApi, {"api_id"})}, - + + {"/queryEGridPeriod", HandlerOptions(&HttpEntity::queryEGridPeriod, {})}, + //{"/insert", HandlerOptions(&HttpEntity::insert, {})}, //{"/update", HandlerOptions(&HttpEntity::update, {})}, //{"/delete", HandlerOptions(&HttpEntity::delete, {})}, @@ -1895,4 +1897,124 @@ Errcode HttpEntity::updateGatewayParams(const httplib::Request& req, njson& json return Errcode::OK; } return Errcode::ERR_PARAM; +} + + +static float SetPeriodRowJson(njson& jsonrow, std::string period, int eIn, int eOut, float priceIn, float priceOut, std::string dt) +{ + jsonrow["dt"] = dt; + jsonrow["period"] = period; + jsonrow["E_in"] = eIn; + jsonrow["E_out"] = eOut; + jsonrow["grid_price"] = Utils::toStr(priceIn); + jsonrow["charge_price"] = Utils::toStr(priceOut); + return eOut * priceOut - eIn * priceIn; +} + +Errcode HttpEntity::queryEGridPeriod(const httplib::Request& req, njson& json, std::string& errmsg) +{ + Fields params; + GetRequestParams(req, {"dt", "station_id"}, params); + + int stationId = params.get("station_id"); + std::string dt = params.value("dt"); + //int year = 0; + //int month = 0; + //int day = 0; + //int ret = sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day); // 解析3个值,成功返回3 + //if (ret != 3) + //{ + // return Errcode::ERR_PARAM; + //} + if (stationId == 0 || dt.empty()) + { + return Errcode::ERR_PARAM; + } + + float priceJ = 0.0; + float priceF = 0.0; + float priceP = 0.0; + float priceG = 0.0; + float priceC = 0.0; + + std::vector result; + auto err = DAO::exec(NULL, "select * from egrid_price;", result); + if (err != Errcode::OK) + { + return err; + } + if (result.size() > 0) + { + for (auto& item: result) + { + int etype = item.get("etype"); + float price = item.get("price", 2); + if (etype == 1) { priceJ = price; } + else if (etype == 2) { priceF = price; } + else if (etype == 3) { priceP = price; } + else if (etype == 4) { priceG = price; } + else if (etype == 9) { priceC = price; } + } + } + + //std::string sql = std::format("SELECT ep.*, ep2.price FROM egrid_period ep LEFT JOIN egrid_price ep2 ON ep.etype=ep2.etype WHERE ep.`month`={};", month); + std::string sql = std::format("select * from stat_total_day WHERE dt='{}' AND station_id='{}';", dt, stationId); + err = DAO::exec(NULL, sql, result); + if (err != Errcode::OK) + { + return err; + } + + njson jsondata = njson::array(); + if (result.size() > 0) + { + auto& item = result[0]; + float incomeTotal = 0.0f; + // 谷 + { + njson jsonrow; + float income = SetPeriodRowJson(jsonrow, "谷", item.get("E_in_G"), item.get("E_out_G"), priceG, priceC, dt); + jsonrow["income"] = Utils::toStr(income); + jsondata.push_back(jsonrow); + incomeTotal += income; + } + // 平 + { + njson jsonrow; + float income = SetPeriodRowJson(jsonrow, "平", item.get("E_in_P"), item.get("E_out_P"), priceP, priceC, dt); + jsonrow["income"] = Utils::toStr(income); + jsondata.push_back(jsonrow); + incomeTotal += income; + } + // 峰 + { + njson jsonrow; + float income = SetPeriodRowJson(jsonrow, "峰", item.get("E_in_F"), item.get("E_out_F"), priceF, priceC, dt); + jsonrow["income"] = Utils::toStr(income); + jsondata.push_back(jsonrow); + incomeTotal += income; + } + // 尖 + { + njson jsonrow; + float income = SetPeriodRowJson(jsonrow, "尖", item.get("E_in_J"), item.get("E_out_J"), priceJ, priceC, dt); + jsonrow["income"] = Utils::toStr(income); + jsondata.push_back(jsonrow); + incomeTotal += income; + } + // 总计 + { + njson jsonrow; + jsonrow["dt"] = dt; + jsonrow["period"] = "总计"; + jsonrow["E_in"] = item.get("E_in"); + jsonrow["E_out"] = item.get("E_out"); + jsonrow["grid_price"] = ""; + jsonrow["charge_price"] = ""; + jsonrow["income"] = Utils::toStr(incomeTotal); + jsondata.push_back(jsonrow); + } + } + json["data"] = jsondata; + return Errcode::OK; } \ No newline at end of file diff --git a/src/protocol/HttpEntity.h b/src/protocol/HttpEntity.h index 9cd9796..0e1dce7 100644 --- a/src/protocol/HttpEntity.h +++ b/src/protocol/HttpEntity.h @@ -126,4 +126,9 @@ public: Errcode deleteServiceApi(const httplib::Request& req, njson& json, std::string& errmsg); Errcode updateGatewayParams(const httplib::Request& req, njson& json, std::string& errmsg); + + /// =========================================================================================== + // 查询电网的时段(尖、峰、平、谷) + // 参数: 日期(年-月-日) + Errcode queryEGridPeriod(const httplib::Request& req, njson& json, std::string& errmsg); }; \ No newline at end of file diff --git a/src/protocol/MqttEntity.cpp b/src/protocol/MqttEntity.cpp index 6fc9f97..8cec414 100644 --- a/src/protocol/MqttEntity.cpp +++ b/src/protocol/MqttEntity.cpp @@ -415,6 +415,8 @@ void MqttClient::ParseArrivedMessage(njson& json, string command, std::shared_pt device->setParam(addr, val); if (command == "MEM_YC") { station->readEnergyData(deviceNo, addr, val); } + else if (command == "EMS_YC") {} + else if (command == "BMS_YC") { station->readBMSData(deviceNo, addr, val); } else if (command == "Charger_YC") { station->readChargeData(deviceNo, addr, val); } else if (command == "Fire40_YX") { station->readFire40Data(deviceNo, addr, val); } else if (command == "TH_YC") { station->readTHData(deviceNo, addr, val); }