IDCertFSM.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_INIT_FINISHED,
  70. USER_EVT_TODO_INIT,
  71. USER_EVT_TODO_INIT_FINISHED,
  72. USER_EVT_READ_AND_SCAN_UTF8, //ex1
  73. USER_EVT_READ_AND_SCAN_UTF8_FINISHED,
  74. USER_EVT_READ_AND_SCAN_UTF8JS,
  75. USER_EVT_READ_AND_SCAN_UTF8JS_FINISHED
  76. };
  77. enum BmpType
  78. {
  79. Bmp_ZP = 1,
  80. Bmp_SCAN = 2,
  81. };
  82. #include "IDCerClass.h"
  83. #include "IDCertificate_def_g.h"
  84. #include "IDCertificate_LogCode.h"
  85. #pragma region forsonar
  86. typedef IDCertificate::IDCertService_CancelRead_Info IDCert_CancelRead_Info;
  87. typedef IDCertificate::IDCertService_ReadAndScanUTF8_Req IDCert_ReadAndScanUTF8_Req; //ex2
  88. typedef IDCertificate::IDCertService_ReadAndScanUTF8_Ans IDCert_ReadAndScanUTF8_Ans;
  89. typedef IDCertificate::IDCertService_ReadAndScanUTF8JS_Req IDCert_ReadAndScanUTF8JS_Req; //js ReadAndScanUTF8
  90. typedef IDCertificate::IDCertService_ReadAndScanUTF8JS_Ans IDCert_ReadAndScanUTF8JS_Ans;
  91. typedef IDCertificate::IDCertService_CancelReadJS_Req IDCert_CancelReadJS_Req; //js CancelRead
  92. typedef IDCertificate::IDCertService_CancelReadJS_Ans IDCert_CancelReadJS_Ans;
  93. typedef IDCertificate::IDCertService_GetDevInfo_Req IDCert_GetDevInfo_Req;
  94. typedef IDCertificate::IDCertService_GetDevInfo_Ans IDCert_GetDevInfo_Ans;
  95. #pragma endregion forsonar
  96. #include <map>
  97. using namespace std;
  98. struct CtxInfo
  99. {
  100. int dataSize;
  101. LPVOID pCtx;
  102. };
  103. struct IDCerTextData
  104. {
  105. unsigned char name[1024];
  106. unsigned char sex[1024];
  107. unsigned char nation[1024];
  108. unsigned char birthday[1024];
  109. unsigned char address[1024];
  110. unsigned char idno[1024];
  111. unsigned char department[1024];
  112. unsigned char startDate[1024];
  113. unsigned char endDate[1024];
  114. unsigned char englishName[1024];
  115. unsigned char nationality[1024];
  116. unsigned char idVersion[1024];
  117. unsigned char idType[1024];
  118. unsigned char reserved[1024];
  119. unsigned char englishNameEx[1024]; //英文名备用字段
  120. unsigned char IssuedSN[1024]; //换证次数
  121. };
  122. class CancelReadEvent : public FSMEvent
  123. {
  124. public:
  125. CancelReadEvent() : FSMEvent(USER_EVT_CANCEL_READ){}
  126. virtual ~CancelReadEvent(){}
  127. virtual void OnUnhandled()
  128. {
  129. LOG_TRACE("idcer cancel read not handled");
  130. }
  131. };
  132. class GetDevInfoEvent : public FSMEvent
  133. {
  134. public:
  135. GetDevInfoEvent() : FSMEvent(USER_EVT_GET_DEVINFO){}
  136. virtual ~GetDevInfoEvent(){}
  137. SpReqAnsContext<IDCert_GetDevInfo_Req, IDCert_GetDevInfo_Ans>::Pointer ctx;
  138. virtual void OnUnhandled()
  139. {
  140. if (ctx != NULL)
  141. ctx->Answer(Error_InvalidState);
  142. }
  143. };
  144. class ReadAndScanUTF8Event : public FSMEvent //ex3
  145. {
  146. public:
  147. ReadAndScanUTF8Event() : FSMEvent(USER_EVT_READ_AND_SCAN_UTF8) {}
  148. virtual ~ReadAndScanUTF8Event() {}
  149. SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx;
  150. virtual void OnUnhandled()
  151. {
  152. if (ctx != NULL)
  153. ctx->Answer(Error_InvalidState);
  154. }
  155. };
  156. class ReadAndScanUTF8JSEvent : public FSMEvent //ex3
  157. {
  158. public:
  159. ReadAndScanUTF8JSEvent() : FSMEvent(USER_EVT_READ_AND_SCAN_UTF8JS) {}
  160. virtual ~ReadAndScanUTF8JSEvent() {}
  161. SpReqAnsContext<IDCert_ReadAndScanUTF8JS_Req, IDCert_ReadAndScanUTF8JS_Ans>::Pointer ctx;
  162. virtual void OnUnhandled()
  163. {
  164. if (ctx != NULL)
  165. ctx->Answer(Error_InvalidState);
  166. }
  167. };
  168. class CIDCertFSM : public CCommDevFSM<CIDCertFSM, IDCerClass>
  169. {
  170. public:
  171. enum {s0,s1,s2,s3};
  172. BEGIN_FSM_STATE(CIDCertFSM)
  173. FSM_STATE_ENTRY(s0,"Normal",s0_on_entry,s0_on_exit,s0_on_event)
  174. FSM_STATE_ENTRY(s1,"Reading",s1_on_entry,s1_on_exit,s1_on_event)
  175. FSM_STATE_ENTRY(s2,"Fail",s2_on_entry,s2_on_exit,s2_on_event)
  176. FSM_STATE_ENTRY(s3, "Init", s3_on_entry, s3_on_exit, s3_on_event)
  177. END_FSM_STATE()
  178. BEGIN_FSM_RULE(CIDCertFSM, s3)
  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_UTF8, 0) //ex4
  182. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ_AND_SCAN_UTF8JS, 0) //ex4
  183. FSM_RULE_ENTRY(s0, s3, USER_EVT_TODO_INIT_FINISHED, 0)
  184. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 0)
  185. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 1)
  186. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 2)
  187. FSM_RULE_ENTRY(s1, s3, USER_EVT_READ_AND_SCAN_UTF8_FINISHED, 3)
  188. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8JS_FINISHED, 0)
  189. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_UTF8JS_FINISHED, 1)
  190. FSM_RULE_ENTRY(s1, s3, USER_EVT_READ_AND_SCAN_UTF8JS_FINISHED, 3)
  191. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  192. FSM_RULE_ENTRY(s1, s0, USER_EVT_CANCEL_READ, 2)
  193. FSM_RULE_ENTRY(s1, s0, USER_EVT_EXIT, 3)
  194. FSM_RULE_ENTRY(s2, s3, USER_EVT_TODO_INIT_FINISHED, 0)
  195. FSM_RULE_ENTRY(s3, s0, USER_EVT_INIT_FINISHED, 0)
  196. FSM_RULE_ENTRY(s3, s2, USER_EVT_INIT_FINISHED, 2)
  197. END_FSM_RULE()
  198. CIDCertFSM() :m_bCancelRead(false), m_bReading(false),
  199. m_bExit(false), m_testResult(Error_Succeed), m_getDevCategory(Error_Unexpect), m_csMachineType(""), m_terminalNo(""), transImgMsg("")
  200. {
  201. ZeroMemory(&m_adapterInfo, sizeof(m_adapterInfo));
  202. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x201);
  203. };
  204. virtual ~CIDCertFSM() {};
  205. virtual ErrorCodeEnum OnInit();
  206. virtual ErrorCodeEnum OnExit();
  207. virtual void s0_on_entry();
  208. virtual void s0_on_exit();
  209. virtual unsigned int s0_on_event(FSMEvent* e);
  210. virtual void s1_on_entry();
  211. virtual void s1_on_exit();
  212. virtual unsigned int s1_on_event(FSMEvent* e);
  213. virtual void s2_on_entry();
  214. virtual void s2_on_exit();
  215. virtual unsigned int s2_on_event(FSMEvent* e);
  216. virtual void s3_on_entry();
  217. virtual void s3_on_exit();
  218. virtual unsigned int s3_on_event(FSMEvent* e);
  219. int ReadAndScanUTF8(SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx);
  220. int ReadAndScanUTF8JS(SpReqAnsContext<IDCert_ReadAndScanUTF8JS_Req, IDCert_ReadAndScanUTF8JS_Ans>::Pointer ctx);
  221. void SetExitFlag(){m_bExit = true;}
  222. bool GetReadFlag(){return m_bReading;}
  223. ErrorCodeEnum GetDevCatInfo(DevCategoryInfo &devInfo, CSimpleStringA& devType);
  224. void DoExit(CSmartPointer<ITransactionContext> pTransactionContext)
  225. {
  226. pTransactionContext->SendAnswer(Error_Succeed);
  227. }
  228. int Initial();
  229. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  230. static void HttpsLogCallBack(const char* logtxt);
  231. private:
  232. ErrorCodeEnum GetVendorDllPath(CSimpleStringA &strPath);
  233. //ErrorCodeEnum GetPngBlob(CBlob &data,bool bClear=false);
  234. ErrorCodeEnum GetPngBlobEx(CBlob &data, CSimpleStringA fileNamePrefix);
  235. //type:1,delete img about zp; type:2,delete img about scan ID;type:3,delete both zp and scan ID
  236. //deleteTiming:0, defalut; deleteTiming:1, delete before Read IDCard; deleteTiming:2, delete after Read IDCard
  237. void DeleteZP(int type, int deleteTiming = 0);
  238. //Delete bmp file in dep directory, you should just convey fileName only without paths -Joseph
  239. //deleteTiming:0, defalut; deleteTiming:1, delete before Read IDCard; deleteTiming:2, delete after Read IDCard
  240. ErrorCodeEnum DeleteFileIfExisted(LPCTSTR fileName, int deleteTiming = 0);
  241. void UCS2_to_UTF8(UINT16* ucs2_code, UINT8* utf8_code);
  242. void GetSexUTF8String(UINT16* in, UINT8* out);
  243. void GetNationalUTF8String(UINT16* in, UINT8* out);
  244. void GetDateStandardFormatUTF8(UINT16* in, UINT8* out);
  245. void RemoveUCS2Blank(UINT16* ucs2_code);
  246. int GetUCS2ByteLength(UINT16* ucs2_code);
  247. void CheckHanZi(UINT16* ucs2_code);
  248. CSimpleStringA GetFileHashStr(CSimpleStringA filePath);
  249. CSimpleStringA GetFileLastModifyTime(CSimpleStringA filePath);
  250. template <typename T>
  251. void CopyIDCerDataToCtx(IDCerInfoEx2 idInfoEx2, T& ctx); //兼容JS接口
  252. BOOL GetScanImg(IDCerInfoEx2 idInfoEx2, CBlob& frontImg, CBlob& backImg);
  253. #ifdef RVC_OS_WIN
  254. char* GetGBKString(UINT16* ucs2_code); //WIN打印源数据日志或其他中文用途
  255. #endif
  256. //记录身份证相关图片的最近修改时间,用于上送信息调研,后续下掉 - 2025.6.13 CJL
  257. void WarnImgCreateTime();
  258. public:
  259. std::string checkImgURL;
  260. private:
  261. bool m_bCancelRead, m_bReading, m_bExit;
  262. ErrorCodeEnum m_testResult;
  263. ErrorCodeEnum m_getDevCategory;
  264. DevCategoryInfo m_devCatInfo;
  265. CSimpleStringA m_csMachineType, m_terminalNo;
  266. CSimpleStringA transImgMsg;
  267. #ifdef RVC_OS_WIN
  268. unordered_map<int, wstring> nationalCode;
  269. #else
  270. unordered_map<int, string> nationalCode;
  271. #endif
  272. BOOL supportUCS2; //支持生僻字
  273. BOOL igestionVer; //吸入式设备
  274. BOOL supportNewForeigner; //支持新版外国人永居证 2023.11.10
  275. BOOL closeImgCheck;
  276. ULLINT m_ullBeginTime, m_ullEndTime;
  277. ULLINT OpenRFControlTime;
  278. ULLINT CloseRFControlTime;
  279. ULLINT IDCerAuthenticateTime;
  280. ULLINT IDCerGetDataEx2Time;
  281. ULLINT ScanIDAndSaveImageTime;
  282. //记录身份证相关图片的最近修改时间,用于上送信息调研,后续下掉 - 2025.6.13 CJL
  283. CSimpleStringA headPhotoTimeStr, frontPhotoTimeStr, backPhotoTimeStr;
  284. time_t headPhotoTime, frontPhotoTime, backPhotoTime, currentSysTime;
  285. };
  286. struct ReadAndScanUTF8Task : public ITaskSp //ex8
  287. {
  288. CIDCertFSM* fsm;
  289. SpReqAnsContext<IDCert_ReadAndScanUTF8_Req, IDCert_ReadAndScanUTF8_Ans>::Pointer ctx;
  290. ReadAndScanUTF8Task(CIDCertFSM* f) : fsm(f) {}
  291. void Process()
  292. {
  293. FSMEvent* e = new FSMEvent(USER_EVT_READ_AND_SCAN_UTF8_FINISHED);
  294. e->param1 = -1;
  295. e->param1 = fsm->ReadAndScanUTF8(ctx);
  296. fsm->PostEventFIFO(e);
  297. }
  298. };
  299. struct ReadAndScanUTF8JSTask : public ITaskSp //ex8
  300. {
  301. CIDCertFSM* fsm;
  302. SpReqAnsContext<IDCert_ReadAndScanUTF8JS_Req, IDCert_ReadAndScanUTF8JS_Ans>::Pointer ctx;
  303. ReadAndScanUTF8JSTask(CIDCertFSM* f) : fsm(f) {}
  304. void Process()
  305. {
  306. FSMEvent* e = new FSMEvent(USER_EVT_READ_AND_SCAN_UTF8JS_FINISHED);
  307. e->param1 = -1;
  308. e->param1 = fsm->ReadAndScanUTF8JS(ctx);
  309. fsm->PostEventFIFO(e);
  310. }
  311. };
  312. struct InitTask : public ITaskSp
  313. {
  314. CIDCertFSM* fsm;
  315. InitTask(CIDCertFSM* f) : fsm(f) {}
  316. void Process()
  317. {
  318. FSMEvent* e = new FSMEvent(USER_EVT_INIT_FINISHED);
  319. e->param1 = fsm->Initial();
  320. fsm->PostEventFIFO(e);
  321. }
  322. };
  323. #endif //IDCERTFSM_H