ModuleConfig.cpp 8.8 KB

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