#include "ConfigManager.h" #include "dao/MachineCfgDao.h" //#include "../GTS/GTSController.h" #include "../global.h" #include "../Logger.h" #include "../Registration/aes.hpp" #include "../LanguageManager.h" #include ConfigManager::ConfigManager() :m_pDB(nullptr) , m_pHbdLanguageDao(nullptr) , m_pMachineCfgDao(nullptr) , m_pBaseConfigDao(nullptr) , m_pAxisConfigDao(nullptr) , m_pIOCfgDao(nullptr) ,m_pCommunicationCfgDao(nullptr) , m_pInfoTextDao(nullptr) , m_pAlarmCfgDao(nullptr) , m_IOCfgWrapper(nullptr) , m_AlarmCfgWrapper(nullptr) , m_pSystemBaseDao(nullptr) , m_Machine(nullptr) , m_pParamSetCfgDao(nullptr) //,m_pLaserCfgDao(nullptr) ,m_PrepareJobCfgDao(nullptr) , m_IOVersionDao(nullptr) , m_ScannerControlCfgDao(nullptr) //, m_pServoCfgDao(nullptr) { } ConfigManager::~ConfigManager() { //if (m_pLaserCfgDao != nullptr)delete m_pLaserCfgDao; if (m_ScannerControlCfgDao != nullptr)delete m_ScannerControlCfgDao; if (m_pAlarmCfgDao != nullptr)delete m_pAlarmCfgDao; if (m_pInfoTextDao != nullptr)delete m_pInfoTextDao; if (m_pCommunicationCfgDao != nullptr)delete m_pCommunicationCfgDao; if (m_pIOCfgDao != nullptr)delete m_pIOCfgDao; if (m_pAxisConfigDao != nullptr)delete m_pAxisConfigDao; if (m_pBaseConfigDao != nullptr)delete m_pBaseConfigDao; if (m_Machine != nullptr)delete m_Machine; if (m_pMachineCfgDao != nullptr)delete m_pMachineCfgDao; if (m_pHbdLanguageDao != nullptr)delete m_pHbdLanguageDao; if (m_pSystemBaseDao != nullptr)delete m_pSystemBaseDao; if (m_pParamSetCfgDao != nullptr)delete m_pParamSetCfgDao; if (m_PrepareJobCfgDao != nullptr)delete m_PrepareJobCfgDao; if (m_IOVersionDao)delete m_IOVersionDao; if (m_pDB)delete m_pDB; for (map::iterator it = m_LanguageMap.begin(); it != m_LanguageMap.end(); it++) { HbdLanguage* hbdl = it->second; delete hbdl; hbdl = NULL; } m_LanguageMap.clear(); for (map::iterator it = m_AxisCfgs.begin(); it != m_AxisCfgs.end(); it++) { AxisCfg* ac = it->second; delete ac; ac = NULL; } m_AxisCfgs.clear(); if (m_IOCfgWrapper != nullptr)delete m_IOCfgWrapper; if (m_AlarmCfgWrapper != nullptr)delete m_AlarmCfgWrapper; /*for (map::iterator it = m_ComServerCfgMap.begin(); it != m_ComServerCfgMap.end(); it++) { ComServerCfg* csc = it->second; delete csc; csc = NULL; } m_ComServerCfgMap.clear();*/ for (auto pps : m_ParamSetCfg.ParamSetVec) { std::map::iterator it; for (it = pps->LaserSetMap.begin(); it != pps->LaserSetMap.end(); it++) delete it->second; pps->LaserSetMap.clear(); for (unsigned int i = 0; i < pps->PowderSets.size(); i++) delete pps->PowderSets[i]; pps->PowderSets.clear(); delete pps; } m_ParamSetCfg.ParamSetVec.clear(); /*for (map::iterator it = m_LaserCfgs.begin(); it != m_LaserCfgs.end(); it++) { LaserCfg* cfg = it->second; delete cfg; cfg = NULL; } m_LaserCfgs.clear();*/ } ConfigManager* ConfigManager::GetInstance() { static ConfigManager manager; return &manager; } void ConfigManager::PreInit() { string databasepath = g_AppPath + "config.hbd"; m_pDB = new SQLite::Database(databasepath, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); m_pDB->exec("PRAGMA key = '!hbd_admin'"); m_pMachineCfgDao = new MachineCfgDao(m_pDB); m_pMachineCfgDao->CreateTable(); m_pMachineCfgDao->Find(m_MachineCfg); } void ConfigManager::Init() { m_Machine = Machine::CreateInstance(MachineTypeCfg::HBD_1000/*m_MachineCfg.m_MachineType*/); m_LoadCfg = m_Machine->CreateLoadAxisCfg(); m_MoldCfg = m_Machine->CreateMoldAxisCfg(); //m_PowderCfg= m_Machine->CreatePowderAxisCfg(); m_ArmCfg = m_Machine->CreateArmAxisCfg(); m_SupplyCfg = m_Machine->CreateSupplyAxisCfg(); m_CleanCfg = m_Machine->CreateCleanAxisCfg(); m_EleCfg = m_Machine->CreateEleAxisCfg(); m_AxisCfgs[GTS_AXIS_ID_MOLD] = m_MoldCfg; m_AxisCfgs[GTS_AXIS_ID_LOAD] = m_LoadCfg; m_AxisCfgs[GTS_AXIS_ID_ARM] = m_ArmCfg; m_AxisCfgs[GTS_AXIS_ID_SUPPLY] = m_SupplyCfg; m_AxisCfgs[GTS_AXIS_ID_CLEAN] = m_CleanCfg; m_AxisCfgs[GTS_AXIS_ID_ELE] = m_EleCfg; //m_AxisCfgs[GTS_AXIS_ID_MOLD] = m_MoldCfg; //m_AxisCfgs[GTS_AXIS_ID_POWDER] = m_PowderCfg; //m_AxisCfgs[GTS_AXIS_ID_ARM] = m_ArmCfg; m_IOCfgWrapper = new IOCfgWrapper(); m_AlarmCfgWrapper = new AlarmCfgWrapper(); m_pSystemBaseDao = new SystemBaseDao(m_pDB); m_pHbdLanguageDao = new HbdLanguageDao(m_pDB); m_pBaseConfigDao = new BaseConfigDao(m_pDB); m_pAxisConfigDao = new AxisCfgDao(m_pDB); m_pIOCfgDao = new IOCfgDao(m_pDB); m_pCommunicationCfgDao = new CommunicationCfgDao(m_pDB); //m_pComServerDao = new ComServerDao(m_pDB); m_pInfoTextDao = new InfoTextDao(m_pDB); m_pAlarmCfgDao = new AlarmCfgDao(m_pDB); m_pParamSetCfgDao = new ParamSetCfgDao(m_pDB); // m_pLaserCfgDao = new LaserCfgDao(m_pDB); m_ScannerControlCfgDao = new ScannerControlCfgDao(m_pDB); m_PrepareJobCfgDao = new PrepareJobCfgDao(m_pDB); m_IOVersionDao = new IOVersionDao(m_pDB); GenerateTable(); ReadConfig(); } bool ConfigManager::InitFromSetup(void) { return false; //string databasepath = g_AppPath + "config.hbd"; //WIN32_FIND_DATA FindFileData; //HANDLE handle = FindFirstFile(databasepath.c_str(), &FindFileData); /*if (handle == INVALID_HANDLE_VALUE && __argc > 1) { int cnt = __argc; m_MachineCfg.m_MachineType = stoi(__wargv[1]); ConfigManager::GetInstance()->PreInit(); g_Lang = &ConfigManager::GetInstance()->GetMachineCfg()->m_I18NLang[0]; Init(); m_MachineCfg.m_MachineType = stoi(__wargv[1]); if (__argc > 2) m_MachineCfg.m_IOVersion = stoi(__wargv[2]); SaveConfig(); return true; } else { return false; }*/ } void ConfigManager::GenerateTable() { m_pSystemBaseDao->CreateTable(); m_pHbdLanguageDao->CreateTable(); // m_pMachineCfgDao->CreateTable(); m_pBaseConfigDao->CreateTable(); m_pAxisConfigDao->CreateTable(); m_pIOCfgDao->CreateTable(); m_pCommunicationCfgDao->CreateTable(); // m_pComServerDao->CreateTable(); m_pInfoTextDao->CreateTable(); m_pAlarmCfgDao->CreateTable(); m_pParamSetCfgDao->CreateTable(); //m_pLaserCfgDao->CreateTable(); m_ScannerControlCfgDao->CreateTable(); m_PrepareJobCfgDao->CreateTable(); m_IOVersionDao->CreateTable(); } void ConfigManager::ReadConfig() { m_pSystemBaseDao->Find(m_SystemBaseCfg); m_pHbdLanguageDao->Find(m_LanguageMap); // m_pMachineCfgDao->Find(m_MachineCfg); m_pBaseConfigDao->FindExtCfg(m_ExtCfg); m_pBaseConfigDao->FindRunCfg(m_RunCfg); m_pBaseConfigDao->FindFavoriteCfg(m_FavoriteCfg); m_pBaseConfigDao->FindPowderEstimate(m_PowderEstimateCfg); m_pBaseConfigDao->FindRecoatCheckCfg(m_RecoatCheckCfg); m_pBaseConfigDao->FindParamLimitCfg(m_ParamLimitCfg); m_pBaseConfigDao->FindCameraCalibrationCfg(m_CameraCalibrationCfg); m_pAxisConfigDao->Find(m_AxisCfgs); m_pIOCfgDao->Find(m_IOCfgWrapper); m_IOCfgWrapper->Init(); //m_pComServerDao->Find(m_ComServerCfgMap); m_pCommunicationCfgDao->Find(m_MachineCfg.m_MachineType); m_pAlarmCfgDao->Find(); //m_pAlarmCfgDao->FindStop(m_AlarmCfgWrapper); //m_pAlarmCfgDao->FindPause(m_AlarmCfgWrapper); //m_pAlarmCfgDao->FindWarn(m_AlarmCfgWrapper); m_AlarmCfgWrapper->Init(); m_pParamSetCfgDao->Find(m_ParamSetCfg); //m_pLaserCfgDao->FindCfg(); m_ScannerControlCfgDao->FindCfg(); m_PrepareJobCfgDao->Find(m_MachineCfg.m_MachineType, m_PrepareJobCfgs); m_Machine->Init(); m_Machine->BindLaser(); m_IOVersionDao->Find(m_IOVersions); m_pAlarmCfgDao->CheckAlarmWithCfg(); //m_Machine->CheckAlarm(); } void ConfigManager::SaveConfig() { if (!m_pDB) return; SQLite::Transaction transaction(*m_pDB); m_pBaseConfigDao->SaveExtCfg(m_ExtCfg); m_pBaseConfigDao->SaveRunCfg(m_RunCfg); m_pBaseConfigDao->SaveFavoriteCfg(m_FavoriteCfg); m_pBaseConfigDao->SavePowderEstimate(m_PowderEstimateCfg); m_pBaseConfigDao->SaveRecoatCheckCfg(m_RecoatCheckCfg); m_pBaseConfigDao->SaveParamLimitCfg(m_ParamLimitCfg); m_pBaseConfigDao->SaveCameraCalibrationCfg(m_CameraCalibrationCfg); m_pAxisConfigDao->Save(m_AxisCfgs); m_pCommunicationCfgDao->Save(); //m_pComServerDao->Save(m_ComServerCfgMap); m_pMachineCfgDao->Save(m_MachineCfg); m_pAlarmCfgDao->Save(); m_pIOCfgDao->Save(m_IOCfgWrapper->m_IOCfgMap); m_pParamSetCfgDao->Save(m_ParamSetCfg); //m_pLaserCfgDao->Save(); m_ScannerControlCfgDao->Save(); m_PrepareJobCfgDao->Save(m_MachineCfg.m_MachineType, m_PrepareJobCfgs); transaction.commit(); } HbdLanguage* ConfigManager::GetLanguage(int id) { map::iterator it = m_LanguageMap.find(id); if (it == m_LanguageMap.end())return NULL; return m_LanguageMap[id]; } void ConfigManager::GetDefaultText(map& textmap) { textmap.clear(); m_pInfoTextDao->Find(textmap); } AxisCfg* ConfigManager::GetArmCfg() { return m_ArmCfg; } AxisCfg* ConfigManager::GetMoldCfg() { return m_MoldCfg; } AxisCfg* ConfigManager::GetLoadCfg() { return m_LoadCfg; } AxisCfg* ConfigManager::GetSupplyCfg() { return m_SupplyCfg; } void ConfigManager::Export(string &content, string file_path) { if ((content.size() % 16) != 0) { size_t size = content.size(); size += 16 - (size % 16); content.resize(size, '\0'); } const uint8_t crypt_key[17] = "Zdcf&mZXfNZZJ%yn"; struct AES_ctx ctx; AES_init_ctx(&ctx, crypt_key); for (size_t i = 0; i < content.size() / 16; i++) AES_ECB_encrypt(&ctx, (uint8_t *)&content[i * 16]); std::FILE *file; fopen_s(&file, file_path.c_str(), "wb"); fwrite(content.data(), sizeof(char), content.size(), file); fclose(file); } void ConfigManager::Import(string &content, string file_path) { std::FILE *file; fopen_s(&file, file_path.c_str(), "rb"); if (!file) return; fseek(file, 0, SEEK_END); int buf_size; int file_size = ftell(file); if (file_size % 16 != 0) buf_size = file_size + 16 - (file_size % 16); else buf_size = file_size; char *buffer = new char[buf_size]; fseek(file, 0, SEEK_SET); size_t ret_code = fread(buffer, sizeof(char), file_size, file); fclose(file); if (ret_code != file_size) return; const uint8_t crypt_key[17] = "Zdcf&mZXfNZZJ%yn"; struct AES_ctx ctx; AES_init_ctx(&ctx, crypt_key); for (int i = 0; i < buf_size / 16; i++) AES_ECB_decrypt(&ctx, (uint8_t *)&buffer[i * 16]); content = (char*)buffer; } void ConfigManager::Export(string file_path) { std::stringstream ss; m_pAlarmCfgDao->Export( ss); m_pBaseConfigDao->ExportExtCfg(m_ExtCfg, ss); m_pBaseConfigDao->ExportRunCfg(m_RunCfg, ss); m_pBaseConfigDao->ExportFavoriteCfg(m_FavoriteCfg, ss); m_pBaseConfigDao->ExportPowderEstimate(m_PowderEstimateCfg, ss); m_pBaseConfigDao->ExportRecoatCheckCfg(m_RecoatCheckCfg, ss); m_pBaseConfigDao->ExportParamLimitCfg(m_ParamLimitCfg, ss); m_pAxisConfigDao->Export(m_AxisCfgs, ss); m_pCommunicationCfgDao->Export(ss); m_pMachineCfgDao->Export(m_MachineCfg, ss); m_pIOCfgDao->Export(m_IOCfgWrapper->m_IOCfgMap, ss); m_pBaseConfigDao->ExportCameraCalibrationCfg(m_CameraCalibrationCfg, ss); //m_pLaserCfgDao->Export(ss); m_ScannerControlCfgDao->Export(ss); //Export(ss.str(), file_path); } void ConfigManager::Import(string file_path) { string sql; Import(sql, file_path); /* std::stringstream ss(sql); string cmd; SQLite::Transaction transaction(*m_pDB); while (getline(ss, cmd)) { try { m_pDB->exec(cmd); } catch (SQLite::Exception &e) { g_log->TraceError(_(u8"数据库错误:%s").c_str(), e.what()); } }; transaction.commit(); //g_StateManager->Stop(); m_pMachineCfgDao->Find(m_MachineCfg); m_Machine = Machine::CreateInstance(m_MachineCfg.m_MachineType); m_pBaseConfigDao->FindExtCfg(m_ExtCfg); m_pBaseConfigDao->FindRunCfg(m_RunCfg); m_pBaseConfigDao->FindFavoriteCfg(m_FavoriteCfg); m_pBaseConfigDao->FindPowderEstimate(m_PowderEstimateCfg); m_pBaseConfigDao->FindRecoatCheckCfg(m_RecoatCheckCfg); m_pBaseConfigDao->FindParamLimitCfg(m_ParamLimitCfg); m_pAxisConfigDao->Find(m_AxisCfgs); m_pCommunicationCfgDao->Find(m_MachineCfg.m_MachineType); m_pAlarmCfgDao->Find(); //m_pAlarmCfgDao->FindStop(m_AlarmCfgWrapper); //m_pAlarmCfgDao->FindPause(m_AlarmCfgWrapper); //m_pAlarmCfgDao->FindWarn(m_AlarmCfgWrapper); m_AlarmCfgWrapper->Init(); m_pLaserCfgDao->FindCfg(); SaveConfig(); // g_StateManager->Start();*/ } void ConfigManager::ExportPC(string file_path) { stringstream ss; m_ScannerControlCfgDao->ExportAllPC(ss); //Export(ss.str(), file_path); } void ConfigManager::ImportPC(string file_path) { string sql; Import(sql, file_path); m_ScannerControlCfgDao->ImportAllPc(sql); } void ConfigManager::ExportParamSet(string file_path) { std::stringstream sql; m_pParamSetCfgDao->Export(m_ParamSetCfg, sql); ////Export(sql.str(), file_path); } void ConfigManager::ImportParamSet(string file_path) { string sql; Import(sql, file_path); m_pParamSetCfgDao->Import(m_ParamSetCfg, sql); } void ConfigManager::UpdateZeroOffset(int id, long value) { m_pAxisConfigDao->UpdateZeroOffset(id, value); } vector* ConfigManager::GetIOVersions(int machineid) { if (m_IOVersions.find(machineid) == m_IOVersions.end()) { return NULL; } return m_IOVersions[machineid]; } void ConfigManager::DeleteMachineIO() { char buffer[256]; string strsql = "DELETE FROM %s WHERE %s=%d"; sprintf_s(buffer, sizeof(buffer), strsql.c_str(), IOCfg::TABLE_NAME.c_str(), IOCfg::FIELD_MACHINE_TYPE.c_str(), m_MachineCfg.m_MachineType); m_pDB->exec(buffer); } void ConfigManager::SendCfgToClients() { m_ParamLimitCfgNew.SetCfg(m_ParamLimitCfg); m_ParamLimitCfgNew.SendToClients(PARAMLIMITCFGPARAM); m_ExtCfgNew.SetCfg(m_ExtCfg); m_ExtCfgNew.SendToClients(EXTCFGPARAM); m_MoldCfg->SendToClients(MOLDCFGPARAM); m_LoadCfg->SendToClients(LOADCFGPARAM); m_ArmCfg->SendToClients(ARMCFGPARAM); m_SupplyCfg->SendToClients(SUPPLYCFGPARAM); m_CleanCfg->SendToClients(CLEANCFGPARAM); m_EleCfg->SendToClients(ELECFGPARAM); } void ConfigManager::UpdateCfg(const ReadData& rd) { switch (rd.dataType) { case PARAMLIMITCFG: m_ParamLimitCfgNew.Update(rd, PARAMLIMITCFGPARAM); m_ParamLimitCfgNew.UpdateCfg(m_ParamLimitCfg); case EXTCFG: m_ExtCfgNew.Update(rd, EXTCFGPARAM); m_ExtCfgNew.UpdateCfg(m_ExtCfg); default: break; } }