#include "ModuleConfig.h" #include "PacketAnalizer.h" #include "common_api.h" #include #include #include #include #include ModuleConfig::ModuleConfig() { } ModuleConfig::~ModuleConfig() { } bool ModuleConfig::CheckAndInitConfig(string ccosDevicePath) { bool ret = false; string configName; if (m_strModuleFileName.length() <= 0) { //标准设备 string devicePath = ccosDevicePath; //CCOS/DEVICE, 11个字符 if (devicePath.substr(0, 11) == "CCOS/DEVICE") { //是设备 //todo 要生成模型文件名 string module = devicePath.substr(12); configName = module; int x, moduleLen, spCount = 0; for (x = 0; x < module.length(); x++) { if (module[x] == '/') { if (spCount <= 3) module[x] = '_'; configName[x] = '_'; spCount++; if (spCount == 3) { moduleLen = x; } } } module = module.substr(0, moduleLen); module += ".json"; m_strModuleFileName = module; configName += ".json"; } } else { if (m_strConfigFilePath.length() <= 0) configName = m_strModuleFileName; else configName = m_strConfigFilePath; } if (m_strModuleFileName.length() <= 0) { //没有取到模型名 return false; } //第二次执行的时候,逻辑要检查。。。 //todo std::string modulePath = GetProcessDirectory(); modulePath += "/DriverDefine/"; // 使用 Linux 路径分隔符 std::string configPath = modulePath; modulePath += m_strModuleFileName; // 添加到模块路径 configPath += "Config/"; // 使用 Linux 路径分隔符 // 创建目录 (Linux 版本) mkdir(configPath.c_str(), 0755); // 设置权限 0755 (rwxr-xr-x) configPath += configName; // 添加配置文件名 try { m_resModuleConfig.loadFile(modulePath.c_str()); m_strModuleFilePath = modulePath; //mLog::FINFO("Got module file [{$}] Read file path [{$}] ok.", m_strModuleFileName, modulePath); } catch (...) { //mLog::FINFO("Got module file [{$}] but Read file path [{$}] failed.", m_strModuleFileName, modulePath); return false; } if (configName.length() > 0) { try { ResDataObject resConfig; resConfig.loadFile(configPath.c_str()); m_resProperties = resConfig[0]; m_strConfigFilePath = configPath; //mLog::FINFO("Got config file path [{$}] Read content ok. [{$}]", m_strConfigFilePath, m_resProperties.encode()); } catch (...) { //没有取到配置,则保存默认配置 m_strConfigFilePath = configPath; ResDataObject resConfig; m_resProperties = m_resModuleConfig["Get"]; resConfig.add("CONFIGURATION", m_resProperties); resConfig.SaveFile(m_strConfigFilePath.c_str()); //mLog::FINFO("No config file at path [{$}] Save content ok. [{$}]", m_strConfigFilePath, m_resProperties.encode()); } } else { m_resProperties = m_resModuleConfig["Get"]; } ret = true; //mLog::FINFO("Module Content: {$}", m_resModuleConfig.encode()); if (m_resModuleConfig.GetKeyCount("Get") > 0) m_resGets = m_resModuleConfig["Get"]; if (m_resModuleConfig.GetKeyCount("Set") > 0) m_resSets = m_resModuleConfig["Set"]; if (m_resModuleConfig.GetKeyCount("Add") > 0) m_resAdds = m_resModuleConfig["Add"]; if (m_resModuleConfig.GetKeyCount("Del") > 0) m_resDels = m_resModuleConfig["Del"]; if (m_resModuleConfig.GetKeyCount("Update") > 0) { //mLog::FINFO("WHAT ? {$}", m_resModuleConfig["Update"].encode()); m_resUpdates = m_resModuleConfig["Update"]; } if (m_resModuleConfig.GetKeyCount("Action") > 0) m_resActions = m_resModuleConfig["Action"]; return ret; } RET_STATUS ModuleConfig::GetDeviceResource(ResDataObject PARAM_OUT* pDeviceResource, const char* pszClientType) { //ResDataObject getPerp = m_resModuleConfig["Get"]; for (int x = 0; x < m_resProperties.size(); x++) { (*pDeviceResource)["Attribute"].update(m_resProperties.GetKey(x), m_resProperties[x]["Value"]); } pDeviceResource->update("ClientType", pszClientType);// DPC_UnitClient); pDeviceResource->add("Get", m_resProperties); ResDataObject setPerp, updatePerp, addPerp, delPerp, actionPerp, msgPerp; setPerp = m_resModuleConfig["Set"]; pDeviceResource->add("Set", setPerp); updatePerp = m_resModuleConfig["Update"]; pDeviceResource->add("Update", updatePerp); addPerp = m_resModuleConfig["Add"]; pDeviceResource->add("Add", addPerp); delPerp = m_resModuleConfig["Del"]; pDeviceResource->add("Del", delPerp); actionPerp = m_resModuleConfig["Action"]; for (int x = 0; x < actionPerp.size(); x++) { (*pDeviceResource)["Action"].update(actionPerp.GetKey(x), actionPerp[x]); } //msgPerp = m_resModuleConfig["Message"]; return RET_SUCCEED; } RET_STATUS ModuleConfig::GetUpdatableItems(string level, ResDataObject& resItems) { ResDataObject resUpdate; resUpdate = m_resModuleConfig["Update"]; bool bOutput = false; for (int x = 0; x < resUpdate.size(); x++) { bOutput = false; string itemLevel; if (resUpdate[x].GetKeyCount("Level") > 0) { itemLevel = (const char*)resUpdate[x]["Level"]; if (itemLevel == level) bOutput = true; else { if (level == "Private") { bOutput = true; } else if (level == "E-COM") { if (itemLevel == "Public" || itemLevel == "E-COM") { bOutput = true; } } } } else { //没有配置Level是Private if (level == "Private") bOutput = true; } if (bOutput) { resItems.update(resUpdate.GetKey(x), resUpdate[x]); } } return RET_SUCCEED; } string ModuleConfig::UpdateItem(const char* pszProperty, const char* pszValueUpdate) { //保存到配置文件 // ResDataObject resNewValue = m_resProperties[pszProperty]; ResDataObject resUpdate; string ret; resUpdate.decode(pszValueUpdate); if (resUpdate.size() > 0) { for (int x = 0; x < resUpdate.size(); x++) { resNewValue["Value"].update(resUpdate.GetKey(x), resUpdate[x]); } //mLog::FINFO("Save Object [{$}] new value {$}", pszProperty, resNewValue.encode()); m_resProperties.update(pszProperty, resNewValue); //通知模型对象,值更新了 //return OnUpdate(pszProperty, m_resProperties[pszProperty]["Value"].encode(), resRespons); ret = m_resProperties[pszProperty]["Value"].encode(); } else { resNewValue.update("Value", pszValueUpdate); //mLog::FINFO("Save [{$}] new value {$}", pszProperty, pszValueUpdate); m_resProperties[pszProperty].update("Value", pszValueUpdate); //通知模型对象,值更新了 //return OnUpdate(pszProperty, pszValueUpdate, resRespons); ret = pszValueUpdate; } return ret; } bool ModuleConfig::CanDo(const char* pszProperty, int packetCmd) { bool bCanDo = false; switch (packetCmd) { case PACKET_CMD_EXE: bCanDo = m_resActions.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_GET: bCanDo = m_resGets.GetKeyCount(pszProperty) > 0; bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_SET: bCanDo = m_resSets.GetKeyCount(pszProperty) > 0; bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_ADD: bCanDo = m_resAdds.GetKeyCount(pszProperty) > 0; bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_DEL: bCanDo = m_resDels.GetKeyCount(pszProperty) > 0; bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_UPDATE: bCanDo = m_resUpdates.GetKeyCount(pszProperty) > 0; bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0; break; case PACKET_CMD_MSG: break; default: break; } return bCanDo; } RET_STATUS ModuleConfig::GetAll(ResDataObject& resRespons, std::function getItem) { string key; for (int x = 0; x < m_resProperties.size(); x++) { key = m_resProperties.GetKey(x); if (getItem(key.c_str(), resRespons) == RET_SUCCEED) { m_resProperties.update(key.c_str(), resRespons); } } resRespons = m_resProperties; ResDataObject resMa, resMa2; //"": "Public", // "DescKey" : "TUBEHEAT", // "Value" : "0" resMa.add("Level", "Public"); resMa.update("DescKey", "Manufacturer"); resMa.update("Value", m_resModuleConfig["Manufacturer"]); resRespons.update("Manufacturer", resMa); resMa2.clear(); resMa2.add("Level", "Public"); resMa2.update("DescKey", "Model"); resMa2.update("Value", m_resModuleConfig["Model"]); resRespons.update("Model", resMa2); return RET_SUCCEED; } RET_STATUS ModuleConfig::SaveToConfigFile() { RET_STATUS ret = RET_FAILED; if (m_strConfigFilePath.length() > 0) { ResDataObject resConfig; //m_resProperties = m_resModuleConfig["Get"]; resConfig.add("CONFIGURATION", m_resProperties); if (resConfig.SaveFile(m_strConfigFilePath.c_str())) { //mLog::FINFO("Save Module Config file [{$}] content {$}", m_strConfigFilePath, resConfig.encode()); } else { //mLog::FINFO("Failed Save Module Config file [{$}] content {$}", m_strConfigFilePath, resConfig.encode()); } ret = RET_SUCCEED; } return ret; } void ModuleConfig::RestoreConfig() { //恢复出厂设置 m_resProperties = m_resModuleConfig["Get"]; SaveToConfigFile(); }