DevEntityCommBase.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. bool IsValid() const
  20. {
  21. return (!strDevice.IsNullOrEmpty()
  22. && !strVendor.IsNullOrEmpty()
  23. && !strVersion.IsNullOrEmpty()
  24. && !strBatch.IsNullOrEmpty());
  25. }
  26. bool IsNotConfig() const
  27. {
  28. return(strVendor.IsNullOrEmpty() && strVersion.IsNullOrEmpty() && strBatch.IsNullOrEmpty());
  29. }
  30. int GetVersionInt() const
  31. {
  32. int result = 0;
  33. if (!strVersion.IsNullOrEmpty())
  34. {
  35. result = atoi(strVersion.GetData());
  36. }
  37. return result;
  38. }
  39. int GetBatchInt() const
  40. {
  41. int result = 0;
  42. if (!strBatch.IsNullOrEmpty())
  43. {
  44. result = atoi(strBatch.GetData());
  45. }
  46. return result;
  47. }
  48. CSimpleStringA toLibNameString() const
  49. {
  50. CSimpleStringA strFullLibName(true);
  51. if (!strDevice.IsNullOrEmpty())
  52. {
  53. strFullLibName = strDevice;
  54. if (!strVendor.IsNullOrEmpty())
  55. {
  56. strFullLibName += ".";
  57. strFullLibName += strVendor;
  58. }
  59. if (!strVersion.IsNullOrEmpty())
  60. {
  61. strFullLibName += ".";
  62. strFullLibName += strVersion;
  63. }
  64. if (!strBatch.IsNullOrEmpty())
  65. {
  66. strFullLibName += ".";
  67. strFullLibName += strBatch;
  68. }
  69. #ifdef RVC_OS_WIN
  70. strFullLibName += ".dll";
  71. #else
  72. CSimpleStringA strPrefix("lib");
  73. strPrefix += strFullLibName;
  74. strPrefix += ".so";
  75. strFullLibName = strPrefix;
  76. #endif //RVC_OS_WIN
  77. }
  78. return strFullLibName;
  79. }
  80. };
  81. class CDevAdptEntityBase : public CEntityBase
  82. {
  83. public:
  84. CDevAdptEntityBase() { }
  85. virtual CSimpleStringA GetVendorLibName()
  86. {
  87. if (!vendorLibInfo.IsValid()) {
  88. ExtractVendorLibName();
  89. }
  90. return vendorLibInfo.toLibNameString();
  91. }
  92. virtual ErrorCodeEnum ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath);
  93. virtual void InitializeVendorLogSwitch();
  94. virtual ~CDevAdptEntityBase() {}
  95. public:
  96. VendorLibInfo vendorLibInfo;
  97. protected:
  98. virtual ErrorCodeEnum CustomVendorLibInfo() { return Error_Succeed; }
  99. private:
  100. ErrorCodeEnum ExtractVendorLibName();
  101. bool IsNotConfigure() const
  102. {
  103. return vendorLibInfo.IsNotConfig();
  104. }
  105. };
  106. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibName()
  107. {
  108. CSmartPointer<IConfigInfo> spRootConfig;
  109. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Root, spRootConfig);
  110. if (IS_SUCCEED(erroCode))
  111. {
  112. CSimpleStringA strSection = CSimpleStringA("Device.") + GetEntityName();
  113. vendorLibInfo.strDevice = GetEntityName();
  114. spRootConfig->ReadConfigValue(strSection, "Vendor", vendorLibInfo.strVendor);
  115. spRootConfig->ReadConfigValue(strSection, "Version", vendorLibInfo.strVersion);
  116. spRootConfig->ReadConfigValue(strSection, "Batch", vendorLibInfo.strBatch);
  117. #if defined(_MSC_VER)
  118. int iVersion = 0;
  119. int iBatch = 0;
  120. if (!vendorLibInfo.strVersion.IsNullOrEmpty()) iVersion = atoi((const char*)vendorLibInfo.strVersion);
  121. if (!vendorLibInfo.strBatch.IsNullOrEmpty()) iBatch = atoi((const char*)vendorLibInfo.strBatch);
  122. CSystemStaticInfo sysInfo;
  123. erroCode = GetFunction()->GetSystemStaticInfo(sysInfo);
  124. if (IS_SUCCEED(erroCode)) {
  125. if (!sysInfo.strMachineType.Compare("RVC.Stand2S", true)) {
  126. CSimpleStringA strVendor(true);
  127. spRootConfig->ReadConfigValue("Device.PinPad", "Vendor", strVendor);
  128. //zss first,kxd second,keba third
  129. //TODO:kxd ,keba
  130. if (!strVendor.Compare("Zss", true)) {
  131. //如果配置大于等于7.1,直接读取root,否则映射加载7.1
  132. if ((iVersion != 0 && iBatch != 0) && !(iVersion > 7 || (iVersion == 7 && iBatch >= 1))) {
  133. //读取运行时信息
  134. CSimpleStringA deviceInfo(true);
  135. GetFunction()->GetSysVar("zssInfo", deviceInfo);
  136. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("zssInfo:%s", deviceInfo.GetData());
  137. if (!deviceInfo.Compare("ZSS9100", true)) {
  138. vendorLibInfo.strVendor = "ZSS";
  139. vendorLibInfo.strVersion = "7";
  140. vendorLibInfo.strBatch = "1";
  141. }
  142. }
  143. } else if (!strVendor.Compare("kxd", true)) {
  144. //如果配置大于等于7.1,直接读取root,否则映射加载7.1
  145. if ((iVersion != 0 && iBatch != 0) && !(iVersion > 7 || (iVersion == 7 && iBatch >= 1))) {
  146. //读取运行时信息
  147. CSimpleStringA deviceInfo(true);
  148. GetFunction()->GetSysVar("kxdInfo", deviceInfo);
  149. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("kxdInfo:%s", deviceInfo.GetData());
  150. if (!deviceInfo.Compare("KS218", true)) {
  151. vendorLibInfo.strVendor = "kxd";
  152. vendorLibInfo.strVersion = "7";
  153. vendorLibInfo.strBatch = "1";
  154. }
  155. }
  156. } else if (!strVendor.Compare("Keba", true)) {
  157. //如果配置大于等于7.1,直接读取root,否则映射加载7.1
  158. if ((iVersion != 0 && iBatch != 0) && !(iVersion > 7 || (iVersion == 7 && iBatch >= 1))) {
  159. //读取运行时信息
  160. CSimpleStringA deviceInfo(true);
  161. GetFunction()->GetSysVar("kebaInfo", deviceInfo);
  162. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("kebaInfo:%s", deviceInfo.GetData());
  163. if (!deviceInfo.Compare("E1080", true)) {
  164. vendorLibInfo.strVendor = "keba";
  165. vendorLibInfo.strVersion = "7";
  166. vendorLibInfo.strBatch = "1";
  167. }
  168. }
  169. }
  170. }
  171. }
  172. #endif //_MSC_VER
  173. }
  174. if (IS_SUCCEED(erroCode)) {
  175. erroCode = CustomVendorLibInfo();
  176. }
  177. return erroCode;
  178. }
  179. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath)
  180. {
  181. CSimpleStringA strLibName = GetVendorLibName();
  182. CSimpleStringA strDepPath;
  183. GetFunction()->GetPath("Dep", strDepPath);
  184. csLibFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)strLibName);
  185. return IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  186. }
  187. inline void CDevAdptEntityBase::InitializeVendorLogSwitch()
  188. {
  189. LOG_FUNCTION();
  190. if (!vendorLibInfo.IsValid()) {
  191. ExtractVendorLibName();
  192. }
  193. struct VendorLogConfig {
  194. VendorLogConfig() :strLevel("OFF"), strType("FILE"), strDllName(""), strLogPath("") {}
  195. CSimpleStringA strLevel;
  196. CSimpleStringA strType;
  197. CSimpleStringA strDllName;
  198. CSimpleStringA strLogPath;
  199. void Settle() {
  200. toolkit_setenv("VENDOR_RECODE_LEVEL", strLevel);
  201. toolkit_setenv("VENDOR_RECODE_TYPE", strType);
  202. toolkit_setenv("VENDOR_DLL_NAME", strDllName);
  203. toolkit_setenv("VENDOR_LOG_PATH", strLogPath);
  204. }
  205. } stLogConfig;
  206. stLogConfig.strDllName = vendorLibInfo.toLibNameString().GetData();
  207. CSmartPointer<IConfigInfo> centerConfig;
  208. GetFunction()->OpenConfig(Config_CenterSetting, centerConfig);
  209. CSimpleStringA str;
  210. centerConfig->ReadConfigValue(GetEntityName(), vendorLibInfo.strVendor, str);
  211. if (str.IsNullOrEmpty()) {
  212. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To get all config");
  213. centerConfig->ReadConfigValue(GetEntityName(), "All", str);
  214. }
  215. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("All: %s", str.GetData());
  216. if (!str.IsNullOrEmpty()) {
  217. stLogConfig.strLevel = str;
  218. }
  219. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  220. CSystemStaticInfo sysInfo;
  221. GetFunction()->GetSystemStaticInfo(sysInfo);
  222. CSimpleStringA strWhiteNameSheet;
  223. centerConfig->ReadConfigValue(GetEntityName(), "AllowTerminals", strWhiteNameSheet);
  224. Dbg("AllowTerminals: %s", strWhiteNameSheet.GetData());
  225. /** 白名单存在,说明需要过滤终端号,但是集中配置改造后已经没有这种操作了,这个逻辑后续可以去掉 Gifur@2023328]*/
  226. if (!strWhiteNameSheet.IsNullOrEmpty()) {
  227. bool bWhiteNameSheet = false;
  228. CAutoArray<CSimpleStringA> arrayWhiteNameSheet = strWhiteNameSheet.Split(',');
  229. for (int i = 0; i < arrayWhiteNameSheet.GetCount(); i++) {
  230. std::regex pattern(arrayWhiteNameSheet[i]);
  231. if (std::regex_match(sysInfo.strTerminalID.GetData(), pattern)) {
  232. bWhiteNameSheet = true;
  233. break;
  234. }
  235. }
  236. //if this terminal is not at WhiteNameSheet,set level with "OFF".
  237. if (!bWhiteNameSheet) {
  238. stLogConfig.strLevel = "OFF";
  239. }
  240. }
  241. }
  242. bool fromLocal = false;
  243. /*从集中配置读取的开关为关闭,尝试读取本地的设置*/
  244. if (stLogConfig.strLevel.Compare("OFF", true) == 0) {
  245. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Try to read from global settings.");
  246. CSmartPointer<IConfigInfo> spConfig;
  247. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Cache, spConfig);
  248. CAutoArray<CSimpleStringA> adapters;
  249. spConfig->ReadAllKeys("AdapterLogSwith", adapters);
  250. CSimpleStringA strKey(true);
  251. for (int i = 0; i < adapters.GetCount(); ++i) {
  252. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%d: %s, %s", i, adapters[i].GetData(), GetEntityName());
  253. if (adapters[i].Compare(GetEntityName(), true) == 0) {
  254. strKey = adapters[i];
  255. break;
  256. }
  257. }
  258. if (!strKey.IsNullOrEmpty()) {
  259. CSimpleStringA strValue(true);
  260. spConfig->ReadConfigValue("AdapterLogSwith", strKey, strValue);
  261. CAutoArray<CSimpleStringA> settings;
  262. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Value: %s", strValue.GetData());
  263. if (!strValue.IsNullOrEmpty() && (settings = strValue.Split(',')).GetCount() == 2) {
  264. if (settings[0].GetLength() == 1 && atoi(settings[0]) >= 1 && settings[1].GetLength() == 1) {
  265. if (settings[1].Compare("1") == 0) stLogConfig.strLevel = "Fatal";
  266. else if (settings[1].Compare("2") == 0) stLogConfig.strLevel = "Error";
  267. else if (settings[1].Compare("3") == 0) stLogConfig.strLevel = "Warn";
  268. else if (settings[1].Compare("4") == 0) stLogConfig.strLevel = "Info";
  269. else if (settings[1].Compare("5") == 0) stLogConfig.strLevel = "Trace";
  270. else stLogConfig.strLevel = "All";
  271. fromLocal = true;
  272. }
  273. }
  274. }
  275. }
  276. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  277. str.Clear();
  278. centerConfig->ReadConfigValue(GetEntityName(), "Type", str);
  279. if (!str.IsNullOrEmpty())
  280. stLogConfig.strType = str;
  281. GetFunction()->GetPath("Dbg", stLogConfig.strLogPath);
  282. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Dbg: %s", stLogConfig.strLogPath.GetData());
  283. stLogConfig.Settle();
  284. do {
  285. CEntityStaticInfo staticInfo;
  286. GetFunction()->GetEntityStaticInfo(GetEntityName(), staticInfo);
  287. DWORD dwUsrCode = fromLocal ? 0xFFFEE : 0xFFFEF;
  288. dwUsrCode |= (staticInfo.wEntityDevelopID << 20);
  289. LogWarn(Severity_Low, Error_Debug, dwUsrCode,
  290. CSimpleStringA::Format("{\"RecordLevel\":\"%s\", \"RecordType\":\"%s\", \"DeterminedBy\":\"%s\"}",
  291. stLogConfig.strLevel.GetData(),
  292. stLogConfig.strType.GetData(),
  293. fromLocal ? "LocalMaintain" : "CenterSettings"));
  294. } while (false);
  295. }
  296. }
  297. #define GET_DEV_ENTITY_BASE_POINTER() \
  298. (dynamic_cast<CDevAdptEntityBase*>(GetEntityBase()))
  299. #endif /*_RVC_DEVICE_ADAPTER_ENTITY_BASE_H__*/