IDCertFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #ifndef IDCERTFSM_H
  2. #define IDCERTFSM_H
  3. #pragma once
  4. #ifdef RVC_OS_LINUX
  5. #include "SpFSM.h"
  6. #include "SpTest.h"
  7. #include "DevFSMCommBase.hpp"
  8. #else //windows
  9. #include "SpFSM.h"
  10. #include "json/json.h"
  11. #include <xstring>
  12. #endif
  13. #include <unordered_map>
  14. #ifdef RVC_OS_WIN
  15. #include "DevFSMCommBase.hpp"
  16. #include "IHttpFunc.h"
  17. #define REFLECTION(var) #var
  18. typedef struct ImgCheckReq : CHTTPReq {
  19. string terminalNo;
  20. string name;
  21. string idNo;
  22. string nationality;
  23. string idType;
  24. string imageBase64;
  25. string fspId;
  26. int topN;
  27. int returnImgBase64;
  28. string ToJson() {
  29. Json::Value value;
  30. value[REFLECTION(terminalNo)] = terminalNo;
  31. value[REFLECTION(name)] = name;
  32. value[REFLECTION(idNo)] = idNo;
  33. value[REFLECTION(nationality)] = nationality;
  34. value[REFLECTION(idType)] = idType;
  35. value[REFLECTION(imageBase64)] = imageBase64;
  36. value[REFLECTION(fspId)] = fspId;
  37. value[REFLECTION(topN)] = topN;
  38. value[REFLECTION(returnImgBase64)] = returnImgBase64;
  39. Json::FastWriter writer;
  40. string strData = writer.write(value);
  41. return strData;
  42. }
  43. } ImgCheckReq;
  44. typedef struct ImgCheckRet : CHTTPRet {
  45. bool m_success;
  46. bool Parse(string strData) {
  47. Json::Value root;
  48. Json::Reader reader;
  49. reader.parse(strData, root, false);
  50. if (root["success"].isBool()) {
  51. m_success = root["success"].asBool();
  52. }
  53. else
  54. {
  55. m_success = false;
  56. }
  57. return true;
  58. }
  59. } ImgCheckRet;
  60. #endif
  61. enum EvtType
  62. {
  63. USER_EVT_TEST = EVT_USER + 1,
  64. USER_EVT_QUIT,
  65. USER_EVT_CANCEL_READ,
  66. USER_EVT_EXIT,
  67. USER_EVT_GET_DEVINFO,
  68. USER_EVT_ERROR,
  69. USER_EVT_EJECT,
  70. USER_EVT_WAIT_FETCH_IDCARD_FINISHED,
  71. USER_EVT_READ_AND_SCAN,
  72. USER_EVT_READ_AND_SCAN_FINISHED,
  73. USER_EVT_INIT_FINISHED,
  74. USER_EVT_TODO_INIT,
  75. USER_EVT_TODO_INIT_FINISHED,
  76. USER_EVT_READ_AND_SCAN_UTF8, //ex1
  77. USER_EVT_READ_AND_SCAN_UTF8_FINISHED,
  78. };
  79. enum BmpType
  80. {
  81. Bmp_ZP = 1,
  82. Bmp_SCAN = 2,
  83. };
  84. #include "IDCerClass.h"
  85. #include "IDCertificate_def_g.h"
  86. #pragma region forsonar
  87. typedef IDCertificate::IDCertService_CancelRead_Info IDCert_CancelRead_Info;
  88. typedef IDCertificate::IDCertService_ReadWaitMore_Info IDCert_ReadWaitMore_Info;
  89. typedef IDCertificate::IDCertService_Exit_Info IDCert_Exit_Info;
  90. typedef IDCertificate::IDCertService_Eject_Req IDCert_Eject_Req;
  91. typedef IDCertificate::IDCertService_Eject_Ans IDCert_Eject_Ans;
  92. typedef IDCertificate::IDCertService_ReadAndScan_Req IDCert_ReadAndScan_Req;
  93. typedef IDCertificate::IDCertService_ReadAndScan_Ans IDCert_ReadAndScan_Ans;
  94. typedef IDCertificate::IDCertService_ReadAndScanUTF8_Req IDCert_ReadAndScanUTF8_Req; //ex2
  95. typedef IDCertificate::IDCertService_ReadAndScanUTF8_Ans IDCert_ReadAndScanUTF8_Ans;
  96. typedef IDCertificate::IDCertService_GetDevInfo_Req IDCert_GetDevInfo_Req;
  97. typedef IDCertificate::IDCertService_GetDevInfo_Ans IDCert_GetDevInfo_Ans;
  98. #pragma endregion forsonar
  99. #include <map>
  100. using namespace std;
  101. typedef ErrorCodeEnum ( *lpCreateDevCom)(DeviceBaseClass *&baseObj);
  102. typedef ErrorCodeEnum ( *lpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
  103. struct CtxInfo
  104. {
  105. int dataSize;
  106. LPVOID pCtx;
  107. };
  108. class CancelReadEvent : public FSMEvent
  109. {
  110. public:
  111. CancelReadEvent() : FSMEvent(USER_EVT_CANCEL_READ){}
  112. virtual ~CancelReadEvent(){}
  113. virtual void OnUnhandled()
  114. {
  115. LOG_TRACE("idcer cancel read not handled");
  116. }
  117. };
  118. class GetDevInfoEvent : public FSMEvent
  119. {
  120. public:
  121. GetDevInfoEvent() : FSMEvent(USER_EVT_GET_DEVINFO){}
  122. virtual ~GetDevInfoEvent(){}
  123. SpReqAnsContext<IDCert_GetDevInfo_Req, IDCert_GetDevInfo_Ans>::Pointer ctx;
  124. virtual void OnUnhandled()
  125. {
  126. if (ctx != NULL)
  127. ctx->Answer(Error_InvalidState);
  128. }
  129. };
  130. class EjectEvent : public FSMEvent
  131. {
  132. public:
  133. EjectEvent() : FSMEvent(USER_EVT_EJECT){}
  134. virtual ~EjectEvent(){}
  135. SpReqAnsContext<IDCert_Eject_Req, IDCert_Eject_Ans>::Pointer ctx;
  136. virtual void OnUnhandled()
  137. {
  138. if (ctx != NULL)
  139. ctx->Answer(Error_InvalidState);
  140. }
  141. };
  142. class ReadAndScanEvent : public FSMEvent
  143. {
  144. public:
  145. ReadAndScanEvent() : FSMEvent(USER_EVT_READ_AND_SCAN) {}
  146. virtual ~ReadAndScanEvent() {}
  147. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx;
  148. virtual void OnUnhandled()
  149. {
  150. if (ctx != NULL)
  151. ctx->Answer(Error_InvalidState);
  152. }
  153. };
  154. class ReadAndScanUTF8Event : public FSMEvent //ex3
  155. {
  156. public:
  157. ReadAndScanUTF8Event() : FSMEvent(USER_EVT_READ_AND_SCAN_UTF8) {}
  158. virtual ~ReadAndScanUTF8Event() {}
  159. SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx;
  160. virtual void OnUnhandled()
  161. {
  162. if (ctx != NULL)
  163. ctx->Answer(Error_InvalidState);
  164. }
  165. };
  166. class CIDCertFSM : public CCommDevFSM<CIDCertFSM, IDCerClass>
  167. {
  168. public:
  169. enum {s0,s1,s2,s3,s4,s5};
  170. BEGIN_FSM_STATE(CIDCertFSM)
  171. FSM_STATE_ENTRY(s0,"Normal",s0_on_entry,s0_on_exit,s0_on_event)
  172. FSM_STATE_ENTRY(s1,"Reading",s1_on_entry,s1_on_exit,s1_on_event)
  173. FSM_STATE_ENTRY(s2,"Fail",s2_on_entry,s2_on_exit,s2_on_event)
  174. FSM_STATE_ENTRY(s3, "Eject", s3_on_entry, s3_on_exit, s3_on_event)
  175. FSM_STATE_ENTRY(s4, "WaitingFetch", s4_on_entry, s4_on_exit, s4_on_event)
  176. FSM_STATE_ENTRY(s5, "Init", s5_on_entry, s5_on_exit, s5_on_event)
  177. END_FSM_STATE()
  178. BEGIN_FSM_RULE(CIDCertFSM, s5)
  179. FSM_RULE_ENTRY(s0, s2, USER_EVT_ERROR, 0)
  180. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  181. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ_AND_SCAN, 0)
  182. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ_AND_SCAN_UTF8, 0) //ex4
  183. FSM_RULE_ENTRY(s0, s5, USER_EVT_TODO_INIT_FINISHED, 0)
  184. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_FINISHED, 0)
  185. FSM_RULE_ENTRY(s1, s2, USER_EVT_READ_AND_SCAN_FINISHED, 1)
  186. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_FINISHED, 2)
  187. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_FINISHED, 3)
  188. FSM_RULE_ENTRY(s1, s4, USER_EVT_READ_AND_SCAN_FINISHED, 4)
  189. FSM_RULE_ENTRY(s1, s5, USER_EVT_READ_AND_SCAN_FINISHED, 5)
  190. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 0)
  191. FSM_RULE_ENTRY(s1, s2, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 1)
  192. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 2)
  193. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 3)
  194. FSM_RULE_ENTRY(s1, s4, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 4)
  195. FSM_RULE_ENTRY(s1, s5, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 5)
  196. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  197. FSM_RULE_ENTRY(s1, s0, USER_EVT_CANCEL_READ, 2)
  198. FSM_RULE_ENTRY(s1, s0, USER_EVT_EXIT, 3)
  199. FSM_RULE_ENTRY(s2, s5, USER_EVT_TODO_INIT_FINISHED, 0)
  200. FSM_RULE_ENTRY(s4, s0, USER_EVT_WAIT_FETCH_IDCARD_FINISHED, 0)
  201. FSM_RULE_ENTRY(s5, s0, USER_EVT_INIT_FINISHED, 0)
  202. FSM_RULE_ENTRY(s5, s2, USER_EVT_INIT_FINISHED, 2)
  203. END_FSM_RULE()
  204. #ifdef RVC_OS_LINUX
  205. CIDCertFSM() :m_devInit(false), m_bCancelRead(false), m_bReading(false), m_bWaitReadMore(false),
  206. m_bExit(false), m_testResult(Error_Succeed), m_getDevCategory(Error_Unexpect),m_csMachineType(""), m_csSite(""), m_terminalNo(""), m_devSN("")
  207. , m_devVendor(""), m_devVer(""), m_devBatch(""){
  208. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x201);
  209. m_FirstStart = TRUE;
  210. ZeroMemory(&m_adapterInfo, sizeof(m_adapterInfo));
  211. };
  212. #else
  213. CIDCertFSM():m_bCancelRead(false),m_bReading(false),m_bWaitReadMore(false),
  214. m_bExit(false),m_testResult(Error_Succeed), m_getDevCategory(Error_Unexpect), m_csMachineType(""), m_csSite(""), m_terminalNo("")
  215. ,m_devVendor(""), m_devVer(""), m_devBatch(""), invalidBreak(false), m_logBefore(false), transImgMsg("")
  216. {
  217. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x201);
  218. };
  219. #endif // RVC_OS_LINUX
  220. virtual ~CIDCertFSM() {};
  221. virtual ErrorCodeEnum OnInit();
  222. virtual ErrorCodeEnum OnExit();
  223. virtual void s0_on_entry();
  224. virtual void s0_on_exit();
  225. virtual unsigned int s0_on_event(FSMEvent* e);
  226. virtual void s1_on_entry();
  227. virtual void s1_on_exit();
  228. virtual unsigned int s1_on_event(FSMEvent* e);
  229. virtual void s2_on_entry();
  230. virtual void s2_on_exit();
  231. virtual unsigned int s2_on_event(FSMEvent* e);
  232. virtual void s3_on_entry();
  233. virtual void s3_on_exit();
  234. virtual unsigned int s3_on_event(FSMEvent* e);
  235. virtual void s4_on_entry();
  236. virtual void s4_on_exit();
  237. virtual unsigned int s4_on_event(FSMEvent* e);
  238. virtual void s5_on_entry();
  239. virtual void s5_on_exit();
  240. virtual unsigned int s5_on_event(FSMEvent* e);
  241. int ReadAndScan(SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx);
  242. int ReadAndScanUTF8(SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx);
  243. int WaitFetchIDCard();
  244. void SetReadMore(){m_bWaitReadMore = true;}
  245. void SetExitFlag(){m_bExit = true;}
  246. bool GetReadFlag(){return m_bReading;}
  247. ErrorCodeEnum GetDevCatInfo(DevCategoryInfo &devInfo, CSimpleStringA& devType);
  248. void GetDevState(DevStateEnum &devState){ devState = m_devState; }
  249. void DoExit(CSmartPointer<ITransactionContext> pTransactionContext)
  250. {
  251. pTransactionContext->SendAnswer(Error_Succeed);
  252. }
  253. #ifdef RVC_OS_WIN
  254. bool GetDevInitFlag() { return m_bOpened; }
  255. void ResetDevInitFlag() { m_bOpened = false; }
  256. #else
  257. bool GetDevInitFlag() { return m_devInit; }
  258. #endif
  259. void SaveCtxAA(int methodID, SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx)
  260. {
  261. m_readAndScanCtx = ctx;
  262. }
  263. void SaveCtxAAEx(int methodID, SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx)//UTF8
  264. {
  265. m_readAndScanUTF8Ctx = ctx;
  266. }
  267. int Initial();
  268. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  269. static void HttpsLogCallBack(const char* logtxt);
  270. private:
  271. ErrorCodeEnum GetVendorDllPath(CSimpleStringA &strPath);
  272. //ErrorCodeEnum GetPngBlob(CBlob &data,bool bClear=false);
  273. ErrorCodeEnum GetPngBlobEx(CBlob &data, CSimpleStringA fileNamePrefix,bool bClear=false);
  274. //type:1,delete img about zp; type:2,delete img about scan ID;type:3,delete both zp and scan ID
  275. void DeleteZP(int type);
  276. ErrorCodeEnum CheckDate(const char* date);
  277. ErrorCodeEnum CheckDate(const char* startDate, const char* endDate);
  278. void LogDate(IDCerInfo idInfo);
  279. void LogDateEx(IDCerInfoEx idInfo);
  280. //Delete bmp file in dep directory, you should just convey fileName only without paths -Joseph
  281. ErrorCodeEnum DeleteFileIfExisted(LPCTSTR fileName);
  282. #ifdef RVC_OS_LINUX
  283. bool GetAttachedTerminal(CSimpleStringA& csTerm);
  284. #else
  285. void ToLogWarnInfoAboutTermCustom();
  286. #endif // RVC_OS_LINUX
  287. BOOL UCS2_to_UTF8(UINT16* ucs2_code, UINT8* utf8_code);
  288. BOOL GetSexUTF8String(UINT16* in, UINT8* out);
  289. BOOL GetNationalUTF8String(UINT16* in, UINT8* out);
  290. BOOL GetDateStandardFormatUTF8(UINT16* in, UINT8* out);
  291. BOOL RemoveUCS2Blank(UINT16* ucs2_code);
  292. int GetUCS2ByteLength(UINT16* ucs2_code);
  293. void CheckHanZi(UINT16* ucs2_code);
  294. private:
  295. #ifdef RVC_OS_LINUX
  296. bool m_devInit;
  297. #endif
  298. bool m_bCancelRead, m_bReading, m_bWaitReadMore, m_bExit;
  299. ErrorCodeEnum m_testResult;
  300. ErrorCodeEnum m_getDevCategory;
  301. DevCategoryInfo m_devCatInfo;
  302. DevStateEnum m_devState;
  303. CSimpleStringA m_csMachineType, m_csSite, m_terminalNo;
  304. CSimpleStringA m_devVendor, m_devVer, m_devBatch, m_devSN/*fwb SN*/;
  305. CSimpleStringA transImgMsg;
  306. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer m_readAndScanCtx;
  307. SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer m_readAndScanUTF8Ctx; //ex7
  308. #ifdef RVC_OS_WIN
  309. unordered_map<int, wstring> nationalCode;
  310. #else
  311. unordered_map<int, string> nationalCode;
  312. #endif
  313. BOOL supportUCS2; //支持生僻字
  314. BOOL igestionVer; //吸入式设备
  315. BOOL supportNewForeigner; //支持新版外国人永居证 2023.11.10
  316. CSimpleStringA bkPicPath; //合成背景的图片路径
  317. BOOL closeImgCheck;
  318. CSimpleStringA m_csAlarmMsg;
  319. bool invalidBreak;
  320. bool m_logBefore;
  321. ULLINT m_ullBeginTime, m_ullEndTime;
  322. };
  323. struct ReadAndScanTask : public ITaskSp
  324. {
  325. CIDCertFSM* fsm;
  326. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx;
  327. ReadAndScanTask(CIDCertFSM* f) : fsm(f) {}
  328. void Process()
  329. {
  330. FSMEvent *e = new FSMEvent(USER_EVT_READ_AND_SCAN_FINISHED);
  331. e->param1 = -1;
  332. e->param1 = fsm->ReadAndScan(ctx);
  333. fsm->PostEventFIFO(e);
  334. }
  335. };
  336. struct ReadAndScanUTF8Task : public ITaskSp //ex8
  337. {
  338. CIDCertFSM* fsm;
  339. SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx;
  340. ReadAndScanUTF8Task(CIDCertFSM* f) : fsm(f) {}
  341. void Process()
  342. {
  343. FSMEvent* e = new FSMEvent(USER_EVT_READ_AND_SCAN_UTF8_FINISHED);
  344. e->param1 = -1;
  345. e->param1 = fsm->ReadAndScanUTF8(ctx);
  346. fsm->PostEventFIFO(e);
  347. }
  348. };
  349. struct WaitFetchIDCardTask : public ITaskSp
  350. {
  351. CIDCertFSM* fsm;
  352. WaitFetchIDCardTask(CIDCertFSM* f) : fsm(f) {}
  353. void Process()
  354. {
  355. FSMEvent *e = new FSMEvent(USER_EVT_WAIT_FETCH_IDCARD_FINISHED);
  356. e->param1 = fsm->WaitFetchIDCard();
  357. fsm->PostEventFIFO(e);
  358. }
  359. };
  360. struct InitTask : public ITaskSp
  361. {
  362. CIDCertFSM* fsm;
  363. InitTask(CIDCertFSM* f) : fsm(f) {}
  364. void Process()
  365. {
  366. FSMEvent* e = new FSMEvent(USER_EVT_INIT_FINISHED);
  367. e->param1 = fsm->Initial();
  368. fsm->PostEventFIFO(e);
  369. }
  370. };
  371. #endif //IDCERTFSM_H