#include "Application.h" #include "common/Utils.h" #include "Config.h" #include "app/Device.h" #include "database/DaoEntity.h" #include "database/Dao.h" #include "app/Station.h" #include "app/Device.h" void InitStation() { AppData& appdata = Application::instance().getAppData(); // 读取数据库 std::vector result; DAO::queryStationList(result); for (auto& fields: result) { int stationId = fields.getInt(DMStation::STATION_ID); auto station = std::make_shared(stationId); station->name = fields.getStr(DMStation::NAME); station->energyCapacity = fields.getDouble(DMStation::CAPACITY); appdata.mapStation[stationId] = station; } } void InitDevice() { AppData& appdata = Application::instance().getAppData(); vector result; DAO::queryDeviceList(result); for (auto& fields: result) { int deviceId = fields.getInt(DMDevice::DEVICE_ID); int stationId = fields.getInt(DMDevice::STATION_ID); auto station = appdata.getStation(stationId); if (station) { auto device = Device::create(fields); station->addDevice(deviceId, device); } else { XLOGE() << "init device error: unknown station_id:[" << stationId << "] device_id=" << deviceId; } } } void InitStatData() { AppData& appdata = Application::instance().getAppData(); std::string curDate = Utils::dateStr(); vector result; DAO::queryStatDataList(curDate, curDate, result); for (auto& fields: result) { std::string dt = fields.getStr(DMStatStation::DT); int stationId = fields.getInt(DMStatStation::STATION_ID); auto station = appdata.getStation(stationId); if (station) { station->storageIn = fields.getFloat(DMStatStation::STORAGE_ELECT_IN); station->storageOut = fields.getFloat(DMStatStation::STORAGE_ELECT_OUT); //station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM); //station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM); station->storageNumErr = fields.getFloat(DMStatStation::STORAGE_NUM_ERR); station->solarGen = fields.getFloat(DMStatStation::SOLAR_ELECT_GEN); station->solarGrid = fields.getFloat(DMStatStation::SOLAR_ELECT_GRID); station->solarNumErr = fields.getFloat(DMStatStation::SOLAR_NUM_ERR); station->chargeElect = fields.getFloat(DMStatStation::CHARGE_ELECT); station->chargeNum = fields.getFloat(DMStatStation::CHARGE_NUM); station->chargeNumErr = fields.getFloat(DMStatStation::CHARGE_NUM_ERR); } else { XLOGE() << "init staticis data error: unknown station_id:[" << stationId << "] dt=" << dt; } } } void Application::init() { // 初始化系统配置,读取配置文件 Config::init("assets/config/app.json"); // 设置数据库配置 DaoEntity::setOption(Config::option.database.host, Config::option.database.port, Config::option.database.user, Config::option.database.passwd, Config::option.database.dbname); XLOGI() << "[APP] set database option: host=" << Config::option.database.host << ", port=" << Config::option.database.port << ", user=" << Config::option.database.user << ", dbname=" << Config::option.database.dbname; // 连接数据库,读取基础信息 // 初始化场站信息 InitStation(); // 读取设备信息,连接设备 InitDevice(); // 读取基础统计信息,在系统总览中需要展示 InitStatData(); // 创建设备处理线程 std::thread([=]() { runThreadDevice(); }).detach(); // 创建主业务循环线程 std::thread([=]() { runThreadMain(); }).detach(); } AppData& Application::getAppData() { return appdata_; } void Application::runThreadMain() { while (!isQuit()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void Application::runThreadDevice() { while (!isQuit()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } }