ModuleConfig.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <unistd.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <limits.h>
  5. #include <string>
  6. #include "ModuleConfig.h"
  7. #include "PacketAnalizer.h"
  8. #include "common_api.h"
  9. #include "LogLocalHelper.h"
  10. #include "Log4CPP.h"
  11. ModuleConfig::ModuleConfig()
  12. {
  13. }
  14. ModuleConfig::~ModuleConfig()
  15. {
  16. }
  17. bool ModuleConfig::CheckAndInitConfig(string ccosDevicePath)
  18. {
  19. bool ret = false;
  20. string configName;
  21. if (m_strModuleFileName.length() <= 0)
  22. {
  23. //标准设备
  24. string devicePath = ccosDevicePath;
  25. //CCOS/DEVICE, 11个字符
  26. if (devicePath.substr(0, 11) == "CCOS/DEVICE")
  27. {
  28. //是设备
  29. //todo 要生成模型文件名
  30. string module = devicePath.substr(12);
  31. configName = module;
  32. int x, moduleLen, spCount = 0;
  33. for (x = 0; x < module.length(); x++)
  34. {
  35. if (module[x] == '/')
  36. {
  37. if (spCount <= 3)
  38. module[x] = '_';
  39. configName[x] = '_';
  40. spCount++;
  41. if (spCount == 3)
  42. {
  43. moduleLen = x;
  44. }
  45. }
  46. }
  47. module = module.substr(0, moduleLen);
  48. module += ".json";
  49. m_strModuleFileName = module;
  50. configName += ".json";
  51. }
  52. }
  53. else
  54. {
  55. if (m_strConfigFilePath.length() <= 0)
  56. configName = m_strModuleFileName;
  57. else
  58. configName = m_strConfigFilePath;
  59. }
  60. if (m_strModuleFileName.length() <= 0)
  61. {
  62. //没有取到模型名
  63. return false;
  64. }
  65. //第二次执行的时候,逻辑要检查。。。
  66. //todo
  67. std::string modulePath = GetProcessDirectory();
  68. modulePath += "/DriverDefine/"; // 使用 Linux 路径分隔符
  69. std::string configPath = modulePath;
  70. modulePath += m_strModuleFileName; // 添加到模块路径
  71. configPath += "Config/"; // 使用 Linux 路径分隔符
  72. // 创建目录 (Linux 版本)
  73. mkdir(configPath.c_str(), 0755); // 设置权限 0755 (rwxr-xr-x)
  74. configPath += configName; // 添加配置文件名
  75. try {
  76. m_resModuleConfig.loadFile(modulePath.c_str());
  77. m_strModuleFilePath = modulePath;
  78. FINFO("Got module file [{$}] Read file path [{$}] ok.", m_strModuleFileName, modulePath);
  79. }
  80. catch (...)
  81. {
  82. FINFO("Got module file [{$}] but Read file path [{$}] failed.", m_strModuleFileName, modulePath);
  83. return false;
  84. }
  85. if (configName.length() > 0)
  86. {
  87. try {
  88. ResDataObject resConfig;
  89. resConfig.loadFile(configPath.c_str());
  90. m_resProperties = resConfig[0];
  91. m_strConfigFilePath = configPath;
  92. FINFO("Got config file path [{$}] Read content ok. [{$}]", m_strConfigFilePath, m_resProperties.encode());
  93. }
  94. catch (...)
  95. {
  96. //没有取到配置,则保存默认配置
  97. m_strConfigFilePath = configPath;
  98. ResDataObject resConfig;
  99. m_resProperties = m_resModuleConfig["Get"];
  100. resConfig.add("CONFIGURATION", m_resProperties);
  101. resConfig.SaveFile(m_strConfigFilePath.c_str());
  102. FINFO("No config file at path [{$}] Save content ok. [{$}]", m_strConfigFilePath, m_resProperties.encode());
  103. }
  104. }
  105. else
  106. {
  107. m_resProperties = m_resModuleConfig["Get"];
  108. }
  109. ret = true;
  110. FINFO("Module Content: {$}", m_resModuleConfig.encode());
  111. if (m_resModuleConfig.GetKeyCount("Get") > 0)
  112. m_resGets = m_resModuleConfig["Get"];
  113. if (m_resModuleConfig.GetKeyCount("Set") > 0)
  114. m_resSets = m_resModuleConfig["Set"];
  115. if (m_resModuleConfig.GetKeyCount("Add") > 0)
  116. m_resAdds = m_resModuleConfig["Add"];
  117. if (m_resModuleConfig.GetKeyCount("Del") > 0)
  118. m_resDels = m_resModuleConfig["Del"];
  119. if (m_resModuleConfig.GetKeyCount("Update") > 0)
  120. {
  121. FINFO("WHAT ? {$}", m_resModuleConfig["Update"].encode());
  122. m_resUpdates = m_resModuleConfig["Update"];
  123. }
  124. if (m_resModuleConfig.GetKeyCount("Action") > 0)
  125. m_resActions = m_resModuleConfig["Action"];
  126. return ret;
  127. }
  128. RET_STATUS ModuleConfig::GetDeviceResource(ResDataObject PARAM_OUT* pDeviceResource, const char* pszClientType)
  129. {
  130. //ResDataObject getPerp = m_resModuleConfig["Get"];
  131. for (int x = 0; x < m_resProperties.size(); x++)
  132. {
  133. (*pDeviceResource)["Attribute"].update(m_resProperties.GetKey(x), m_resProperties[x]["Value"]);
  134. }
  135. pDeviceResource->update("ClientType", pszClientType);// DPC_UnitClient);
  136. pDeviceResource->add("Get", m_resProperties);
  137. ResDataObject setPerp, updatePerp, addPerp, delPerp, actionPerp, msgPerp;
  138. setPerp = m_resModuleConfig["Set"];
  139. pDeviceResource->add("Set", setPerp);
  140. updatePerp = m_resModuleConfig["Update"];
  141. pDeviceResource->add("Update", updatePerp);
  142. addPerp = m_resModuleConfig["Add"];
  143. pDeviceResource->add("Add", addPerp);
  144. delPerp = m_resModuleConfig["Del"];
  145. pDeviceResource->add("Del", delPerp);
  146. actionPerp = m_resModuleConfig["Action"];
  147. for (int x = 0; x < actionPerp.size(); x++)
  148. {
  149. (*pDeviceResource)["Action"].update(actionPerp.GetKey(x), actionPerp[x]);
  150. }
  151. //msgPerp = m_resModuleConfig["Message"];
  152. return RET_SUCCEED;
  153. }
  154. RET_STATUS ModuleConfig::GetUpdatableItems(string level, ResDataObject& resItems)
  155. {
  156. ResDataObject resUpdate;
  157. resUpdate = m_resModuleConfig["Update"];
  158. bool bOutput = false;
  159. for (int x = 0; x < resUpdate.size(); x++)
  160. {
  161. bOutput = false;
  162. string itemLevel;
  163. if (resUpdate[x].GetKeyCount("Level") > 0)
  164. {
  165. itemLevel = (const char*)resUpdate[x]["Level"];
  166. if (itemLevel == level)
  167. bOutput = true;
  168. else
  169. {
  170. if (level == "Private") {
  171. bOutput = true;
  172. }
  173. else if (level == "E-COM") {
  174. if (itemLevel == "Public" || itemLevel == "E-COM") {
  175. bOutput = true;
  176. }
  177. }
  178. }
  179. }
  180. else
  181. {
  182. //没有配置Level是Private
  183. if (level == "Private")
  184. bOutput = true;
  185. }
  186. if (bOutput)
  187. {
  188. resItems.update(resUpdate.GetKey(x), resUpdate[x]);
  189. }
  190. }
  191. return RET_SUCCEED;
  192. }
  193. string ModuleConfig::UpdateItem(const char* pszProperty, const char* pszValueUpdate)
  194. {
  195. //保存到配置文件
  196. //
  197. ResDataObject resNewValue = m_resProperties[pszProperty];
  198. ResDataObject resUpdate;
  199. string ret;
  200. resUpdate.decode(pszValueUpdate);
  201. if (resUpdate.size() > 0)
  202. {
  203. for (int x = 0; x < resUpdate.size(); x++)
  204. {
  205. resNewValue["Value"].update(resUpdate.GetKey(x), resUpdate[x]);
  206. }
  207. FINFO("Save Object [{$}] new value {$}", pszProperty, resNewValue.encode());
  208. m_resProperties.update(pszProperty, resNewValue);
  209. //通知模型对象,值更新了
  210. //return OnUpdate(pszProperty, m_resProperties[pszProperty]["Value"].encode(), resRespons);
  211. ret = m_resProperties[pszProperty]["Value"].encode();
  212. }
  213. else
  214. {
  215. resNewValue.update("Value", pszValueUpdate);
  216. FINFO("Save [{$}] new value {$}", pszProperty, pszValueUpdate);
  217. m_resProperties[pszProperty].update("Value", pszValueUpdate);
  218. //通知模型对象,值更新了
  219. //return OnUpdate(pszProperty, pszValueUpdate, resRespons);
  220. ret = pszValueUpdate;
  221. }
  222. return ret;
  223. }
  224. bool ModuleConfig::CanDo(const char* pszProperty, int packetCmd)
  225. {
  226. bool bCanDo = false;
  227. switch (packetCmd)
  228. {
  229. case PACKET_CMD_EXE:
  230. bCanDo = m_resActions.GetKeyCount(pszProperty) > 0;
  231. break;
  232. case PACKET_CMD_GET:
  233. bCanDo = m_resGets.GetKeyCount(pszProperty) > 0;
  234. bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0;
  235. break;
  236. case PACKET_CMD_SET:
  237. bCanDo = m_resSets.GetKeyCount(pszProperty) > 0;
  238. bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0;
  239. break;
  240. case PACKET_CMD_ADD:
  241. bCanDo = m_resAdds.GetKeyCount(pszProperty) > 0;
  242. bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0;
  243. break;
  244. case PACKET_CMD_DEL:
  245. bCanDo = m_resDels.GetKeyCount(pszProperty) > 0;
  246. bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0;
  247. break;
  248. case PACKET_CMD_UPDATE:
  249. bCanDo = m_resUpdates.GetKeyCount(pszProperty) > 0;
  250. bCanDo &= m_resProperties.GetKeyCount(pszProperty) > 0;
  251. break;
  252. case PACKET_CMD_MSG:
  253. break;
  254. default:
  255. break;
  256. }
  257. return bCanDo;
  258. }
  259. RET_STATUS ModuleConfig::GetAll(ResDataObject& resRespons, std::function<RET_STATUS(const char*, ResDataObject&)> getItem)
  260. {
  261. string key;
  262. for (int x = 0; x < m_resProperties.size(); x++)
  263. {
  264. key = m_resProperties.GetKey(x);
  265. if (getItem(key.c_str(), resRespons) == RET_SUCCEED)
  266. {
  267. m_resProperties.update(key.c_str(), resRespons);
  268. }
  269. }
  270. resRespons = m_resProperties;
  271. ResDataObject resMa, resMa2;
  272. //"": "Public",
  273. // "DescKey" : "TUBEHEAT",
  274. // "Value" : "0"
  275. resMa.add("Level", "Public");
  276. resMa.update("DescKey", "Manufacturer");
  277. resMa.update("Value", m_resModuleConfig["Manufacturer"]);
  278. resRespons.update("Manufacturer", resMa);
  279. resMa2.clear();
  280. resMa2.add("Level", "Public");
  281. resMa2.update("DescKey", "Model");
  282. resMa2.update("Value", m_resModuleConfig["Model"]);
  283. resRespons.update("Model", resMa2);
  284. return RET_SUCCEED;
  285. }
  286. RET_STATUS ModuleConfig::SaveToConfigFile()
  287. {
  288. RET_STATUS ret = RET_FAILED;
  289. if (m_strConfigFilePath.length() > 0)
  290. {
  291. ResDataObject resConfig;
  292. //m_resProperties = m_resModuleConfig["Get"];
  293. resConfig.add("CONFIGURATION", m_resProperties);
  294. if (resConfig.SaveFile(m_strConfigFilePath.c_str()))
  295. {
  296. FINFO("Save Module Config file [{$}] content {$}", m_strConfigFilePath, resConfig.encode());
  297. }
  298. else
  299. {
  300. FINFO("Failed Save Module Config file [{$}] content {$}", m_strConfigFilePath, resConfig.encode());
  301. }
  302. ret = RET_SUCCEED;
  303. }
  304. return ret;
  305. }
  306. void ModuleConfig::RestoreConfig()
  307. {
  308. //恢复出厂设置
  309. m_resProperties = m_resModuleConfig["Get"];
  310. SaveToConfigFile();
  311. }