mod_ScannerSet.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #ifndef _MOD_HSPSCANNER_H_
  2. #define _MOD_HSPSCANNER_H_
  3. #pragma once
  4. #include "modVer.h"
  5. #if defined(RVC_OS_LINUX)
  6. #include "SpTest.h"
  7. #endif //RVC_OS_LINUX
  8. #include "ScannerSetFSM.h"
  9. #include "EventCode.h"
  10. #include "ScannerSet_UserErrorCode.h"
  11. #include "HSPScanner_client_g.h"
  12. using namespace HSPScanner;
  13. using namespace ScannerSet;
  14. class CScannerEntity;
  15. class ScannerServerSession: public ScannerService_ServerSessionBase
  16. {
  17. public:
  18. ScannerServerSession(CScannerEntity *pEntity):m_pEntity(pEntity){}
  19. virtual ~ScannerServerSession(){}
  20. virtual void Handle_StartPreview(SpReqAnsContext<ScannerService_StartPreview_Req,
  21. ScannerService_StartPreview_Ans>::Pointer ctx);
  22. virtual void Handle_CancelPreview(SpReqAnsContext<ScannerService_CancelPreview_Req,
  23. ScannerService_CancelPreview_Ans>::Pointer ctx);
  24. virtual void Handle_ScanImage(SpReqAnsContext<ScannerService_ScanImage_Req,
  25. ScannerService_ScanImage_Ans>::Pointer ctx);
  26. virtual void Handle_SetWindowPos(SpReqAnsContext<ScannerService_SetWindowPos_Req,
  27. ScannerService_SetWindowPos_Ans>::Pointer ctx);
  28. virtual void Handle_SetProperty(SpReqAnsContext<ScannerService_SetProperty_Req,
  29. ScannerService_SetProperty_Ans>::Pointer ctx);
  30. virtual void Handle_ShowProperty(SpReqAnsContext<ScannerService_ShowProperty_Req,
  31. ScannerService_ShowProperty_Ans>::Pointer ctx);
  32. virtual void Handle_GetDevStatus(SpReqAnsContext<ScannerService_GetDevStatus_Req,
  33. ScannerService_GetDevStatus_Ans>::Pointer ctx);
  34. virtual void Handle_CrossTermCall(SpReqAnsContext<ScannerService_CrossTermCall_Req,
  35. ScannerService_CrossTermCall_Ans>::Pointer ctx);
  36. virtual void Handle_CrossTermInvokeInfo(SpOnewayCallContext<ScannerService_CrossTermInvokeInfo_Info>::Pointer ctx);
  37. virtual void Handle_GetDevInfo(SpReqAnsContext<ScannerService_GetDevInfo_Req,
  38. ScannerService_GetDevInfo_Ans>::Pointer ctx);
  39. virtual void Handle_GetOnlineCameras(SpReqAnsContext<ScannerService_GetOnlineCameras_Req, ScannerService_GetOnlineCameras_Ans>::Pointer ctx);
  40. virtual void Handle_Exit(SpOnewayCallContext<ScannerService_Exit_Info>::Pointer ctx);
  41. private:
  42. CScannerEntity *m_pEntity;
  43. };
  44. class HSPScannerClient : public HSPScannerService_ClientBase
  45. {
  46. public:
  47. HSPScannerClient(CScannerEntity *pEntity);
  48. };
  49. class PortableScannerClient : public PortableScannerService_ClientBase
  50. {
  51. public:
  52. PortableScannerClient(CScannerEntity *pEntity);
  53. };
  54. class CScannerEntity : public CEntityBase, public ILogListener, public IEntityStateListener
  55. {
  56. public:
  57. CScannerEntity() {}
  58. virtual ~CScannerEntity()
  59. {
  60. if(GetFunction()->HasPrivilege()) {
  61. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  62. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  63. pFuncPrivilege->UnregistEntityStateEvent(m_fsm.GetChildEntityName());
  64. }
  65. }
  66. virtual const char *GetEntityName() const { return "ScannerSet"; }
  67. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  68. #if defined(RVC_OS_LINUX)
  69. ON_ENTITYT_TEST()
  70. #endif //RVC_OS_LINUX
  71. virtual CServerSessionBase* OnNewSession(const char* , const char * )
  72. {
  73. return new ScannerServerSession(this);
  74. }
  75. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  76. CSmartPointer<ITransactionContext> pTransactionContext)
  77. {
  78. ErrorCodeEnum erroCode = m_fsm.Init(this);
  79. #if defined(RVC_OS_WIN)
  80. GetFunction()->SubscribeLog(m_uuidPortableScanner, this,
  81. Log_Error, Severity_High, Error_IgnoreAll, -1, "PortableScanner");
  82. #endif //RVC_OS_WIN
  83. pTransactionContext->SendAnswer(erroCode);
  84. }
  85. void OnStarted()
  86. {
  87. if(GetFunction()->HasPrivilege()) {
  88. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  89. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  90. pFuncPrivilege->RegistEntityStateEvent(m_fsm.GetChildEntityName(), this);
  91. } else {
  92. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cur entity has no priviledge!");
  93. }
  94. m_fsm.AfterInit();
  95. }
  96. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  97. {
  98. LOG_FUNCTION();
  99. #ifdef TWINKLE_LOCAL_DEBUG
  100. const UINT result = UniTest(this);
  101. if(result != 0) {
  102. LogError(Severity_High, Error_Unexpect, result, CSimpleStringA::Format("line: %u", result));
  103. }
  104. #endif
  105. SpReqAnsContext<ScannerService_StartPreview_Req,
  106. ScannerService_StartPreview_Ans>::Pointer ctx =
  107. new SpReqAnsContext<ScannerService_StartPreview_Req,
  108. ScannerService_StartPreview_Ans>(pTransactionContext);
  109. Preview(ctx);
  110. pTransactionContext->SendAnswer(Error_Succeed);
  111. }
  112. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  113. {
  114. Exit(NULL);
  115. pTransactionContext->SendAnswer(Error_Succeed);
  116. }
  117. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,
  118. CSmartPointer<ITransactionContext> pTransactionContext)
  119. {
  120. ErrorCodeEnum erroCode = m_fsm.OnExit();
  121. pTransactionContext->SendAnswer(erroCode);
  122. }
  123. void Preview(SpReqAnsContext<ScannerService_StartPreview_Req,
  124. ScannerService_StartPreview_Ans>::Pointer ctx)
  125. {
  126. StartPreviewEvent* pEvent = new StartPreviewEvent();
  127. pEvent->m_ctx = ctx;
  128. m_fsm.PostEventFIFO(pEvent);
  129. }
  130. void CancelPreview(SpReqAnsContext<ScannerService_CancelPreview_Req,
  131. ScannerService_CancelPreview_Ans>::Pointer ctx)
  132. {
  133. StopPreviewEvent* pEvent = new StopPreviewEvent();
  134. pEvent->m_ctx = ctx;
  135. m_fsm.PostEventFIFO(pEvent);
  136. }
  137. void ScanImage(SpReqAnsContext<ScannerService_ScanImage_Req,
  138. ScannerService_ScanImage_Ans>::Pointer ctx)
  139. {
  140. ScanImageEvent* pEvent = new ScanImageEvent();
  141. pEvent->m_ctx = ctx;
  142. m_fsm.PostEventFIFO(pEvent);
  143. }
  144. void Exit(SpOnewayCallContext<ScannerService_Exit_Info>::Pointer ctx)
  145. {
  146. ErrorCodeEnum erroCode = m_fsm.OnDevExit();
  147. }
  148. void GetDevInfo(SpReqAnsContext<ScannerService_GetDevInfo_Req,
  149. ScannerService_GetDevInfo_Ans>::Pointer ctx)
  150. {
  151. m_fsm.GetDevInfo(ctx);
  152. return;
  153. }
  154. void GetOnlineCameras(SpReqAnsContext<ScannerService_GetOnlineCameras_Req,
  155. ScannerService_GetOnlineCameras_Ans>::Pointer ctx)
  156. {
  157. m_fsm.GetOnlineDevices(ctx);
  158. }
  159. void SetWinPos(SpReqAnsContext<ScannerService_SetWindowPos_Req,
  160. ScannerService_SetWindowPos_Ans>::Pointer ctx)
  161. {
  162. static ScannerService_SetWindowPos_Req lastReq = {-1, -1, -1, -1, -1};
  163. if((ctx->Req.type != lastReq.type
  164. || ctx->Req.pointX != lastReq.pointX
  165. || ctx->Req.pointY != lastReq.pointY
  166. || ctx->Req.nWidth != lastReq.nWidth
  167. /*|| ctx->Req.reserved4 != lastReq.reserved4*/)) {
  168. lastReq.type = ctx->Req.type;
  169. lastReq.pointX = ctx->Req.pointX;
  170. lastReq.pointY = ctx->Req.pointY;
  171. lastReq.nWidth = ctx->Req.nWidth;
  172. lastReq.reserved4 = ctx->Req.reserved4;
  173. PendingTask* pTask = new PendingTask(&m_fsm);
  174. pTask->SetContext(ctx);
  175. GetFunction()->PostThreadPoolTask(pTask);
  176. } else {
  177. LogWarn(Severity_Low, Error_Debug, LOG_WARN_DUPLICATE_SETWINPOS
  178. , CSimpleStringA::Format("same request with last-one: %d,%d,%d,%d,%d"
  179. , ctx->Req.type, ctx->Req.pointX, ctx->Req.pointY, ctx->Req.nWidth, ctx->Req.reserved4));
  180. ctx->Answer(Error_Succeed);
  181. }
  182. }
  183. void SetProperty(SpReqAnsContext<ScannerService_SetProperty_Req,
  184. ScannerService_SetProperty_Ans>::Pointer ctx)
  185. {
  186. WaitingFSMForIdle();
  187. SetPropertyEvent* pEvent = new SetPropertyEvent();
  188. pEvent->m_ctx = ctx;
  189. m_fsm.PostEventFIFO(pEvent);
  190. }
  191. void ShowProperty(SpReqAnsContext<ScannerService_ShowProperty_Req,
  192. ScannerService_ShowProperty_Ans>::Pointer ctx)
  193. {
  194. ShowPropertyEvent* pEvent = new ShowPropertyEvent();
  195. pEvent->m_ctx = ctx;
  196. m_fsm.PostEventFIFO(pEvent);
  197. }
  198. void GetDevStatus(SpReqAnsContext<ScannerService_GetDevStatus_Req,
  199. ScannerService_GetDevStatus_Ans>::Pointer ctx)
  200. {
  201. m_fsm.GetDevStatus(ctx);
  202. return;
  203. }
  204. void CrossTermCall(SpReqAnsContext<ScannerService_CrossTermCall_Req,
  205. ScannerService_CrossTermCall_Ans>::Pointer ctx)
  206. {
  207. ctx->Answer(Error_NotSupport);
  208. }
  209. void CrossTermInvokeInfo(SpOnewayCallContext<ScannerService_CrossTermInvokeInfo_Info>::Pointer ctx)
  210. {
  211. }
  212. void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  213. {
  214. m_fsm.SelfTest(eTestType, pTransactionContext);
  215. }
  216. virtual bool IsService() const { return true; }
  217. virtual bool IsMultiThread() const { return true; };
  218. virtual void OnLog(
  219. const CAutoArray<CUUID> &SubIDs,
  220. const CUUID nLogID,
  221. const LogTypeEnum eLogType,
  222. const SeverityLevelEnum eLevel,
  223. const DWORD dwSysError,
  224. const DWORD dwUserCode,
  225. const DWORD dwEntityInstanceID,
  226. const WORD wEntityDevelID,
  227. const CAutoArray<DWORD> &Param,
  228. const char *pszEntityName,
  229. const char *pszModuleName,
  230. const char *pszMessage, const linkContext &pLinkInfo)
  231. {
  232. switch (dwUserCode)
  233. {
  234. case LOG_ERROR_PORTABLESCANNER_IN_PREVIEW:
  235. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("LOG_ERROR_PORTABLESCANNER_IN_PREVIEW: %s", pszMessage);
  236. {
  237. m_fsm.PSEntityPostCameraError(pszMessage);
  238. }
  239. break;
  240. }
  241. }
  242. void OnEntityStateHook(const char *pszEntityName,const char *pszTriggerEntity,
  243. EntityStateEnum eState,EntityStateEnum eLastState)
  244. {
  245. m_fsm.OnChildEntityStateChange(eLastState, eState);
  246. }
  247. void OnUserStateHook(const char *pszEntityName,DWORD dwState,DWORD dwLastState)
  248. {
  249. }
  250. void OnCeateConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  251. {
  252. }
  253. void OnCloseConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  254. {
  255. }
  256. void WaitingFSMForIdle()
  257. {
  258. static const int waitTimes = 30;
  259. static const int waitEachTimeout = 300;
  260. int waitCurrTimes = 0;
  261. while(m_fsm.IsWorking() && (++waitCurrTimes <= waitTimes)) {
  262. Sleep(waitEachTimeout);
  263. }
  264. if(waitCurrTimes > waitTimes) {
  265. LogWarn(Severity_Low, Error_TimeOut, 0, "Waiting FSM to Idle timeout!");
  266. }
  267. }
  268. private:
  269. CScannerFSM m_fsm;
  270. CUUID m_uuidPortableScanner;
  271. };
  272. HSPScannerClient::HSPScannerClient(CScannerEntity *pEntity)
  273. :HSPScannerService_ClientBase(pEntity)
  274. {
  275. }
  276. #endif