DevEntityCommBase.hpp 7.2 KB

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