GetDevInfoHelper.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #pragma once
  2. #include "spHelper.h"
  3. #include "SimpleString.h"
  4. #include "Blob.h"
  5. //#define DeviceService_Method_GetDevInfo 65535
  6. //#define DeviceService_MethodSignature_GetDevInfo 296205965
  7. //
  8. //struct DeviceService_GetDevInfo_Req
  9. //{
  10. // void Serialize(SpBuffer &Buf)
  11. // {
  12. // }
  13. //};
  14. //
  15. //struct DeviceService_GetDevInfo_Ans
  16. //{
  17. // CSimpleStringA type;
  18. // CSimpleStringA model;
  19. // CSimpleStringA vendor;
  20. // int state;
  21. // WORD wMajor; //release major version
  22. // WORD wMinor; //release minor version
  23. // WORD wRevision; //bug repair version with the major and minor version remains the same
  24. // WORD wBuild; //compile version
  25. //
  26. // void Serialize(SpBuffer &Buf)
  27. // {
  28. // Buf & type & model & vendor & state & wMajor & wMinor & wRevision & wBuild;
  29. // }
  30. //};
  31. //
  32. //class DeviceService_ClientBase : public CClientSessionBase {
  33. //public:
  34. // DeviceService_ClientBase(CEntityBase *pEntity) : m_pEntityBase(pEntity), m_bSysManaged(false) {}
  35. //
  36. //protected:
  37. // virtual ~DeviceService_ClientBase() {}
  38. //
  39. //public:
  40. //
  41. // ErrorCodeEnum Connect(CSmartPointer<IAsynWaitSp> &spAsyncWait, const CSimpleStringA &strRemoteEntity)
  42. // {
  43. // CSmartPointer<IEntityFunction> pFunc = m_pEntityBase->GetFunction();
  44. //
  45. // CSimpleStringA strServiceName = strRemoteEntity + "Service";
  46. // ErrorCodeEnum Error = pFunc->ConnectRemoteEntity(this, (const char*)strRemoteEntity, (const char*)strServiceName, spAsyncWait);
  47. // if (Error == Error_Succeed) {
  48. // m_bSysManaged = true;
  49. // }
  50. // return Error;
  51. // }
  52. //
  53. // ErrorCodeEnum Connect(const CSimpleStringA &strRemoteEntity)
  54. // {
  55. // CSmartPointer<IAsynWaitSp> spAsyncWait;
  56. // ErrorCodeEnum Error = Connect(spAsyncWait, strRemoteEntity);
  57. // if (Error == Error_Succeed) {
  58. // Error = spAsyncWait->WaitAnswer();
  59. // }
  60. // return Error;
  61. // }
  62. //
  63. // ErrorCodeEnum GetDevInfo(DeviceService_GetDevInfo_Req &Req, CSmartPointer<IAsynWaitSp> &spAsyncWait, DWORD dwTimeout)
  64. // {
  65. // CSmartPointer<IClientSessionFunction> pFunc = GetFunction();
  66. // CAutoBuffer Buf = SpObject2Buffer(Req);
  67. // return pFunc->AsyncRequest(DeviceService_Method_GetDevInfo, DeviceService_MethodSignature_GetDevInfo, Buf, spAsyncWait, dwTimeout);
  68. // }
  69. //
  70. // ErrorCodeEnum GetDevInfo(DeviceService_GetDevInfo_Req &Req, DeviceService_GetDevInfo_Ans &Ans, DWORD dwTimeout)
  71. // {
  72. // CSmartPointer<IAsynWaitSp> spAsyncWait;
  73. // ErrorCodeEnum Error = GetDevInfo(Req, spAsyncWait, dwTimeout);
  74. // if (Error == Error_Succeed) {
  75. // bool bEnd = false;
  76. // Error = SpWaitAnswerObject(spAsyncWait, Ans, bEnd);
  77. // LOG_ASSERT(Error || bEnd);
  78. // }
  79. // return Error;
  80. // }
  81. //
  82. // bool SafeDelete()
  83. // {
  84. // if (!m_bSysManaged) {
  85. // delete this;
  86. // }
  87. // return m_bSysManaged;
  88. // }
  89. //
  90. //protected:
  91. // bool m_bSysManaged;
  92. // CEntityBase *m_pEntityBase;
  93. //};
  94. //
  95. //ErrorCodeEnum SpGetDeviceInfo(CEntityBase *pEntity, const CSimpleStringA &devDevEntityName,
  96. // CSimpleStringA &strType, CSimpleStringA &strModel, CSimpleStringA &strVendor, int &nState, CBlob &blobVersion)
  97. //{
  98. // DeviceService_ClientBase *pClient = new DeviceService_ClientBase(pEntity);
  99. // ErrorCodeEnum rc = pClient->Connect(devDevEntityName);
  100. // if (rc == Error_Succeed)
  101. // {
  102. // DeviceService_GetDevInfo_Req req;
  103. // DeviceService_GetDevInfo_Ans ans;
  104. // rc = pClient->GetDevInfo(req, ans, 5000);
  105. // if (rc == Error_Succeed)
  106. // {
  107. // strType = ans.type;
  108. // strModel = ans.model;
  109. // strVendor = ans.vendor;
  110. // nState = ans.state;
  111. // blobVersion.Alloc(8);
  112. // ((BYTE*)blobVersion.m_pData)[0] = (ans.wMajor >> 8) & 0xFF;
  113. // ((BYTE*)blobVersion.m_pData)[1] = ans.wMajor & 0xFF;
  114. // ((BYTE*)blobVersion.m_pData)[2] = (ans.wMinor >> 8) & 0xFF;
  115. // ((BYTE*)blobVersion.m_pData)[3] = ans.wMinor & 0xFF;
  116. // ((BYTE*)blobVersion.m_pData)[4] = (ans.wRevision >> 8) & 0xFF;
  117. // ((BYTE*)blobVersion.m_pData)[5] = ans.wRevision & 0xFF;
  118. // ((BYTE*)blobVersion.m_pData)[6] = (ans.wBuild >> 8) & 0xFF;
  119. // ((BYTE*)blobVersion.m_pData)[7] = ans.wBuild & 0xFF;
  120. // }
  121. // pClient->GetFunction()->CloseSession();
  122. // }
  123. // pClient->SafeDelete();
  124. // return rc;
  125. //}
  126. ErrorCodeEnum SpGetAllDevices(CEntityBase *pEntity, CAutoArray<CSimpleStringA> &devs)
  127. {
  128. CSmartPointer<IConfigInfo> pConfig;
  129. ErrorCodeEnum rc = pEntity->GetFunction()->OpenConfig(Config_Root, pConfig);
  130. if (rc == Error_Succeed)
  131. {
  132. int nCount(0);
  133. rc = pConfig->ReadConfigValueInt("Device", "Number", nCount);
  134. if (rc == Error_Succeed && nCount>0)
  135. {
  136. devs.Init(nCount);
  137. for(int i=0; i<nCount; i++)
  138. {
  139. CSimpleStringA str = CSimpleStringA::Format("%d", i+1);
  140. rc = pConfig->ReadConfigValue("Device", (const char*)str, devs[i]);
  141. }
  142. }
  143. }
  144. return rc;
  145. }
  146. ErrorCodeEnum SpGetDeviceInfo(CEntityBase *pCallerEntity, const CSimpleStringA &devDeviceName,
  147. CSimpleStringA &strModel, CSimpleStringA &strVendor, CSimpleStringA &strVersion)
  148. {
  149. CSmartPointer<IConfigInfo> pConfig;
  150. ErrorCodeEnum rc = pCallerEntity->GetFunction()->OpenConfig(Config_Root, pConfig);
  151. if (rc == Error_Succeed)
  152. {
  153. CSimpleStringA strSection = CSimpleStringA("Device.") + devDeviceName;
  154. pConfig->ReadConfigValue(strSection, "Vendor", strVendor);
  155. pConfig->ReadConfigValue(strSection, "Version", strVersion);
  156. strModel = devDeviceName;
  157. if (!strVendor.IsNullOrEmpty())
  158. {
  159. strModel += ".";
  160. strModel += strVendor;
  161. }
  162. if (!strVersion.IsNullOrEmpty())
  163. {
  164. strModel += ".";
  165. strModel += strVersion;
  166. }
  167. }
  168. return rc;
  169. }
  170. ErrorCodeEnum SpGetDevAdaptorPath(CEntityBase *pCallerEntity,
  171. const CSimpleStringA &devDeviceName,
  172. CSimpleStringA &strDevAdaptorPath)
  173. {
  174. CSmartPointer<IConfigInfo> pConfig;
  175. ErrorCodeEnum rc = pCallerEntity->GetFunction()->OpenConfig(Config_Root, pConfig);
  176. if (rc == Error_Succeed)
  177. {
  178. CSimpleStringA strSection = CSimpleStringA("Device.") + devDeviceName;
  179. strDevAdaptorPath = devDeviceName;
  180. CSimpleStringA str;
  181. pConfig->ReadConfigValue(strSection, "Vendor", str);
  182. if (!str.IsNullOrEmpty())
  183. {
  184. strDevAdaptorPath += ".";
  185. strDevAdaptorPath += str;
  186. }
  187. str.Clear();
  188. pConfig->ReadConfigValue(strSection, "Version", str);
  189. if (!str.IsNullOrEmpty())
  190. {
  191. strDevAdaptorPath += ".";
  192. strDevAdaptorPath += str;
  193. }
  194. str.Clear();
  195. pConfig->ReadConfigValue(strSection, "Batch", str);
  196. if (!str.IsNullOrEmpty())
  197. {
  198. strDevAdaptorPath += ".";
  199. strDevAdaptorPath += str;
  200. }
  201. CSimpleStringA strDepPath;
  202. pCallerEntity->GetFunction()->GetPath("Dep", strDepPath);
  203. strDevAdaptorPath = CSimpleStringA::Format("%s\\%s.dll",
  204. (const char*)strDepPath,
  205. (const char*)strDevAdaptorPath);
  206. }
  207. return rc;
  208. }
  209. // add by ly
  210. ErrorCodeEnum SpGetDevAdaptorPathByIndex(int nIndex,
  211. CEntityBase *pCallerEntity,
  212. const CSimpleStringA &devDeviceName,
  213. CSimpleStringA &strDevAdaptorPath)
  214. {
  215. CSmartPointer<IConfigInfo> pConfig;
  216. ErrorCodeEnum rc = pCallerEntity->GetFunction()->OpenConfig(Config_Root, pConfig);
  217. if (rc == Error_Succeed)
  218. {
  219. char tmp[64] = {0};
  220. CSimpleStringA strSection = CSimpleStringA("Device.") + devDeviceName;
  221. strDevAdaptorPath = devDeviceName;
  222. CSimpleStringA str;
  223. sprintf(tmp, "Vendor_%d", nIndex);
  224. pConfig->ReadConfigValue(strSection, tmp, str);
  225. if (!str.IsNullOrEmpty())
  226. {
  227. strDevAdaptorPath += ".";
  228. strDevAdaptorPath += str;
  229. }
  230. str.Clear();
  231. sprintf(tmp, "Version_%d", nIndex);
  232. pConfig->ReadConfigValue(strSection, tmp, str);
  233. if (!str.IsNullOrEmpty())
  234. {
  235. strDevAdaptorPath += ".";
  236. strDevAdaptorPath += str;
  237. }
  238. str.Clear();
  239. sprintf(tmp, "Batch_%d", nIndex);
  240. pConfig->ReadConfigValue(strSection, tmp, str);
  241. if (!str.IsNullOrEmpty())
  242. {
  243. strDevAdaptorPath += ".";
  244. strDevAdaptorPath += str;
  245. }
  246. CSimpleStringA strDepPath;
  247. pCallerEntity->GetFunction()->GetPath("Dep", strDepPath);
  248. strDevAdaptorPath = CSimpleStringA::Format("%s\\%s.dll",
  249. (const char*)strDepPath,
  250. (const char*)strDevAdaptorPath);
  251. }
  252. return rc;
  253. }