PortableScannerFSM.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. #include "StdAfx.h"
  2. #include "PortableScannerFSM.h"
  3. #include "DocScannerCap.h"
  4. #include "EventCode.h"
  5. #include "UserEventCode.h"
  6. #define RESET_TIMER_ID 1
  7. #define CHECK_CONNECT_TIMER_ID 2
  8. const int MILLISECOND_TO_RESET = 30 * 1000;
  9. const int MILLISECOND_TO_CHECK_CONNECT = 30 * 1000;
  10. const int MAX_PATH_SIZE = 256;
  11. const int MAX_IMAGEDATA_BUFSIZE = 1 * 1024*1024;
  12. const int MAX_IMAGEDATA_SUFFIX = 1024;
  13. const int MAX_ERROR_TIMES = 2;
  14. const int MAX_PREVIEW_ERROR_TIMES = 3;
  15. float m_roiRate[MAX_RIOMODE] = { -1.0f, -1.0f, -1.0f, -1.0f };
  16. static unsigned int s_err_preview_times = 0;
  17. #define TEMP_WIDTH 1280
  18. #define MOD_VERSION "1.1.0.0"
  19. #define MACRO_EVTTOSTRRING(x) \
  20. case x : return #x; \
  21. break;
  22. const char* EvtTypeToString(int nEvtType)
  23. {
  24. switch(nEvtType)
  25. {
  26. MACRO_EVTTOSTRRING(USER_EVT_STARTPREVIEW)
  27. MACRO_EVTTOSTRRING(USER_EVT_STARTPREVIEW_FINISHED)
  28. MACRO_EVTTOSTRRING(USER_EVT_STOPPREVIEW)
  29. MACRO_EVTTOSTRRING(USER_EVT_STOPPREVIEW_FINISHED)
  30. MACRO_EVTTOSTRRING(USER_EVT_SETWINPOS)
  31. MACRO_EVTTOSTRRING(USER_EVT_SETWINPOSFINISHED)
  32. MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGE)
  33. MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGE_EX)
  34. MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGEFINISHED)
  35. MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTY)
  36. MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTY_FINISHED)
  37. MACRO_EVTTOSTRRING(USER_EVT_GETSTATUS)
  38. MACRO_EVTTOSTRRING(USER_EVT_GETINFO)
  39. MACRO_EVTTOSTRRING(USER_EVT_DISCONNECT)
  40. MACRO_EVTTOSTRRING(USER_EVT_EXIT)
  41. MACRO_EVTTOSTRRING(USER_EVT_QUIT)
  42. MACRO_EVTTOSTRRING(USER_EVT_RESET)
  43. MACRO_EVTTOSTRRING(USER_EVT_SHOWPROPERTY)
  44. MACRO_EVTTOSTRRING(USER_EVT_SHOWPROPERTY_FINISHED)
  45. MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTYINVIEW_FINISHED)
  46. MACRO_EVTTOSTRRING(USER_EVT_EXIT_FINISHED)
  47. MACRO_EVTTOSTRRING(USER_EVT_NO_CONTAINS_DEVICE)
  48. MACRO_EVTTOSTRRING(USER_EVT_HIDEPREVIEW)
  49. MACRO_EVTTOSTRRING(USER_EVT_BIND_DEVICE)
  50. MACRO_EVTTOSTRRING(USER_EVT_BIND_DEVICE_FINISHED)
  51. MACRO_EVTTOSTRRING(USER_EVT_DROP_OFF_DEVICE)
  52. MACRO_EVTTOSTRRING(USER_EVT_ERROR_IN_PREVIEW)
  53. default:
  54. return "Unknown EventType";
  55. break;
  56. }
  57. return "Unknown EventType";
  58. }
  59. CPortableScannerFSM::CPortableScannerFSM(void)
  60. :
  61. #ifndef SHIELD_APDATER_INTERFACE
  62. m_version(0),
  63. m_batch(0),
  64. m_hVendorDll(NULL),
  65. CreateDevComponent(NULL),
  66. ReleaseDevComponent(NULL),
  67. m_pHSPS(NULL),
  68. #endif
  69. m_ecSelfTest(Error_Succeed),
  70. m_dwErroCode(0),
  71. m_bOperating(FALSE),
  72. m_nTickTimes(0),
  73. m_nFatalTimes(0),
  74. m_nSrcState(s0),
  75. m_cameraName(""),
  76. m_csDllName(""),
  77. m_eDevStatus(DEVICE_STATUS_NOT_READY),
  78. m_desiredAction(USER_EVT_QUIT),
  79. m_pCapture(NULL),
  80. m_capCurrentType(CAMTYPE_UNKNOWN),
  81. m_dwEntDevelID(0x21C)
  82. {
  83. }
  84. CPortableScannerFSM::~CPortableScannerFSM(void)
  85. {
  86. LOG_FUNCTION();
  87. #ifndef SHIELD_APDATER_INTERFACE
  88. SAFE_FREE_LIBRARY(m_hVendorDll);
  89. #endif
  90. #if 1
  91. if(m_pCapture) {
  92. delete m_pCapture;
  93. m_pCapture = NULL;
  94. }
  95. #else
  96. Dbg("don't delete capture object!");
  97. #endif
  98. }
  99. ErrorCodeEnum CPortableScannerFSM::OnInit()
  100. {
  101. LOG_FUNCTION();
  102. ErrorCodeEnum erroCode = Error_Succeed;
  103. Dbg("Version: %s, Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
  104. LOG_ASSERT(m_pCapture == NULL);
  105. m_pCapture = new CDocScannerCap(this);
  106. if(m_pCapture == NULL) {
  107. erroCode = Error_Resource;
  108. LogWarn(Severity_Middle, erroCode, 0, "Create CDocScannerCap object failed !");
  109. return erroCode;
  110. }
  111. RetrieveROIFromRunTime();
  112. m_eDevStatus = DEVICE_STATUS_NORMAL;
  113. return erroCode;
  114. }
  115. ErrorCodeEnum CPortableScannerFSM::OnExit()
  116. {
  117. return Error_Succeed;
  118. }
  119. void CPortableScannerFSM::s0_on_entry()
  120. {
  121. LOG_FUNCTION();
  122. m_bOperating = FALSE;
  123. m_nFatalTimes = 0;
  124. m_ecSelfTest = Error_Succeed;
  125. if(!m_pCapture->IsAttached()) {
  126. PostEventFIFO(new FSMEvent(USER_EVT_NO_CONTAINS_DEVICE));
  127. }
  128. }
  129. void CPortableScannerFSM::s0_on_exit()
  130. {
  131. m_bOperating = FALSE;
  132. }
  133. unsigned int CPortableScannerFSM::s0_on_event(FSMEvent* e)
  134. {
  135. Dbg("s0_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  136. unsigned int uRet = 0;
  137. switch(e->iEvt)
  138. {
  139. case USER_EVT_BIND_DEVICE:
  140. {
  141. BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
  142. if(!!pEvt->m_ctx->Req.force) {
  143. BindDeviceTask* pTask = new BindDeviceTask(this);
  144. pTask->SetContext(pEvt->m_ctx);
  145. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  146. e->SetHandled();
  147. }
  148. }
  149. break;
  150. case USER_EVT_BIND_DEVICE_FINISHED:
  151. {
  152. if(m_pCapture->IsAttached()) {
  153. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  154. uRet = 6;
  155. }else if(m_pCapture->IsPreview()) {
  156. uRet = 4;
  157. }else {
  158. uRet = 1;
  159. }
  160. }
  161. }
  162. e->SetHandled();
  163. break;
  164. case USER_EVT_DROP_OFF_DEVICE:
  165. {
  166. if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
  167. m_capCurrentType = CAMTYPE_UNKNOWN;
  168. uRet = 1;
  169. }
  170. e->SetHandled();
  171. }
  172. break;
  173. case USER_EVT_STARTPREVIEW:
  174. {
  175. StartPreviewTask* pTask = new StartPreviewTask(this);
  176. StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
  177. pTask->SetContext(pEvt->m_ctx);
  178. GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
  179. e->SetHandled();
  180. }
  181. break;
  182. case USER_EVT_STARTPREVIEW_FINISHED:
  183. {
  184. uRet = e->param1;
  185. e->SetHandled();
  186. if(uRet == 1) {
  187. m_desiredAction = USER_EVT_STARTPREVIEW;
  188. }
  189. }
  190. break;
  191. case USER_EVT_SETWINPOS:
  192. {
  193. SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
  194. if(pEvt->m_ctx->Req.type == 2)
  195. {//在s0状态,仅能用于设置位置和窗体大小
  196. SetWinPosTask* pTask = new SetWinPosTask(this);
  197. pTask->SetContext(pEvt->m_ctx);
  198. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  199. e->SetHandled();
  200. }
  201. }
  202. break;
  203. case USER_EVT_SETPROPERTY:
  204. {
  205. SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
  206. SetProperty(pEvt->m_ctx);
  207. e->SetHandled();
  208. }
  209. break;
  210. case USER_EVT_GETSTATUS:
  211. {
  212. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  213. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  214. pTask->SetContext(pEvt->m_ctx);
  215. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  216. }
  217. e->SetHandled();
  218. break;
  219. case USER_EVT_GETINFO:
  220. {
  221. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  222. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  223. pTask->SetContext(pEvt->m_ctx);
  224. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  225. }
  226. e->SetHandled();
  227. break;
  228. case USER_EVT_NO_CONTAINS_DEVICE:
  229. {
  230. }
  231. break;
  232. default:
  233. {
  234. Dbg("What a unhandled event! should i miss it?");
  235. }
  236. break;
  237. }
  238. return uRet;
  239. }
  240. void CPortableScannerFSM::s1_on_entry()
  241. {
  242. }
  243. void CPortableScannerFSM::s1_on_exit()
  244. {
  245. }
  246. unsigned int CPortableScannerFSM::s1_on_event(FSMEvent* e)
  247. {
  248. Dbg("s1_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  249. unsigned int uRet = 0;
  250. switch(e->iEvt)
  251. {
  252. case USER_EVT_BIND_DEVICE:
  253. {
  254. BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
  255. if(!!pEvt->m_ctx->Req.force) {
  256. BindDeviceTask* pTask = new BindDeviceTask(this);
  257. pTask->SetContext(pEvt->m_ctx);
  258. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  259. e->SetHandled();
  260. }
  261. }
  262. break;
  263. case USER_EVT_BIND_DEVICE_FINISHED:
  264. {
  265. if(m_pCapture->IsAttached()) {
  266. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  267. uRet = 6;
  268. }else if(m_pCapture->IsPreview()) {
  269. uRet = 4;
  270. }else {
  271. uRet = 1;
  272. }
  273. }
  274. }
  275. e->SetHandled();
  276. break;
  277. case USER_EVT_DROP_OFF_DEVICE:
  278. {
  279. if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
  280. m_capCurrentType = CAMTYPE_UNKNOWN;
  281. uRet = 1;
  282. }
  283. e->SetHandled();
  284. }
  285. break;
  286. case USER_EVT_STARTPREVIEW:
  287. {
  288. // Maybe Just SetProperty [Josephus in 15:44:43 2016/11/19]
  289. SetPropertyInPreviewTask* pTask = new SetPropertyInPreviewTask(this);
  290. StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
  291. pTask->SetContext(pEvt->m_ctx);
  292. GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
  293. e->SetHandled();
  294. }
  295. break;
  296. case USER_EVT_ERROR_IN_PREVIEW:
  297. {
  298. if(!!m_pCapture) {
  299. m_cameraName = m_pCapture->GetCurDevName().c_str();
  300. }
  301. FireLogEventMsg(PREVIEWING_OCCURS_EXCEPTION, CSimpleStringA::Format(
  302. "camera occurs exception in previewing %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  303. m_desiredAction = USER_EVT_STARTPREVIEW;
  304. }
  305. e->SetHandled();
  306. break;
  307. case USER_EVT_SETPROPERTYINVIEW_FINISHED:
  308. {
  309. }
  310. e->SetHandled();
  311. break;
  312. case USER_EVT_STOPPREVIEW:
  313. {
  314. StopPreviewTask* pTask = new StopPreviewTask(this);
  315. StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
  316. pTask->SetContext(pEvt->m_ctx);
  317. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  318. }
  319. e->SetHandled();
  320. break;
  321. case USER_EVT_STOPPREVIEW_FINISHED:
  322. {
  323. uRet = e->param1;
  324. if(uRet == 0) {
  325. m_desiredAction = USER_EVT_STOPPREVIEW;
  326. }
  327. else if(uRet == 4) {
  328. m_desiredAction = USER_EVT_HIDEPREVIEW;
  329. }
  330. }
  331. e->SetHandled();
  332. break;
  333. case USER_EVT_SCANIMAGE:
  334. {
  335. ScanImageTask* pTask = new ScanImageTask(this);
  336. ScanImageEvent* pEvt = dynamic_cast<ScanImageEvent*>(e);
  337. pTask->SetContext(pEvt->m_ctx);
  338. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  339. }
  340. e->SetHandled();
  341. break;
  342. case USER_EVT_SCANIMAGE_EX:
  343. {
  344. ScanImageExTask* pTask = new ScanImageExTask(this);
  345. ScanImageExEvent* pEvt = dynamic_cast<ScanImageExEvent*>(e);
  346. pTask->SetContext(pEvt->m_ctx);
  347. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  348. }
  349. e->SetHandled();
  350. break;
  351. case USER_EVT_SETPROPERTY:
  352. {
  353. SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
  354. SetProperty(pEvt->m_ctx);
  355. }
  356. e->SetHandled();
  357. break;
  358. case USER_EVT_SETWINPOS:
  359. {
  360. SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
  361. SetWinPosTask* pTask = new SetWinPosTask(this);
  362. pTask->SetContext(pEvt->m_ctx);
  363. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  364. }
  365. e->SetHandled();
  366. break;
  367. case USER_EVT_DISCONNECT:
  368. {
  369. }
  370. e->SetHandled();
  371. break;
  372. case USER_EVT_GETSTATUS:
  373. {
  374. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  375. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  376. pTask->SetContext(pEvt->m_ctx);
  377. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  378. }
  379. e->SetHandled();
  380. break;
  381. case USER_EVT_GETINFO:
  382. {
  383. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  384. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  385. pTask->SetContext(pEvt->m_ctx);
  386. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  387. }
  388. e->SetHandled();
  389. break;
  390. case USER_EVT_EXIT:
  391. {
  392. m_pCapture->TerminatePreview();
  393. }
  394. e->SetHandled();
  395. break;
  396. default:
  397. {
  398. }
  399. break;
  400. }
  401. return uRet;
  402. }
  403. void CPortableScannerFSM::s3_on_entry()
  404. {
  405. LOG_FUNCTION();
  406. s_err_preview_times = m_desiredAction == USER_EVT_STARTPREVIEW ? s_err_preview_times + 1 : 0;
  407. m_eDevStatus = DEVICE_STATUS_FAULT;
  408. m_nTickTimes = 0;
  409. m_bOperating = FALSE;
  410. if(s_err_preview_times >= MAX_PREVIEW_ERROR_TIMES) {
  411. LogError(Severity_Middle, Error_DevMedia, LOG_ERROR_PORTABLESCANNER_IN_PREVIEW, m_cameraName);
  412. }
  413. else {
  414. Dbg("error times: %d", s_err_preview_times);
  415. ScheduleTimer(RESET_TIMER_ID, MILLISECOND_TO_RESET / 20);
  416. }
  417. }
  418. void CPortableScannerFSM::s3_on_exit()
  419. {
  420. m_eDevStatus = DEVICE_STATUS_NORMAL;
  421. m_bOperating = FALSE;
  422. m_ecSelfTest = Error_Succeed;
  423. m_nFatalTimes = 0;
  424. m_desiredAction = USER_EVT_QUIT;
  425. CancelTimer(RESET_TIMER_ID);
  426. }
  427. unsigned int CPortableScannerFSM::s3_on_event(FSMEvent* e)
  428. {
  429. Dbg("s3_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  430. unsigned int uRet = 0;
  431. switch(e->iEvt)
  432. {
  433. case USER_EVT_BIND_DEVICE:
  434. {
  435. BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
  436. if(!!pEvt->m_ctx->Req.force) {
  437. BindDeviceTask* pTask = new BindDeviceTask(this);
  438. pTask->SetContext(pEvt->m_ctx);
  439. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  440. e->SetHandled();
  441. }
  442. }
  443. break;
  444. case USER_EVT_BIND_DEVICE_FINISHED:
  445. {
  446. if(m_pCapture->IsAttached()) {
  447. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  448. uRet = 6;
  449. }else if(m_pCapture->IsPreview()) {
  450. uRet = 4;
  451. }else {
  452. uRet = 1;
  453. }
  454. }
  455. }
  456. e->SetHandled();
  457. break;
  458. case USER_EVT_STARTPREVIEW:
  459. {
  460. StartPreviewTask* pTask = new StartPreviewTask(this);
  461. StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
  462. pTask->SetContext(pEvt->m_ctx);
  463. GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
  464. e->SetHandled();
  465. }
  466. break;
  467. case USER_EVT_STARTPREVIEW_FINISHED:
  468. {
  469. uRet = e->param1;
  470. e->SetHandled();
  471. }
  472. break;
  473. case USER_EVT_STOPPREVIEW:
  474. {
  475. CancelTimer(RESET_TIMER_ID);
  476. StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
  477. StopPreviewTask* pTask = new StopPreviewTask(this);
  478. pTask->SetContext(pEvt->m_ctx);
  479. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  480. }
  481. e->SetHandled();
  482. break;
  483. case USER_EVT_STOPPREVIEW_FINISHED:
  484. {
  485. uRet = e->param1;
  486. if(uRet == 0) {
  487. m_desiredAction = USER_EVT_STOPPREVIEW;
  488. }
  489. else if(uRet == 4) {
  490. m_desiredAction = USER_EVT_HIDEPREVIEW;
  491. }
  492. FSMEvent* timerEvent = new FSMEvent(EVT_TIMER);
  493. timerEvent->param1 = RESET_TIMER_ID;
  494. PostEventFIFO(timerEvent);
  495. }
  496. e->SetHandled();
  497. break;
  498. case USER_EVT_RESET:
  499. {
  500. if(m_pCapture->ResetCamera()) {
  501. if(m_pCapture->IsAttached()) {
  502. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  503. uRet = 6;
  504. } else if(m_pCapture->IsPreview()) {
  505. uRet = 4;
  506. } else {
  507. uRet = 1;
  508. }
  509. } else {
  510. uRet = 0;
  511. }
  512. } else {
  513. ScheduleTimer(RESET_TIMER_ID, MILLISECOND_TO_RESET / 20);
  514. uRet = 2;
  515. }
  516. }
  517. e->SetHandled();
  518. break;
  519. case EVT_TIMER:
  520. {
  521. if(e->param1 == RESET_TIMER_ID)
  522. {
  523. FSMEvent* resetEvent = new FSMEvent(USER_EVT_RESET);
  524. resetEvent->param1 = m_desiredAction;
  525. PostEventFIFO(resetEvent);
  526. }
  527. }
  528. e->SetHandled();
  529. break;
  530. case USER_EVT_DISCONNECT:
  531. {
  532. }
  533. e->SetHandled();
  534. break;
  535. case USER_EVT_GETSTATUS:
  536. {
  537. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  538. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  539. pTask->SetContext(pEvt->m_ctx);
  540. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  541. }
  542. e->SetHandled();
  543. break;
  544. case USER_EVT_GETINFO:
  545. {
  546. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  547. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  548. pTask->SetContext(pEvt->m_ctx);
  549. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  550. }
  551. e->SetHandled();
  552. break;
  553. case USER_EVT_DROP_OFF_DEVICE:
  554. {
  555. if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
  556. m_capCurrentType = CAMTYPE_UNKNOWN;
  557. uRet = 1;
  558. }
  559. e->SetHandled();
  560. }
  561. break;
  562. }
  563. return uRet;
  564. }
  565. void CPortableScannerFSM::s4_on_entry()
  566. {
  567. LOG_FUNCTION();
  568. }
  569. void CPortableScannerFSM::s4_on_exit()
  570. {
  571. }
  572. unsigned int CPortableScannerFSM::s4_on_event(FSMEvent* e)
  573. {
  574. Dbg("s4_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  575. unsigned int uRet = 0;
  576. switch(e->iEvt)
  577. {
  578. case USER_EVT_SCANIMAGEFINISHED:
  579. {
  580. uRet = e->param1;
  581. if(uRet == 2 || uRet == 4)
  582. {
  583. m_desiredAction = USER_EVT_SCANIMAGE;
  584. }
  585. }
  586. e->SetHandled();
  587. break;
  588. case USER_EVT_DISCONNECT:
  589. {
  590. }
  591. e->SetHandled();
  592. break;
  593. case USER_EVT_GETSTATUS:
  594. {
  595. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  596. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  597. pTask->SetContext(pEvt->m_ctx);
  598. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  599. }
  600. e->SetHandled();
  601. break;
  602. case USER_EVT_GETINFO:
  603. {
  604. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  605. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  606. pTask->SetContext(pEvt->m_ctx);
  607. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  608. }
  609. e->SetHandled();
  610. break;
  611. }
  612. return uRet;
  613. }
  614. void CPortableScannerFSM::s5_on_entry()
  615. {
  616. LOG_FUNCTION();
  617. LogEvent(Severity_Middle, LOG_EVT_PORTABLESCANNER_NO_AVAILABLE, "Not hold camera for now!");
  618. ScheduleTimer(CHECK_CONNECT_TIMER_ID, MILLISECOND_TO_CHECK_CONNECT);
  619. }
  620. void CPortableScannerFSM::s5_on_exit()
  621. {
  622. m_eDevStatus = DEVICE_STATUS_NORMAL;
  623. CancelTimer(CHECK_CONNECT_TIMER_ID);
  624. }
  625. unsigned int CPortableScannerFSM::s5_on_event(FSMEvent* e)
  626. {
  627. Dbg("s5_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  628. unsigned int uRet = 0;
  629. switch(e->iEvt)
  630. {
  631. case USER_EVT_BIND_DEVICE:
  632. {
  633. BindDeviceTask* pTask = new BindDeviceTask(this);
  634. BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
  635. pTask->SetContext(pEvt->m_ctx);
  636. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  637. }
  638. e->SetHandled();
  639. break;
  640. case USER_EVT_BIND_DEVICE_FINISHED:
  641. {
  642. if(m_pCapture->IsAttached()) {
  643. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  644. uRet = 6;
  645. }else if(m_pCapture->IsPreview()) {
  646. uRet = 4;
  647. }else {
  648. uRet = 1;
  649. }
  650. }
  651. }
  652. e->SetHandled();
  653. break;
  654. case EVT_TIMER:
  655. {
  656. }
  657. break;
  658. case USER_EVT_GETSTATUS:
  659. {
  660. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  661. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  662. pTask->SetContext(pEvt->m_ctx);
  663. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  664. }
  665. e->SetHandled();
  666. break;
  667. case USER_EVT_GETINFO:
  668. {
  669. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  670. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  671. pTask->SetContext(pEvt->m_ctx);
  672. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  673. }
  674. e->SetHandled();
  675. break;
  676. case USER_EVT_SETWINPOS:
  677. {
  678. SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
  679. SetWinPosTask* pTask = new SetWinPosTask(this);
  680. pTask->SetContext(pEvt->m_ctx);
  681. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  682. }
  683. e->SetHandled();
  684. break;
  685. }
  686. return uRet;
  687. }
  688. void CPortableScannerFSM::s6_on_entry()
  689. {
  690. LOG_FUNCTION();
  691. }
  692. void CPortableScannerFSM::s6_on_exit()
  693. {
  694. }
  695. unsigned int CPortableScannerFSM::s6_on_event(FSMEvent* e)
  696. {
  697. Dbg("s6_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  698. unsigned int uRet = 0;
  699. switch(e->iEvt)
  700. {
  701. case USER_EVT_BIND_DEVICE:
  702. {
  703. BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
  704. if(!!pEvt->m_ctx->Req.force) {
  705. BindDeviceTask* pTask = new BindDeviceTask(this);
  706. pTask->SetContext(pEvt->m_ctx);
  707. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  708. e->SetHandled();
  709. }
  710. }
  711. break;
  712. case USER_EVT_BIND_DEVICE_FINISHED:
  713. {
  714. if(m_pCapture->IsAttached()) {
  715. if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
  716. uRet = 6;
  717. }else if(m_pCapture->IsPreview()) {
  718. uRet = 4;
  719. }else {
  720. uRet = 1;
  721. }
  722. }
  723. }
  724. e->SetHandled();
  725. break;
  726. case USER_EVT_DROP_OFF_DEVICE:
  727. {
  728. if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
  729. m_capCurrentType = CAMTYPE_UNKNOWN;
  730. uRet = 1;
  731. }
  732. e->SetHandled();
  733. }
  734. break;
  735. case USER_EVT_STARTPREVIEW:
  736. {
  737. StartPreviewTask* pTask = new StartPreviewTask(this);
  738. StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
  739. pTask->SetContext(pEvt->m_ctx);
  740. GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
  741. e->SetHandled();
  742. }
  743. break;
  744. case USER_EVT_STARTPREVIEW_FINISHED:
  745. {
  746. uRet = e->param1;
  747. if(uRet == 1)
  748. {
  749. m_desiredAction = USER_EVT_STARTPREVIEW;
  750. }
  751. }
  752. e->SetHandled();
  753. break;
  754. case USER_EVT_SETPROPERTY:
  755. {
  756. SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
  757. SetProperty(pEvt->m_ctx);
  758. }
  759. e->SetHandled();
  760. break;
  761. case USER_EVT_STOPPREVIEW:
  762. {
  763. StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
  764. if(!pEvt->m_ctx->Req.bOnlyHide)
  765. {//业务在s6状态仅能进行彻底取消预览的操作
  766. StopPreviewTask* pTask = new StopPreviewTask(this);
  767. pTask->SetContext(pEvt->m_ctx);
  768. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  769. e->SetHandled();
  770. }
  771. }
  772. break;
  773. case USER_EVT_STOPPREVIEW_FINISHED:
  774. {
  775. uRet = e->param1;
  776. }
  777. e->SetHandled();
  778. break;
  779. case USER_EVT_SETWINPOS:
  780. {
  781. SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
  782. if(pEvt->m_ctx->Req.type == 2)
  783. {//在s6状态,不允许进行显示和隐藏操作,业务必须通过XXXPreview来进行显示隐藏操作
  784. SetWinPosTask* pTask = new SetWinPosTask(this);
  785. pTask->SetContext(pEvt->m_ctx);
  786. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  787. e->SetHandled();
  788. }
  789. }
  790. break;
  791. case USER_EVT_DISCONNECT:
  792. {
  793. }
  794. e->SetHandled();
  795. break;
  796. case USER_EVT_GETSTATUS:
  797. {
  798. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  799. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  800. pTask->SetContext(pEvt->m_ctx);
  801. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  802. }
  803. e->SetHandled();
  804. break;
  805. case USER_EVT_GETINFO:
  806. {
  807. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  808. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  809. pTask->SetContext(pEvt->m_ctx);
  810. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  811. }
  812. e->SetHandled();
  813. break;
  814. case USER_EVT_EXIT:
  815. {
  816. m_pCapture->TerminatePreview();
  817. }
  818. e->SetHandled();
  819. break;
  820. default:
  821. {
  822. }
  823. break;
  824. }
  825. return uRet;
  826. }
  827. void CPortableScannerFSM::s7_on_entry()
  828. {
  829. LOG_FUNCTION();
  830. FSMEvent* pEvt = new FSMEvent(USER_EVT_EXIT_FINISHED);
  831. pEvt->param1 = OnDevExit();
  832. PostEventFIFO(pEvt);
  833. }
  834. void CPortableScannerFSM::s7_on_exit()
  835. {
  836. }
  837. unsigned int CPortableScannerFSM::s7_on_event(FSMEvent* e)
  838. {
  839. Dbg("s7_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  840. unsigned int uRet = 0;
  841. switch(e->iEvt)
  842. {
  843. case USER_EVT_EXIT_FINISHED:
  844. {
  845. uRet = e->param1;
  846. if(uRet == 1)
  847. {
  848. m_desiredAction = USER_EVT_EXIT;
  849. }
  850. }
  851. e->SetHandled();
  852. break;
  853. case USER_EVT_GETSTATUS:
  854. {
  855. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  856. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  857. pTask->SetContext(pEvt->m_ctx);
  858. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  859. }
  860. e->SetHandled();
  861. break;
  862. case USER_EVT_GETINFO:
  863. {
  864. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  865. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  866. pTask->SetContext(pEvt->m_ctx);
  867. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  868. }
  869. e->SetHandled();
  870. break;
  871. }
  872. return uRet;
  873. }
  874. unsigned int CPortableScannerFSM::s8_on_event(FSMEvent* e)
  875. {
  876. Dbg("s8_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
  877. unsigned int uRet = 0;
  878. switch(e->iEvt)
  879. {
  880. case USER_EVT_GETSTATUS:
  881. {
  882. GetDevStatusTask* pTask = new GetDevStatusTask(this);
  883. GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
  884. pEvt->m_ctx->Answer(Error_InvalidState);
  885. }
  886. e->SetHandled();
  887. break;
  888. case USER_EVT_GETINFO:
  889. {
  890. GetDevInfoTask* pTask = new GetDevInfoTask(this);
  891. GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
  892. pTask->SetContext(pEvt->m_ctx);
  893. GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
  894. }
  895. e->SetHandled();
  896. break;
  897. }
  898. return uRet;
  899. }
  900. int CPortableScannerFSM::StartPreview(SpReqAnsContext<PortableScannerService_StartPreview_Req,
  901. PortableScannerService_StartPreview_Ans>::Pointer ctx)
  902. {
  903. bool nRes = 0;
  904. ErrorCodeEnum erroCode = Error_Unexpect;
  905. if(ctx->Req.colorType || ctx->Req.scanType || ctx->Req.rotateType)
  906. {
  907. erroCode = SetPreview(ctx);
  908. Dbg("SetPreview in StartPreview returned: %d, testCode: %d.", erroCode, ctx->Ans.reserved1);
  909. }
  910. if(!m_pCapture->IsAttached()) {
  911. erroCode = Error_InvalidState;
  912. ctx->Answer(erroCode);
  913. //TODO:
  914. return nRes;
  915. }
  916. if(m_pCapture->IsPreview() && m_pCapture->IsWinHide()) {
  917. m_pCapture->SetWinShown();
  918. Dbg("SetPreview changeTo SetVisible");
  919. erroCode = Error_Succeed;
  920. }else if(!m_pCapture->IsPreview()) {
  921. Dbg("StartPreview...");
  922. if(!m_pCapture->StartPreview()) {
  923. nRes = 1;
  924. FireLogEventMsg(START_PREVIEW_FAILED, CSimpleStringA::Format(
  925. "camera start to preview failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  926. } else {
  927. erroCode = Error_Succeed;
  928. }
  929. Dbg("StartPreview returned %d", nRes);
  930. }
  931. ctx->Answer(erroCode);
  932. return nRes;
  933. }
  934. int CPortableScannerFSM::StopPreview(SpReqAnsContext<PortableScannerService_CancelPreview_Req,
  935. PortableScannerService_CancelPreview_Ans>::Pointer ctx)
  936. {
  937. ErrorCodeEnum erroCode = Error_Unexpect;
  938. int nRes = 0;
  939. if(ctx->Req.bOnlyHide) {
  940. m_pCapture->SetWinShown(false);
  941. nRes = 1;
  942. erroCode = Error_Succeed;
  943. } else {
  944. Dbg("TerminatePreview...");
  945. if(m_pCapture->TerminatePreview()) {
  946. nRes = 2;
  947. erroCode = Error_Succeed;
  948. } else {
  949. FireLogEventMsg(STOP_PREVIEW_FAILED, CSimpleStringA::Format(
  950. "camera stop previewing failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  951. nRes = 0;
  952. }
  953. Dbg("TerminatePreview returned %d.", nRes);
  954. }
  955. ctx->Answer(erroCode);
  956. return nRes;
  957. }
  958. int CPortableScannerFSM::ScanImage(SpReqAnsContext<PortableScannerService_ScanImage_Req,
  959. PortableScannerService_ScanImage_Ans>::Pointer ctx)
  960. {
  961. int nRes = 0;
  962. ErrorCodeEnum erroCode = Error_Unexpect;
  963. CSimpleStringA csImageFile;
  964. GetCurImageName(csImageFile);
  965. BOOL bRet = DelAndGetNewFileName(csImageFile);
  966. Dbg("aimImageFileName: %s, bRet: %d.", (LPCTSTR)csImageFile, bRet);
  967. if(!bRet)
  968. {
  969. ctx->Answer(Error_Resource);
  970. //Todo: SetCustLastErrorCode();
  971. Dbg("Create imgPath failed.");
  972. return 1;
  973. }
  974. if(m_pCapture->ScanImage(csImageFile)) {
  975. erroCode = Error_Succeed;
  976. Dbg("ScanImage suc.");
  977. //TODO:
  978. ctx->Ans.filePath = csImageFile;
  979. CBlob& cbData = ctx->Ans.imageData;
  980. FILE* fHandle = fopen(csImageFile, "rb");
  981. if(fHandle)
  982. {
  983. fseek(fHandle, 0, SEEK_END);
  984. long fileSize = ftell(fHandle);
  985. if(fileSize <= 0) {
  986. LOG_TRACE("filesize(%s) error: %ld.", (LPCTSTR)csImageFile, fileSize);
  987. erroCode = Error_NotExist;
  988. fclose(fHandle);
  989. nRes = 4;
  990. }
  991. else
  992. {
  993. Dbg("Fetch the fileSize: %ld", fileSize);
  994. cbData.Alloc(fileSize);
  995. fseek(fHandle, 0, SEEK_SET);
  996. fread(cbData.m_pData, 1, fileSize, fHandle);
  997. fclose(fHandle);
  998. }
  999. }
  1000. else {
  1001. LOG_TRACE("fopen(%s) failed.", (LPCTSTR)csImageFile);
  1002. erroCode = Error_IO;
  1003. nRes = 4;
  1004. }
  1005. if(ISSUCCEEDED(erroCode)) {
  1006. if(true/*TODO: */) {
  1007. m_pCapture->SetWinShown(false);
  1008. nRes = 6;
  1009. }
  1010. }
  1011. ctx->Answer(erroCode);
  1012. } else {
  1013. Dbg("ScanImage failed");
  1014. erroCode = Error_Unexpect;
  1015. ctx->Answer(erroCode);
  1016. //Todo: SetCustLastErrorCode();
  1017. FireLogEventMsg(SCAN_IMAGE_FAILED, CSimpleStringA::Format(
  1018. "camera scans image failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  1019. nRes = 2;
  1020. }
  1021. return nRes;
  1022. }
  1023. int CPortableScannerFSM::ScanImageEx(SpReqAnsContext<PortableScannerService_ScanImageEx_Req,
  1024. PortableScannerService_ScanImageEx_Ans>::Pointer ctx)
  1025. {
  1026. LOG_FUNCTION();
  1027. int nRes = 0;
  1028. ErrorCodeEnum erroCode = Error_Unexpect;
  1029. CSimpleStringA csImageFile;
  1030. GetCurImageName(csImageFile);
  1031. BOOL bRet = DelAndGetNewFileName(csImageFile);
  1032. Dbg("aimImageFileName: %s, bRet: %d.", (LPCTSTR)csImageFile, bRet);
  1033. if(!bRet)
  1034. {
  1035. ctx->Answer(Error_Resource);
  1036. //Todo: SetCustLastErrorCode();
  1037. Dbg("Create imgPath failed.");
  1038. return 1;
  1039. }
  1040. if(m_pCapture->ScanImage(csImageFile)) {
  1041. erroCode = Error_Succeed;
  1042. Dbg("ScanImage suc.");
  1043. //TODO:
  1044. ctx->Ans.filePath = csImageFile;
  1045. CBlob& cbData = ctx->Ans.imageData;
  1046. FILE* fHandle = fopen(csImageFile, "rb");
  1047. if(fHandle)
  1048. {
  1049. fseek(fHandle, 0, SEEK_END);
  1050. long fileSize = ftell(fHandle);
  1051. if(fileSize <= 0) {
  1052. LOG_TRACE("filesize(%s) error: %ld.", (LPCTSTR)csImageFile, fileSize);
  1053. erroCode = Error_NotExist;
  1054. fclose(fHandle);
  1055. nRes = 4;
  1056. }
  1057. else
  1058. {
  1059. cbData.Alloc(fileSize);
  1060. fseek(fHandle, 0, SEEK_SET);
  1061. fread(cbData.m_pData, 1, fileSize, fHandle);
  1062. fclose(fHandle);
  1063. }
  1064. }
  1065. else {
  1066. LOG_TRACE("fopen(%s) failed.", (LPCTSTR)csImageFile);
  1067. erroCode = Error_IO;
  1068. nRes = 4;
  1069. }
  1070. if(ISSUCCEEDED(erroCode)) {
  1071. if(true/*TODO: */) {
  1072. m_pCapture->SetWinShown(false);
  1073. }
  1074. }
  1075. ctx->Answer(erroCode);
  1076. } else {
  1077. Dbg("ScanImage failed");
  1078. erroCode = Error_Unexpect;
  1079. ctx->Answer(erroCode);
  1080. //Todo: SetCustLastErrorCode();
  1081. FireLogEventMsg(SCAN_IMAGE_EX_FAILED, CSimpleStringA::Format(
  1082. "camera scans image ex failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  1083. nRes = 2;
  1084. }
  1085. return nRes;
  1086. }
  1087. int CPortableScannerFSM::ShowProperty(SpReqAnsContext<PortableScannerService_ShowProperty_Req,
  1088. PortableScannerService_ShowProperty_Ans>::Pointer ctx)
  1089. {
  1090. ctx->Answer(Error_NotImpl);
  1091. return 0;
  1092. }
  1093. int CPortableScannerFSM::SetProperty(SpReqAnsContext<PortableScannerService_SetProperty_Req,
  1094. PortableScannerService_SetProperty_Ans>::Pointer ctx)
  1095. {
  1096. int nRes = 0;
  1097. ErrorCodeEnum erroCode = Error_Succeed;
  1098. int retCode = 0;
  1099. //////////////////////////////////////////////////////////////////////////
  1100. if(ctx->Req.colorType == 1)
  1101. {
  1102. m_pCapture->SetColor(COLORFUL);
  1103. Dbg("SetColor(COLORFUL)");
  1104. }
  1105. else if(ctx->Req.colorType == 2)
  1106. {
  1107. m_pCapture->SetColor(GRAY);
  1108. Dbg("SetColor(GRAY)");
  1109. }
  1110. //////////////////////////////////////////////////////////////////////////
  1111. if(ctx->Req.rotateType == 1)
  1112. {
  1113. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE);
  1114. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE) returned %d.", erroCode);
  1115. }
  1116. else if(ctx->Req.rotateType == 2)
  1117. {
  1118. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT);
  1119. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT) returned %d.", erroCode);
  1120. }
  1121. else if(ctx->Req.rotateType == 3)
  1122. {
  1123. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR);
  1124. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR) returned %d.", erroCode);
  1125. }
  1126. else if(ctx->Req.rotateType == 4)
  1127. {
  1128. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT);
  1129. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT) returned %d.", erroCode);
  1130. }
  1131. //////////////////////////////////////////////////////////////////////////
  1132. if(ctx->Req.scanType == 1)
  1133. {
  1134. if(m_roiRate[ROI_DEFAULT] > 0) {
  1135. m_pCapture->SetROCValue(m_roiRate[ROI_DEFAULT]);
  1136. Dbg("SetROCValue(%f)", m_roiRate[ROI_DEFAULT]);
  1137. } else {
  1138. m_pCapture->SetROC(ROI_DEFAULT);
  1139. Dbg("SetROC(ROI_DEFAULT)");
  1140. }
  1141. }
  1142. else if(ctx->Req.scanType == 2)
  1143. {
  1144. if(m_roiRate[ROI_A4] > 0) {
  1145. m_pCapture->SetROCValue(m_roiRate[ROI_A4]);
  1146. Dbg("SetROCValue(%f)", m_roiRate[ROI_A4]);
  1147. } else {
  1148. m_pCapture->SetROC(ROI_A4);
  1149. Dbg("SetROC(ROI_A4)");
  1150. }
  1151. }
  1152. else if(ctx->Req.scanType == 3)
  1153. {
  1154. if(m_roiRate[ROI_IDCER] > 0) {
  1155. m_pCapture->SetROCValue(m_roiRate[ROI_IDCER]);
  1156. Dbg("SetROCValue(%f)", m_roiRate[ROI_IDCER]);
  1157. } else {
  1158. m_pCapture->SetROC(ROI_IDCER);
  1159. Dbg("SetROC(ROI_IDCER)");
  1160. }
  1161. }
  1162. ctx->Ans.retCode = retCode;
  1163. ctx->Answer(Error_Succeed);
  1164. return nRes;
  1165. }
  1166. int CPortableScannerFSM::SetWinPos(SpReqAnsContext<PortableScannerService_SetWinPos_Req,
  1167. PortableScannerService_SetWinPos_Ans>::Pointer ctx)
  1168. {
  1169. LOG_FUNCTION();
  1170. int nRes = 0;
  1171. ErrorCodeEnum erroCode = Error_Unexpect;
  1172. bool bSetCustom = ctx->Req.type == 2;
  1173. if(ctx->Req.type == 2) {
  1174. Dbg("Start to SetViewPos x(%d), y(%d), w(%d), reserved(%d).",
  1175. ctx->Req.pointX, ctx->Req.pointY, ctx->Req.nWidth, ctx->Req.reserved4);
  1176. /*if((ctx->Req.nWidth == 0)) {
  1177. if(!m_pCapture->IsWinHide()) {
  1178. m_pCapture->SetWinShown(false);
  1179. }
  1180. } else*/ {
  1181. m_pCapture->SetWinPosition(ctx->Req.pointX, ctx->Req.pointY,
  1182. ctx->Req.nWidth, ctx->Req.nWidth * 9 / 16);
  1183. /*if(m_pCapture->IsWinHide()) {
  1184. m_pCapture->SetWinShown(true);
  1185. }*/
  1186. }
  1187. erroCode = Error_Succeed;
  1188. m_nRecX = ctx->Req.pointX;
  1189. m_nRecY = ctx->Req.pointY;
  1190. m_nRecW = ctx->Req.nWidth;
  1191. }
  1192. else
  1193. {
  1194. Dbg("SetWinShown(%d)", ctx->Req.type);
  1195. m_pCapture->SetWinShown(ctx->Req.type == 0 ? false : true);
  1196. erroCode = Error_Succeed;
  1197. }
  1198. ctx->Answer(erroCode);
  1199. return nRes;
  1200. }
  1201. int CPortableScannerFSM::GetDevStatus(SpReqAnsContext<PortableScannerService_GetDevStatus_Req,
  1202. PortableScannerService_GetDevStatus_Ans>::Pointer ctx)
  1203. {
  1204. int nRes = 0;
  1205. ctx->Ans.connected = m_pCapture->IsAttached();
  1206. ctx->Ans.previewing = m_pCapture->IsPreview();
  1207. ctx->Ans.showing = (int)!m_pCapture->IsWinHide();
  1208. ctx->Ans.devIndex = m_pCapture->GetCurScanID();
  1209. ctx->Ans.type = (short)m_capCurrentType;
  1210. ctx->Ans.checkcode = m_pCapture->GetCurDevName().c_str();
  1211. ctx->Answer(Error_Succeed);
  1212. return nRes;
  1213. }
  1214. int CPortableScannerFSM::GetDevInfo(SpReqAnsContext<PortableScannerService_GetDevInfo_Req,
  1215. PortableScannerService_GetDevInfo_Ans>::Pointer ctx)
  1216. {
  1217. int nRes = 0;
  1218. ctx->Ans.model = "CMB.PortableScanner";
  1219. ctx->Ans.state = int(m_eDevStatus);
  1220. if(m_pCapture->IsAttached()) {
  1221. ctx->Ans.type = m_pCapture->GetCurDevName().c_str();
  1222. }else {
  1223. ctx->Ans.type = "not-known";
  1224. }
  1225. ctx->Ans.version = CSimpleStringA::Format("%s.%d", MOD_VERSION, (int)m_capCurrentType);
  1226. ctx->Answer(Error_Succeed);
  1227. return nRes;
  1228. }
  1229. BOOL CPortableScannerFSM::GetCurImageName(CSimpleStringA& csImagName, bool bExt /*= false*/)
  1230. {
  1231. //SYSTEMTIME st;
  1232. //GetLocalTime(&st);
  1233. //char szBuffer[MAX_PATH_SIZE] = {0};
  1234. //sprintf_s(szBuffer, MAX_PATH_SIZE, "%02d%02d%02d%03d.jpg", st.wHour, st.wMinute, st.wSecond,
  1235. // st.wMilliseconds);
  1236. csImagName.Clear();
  1237. if(bExt)
  1238. {
  1239. csImagName = "PortableMaterialImageExt.jpg";
  1240. }
  1241. else
  1242. {
  1243. csImagName = "PortableMaterialImage.jpg";
  1244. }
  1245. return (csImagName.GetLength() > 0);
  1246. }
  1247. BOOL CPortableScannerFSM::DelAndGetNewFileName(CSimpleStringA& csfileName)
  1248. {
  1249. LOG_FUNCTION();
  1250. char fileName[MAX_PATH_SIZE] = {0};
  1251. memset(fileName, 0, sizeof(char)*MAX_PATH_SIZE);
  1252. strcpy_s(fileName, MAX_PATH_SIZE, (LPCTSTR)csfileName);
  1253. if(strlen(fileName) == 0 || strchr(fileName, (int)'*') != NULL)
  1254. {
  1255. Dbg("Invalid or empty fileName occurs: (%s)", fileName);
  1256. return FALSE;
  1257. }
  1258. CSimpleStringA strPath, strAimPath;
  1259. ErrorCodeEnum erroCode = m_pEntity->GetFunction()->GetPath("Dep", strPath);
  1260. strAimPath = strPath + "\\" + fileName;
  1261. csfileName = strAimPath;
  1262. return (DeleteFileIfExisted(strAimPath) == Error_Succeed);
  1263. }
  1264. int CPortableScannerFSM::OnDevExit()
  1265. {
  1266. ErrorCodeEnum erroCode = Error_Succeed;
  1267. int nRes = 1;
  1268. if(m_pCapture->IsAttached()) {
  1269. /*无论是高拍仪还是普通摄像头,退回到主页后都释放掉*/
  1270. if(true || m_capCurrentType != CAMTYPE_HSPS) {
  1271. m_pCapture->CloseCamera();
  1272. m_capCurrentType = CAMTYPE_UNKNOWN;
  1273. }else {
  1274. nRes = 0;
  1275. if(m_pCapture->IsPreview()) {
  1276. m_pCapture->TerminatePreview();
  1277. }
  1278. Dbg("Make the hsps camera occupied");
  1279. }
  1280. }
  1281. return nRes;
  1282. }
  1283. ErrorCodeEnum CPortableScannerFSM::UnitTest()
  1284. {
  1285. #ifdef TWINKLE_LOCAL_DEBUG
  1286. LOG_FUNCTION();
  1287. CSimpleStringA strVendorName(true);
  1288. BOOL res = RetrieveVendorName("nantian#doWnCamera", strVendorName);
  1289. if(res != TRUE || strVendorName.Compare("nantian") != 0)
  1290. return Error_NotImpl;
  1291. res = RetrieveVendorName("doWnCamera", strVendorName);
  1292. if(res == TRUE || !strVendorName.IsNullOrEmpty())
  1293. return Error_NotInit;
  1294. StuHSPSParam param;
  1295. GetPortableResRatio("dfsklfdlf", param);
  1296. if(param.resolutionX != 1920 || param.resolutionY != 1080 || param.framePerSec != 0)
  1297. return Error_DataCheck;
  1298. GetPortableResRatio("nantian#downcamera", param);
  1299. if(param.resolutionX != 1920 || param.resolutionY != 1080 || param.framePerSec != 0)
  1300. return Error_DataCheck;
  1301. GetPortableResRatio("exIST#downcamera", param);
  1302. if(param.resolutionX != 1024 || param.resolutionY != 1024 || param.framePerSec != 1024)
  1303. return Error_NotIntegrated;
  1304. if(GetPortableDevID("exIST#downcamera") != NORMAL_DEVICE_ID)
  1305. return Error_NotIntegrated;
  1306. if(GetPortableDevID("Eloam#DownCamera") != ELOAM_DEVICE_ID)
  1307. return Error_NotIntegrated;
  1308. if(GetPortableDevID("eloam#DownCamera") != ELOAM_DEVICE_ID)
  1309. return Error_NotIntegrated;
  1310. if(GetPortableDevID("KGR#DownCamera") != KGR_DEVICE_ID)
  1311. return Error_NotIntegrated;
  1312. if(GetPortableDevID("Hanvon#DownCamera") != HANVON_DEVICE_ID)
  1313. return Error_NotIntegrated;
  1314. if(GetPortableDevID("1434432434") != NORMAL_DEVICE_ID)
  1315. return Error_NotIntegrated;
  1316. if(GetPortableDevID("") != NORMAL_DEVICE_ID)
  1317. return Error_NotIntegrated;
  1318. const char* devicNames[] = {"",
  1319. "Hanvon#DownCamera",
  1320. "Unkonwn",
  1321. "KGR#DownCamera",
  1322. "Other#DownCamera",
  1323. "Eloam#DownCamera"
  1324. };
  1325. for(int j=0; j<sizeof(devicNames)/sizeof(devicNames[0]); ++j)
  1326. {
  1327. m_cameraName = devicNames[j];
  1328. Dbg("set camera name: %s", m_cameraName.GetData());
  1329. for(int i=OPEN_CAMERA_FAILED; i<=RESET_CAMERE_FAILED; ++i)
  1330. {
  1331. FireLogEventMsg(i, CSimpleStringA::Format(
  1332. "camera scans image ex failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  1333. }
  1334. }
  1335. #endif
  1336. return Error_Succeed;
  1337. }
  1338. // Default: 1920*1080 [7/15/2020 10:39 @Gifur]
  1339. ErrorCodeEnum CPortableScannerFSM::GetHSPSParams(const CSimpleStringA& strSection, StuHSPSParam& param)
  1340. {
  1341. CSmartPointer<IEntityFunction> spFunction = this->GetEntityBase()->GetFunction();
  1342. CSmartPointer<IConfigInfo> spRootConfig;
  1343. ErrorCodeEnum ec = spFunction->OpenConfig(Config_Software, spRootConfig);
  1344. const CSimpleStringA strSec = strSection.IsNullOrEmpty() ? "HSPS" : strSection;
  1345. int ratioX = 0;
  1346. int ratioY = 0;
  1347. int fps = 0;
  1348. Dbg("get vendor config: %s", strSec.GetData());
  1349. if(ec == Error_Succeed)
  1350. {
  1351. spRootConfig->ReadConfigValueInt(strSec, "ResRatioX", ratioX);
  1352. spRootConfig->ReadConfigValueInt(strSec, "ResRatioY", ratioY);
  1353. spRootConfig->ReadConfigValueInt(strSec, "FPS", fps);
  1354. } else {
  1355. Dbg("OpenConfig for Software failed: %u", ec);
  1356. }
  1357. memset(&param, 0, sizeof(StuHSPSParam));
  1358. param.framePerSec = 0;
  1359. param.resolutionX = 1920;
  1360. param.resolutionY = 1080;
  1361. if(ratioX != 0 && ratioY != 0) {
  1362. Dbg("Overwrite resolution ratio x: %d", ratioX);
  1363. param.resolutionX = ratioX;
  1364. Dbg("Overwrite resolution ratio y: %d", ratioY);
  1365. param.resolutionY = ratioY;
  1366. }
  1367. if(fps != 0) {
  1368. Dbg("Overwrite frame per secondes: %d", fps);
  1369. param.framePerSec = fps;
  1370. }
  1371. return ec;
  1372. }
  1373. void CPortableScannerFSM::SelfTest(EntityTestEnum eTestType,
  1374. CSmartPointer<ITransactionContext> pTransactionContext)
  1375. {
  1376. pTransactionContext->SendAnswer(m_ecSelfTest);
  1377. }
  1378. void CPortableScannerFSM::OnStateTrans(int iSrcState, int iDstState)
  1379. {
  1380. Dbg("FSMState transfers from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  1381. m_nSrcState = iSrcState;
  1382. }
  1383. ErrorCodeEnum CPortableScannerFSM::SetPreview(SpReqAnsContext<PortableScannerService_StartPreview_Req,
  1384. PortableScannerService_StartPreview_Ans>::Pointer ctx)
  1385. {
  1386. LOG_FUNCTION();
  1387. int nRes = 0;
  1388. ErrorCodeEnum erroCode = Error_Succeed;
  1389. int retCode = 0;
  1390. //////////////////////////////////////////////////////////////////////////
  1391. if(ctx->Req.colorType == 1)
  1392. {
  1393. m_pCapture->SetColor(COLORFUL);
  1394. Dbg("SetColor(COLORFUL)");
  1395. }
  1396. else if(ctx->Req.colorType == 2)
  1397. {
  1398. m_pCapture->SetColor(GRAY);
  1399. Dbg("SetColor(GRAY)");
  1400. }
  1401. //////////////////////////////////////////////////////////////////////////
  1402. if(ctx->Req.rotateType == 1)
  1403. {
  1404. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE);
  1405. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE) returned %d.", erroCode);
  1406. }
  1407. else if(ctx->Req.rotateType == 2)
  1408. {
  1409. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT);
  1410. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT) returned %d.", erroCode);
  1411. }
  1412. else if(ctx->Req.rotateType == 3)
  1413. {
  1414. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR);
  1415. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR) returned %d.", erroCode);
  1416. }
  1417. else if(ctx->Req.rotateType == 4)
  1418. {
  1419. //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT);
  1420. //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT) returned %d.", erroCode);
  1421. }
  1422. //////////////////////////////////////////////////////////////////////////
  1423. if(ctx->Req.scanType == 1)
  1424. {
  1425. if(m_roiRate[ROI_DEFAULT] > 0) {
  1426. m_pCapture->SetROCValue(m_roiRate[ROI_DEFAULT]);
  1427. Dbg("SetROCValue(%f)", m_roiRate[ROI_DEFAULT]);
  1428. } else {
  1429. m_pCapture->SetROC(ROI_DEFAULT);
  1430. Dbg("SetROC(ROI_DEFAULT)");
  1431. }
  1432. }
  1433. else if(ctx->Req.scanType == 2)
  1434. {
  1435. if(m_roiRate[ROI_A4] > 0) {
  1436. m_pCapture->SetROCValue(m_roiRate[ROI_A4]);
  1437. Dbg("SetROCValue(%f)", m_roiRate[ROI_A4]);
  1438. } else {
  1439. m_pCapture->SetROC(ROI_A4);
  1440. Dbg("SetROC(ROI_A4)");
  1441. }
  1442. }
  1443. else if(ctx->Req.scanType == 3)
  1444. {
  1445. if(m_roiRate[ROI_IDCER] > 0) {
  1446. m_pCapture->SetROCValue(m_roiRate[ROI_IDCER]);
  1447. Dbg("SetROCValue(%f)", m_roiRate[ROI_IDCER]);
  1448. } else {
  1449. m_pCapture->SetROC(ROI_IDCER);
  1450. Dbg("SetROC(ROI_IDCER)");
  1451. }
  1452. }
  1453. ctx->Ans.retCode = retCode;
  1454. ctx->Ans.reserved1 = 404040404;
  1455. Dbg("retCode: %d, testCode: %d.", ctx->Ans.retCode, ctx->Ans.reserved1);
  1456. if(retCode == 0) {
  1457. return Error_Succeed;
  1458. }
  1459. return Error_DevCommFailed;
  1460. }
  1461. ErrorCodeEnum CPortableScannerFSM::DeleteFileIfExisted(LPCTSTR fileName)
  1462. {
  1463. ErrorCodeEnum erroCode = Error_Unexpect;
  1464. WIN32_FIND_DATA findData;
  1465. if(FindFirstFileA(fileName, &findData) != INVALID_HANDLE_VALUE)
  1466. {
  1467. if(DeleteFileA(fileName) != 0)
  1468. {
  1469. Dbg("DeleteFile(%s) suc.", fileName);
  1470. return Error_Succeed;
  1471. }
  1472. else
  1473. {
  1474. Dbg("DeleteFile(%s) failed LastError(%d).", fileName, GetLastError());
  1475. return Error_Unexpect;
  1476. }
  1477. }
  1478. if(GetLastError() == ERROR_FILE_NOT_FOUND)
  1479. {
  1480. return Error_Succeed;
  1481. }
  1482. Dbg("DeleteFile(%s) Unexpect GetLastError(%d).", fileName, GetLastError());
  1483. return Error_Unexpect;
  1484. }
  1485. int CPortableScannerFSM::BindDevice(SpReqAnsContext<PortableScannerService_BindCameraDevice_Req,
  1486. PortableScannerService_BindCameraDevice_Ans>::Pointer ctx)
  1487. {
  1488. LOG_FUNCTION();
  1489. if(ctx->Req.devId == -1 && ctx->Req.devName.GetLength() <= 0) {
  1490. ctx->Answer(Error_Param);
  1491. Dbg("ctx::req param in BindDevice is invalid.");
  1492. m_cameraName = "";
  1493. return -1;
  1494. }
  1495. bool bOpen = false;
  1496. bool bPrePreveiw = false;
  1497. bool bShown = false;
  1498. if(m_pCapture->IsAttached()) {
  1499. bPrePreveiw = m_pCapture->IsPreview();
  1500. bShown = !m_pCapture->IsWinHide();
  1501. ctx->Ans.usedId = m_pCapture->GetCurScanID();
  1502. ctx->Ans.checkcode = m_pCapture->GetCurDevName().c_str();
  1503. if(ctx->Req.devName.GetLength() > 0) {
  1504. if(!ctx->Ans.checkcode.Compare(ctx->Req.devName)) {
  1505. Dbg("The required device is already attached, return previously.");
  1506. ctx->Answer(Error_Succeed);
  1507. m_cameraName = ctx->Req.devName;
  1508. return 0;
  1509. }
  1510. }
  1511. }else {
  1512. ctx->Ans.usedId = -1;
  1513. ctx->Ans.checkcode = "";
  1514. }
  1515. int nRes = -1;
  1516. if(ctx->Req.devName.GetLength() > 0) {
  1517. m_cameraName = ctx->Req.devName;
  1518. Dbg("Try to OpenCamera(%s)", (LPCTSTR)m_cameraName);
  1519. bOpen = m_pCapture->OpenCamera(ctx->Req.devName);
  1520. if(!bOpen && ctx->Req.devId != -1) {
  1521. bOpen = m_pCapture->Open(ctx->Req.devId);
  1522. }
  1523. }else {
  1524. Dbg("Try to Open(%d)", ctx->Req.devId);
  1525. bOpen = m_pCapture->Open(ctx->Req.devId);
  1526. m_cameraName = "";
  1527. }
  1528. if(bOpen) {
  1529. if(s_err_preview_times != 0) {
  1530. Dbg("Reset error times");
  1531. s_err_preview_times = 0;
  1532. }
  1533. m_capCurrentType = (CapTypeEnum)ctx->Req.type;
  1534. m_pCapture->SetHSPSType(m_capCurrentType == CAMTYPE_HSPS);
  1535. Dbg("Attach camera succ: devInfo: (%d)[%s]",
  1536. (int)m_pCapture->GetCurScanID(), m_pCapture->GetCurDevName().c_str());
  1537. nRes = 1;
  1538. m_cameraName = m_pCapture->GetCurDevName().c_str();
  1539. {
  1540. StuHSPSParam ratio;
  1541. GetPortableResRatio(m_cameraName, ratio);
  1542. Dbg("Update resolution ratio: %d, %d, %d", ratio.resolutionX, ratio.resolutionY, ratio.framePerSec);
  1543. m_pCapture->SetResoultionRatio(ratio.resolutionX, ratio.resolutionY, ratio.framePerSec);
  1544. }
  1545. ctx->Answer(Error_Succeed);
  1546. if(bPrePreveiw && bShown) {
  1547. if(m_pCapture->StartPreview()) {
  1548. nRes = 6;
  1549. }else {
  1550. Dbg("StartPreview failed !!");
  1551. nRes = -1;
  1552. }
  1553. }else if(bPrePreveiw) {
  1554. //TODO:
  1555. }
  1556. } else {
  1557. Dbg("Attach camera failed: %u", m_pCapture->GetError());
  1558. ctx->Answer(Error_DevCommFailed);
  1559. FireLogEventMsg(OPEN_CAMERA_FAILED, CSimpleStringA::Format(
  1560. "Open camre failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
  1561. m_cameraName = "";
  1562. }
  1563. return nRes;
  1564. }
  1565. void CPortableScannerFSM::RetrieveROIFromRunTime()
  1566. {
  1567. for(int i=0; i<MAX_RIOMODE; ++i) {
  1568. m_roiRate[i] = -1.0f;
  1569. }
  1570. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  1571. CSmartPointer<IConfigInfo> spConfig;
  1572. ErrorCodeEnum ec = spFunction->OpenConfig(Config_Run, spConfig);
  1573. if (Error_Succeed == ec) {
  1574. int nValue;
  1575. ec = spConfig->ReadConfigValueInt("ROIMode", "Default", nValue);
  1576. if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
  1577. m_roiRate[ROI_DEFAULT] = 1.0f*nValue / 10;
  1578. Dbg("Hey Default %f", m_roiRate[ROI_DEFAULT]);
  1579. }
  1580. ec = spConfig->ReadConfigValueInt("ROIMode", "A4", nValue);
  1581. if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
  1582. m_roiRate[ROI_A4] = 1.0f*nValue / 10;
  1583. Dbg("Hey A4 %f", m_roiRate[ROI_A4]);
  1584. }
  1585. ec = spConfig->ReadConfigValueInt("ROIMode", "IDCer", nValue);
  1586. if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
  1587. m_roiRate[ROI_IDCER] = 1.0f*nValue / 10;
  1588. Dbg("Hey IDCer %f", m_roiRate[ROI_IDCER]);
  1589. }
  1590. ec = spConfig->ReadConfigValueInt("ROIMode", "Custom", nValue);
  1591. if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
  1592. m_roiRate[ROI_CUSTOM] = 1.0f*nValue / 10;
  1593. Dbg("Hey Custom %f", m_roiRate[ROI_CUSTOM]);
  1594. }
  1595. }
  1596. }
  1597. void CPortableScannerFSM::GetPortableResRatio(LPCTSTR lpcszDevName, StuHSPSParam& param)
  1598. {
  1599. CSimpleStringA strVendorName(true);
  1600. RetrieveVendorName(lpcszDevName, strVendorName);
  1601. GetHSPSParams(strVendorName, param);
  1602. }
  1603. BOOL CPortableScannerFSM::RetrieveVendorName(LPCTSTR lpcszDevName, CSimpleStringA& strVendorName)
  1604. {
  1605. auto arrays = CSimpleStringA(lpcszDevName).Split('#');
  1606. if(arrays.GetCount() == 2)
  1607. {
  1608. if(arrays[1].Compare("downcamera", true) == 0) {
  1609. strVendorName = arrays[0];
  1610. //Dbg("Retrive name: %s", strVendorName.GetData());
  1611. return TRUE;
  1612. }
  1613. }
  1614. strVendorName = "";
  1615. return FALSE;
  1616. }
  1617. DWORD CPortableScannerFSM::GetPortableDevID(LPCTSTR lpcszDevName)
  1618. {
  1619. CSimpleStringA strVendorName(true);
  1620. if(!RetrieveVendorName(lpcszDevName, strVendorName))
  1621. {
  1622. return NORMAL_DEVICE_ID;
  1623. }
  1624. if(strVendorName.Compare("Eloam", true) == 0)
  1625. {
  1626. return ELOAM_DEVICE_ID;
  1627. } else if(strVendorName.Compare("KGR", true) == 0)
  1628. {
  1629. return KGR_DEVICE_ID;
  1630. } else if(strVendorName.Compare("Hanvon", true) == 0)
  1631. {
  1632. return HANVON_DEVICE_ID;
  1633. }
  1634. return NORMAL_DEVICE_ID;
  1635. }
  1636. void CPortableScannerFSM::FireLogEventMsg(DWORD dwUserPlainCode, LPCTSTR lpcszMessage)
  1637. {
  1638. if(dwUserPlainCode != 0)
  1639. {
  1640. const DWORD dwComplUserCode = CombineEventCodeWithDevelID(
  1641. CombineEventCodeWithDevID(GetPortableDevID(m_cameraName), dwUserPlainCode));
  1642. LogWarn(Severity_Middle, Error_DevMedia, dwComplUserCode, lpcszMessage);
  1643. }
  1644. }