DevEntityCommBase.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__
  2. #define _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__
  3. #pragma once
  4. #include "SpBase.h"
  5. #include "SpHelper.h"
  6. #include "DeviceBaseClass.h"
  7. #include "SimpleString.h"
  8. #include "path.h"
  9. #include "toolkit.h"
  10. #include <regex>
  11. struct VendorLibInfo
  12. {
  13. CSimpleStringA strDevice;
  14. CSimpleStringA strVendor;
  15. CSimpleStringA strVersion;
  16. CSimpleStringA strBatch;
  17. bool fileServering;
  18. CSimpleStringA strFileValue;
  19. VendorLibInfo() :strDevice(true), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true){}
  20. VendorLibInfo(const CSimpleStringA& strDeviceName) :strDevice(strDeviceName), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true) {}
  21. VendorLibInfo(const VendorLibInfo& rhs) :strDevice(rhs.strDevice), strVendor(rhs.strVendor), strVersion(rhs.strVersion), strBatch(rhs.strBatch), fileServering(rhs.fileServering), strFileValue(rhs.strFileValue) {}
  22. VendorLibInfo& operator = (const VendorLibInfo& rhs) {
  23. strDevice = rhs.strDevice;
  24. strVendor = rhs.strVendor;
  25. strVersion = rhs.strVersion;
  26. strBatch = rhs.strBatch;
  27. fileServering = rhs.fileServering;
  28. strFileValue = rhs.strFileValue;
  29. return *this;
  30. }
  31. bool IsValid() const
  32. {
  33. if (!strFileValue.IsNullOrEmpty()) return true;
  34. return (!strDevice.IsNullOrEmpty()
  35. && !strVendor.IsNullOrEmpty()
  36. && !strVersion.IsNullOrEmpty()
  37. && !strBatch.IsNullOrEmpty());
  38. }
  39. bool IsNotConfig() const
  40. {
  41. return(strFileValue.IsNullOrEmpty() && strVendor.IsNullOrEmpty() && strVersion.IsNullOrEmpty() && strBatch.IsNullOrEmpty());
  42. }
  43. int GetVersionInt() const
  44. {
  45. int result = 0;
  46. if (!strVersion.IsNullOrEmpty())
  47. {
  48. result = atoi(strVersion.GetData());
  49. }
  50. return result;
  51. }
  52. int GetBatchInt() const
  53. {
  54. int result = 0;
  55. if (!strBatch.IsNullOrEmpty())
  56. {
  57. result = atoi(strBatch.GetData());
  58. }
  59. return result;
  60. }
  61. CSimpleStringA toLibNameString() const
  62. {
  63. if (!strFileValue.IsNullOrEmpty()) {
  64. return strFileValue;
  65. }
  66. CSimpleStringA strFullLibName(true);
  67. if (!strDevice.IsNullOrEmpty())
  68. {
  69. strFullLibName = strDevice;
  70. if (!strVendor.IsNullOrEmpty())
  71. {
  72. strFullLibName += ".";
  73. strFullLibName += strVendor;
  74. }
  75. if (!strVersion.IsNullOrEmpty())
  76. {
  77. strFullLibName += ".";
  78. strFullLibName += strVersion;
  79. }
  80. if (!strBatch.IsNullOrEmpty())
  81. {
  82. strFullLibName += ".";
  83. strFullLibName += strBatch;
  84. }
  85. #ifdef RVC_OS_WIN
  86. strFullLibName += ".dll";
  87. #else
  88. CSimpleStringA strPrefix("lib");
  89. strPrefix += strFullLibName;
  90. strPrefix += ".so";
  91. strFullLibName = strPrefix;
  92. #endif //RVC_OS_WIN
  93. }
  94. return strFullLibName;
  95. }
  96. };
  97. class CDevAdptEntityBase : public CEntityBase
  98. {
  99. public:
  100. CDevAdptEntityBase() { }
  101. ErrorCodeEnum LoadVendorLibName()
  102. {
  103. if (!vendorLibInfo.IsValid()) {
  104. return ExtractVendorLibName();
  105. }
  106. return IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  107. }
  108. virtual CSimpleStringA GetVendorLibName()
  109. {
  110. if (!vendorLibInfo.IsValid()) {
  111. ExtractVendorLibName();
  112. }
  113. return vendorLibInfo.toLibNameString();
  114. }
  115. virtual ErrorCodeEnum ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath);
  116. virtual void InitializeVendorLogSwitch();
  117. virtual ~CDevAdptEntityBase() {}
  118. public:
  119. VendorLibInfo vendorLibInfo;
  120. protected:
  121. virtual ErrorCodeEnum CustomVendorLibInfo() { return Error_Succeed; }
  122. private:
  123. ErrorCodeEnum ExtractVendorLibName();
  124. bool IsNotConfigure() const
  125. {
  126. return vendorLibInfo.IsNotConfig();
  127. }
  128. };
  129. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibName()
  130. {
  131. CSmartPointer<IConfigInfo> spRootConfig;
  132. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Root, spRootConfig);
  133. if (IS_SUCCEED(erroCode))
  134. {
  135. CSimpleStringA strDeviceEntityName = GetEntityName();
  136. #ifndef DEVOPS_ON_PRD
  137. //非生产情况下,才支持测试模式
  138. CSystemRunInfo runInfo = { 0 };
  139. ErrorCodeEnum ec = GetFunction()->GetSystemRunInfo(runInfo);
  140. if (ec == Error_Succeed && runInfo.autoTest) //识别成测试模式的条件
  141. {
  142. vendorLibInfo.strDevice = strDeviceEntityName;
  143. vendorLibInfo.strVendor = "simulator";
  144. vendorLibInfo.strVersion = "1";
  145. vendorLibInfo.strBatch = "1";
  146. }
  147. else
  148. #endif
  149. {
  150. if (strDeviceEntityName.Compare("CardIssuerStore", true) == 0 || strDeviceEntityName.Compare("CardIssuerStand", true) == 0) {
  151. strDeviceEntityName = "CardIssuer";
  152. }
  153. CSimpleStringA strSection = CSimpleStringA("Device.") + strDeviceEntityName;
  154. vendorLibInfo.strDevice = strDeviceEntityName;
  155. CSimpleStringA strValue(true);
  156. spRootConfig->ReadConfigValue(strSection, "File", strValue);
  157. if (!strValue.IsNullOrEmpty()) { vendorLibInfo.strFileValue = strValue; vendorLibInfo.fileServering = true; }
  158. else {
  159. //TODO: 需要处理部分硬件实体依赖Vendor字段实现某些功能的风险 [Gifur@20241021]
  160. spRootConfig->ReadConfigValue(strSection, "Vendor", vendorLibInfo.strVendor);
  161. spRootConfig->ReadConfigValue(strSection, "Version", vendorLibInfo.strVersion);
  162. spRootConfig->ReadConfigValue(strSection, "Batch", vendorLibInfo.strBatch);
  163. }
  164. }
  165. }
  166. if (IS_SUCCEED(erroCode)) {
  167. erroCode = CustomVendorLibInfo();
  168. }
  169. return erroCode;
  170. }
  171. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath)
  172. {
  173. CSimpleStringA strLibName = GetVendorLibName();
  174. CSimpleStringA strDepPath;
  175. ErrorCodeEnum rc = GetFunction()->GetPath("Dep", strDepPath);
  176. if (rc == Error_Succeed) {
  177. csLibFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)strLibName);
  178. rc = IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  179. }
  180. else {
  181. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode("RTA0048")("Get Dep path failed: %s", SpStrError(rc));
  182. }
  183. return rc;
  184. }
  185. inline void CDevAdptEntityBase::InitializeVendorLogSwitch()
  186. {
  187. if (!vendorLibInfo.IsValid()) {
  188. ExtractVendorLibName();
  189. }
  190. struct VendorLogConfig {
  191. VendorLogConfig() :strLevel("OFF"), strType("FILE"), strDllName(""), strLogPath(""), strModuleName(""){}
  192. CSimpleStringA strLevel;
  193. CSimpleStringA strType;
  194. CSimpleStringA strDllName;
  195. CSimpleStringA strLogPath;
  196. CSimpleStringA strModuleName;
  197. void Settle() {
  198. toolkit_setenv("VENDOR_RECODE_LEVEL", strLevel);
  199. toolkit_setenv("VENDOR_RECODE_TYPE", strType);
  200. toolkit_setenv("VENDOR_DLL_NAME", strDllName);
  201. toolkit_setenv("VENDOR_LOG_PATH", strLogPath);
  202. toolkit_setenv("VENDOR_PARENT_NAME", strModuleName);
  203. }
  204. } stLogConfig;
  205. stLogConfig.strDllName = vendorLibInfo.toLibNameString().GetData();
  206. stLogConfig.strModuleName = GetEntityName();
  207. CSmartPointer<IConfigInfo> centerConfig;
  208. GetFunction()->OpenConfig(Config_CenterSetting, centerConfig);
  209. CSimpleStringA str;
  210. centerConfig->ReadConfigValue(GetEntityName(), "All", str);
  211. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("All: %s", str.GetData());
  212. if (!str.IsNullOrEmpty()) {
  213. stLogConfig.strLevel = str;
  214. }
  215. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  216. stLogConfig.strType = "UPLOAD";
  217. int nSaveFileOrNot(0);
  218. centerConfig->ReadConfigValueInt("Common", "SaveFile", nSaveFileOrNot);
  219. if ((nSaveFileOrNot & 1) == 1) {
  220. if (!stLogConfig.strType.IsNullOrEmpty()) stLogConfig.strType += "|";
  221. stLogConfig.strType += "FILE";
  222. }
  223. ErrorCodeEnum ec = GetFunction()->GetPath("Dbg", stLogConfig.strLogPath);
  224. if (IS_FAILURED(ec)) {
  225. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode("RTA0048")("Get Dbg path failed: %s", SpStrError(ec));
  226. }
  227. stLogConfig.Settle();
  228. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("{\"RecordLevel\":\"%s\", \"RecordType\":\"%s\"}",
  229. stLogConfig.strLevel.GetData(),stLogConfig.strType.GetData());
  230. }
  231. }
  232. #define GET_DEV_ENTITY_BASE_POINTER() \
  233. (dynamic_cast<CDevAdptEntityBase*>(GetEntityBase()))
  234. #endif /*_RVC_DEVICE_ADAPTER_ENTITY_BASE_H__*/