DevEntityCommBase.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. VendorLibInfo() :strDevice(true), strVendor(true), strVersion(true), strBatch(true) {}
  18. bool IsValid() const
  19. {
  20. return (!strDevice.IsNullOrEmpty()
  21. && !strVendor.IsNullOrEmpty()
  22. && !strVersion.IsNullOrEmpty()
  23. && !strBatch.IsNullOrEmpty());
  24. }
  25. bool IsNotConfig() const
  26. {
  27. return(strVendor.IsNullOrEmpty() && strVersion.IsNullOrEmpty() && strBatch.IsNullOrEmpty());
  28. }
  29. int GetVersionInt() const
  30. {
  31. int result = 0;
  32. if (!strVersion.IsNullOrEmpty())
  33. {
  34. result = atoi(strVersion.GetData());
  35. }
  36. return result;
  37. }
  38. int GetBatchInt() const
  39. {
  40. int result = 0;
  41. if (!strBatch.IsNullOrEmpty())
  42. {
  43. result = atoi(strBatch.GetData());
  44. }
  45. return result;
  46. }
  47. CSimpleStringA toLibNameString() const
  48. {
  49. CSimpleStringA strFullLibName(true);
  50. if (!strDevice.IsNullOrEmpty())
  51. {
  52. strFullLibName = strDevice;
  53. if (!strVendor.IsNullOrEmpty())
  54. {
  55. strFullLibName += ".";
  56. strFullLibName += strVendor;
  57. }
  58. if (!strVersion.IsNullOrEmpty())
  59. {
  60. strFullLibName += ".";
  61. strFullLibName += strVersion;
  62. }
  63. if (!strBatch.IsNullOrEmpty())
  64. {
  65. strFullLibName += ".";
  66. strFullLibName += strBatch;
  67. }
  68. #ifdef RVC_OS_WIN
  69. strFullLibName += ".dll";
  70. #else
  71. CSimpleStringA strPrefix("lib");
  72. strPrefix += strFullLibName;
  73. strPrefix += ".so";
  74. strFullLibName = strPrefix;
  75. #endif //RVC_OS_WIN
  76. }
  77. return strFullLibName;
  78. }
  79. };
  80. class CDevAdptEntityBase : public CEntityBase
  81. {
  82. public:
  83. CDevAdptEntityBase() { }
  84. virtual CSimpleStringA GetVendorLibName()
  85. {
  86. if (!vendorLibInfo.IsValid()) {
  87. ExtractVendorLibName();
  88. }
  89. return vendorLibInfo.toLibNameString();
  90. }
  91. virtual ErrorCodeEnum ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath);
  92. virtual void InitializeVendorLogSwitch();
  93. virtual ~CDevAdptEntityBase() {}
  94. public:
  95. VendorLibInfo vendorLibInfo;
  96. private:
  97. ErrorCodeEnum ExtractVendorLibName();
  98. bool IsNotConfigure() const
  99. {
  100. return vendorLibInfo.IsNotConfig();
  101. }
  102. };
  103. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibName()
  104. {
  105. CSmartPointer<IConfigInfo> spRootConfig;
  106. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Root, spRootConfig);
  107. if (IS_SUCCEED(erroCode))
  108. {
  109. CSimpleStringA strSection = CSimpleStringA("Device.") + GetEntityName();
  110. vendorLibInfo.strDevice = GetEntityName();
  111. spRootConfig->ReadConfigValue(strSection, "Vendor", vendorLibInfo.strVendor);
  112. spRootConfig->ReadConfigValue(strSection, "Version", vendorLibInfo.strVersion);
  113. spRootConfig->ReadConfigValue(strSection, "Batch", vendorLibInfo.strBatch);
  114. }
  115. return erroCode;
  116. }
  117. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath)
  118. {
  119. CSimpleStringA strLibName = GetVendorLibName();
  120. CSimpleStringA strDepPath;
  121. GetFunction()->GetPath("Dep", strDepPath);
  122. csLibFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)strLibName);
  123. return IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  124. }
  125. inline void CDevAdptEntityBase::InitializeVendorLogSwitch()
  126. {
  127. LOG_FUNCTION();
  128. if (!vendorLibInfo.IsValid()) {
  129. ExtractVendorLibName();
  130. }
  131. struct VendorLogConfig {
  132. CSimpleStringA strLevel = "OFF";
  133. CSimpleStringA strType = "FILE";
  134. CSimpleStringA strDllName = "";
  135. CSimpleStringA strLogPath = "";
  136. void Settle() {
  137. toolkit_setenv("VENDOR_RECODE_LEVEL", strLevel);
  138. toolkit_setenv("VENDOR_RECODE_TYPE", strType);
  139. toolkit_setenv("VENDOR_DLL_NAME", strDllName);
  140. toolkit_setenv("VENDOR_LOG_PATH", strLogPath);
  141. }
  142. } stLogConfig;
  143. stLogConfig.strDllName = vendorLibInfo.toLibNameString().GetData();
  144. CSmartPointer<IConfigInfo> centerConfig;
  145. GetFunction()->OpenConfig(Config_CenterSetting, centerConfig);
  146. CSimpleStringA str;
  147. centerConfig->ReadConfigValue(GetEntityName(), vendorLibInfo.strVendor, str);
  148. if (str.IsNullOrEmpty()) {
  149. Dbg("To get all config");
  150. centerConfig->ReadConfigValue(GetEntityName(), "All", str);
  151. }
  152. Dbg("All: %s", str.GetData());
  153. if (!str.IsNullOrEmpty()) {
  154. stLogConfig.strLevel = str;
  155. }
  156. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  157. CSystemStaticInfo sysInfo;
  158. GetFunction()->GetSystemStaticInfo(sysInfo);
  159. CSimpleStringA strWhiteNameSheet;
  160. centerConfig->ReadConfigValue(GetEntityName(), "AllowTerminals", strWhiteNameSheet);
  161. Dbg("AllowTerminals: %s", strWhiteNameSheet.GetData());
  162. if (!strWhiteNameSheet.IsNullOrEmpty()) {
  163. bool bWhiteNameSheet = false;
  164. CAutoArray<CSimpleStringA> arrayWhiteNameSheet = strWhiteNameSheet.Split(',');
  165. for (int i = 0; i < arrayWhiteNameSheet.GetCount(); i++) {
  166. std::regex pattern(arrayWhiteNameSheet[i]);
  167. if (std::regex_match(sysInfo.strTerminalID.GetData(), pattern)) {
  168. bWhiteNameSheet = true;
  169. break;
  170. }
  171. }
  172. //if this terminal is not at WhiteNameSheet,set level with "OFF".
  173. if (!bWhiteNameSheet) {
  174. stLogConfig.strLevel = "OFF";
  175. }
  176. }
  177. }
  178. bool fromLocal = false;
  179. if (stLogConfig.strLevel.Compare("OFF", true) == 0) {
  180. Dbg("Try to read from global settings.");
  181. CSmartPointer<IConfigInfo> spConfig;
  182. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Cache, spConfig);
  183. CAutoArray<CSimpleStringA> adapters;
  184. spConfig->ReadAllKeys("AdapterLogSwith", adapters);
  185. CSimpleStringA strKey(true);
  186. for (int i = 0; i < adapters.GetCount(); ++i) {
  187. Dbg("%d: %s, %s", i, adapters[i].GetData(), GetEntityName());
  188. if (adapters[i].Compare(GetEntityName(), true) == 0) {
  189. strKey = adapters[i];
  190. break;
  191. }
  192. }
  193. if (!strKey.IsNullOrEmpty()) {
  194. CSimpleStringA strValue(true);
  195. spConfig->ReadConfigValue("AdapterLogSwith", strKey, strValue);
  196. CAutoArray<CSimpleStringA> settings;
  197. Dbg("Value: %s", strValue.GetData());
  198. if (!strValue.IsNullOrEmpty() && (settings = strValue.Split(',')).GetCount() == 2) {
  199. if (settings[0].GetLength() == 1 && atoi(settings[0]) >= 1 && settings[1].GetLength() == 1) {
  200. if (settings[1].Compare("1") == 0) stLogConfig.strLevel = "Fatal";
  201. else if (settings[1].Compare("2") == 0) stLogConfig.strLevel = "Error";
  202. else if (settings[1].Compare("3") == 0) stLogConfig.strLevel = "Warn";
  203. else if (settings[1].Compare("4") == 0) stLogConfig.strLevel = "Info";
  204. else if (settings[1].Compare("5") == 0) stLogConfig.strLevel = "Trace";
  205. else stLogConfig.strLevel = "All";
  206. fromLocal = true;
  207. }
  208. }
  209. }
  210. }
  211. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  212. str.Clear();
  213. centerConfig->ReadConfigValue(GetEntityName(), "Type", str);
  214. if (!str.IsNullOrEmpty())
  215. stLogConfig.strType = str;
  216. GetFunction()->GetPath("Dbg", stLogConfig.strLogPath);
  217. Dbg("Dbg: %s", stLogConfig.strLogPath.GetData());
  218. stLogConfig.Settle();
  219. do {
  220. CEntityStaticInfo staticInfo;
  221. GetFunction()->GetEntityStaticInfo(GetEntityName(), staticInfo);
  222. DWORD dwUsrCode = fromLocal ? 0xFFFEE : 0xFFFEF;
  223. dwUsrCode |= (staticInfo.wEntityDevelopID << 20);
  224. LogWarn(Severity_Middle, Error_Debug, dwUsrCode,
  225. CSimpleStringA::Format("{\"RecordLevel\":\"%s\", \"RecordType\":\"%s\", \"DeterminedBy\":\"%s\"}",
  226. stLogConfig.strLevel.GetData(),
  227. stLogConfig.strType.GetData(),
  228. fromLocal ? "LocalMaintain" : "CenterSettings"));
  229. } while (false);
  230. }
  231. }
  232. #define GET_DEV_ENTITY_BASE_POINTER() \
  233. (dynamic_cast<CDevAdptEntityBase*>(GetEntityBase()))
  234. #endif /*_RVC_DEVICE_ADAPTER_ENTITY_BASE_H__*/