1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750 |
- #include "StdAfx.h"
- #include "PortableScannerFSM.h"
- #include "DocScannerCap.h"
- #include "EventCode.h"
- #include "UserEventCode.h"
- #define RESET_TIMER_ID 1
- #define CHECK_CONNECT_TIMER_ID 2
- const int MILLISECOND_TO_RESET = 30 * 1000;
- const int MILLISECOND_TO_CHECK_CONNECT = 30 * 1000;
- const int MAX_PATH_SIZE = 256;
- const int MAX_IMAGEDATA_BUFSIZE = 1 * 1024*1024;
- const int MAX_IMAGEDATA_SUFFIX = 1024;
- const int MAX_ERROR_TIMES = 2;
- const int MAX_PREVIEW_ERROR_TIMES = 3;
- float m_roiRate[MAX_RIOMODE] = { -1.0f, -1.0f, -1.0f, -1.0f };
- static unsigned int s_err_preview_times = 0;
- #define TEMP_WIDTH 1280
- #define MOD_VERSION "1.1.0.0"
- #define MACRO_EVTTOSTRRING(x) \
- case x : return #x; \
- break;
- const char* EvtTypeToString(int nEvtType)
- {
- switch(nEvtType)
- {
- MACRO_EVTTOSTRRING(USER_EVT_STARTPREVIEW)
- MACRO_EVTTOSTRRING(USER_EVT_STARTPREVIEW_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_STOPPREVIEW)
- MACRO_EVTTOSTRRING(USER_EVT_STOPPREVIEW_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_SETWINPOS)
- MACRO_EVTTOSTRRING(USER_EVT_SETWINPOSFINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGE)
- MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGE_EX)
- MACRO_EVTTOSTRRING(USER_EVT_SCANIMAGEFINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTY)
- MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTY_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_GETSTATUS)
- MACRO_EVTTOSTRRING(USER_EVT_GETINFO)
- MACRO_EVTTOSTRRING(USER_EVT_DISCONNECT)
- MACRO_EVTTOSTRRING(USER_EVT_EXIT)
- MACRO_EVTTOSTRRING(USER_EVT_QUIT)
- MACRO_EVTTOSTRRING(USER_EVT_RESET)
- MACRO_EVTTOSTRRING(USER_EVT_SHOWPROPERTY)
- MACRO_EVTTOSTRRING(USER_EVT_SHOWPROPERTY_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_SETPROPERTYINVIEW_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_EXIT_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_NO_CONTAINS_DEVICE)
- MACRO_EVTTOSTRRING(USER_EVT_HIDEPREVIEW)
- MACRO_EVTTOSTRRING(USER_EVT_BIND_DEVICE)
- MACRO_EVTTOSTRRING(USER_EVT_BIND_DEVICE_FINISHED)
- MACRO_EVTTOSTRRING(USER_EVT_DROP_OFF_DEVICE)
- MACRO_EVTTOSTRRING(USER_EVT_ERROR_IN_PREVIEW)
- default:
- return "Unknown EventType";
- break;
- }
- return "Unknown EventType";
- }
- CPortableScannerFSM::CPortableScannerFSM(void)
- :
- #ifndef SHIELD_APDATER_INTERFACE
- m_version(0),
- m_batch(0),
- m_hVendorDll(NULL),
- CreateDevComponent(NULL),
- ReleaseDevComponent(NULL),
- m_pHSPS(NULL),
- #endif
- m_ecSelfTest(Error_Succeed),
- m_dwErroCode(0),
- m_bOperating(FALSE),
- m_nTickTimes(0),
- m_nFatalTimes(0),
- m_nSrcState(s0),
- m_cameraName(""),
- m_csDllName(""),
- m_eDevStatus(DEVICE_STATUS_NOT_READY),
- m_desiredAction(USER_EVT_QUIT),
- m_pCapture(NULL),
- m_capCurrentType(CAMTYPE_UNKNOWN),
- m_dwEntDevelID(0x21C)
- {
- }
- CPortableScannerFSM::~CPortableScannerFSM(void)
- {
- LOG_FUNCTION();
- #ifndef SHIELD_APDATER_INTERFACE
- SAFE_FREE_LIBRARY(m_hVendorDll);
- #endif
- #if 1
- if(m_pCapture) {
- delete m_pCapture;
- m_pCapture = NULL;
- }
- #else
- Dbg("don't delete capture object!");
- #endif
- }
- ErrorCodeEnum CPortableScannerFSM::OnInit()
- {
- LOG_FUNCTION();
- ErrorCodeEnum erroCode = Error_Succeed;
- Dbg("Version: %s, Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
- LOG_ASSERT(m_pCapture == NULL);
- m_pCapture = new CDocScannerCap(this);
- if(m_pCapture == NULL) {
- erroCode = Error_Resource;
- LogWarn(Severity_Middle, erroCode, 0, "Create CDocScannerCap object failed !");
- return erroCode;
- }
- RetrieveROIFromRunTime();
- m_eDevStatus = DEVICE_STATUS_NORMAL;
- return erroCode;
- }
- ErrorCodeEnum CPortableScannerFSM::OnExit()
- {
- return Error_Succeed;
- }
- void CPortableScannerFSM::s0_on_entry()
- {
- LOG_FUNCTION();
- m_bOperating = FALSE;
- m_nFatalTimes = 0;
- m_ecSelfTest = Error_Succeed;
- if(!m_pCapture->IsAttached()) {
- PostEventFIFO(new FSMEvent(USER_EVT_NO_CONTAINS_DEVICE));
- }
- }
- void CPortableScannerFSM::s0_on_exit()
- {
- m_bOperating = FALSE;
- }
- unsigned int CPortableScannerFSM::s0_on_event(FSMEvent* e)
- {
- Dbg("s0_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_BIND_DEVICE:
- {
- BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
- if(!!pEvt->m_ctx->Req.force) {
- BindDeviceTask* pTask = new BindDeviceTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_BIND_DEVICE_FINISHED:
- {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- }else if(m_pCapture->IsPreview()) {
- uRet = 4;
- }else {
- uRet = 1;
- }
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_DROP_OFF_DEVICE:
- {
- if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
- m_capCurrentType = CAMTYPE_UNKNOWN;
- uRet = 1;
- }
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW:
- {
- StartPreviewTask* pTask = new StartPreviewTask(this);
- StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW_FINISHED:
- {
- uRet = e->param1;
- e->SetHandled();
- if(uRet == 1) {
- m_desiredAction = USER_EVT_STARTPREVIEW;
- }
- }
- break;
- case USER_EVT_SETWINPOS:
- {
- SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
- if(pEvt->m_ctx->Req.type == 2)
- {//在s0状态,仅能用于设置位置和窗体大小
- SetWinPosTask* pTask = new SetWinPosTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_SETPROPERTY:
- {
- SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
- SetProperty(pEvt->m_ctx);
- e->SetHandled();
- }
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_NO_CONTAINS_DEVICE:
- {
-
- }
- break;
- default:
- {
- Dbg("What a unhandled event! should i miss it?");
- }
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s1_on_entry()
- {
- }
- void CPortableScannerFSM::s1_on_exit()
- {
- }
- unsigned int CPortableScannerFSM::s1_on_event(FSMEvent* e)
- {
- Dbg("s1_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_BIND_DEVICE:
- {
- BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
- if(!!pEvt->m_ctx->Req.force) {
- BindDeviceTask* pTask = new BindDeviceTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_BIND_DEVICE_FINISHED:
- {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- }else if(m_pCapture->IsPreview()) {
- uRet = 4;
- }else {
- uRet = 1;
- }
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_DROP_OFF_DEVICE:
- {
- if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
- m_capCurrentType = CAMTYPE_UNKNOWN;
- uRet = 1;
- }
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW:
- {
- // Maybe Just SetProperty [Josephus in 15:44:43 2016/11/19]
- SetPropertyInPreviewTask* pTask = new SetPropertyInPreviewTask(this);
- StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
- e->SetHandled();
- }
- break;
- case USER_EVT_ERROR_IN_PREVIEW:
- {
- if(!!m_pCapture) {
- m_cameraName = m_pCapture->GetCurDevName().c_str();
- }
- FireLogEventMsg(PREVIEWING_OCCURS_EXCEPTION, CSimpleStringA::Format(
- "camera occurs exception in previewing %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- m_desiredAction = USER_EVT_STARTPREVIEW;
- }
- e->SetHandled();
- break;
- case USER_EVT_SETPROPERTYINVIEW_FINISHED:
- {
-
- }
- e->SetHandled();
- break;
- case USER_EVT_STOPPREVIEW:
- {
- StopPreviewTask* pTask = new StopPreviewTask(this);
- StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_STOPPREVIEW_FINISHED:
- {
- uRet = e->param1;
- if(uRet == 0) {
- m_desiredAction = USER_EVT_STOPPREVIEW;
- }
- else if(uRet == 4) {
- m_desiredAction = USER_EVT_HIDEPREVIEW;
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_SCANIMAGE:
- {
- ScanImageTask* pTask = new ScanImageTask(this);
- ScanImageEvent* pEvt = dynamic_cast<ScanImageEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_SCANIMAGE_EX:
- {
- ScanImageExTask* pTask = new ScanImageExTask(this);
- ScanImageExEvent* pEvt = dynamic_cast<ScanImageExEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_SETPROPERTY:
- {
- SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
- SetProperty(pEvt->m_ctx);
- }
- e->SetHandled();
- break;
- case USER_EVT_SETWINPOS:
- {
- SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
- SetWinPosTask* pTask = new SetWinPosTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_DISCONNECT:
- {
- }
- e->SetHandled();
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_EXIT:
- {
- m_pCapture->TerminatePreview();
- }
- e->SetHandled();
- break;
- default:
- {
- }
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s3_on_entry()
- {
- LOG_FUNCTION();
- s_err_preview_times = m_desiredAction == USER_EVT_STARTPREVIEW ? s_err_preview_times + 1 : 0;
- m_eDevStatus = DEVICE_STATUS_FAULT;
- m_nTickTimes = 0;
- m_bOperating = FALSE;
- if(s_err_preview_times >= MAX_PREVIEW_ERROR_TIMES) {
- LogError(Severity_Middle, Error_DevMedia, LOG_ERROR_PORTABLESCANNER_IN_PREVIEW, m_cameraName);
- }
- else {
- Dbg("error times: %d", s_err_preview_times);
- ScheduleTimer(RESET_TIMER_ID, MILLISECOND_TO_RESET / 20);
- }
- }
- void CPortableScannerFSM::s3_on_exit()
- {
- m_eDevStatus = DEVICE_STATUS_NORMAL;
- m_bOperating = FALSE;
- m_ecSelfTest = Error_Succeed;
- m_nFatalTimes = 0;
- m_desiredAction = USER_EVT_QUIT;
- CancelTimer(RESET_TIMER_ID);
- }
- unsigned int CPortableScannerFSM::s3_on_event(FSMEvent* e)
- {
- Dbg("s3_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_BIND_DEVICE:
- {
- BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
- if(!!pEvt->m_ctx->Req.force) {
- BindDeviceTask* pTask = new BindDeviceTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_BIND_DEVICE_FINISHED:
- {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- }else if(m_pCapture->IsPreview()) {
- uRet = 4;
- }else {
- uRet = 1;
- }
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_STARTPREVIEW:
- {
- StartPreviewTask* pTask = new StartPreviewTask(this);
- StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW_FINISHED:
- {
- uRet = e->param1;
- e->SetHandled();
- }
- break;
- case USER_EVT_STOPPREVIEW:
- {
- CancelTimer(RESET_TIMER_ID);
- StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
- StopPreviewTask* pTask = new StopPreviewTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_STOPPREVIEW_FINISHED:
- {
- uRet = e->param1;
- if(uRet == 0) {
- m_desiredAction = USER_EVT_STOPPREVIEW;
- }
- else if(uRet == 4) {
- m_desiredAction = USER_EVT_HIDEPREVIEW;
- }
- FSMEvent* timerEvent = new FSMEvent(EVT_TIMER);
- timerEvent->param1 = RESET_TIMER_ID;
- PostEventFIFO(timerEvent);
- }
- e->SetHandled();
- break;
- case USER_EVT_RESET:
- {
- if(m_pCapture->ResetCamera()) {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- } else if(m_pCapture->IsPreview()) {
- uRet = 4;
- } else {
- uRet = 1;
- }
- } else {
- uRet = 0;
- }
-
- } else {
- ScheduleTimer(RESET_TIMER_ID, MILLISECOND_TO_RESET / 20);
- uRet = 2;
- }
- }
- e->SetHandled();
- break;
- case EVT_TIMER:
- {
- if(e->param1 == RESET_TIMER_ID)
- {
- FSMEvent* resetEvent = new FSMEvent(USER_EVT_RESET);
- resetEvent->param1 = m_desiredAction;
- PostEventFIFO(resetEvent);
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_DISCONNECT:
- {
- }
- e->SetHandled();
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_DROP_OFF_DEVICE:
- {
- if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
- m_capCurrentType = CAMTYPE_UNKNOWN;
- uRet = 1;
- }
- e->SetHandled();
- }
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s4_on_entry()
- {
- LOG_FUNCTION();
- }
- void CPortableScannerFSM::s4_on_exit()
- {
- }
- unsigned int CPortableScannerFSM::s4_on_event(FSMEvent* e)
- {
- Dbg("s4_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_SCANIMAGEFINISHED:
- {
- uRet = e->param1;
- if(uRet == 2 || uRet == 4)
- {
- m_desiredAction = USER_EVT_SCANIMAGE;
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_DISCONNECT:
- {
- }
- e->SetHandled();
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s5_on_entry()
- {
- LOG_FUNCTION();
- LogEvent(Severity_Middle, LOG_EVT_PORTABLESCANNER_NO_AVAILABLE, "Not hold camera for now!");
- ScheduleTimer(CHECK_CONNECT_TIMER_ID, MILLISECOND_TO_CHECK_CONNECT);
- }
- void CPortableScannerFSM::s5_on_exit()
- {
- m_eDevStatus = DEVICE_STATUS_NORMAL;
- CancelTimer(CHECK_CONNECT_TIMER_ID);
- }
- unsigned int CPortableScannerFSM::s5_on_event(FSMEvent* e)
- {
- Dbg("s5_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_BIND_DEVICE:
- {
- BindDeviceTask* pTask = new BindDeviceTask(this);
- BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_BIND_DEVICE_FINISHED:
- {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- }else if(m_pCapture->IsPreview()) {
- uRet = 4;
- }else {
- uRet = 1;
- }
- }
- }
- e->SetHandled();
- break;
- case EVT_TIMER:
- {
- }
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_SETWINPOS:
- {
- SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
- SetWinPosTask* pTask = new SetWinPosTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s6_on_entry()
- {
- LOG_FUNCTION();
- }
- void CPortableScannerFSM::s6_on_exit()
- {
- }
- unsigned int CPortableScannerFSM::s6_on_event(FSMEvent* e)
- {
- Dbg("s6_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_BIND_DEVICE:
- {
- BindDeviceEvent* pEvt = dynamic_cast<BindDeviceEvent*>(e);
- if(!!pEvt->m_ctx->Req.force) {
- BindDeviceTask* pTask = new BindDeviceTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_BIND_DEVICE_FINISHED:
- {
- if(m_pCapture->IsAttached()) {
- if(m_pCapture->IsPreview() && !m_pCapture->IsWinHide()) {
- uRet = 6;
- }else if(m_pCapture->IsPreview()) {
- uRet = 4;
- }else {
- uRet = 1;
- }
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_DROP_OFF_DEVICE:
- {
- if(m_pCapture->CloseCamera() && !m_pCapture->IsAttached()) {
- m_capCurrentType = CAMTYPE_UNKNOWN;
- uRet = 1;
- }
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW:
- {
- StartPreviewTask* pTask = new StartPreviewTask(this);
- StartPreviewEvent* pEvt = dynamic_cast<StartPreviewEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(pTask);
- e->SetHandled();
- }
- break;
- case USER_EVT_STARTPREVIEW_FINISHED:
- {
- uRet = e->param1;
- if(uRet == 1)
- {
- m_desiredAction = USER_EVT_STARTPREVIEW;
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_SETPROPERTY:
- {
- SetPropertyEvent* pEvt = dynamic_cast<SetPropertyEvent*>(e);
- SetProperty(pEvt->m_ctx);
- }
- e->SetHandled();
- break;
- case USER_EVT_STOPPREVIEW:
- {
- StopPreviewEvent* pEvt = dynamic_cast<StopPreviewEvent*>(e);
- if(!pEvt->m_ctx->Req.bOnlyHide)
- {//业务在s6状态仅能进行彻底取消预览的操作
- StopPreviewTask* pTask = new StopPreviewTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_STOPPREVIEW_FINISHED:
- {
- uRet = e->param1;
- }
- e->SetHandled();
- break;
- case USER_EVT_SETWINPOS:
- {
- SetWinPosEvent* pEvt = dynamic_cast<SetWinPosEvent*>(e);
- if(pEvt->m_ctx->Req.type == 2)
- {//在s6状态,不允许进行显示和隐藏操作,业务必须通过XXXPreview来进行显示隐藏操作
- SetWinPosTask* pTask = new SetWinPosTask(this);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- e->SetHandled();
- }
- }
- break;
- case USER_EVT_DISCONNECT:
- {
- }
- e->SetHandled();
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_EXIT:
- {
- m_pCapture->TerminatePreview();
- }
- e->SetHandled();
- break;
- default:
- {
- }
- break;
- }
- return uRet;
- }
- void CPortableScannerFSM::s7_on_entry()
- {
- LOG_FUNCTION();
- FSMEvent* pEvt = new FSMEvent(USER_EVT_EXIT_FINISHED);
- pEvt->param1 = OnDevExit();
- PostEventFIFO(pEvt);
- }
- void CPortableScannerFSM::s7_on_exit()
- {
- }
- unsigned int CPortableScannerFSM::s7_on_event(FSMEvent* e)
- {
- Dbg("s7_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_EXIT_FINISHED:
- {
- uRet = e->param1;
- if(uRet == 1)
- {
- m_desiredAction = USER_EVT_EXIT;
- }
- }
- e->SetHandled();
- break;
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- }
- return uRet;
- }
- unsigned int CPortableScannerFSM::s8_on_event(FSMEvent* e)
- {
- Dbg("s8_on_event: %s(%d), %d.", EvtTypeToString(e->iEvt), e->iEvt, e->param1);
- unsigned int uRet = 0;
- switch(e->iEvt)
- {
- case USER_EVT_GETSTATUS:
- {
- GetDevStatusTask* pTask = new GetDevStatusTask(this);
- GetDevStatusEvent* pEvt = dynamic_cast<GetDevStatusEvent*>(e);
- pEvt->m_ctx->Answer(Error_InvalidState);
- }
- e->SetHandled();
- break;
- case USER_EVT_GETINFO:
- {
- GetDevInfoTask* pTask = new GetDevInfoTask(this);
- GetDevInfoEvent* pEvt = dynamic_cast<GetDevInfoEvent*>(e);
- pTask->SetContext(pEvt->m_ctx);
- GetEntityBase()->GetFunction()->PostEntityTaskFIFO(pTask);
- }
- e->SetHandled();
- break;
- }
- return uRet;
- }
- int CPortableScannerFSM::StartPreview(SpReqAnsContext<PortableScannerService_StartPreview_Req,
- PortableScannerService_StartPreview_Ans>::Pointer ctx)
- {
- bool nRes = 0;
- ErrorCodeEnum erroCode = Error_Unexpect;
- if(ctx->Req.colorType || ctx->Req.scanType || ctx->Req.rotateType)
- {
- erroCode = SetPreview(ctx);
- Dbg("SetPreview in StartPreview returned: %d, testCode: %d.", erroCode, ctx->Ans.reserved1);
- }
- if(!m_pCapture->IsAttached()) {
- erroCode = Error_InvalidState;
- ctx->Answer(erroCode);
- //TODO:
- return nRes;
- }
- if(m_pCapture->IsPreview() && m_pCapture->IsWinHide()) {
- m_pCapture->SetWinShown();
- Dbg("SetPreview changeTo SetVisible");
- erroCode = Error_Succeed;
- }else if(!m_pCapture->IsPreview()) {
- Dbg("StartPreview...");
- if(!m_pCapture->StartPreview()) {
- nRes = 1;
- FireLogEventMsg(START_PREVIEW_FAILED, CSimpleStringA::Format(
- "camera start to preview failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- } else {
- erroCode = Error_Succeed;
- }
- Dbg("StartPreview returned %d", nRes);
- }
- ctx->Answer(erroCode);
- return nRes;
- }
- int CPortableScannerFSM::StopPreview(SpReqAnsContext<PortableScannerService_CancelPreview_Req,
- PortableScannerService_CancelPreview_Ans>::Pointer ctx)
- {
- ErrorCodeEnum erroCode = Error_Unexpect;
- int nRes = 0;
- if(ctx->Req.bOnlyHide) {
- m_pCapture->SetWinShown(false);
- nRes = 1;
- erroCode = Error_Succeed;
- } else {
- Dbg("TerminatePreview...");
- if(m_pCapture->TerminatePreview()) {
- nRes = 2;
- erroCode = Error_Succeed;
- } else {
- FireLogEventMsg(STOP_PREVIEW_FAILED, CSimpleStringA::Format(
- "camera stop previewing failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- nRes = 0;
- }
- Dbg("TerminatePreview returned %d.", nRes);
- }
- ctx->Answer(erroCode);
- return nRes;
- }
- int CPortableScannerFSM::ScanImage(SpReqAnsContext<PortableScannerService_ScanImage_Req,
- PortableScannerService_ScanImage_Ans>::Pointer ctx)
- {
- int nRes = 0;
- ErrorCodeEnum erroCode = Error_Unexpect;
- CSimpleStringA csImageFile;
- GetCurImageName(csImageFile);
- BOOL bRet = DelAndGetNewFileName(csImageFile);
- Dbg("aimImageFileName: %s, bRet: %d.", (LPCTSTR)csImageFile, bRet);
- if(!bRet)
- {
- ctx->Answer(Error_Resource);
- //Todo: SetCustLastErrorCode();
- Dbg("Create imgPath failed.");
- return 1;
- }
- if(m_pCapture->ScanImage(csImageFile)) {
- erroCode = Error_Succeed;
- Dbg("ScanImage suc.");
- //TODO:
- ctx->Ans.filePath = csImageFile;
- CBlob& cbData = ctx->Ans.imageData;
- FILE* fHandle = fopen(csImageFile, "rb");
- if(fHandle)
- {
- fseek(fHandle, 0, SEEK_END);
- long fileSize = ftell(fHandle);
- if(fileSize <= 0) {
- LOG_TRACE("filesize(%s) error: %ld.", (LPCTSTR)csImageFile, fileSize);
- erroCode = Error_NotExist;
- fclose(fHandle);
- nRes = 4;
- }
- else
- {
- Dbg("Fetch the fileSize: %ld", fileSize);
- cbData.Alloc(fileSize);
- fseek(fHandle, 0, SEEK_SET);
- fread(cbData.m_pData, 1, fileSize, fHandle);
- fclose(fHandle);
- }
- }
- else {
- LOG_TRACE("fopen(%s) failed.", (LPCTSTR)csImageFile);
- erroCode = Error_IO;
- nRes = 4;
- }
- if(ISSUCCEEDED(erroCode)) {
- if(true/*TODO: */) {
- m_pCapture->SetWinShown(false);
- nRes = 6;
- }
- }
- ctx->Answer(erroCode);
- } else {
- Dbg("ScanImage failed");
- erroCode = Error_Unexpect;
- ctx->Answer(erroCode);
- //Todo: SetCustLastErrorCode();
- FireLogEventMsg(SCAN_IMAGE_FAILED, CSimpleStringA::Format(
- "camera scans image failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- nRes = 2;
- }
- return nRes;
- }
- int CPortableScannerFSM::ScanImageEx(SpReqAnsContext<PortableScannerService_ScanImageEx_Req,
- PortableScannerService_ScanImageEx_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- int nRes = 0;
- ErrorCodeEnum erroCode = Error_Unexpect;
- CSimpleStringA csImageFile;
- GetCurImageName(csImageFile);
- BOOL bRet = DelAndGetNewFileName(csImageFile);
- Dbg("aimImageFileName: %s, bRet: %d.", (LPCTSTR)csImageFile, bRet);
- if(!bRet)
- {
- ctx->Answer(Error_Resource);
- //Todo: SetCustLastErrorCode();
- Dbg("Create imgPath failed.");
- return 1;
- }
- if(m_pCapture->ScanImage(csImageFile)) {
- erroCode = Error_Succeed;
- Dbg("ScanImage suc.");
- //TODO:
- ctx->Ans.filePath = csImageFile;
- CBlob& cbData = ctx->Ans.imageData;
- FILE* fHandle = fopen(csImageFile, "rb");
- if(fHandle)
- {
- fseek(fHandle, 0, SEEK_END);
- long fileSize = ftell(fHandle);
- if(fileSize <= 0) {
- LOG_TRACE("filesize(%s) error: %ld.", (LPCTSTR)csImageFile, fileSize);
- erroCode = Error_NotExist;
- fclose(fHandle);
- nRes = 4;
- }
- else
- {
- cbData.Alloc(fileSize);
- fseek(fHandle, 0, SEEK_SET);
- fread(cbData.m_pData, 1, fileSize, fHandle);
- fclose(fHandle);
- }
- }
- else {
- LOG_TRACE("fopen(%s) failed.", (LPCTSTR)csImageFile);
- erroCode = Error_IO;
- nRes = 4;
- }
- if(ISSUCCEEDED(erroCode)) {
- if(true/*TODO: */) {
- m_pCapture->SetWinShown(false);
- }
- }
- ctx->Answer(erroCode);
- } else {
- Dbg("ScanImage failed");
- erroCode = Error_Unexpect;
- ctx->Answer(erroCode);
- //Todo: SetCustLastErrorCode();
- FireLogEventMsg(SCAN_IMAGE_EX_FAILED, CSimpleStringA::Format(
- "camera scans image ex failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- nRes = 2;
- }
- return nRes;
- }
- int CPortableScannerFSM::ShowProperty(SpReqAnsContext<PortableScannerService_ShowProperty_Req,
- PortableScannerService_ShowProperty_Ans>::Pointer ctx)
- {
- ctx->Answer(Error_NotImpl);
- return 0;
- }
- int CPortableScannerFSM::SetProperty(SpReqAnsContext<PortableScannerService_SetProperty_Req,
- PortableScannerService_SetProperty_Ans>::Pointer ctx)
- {
- int nRes = 0;
- ErrorCodeEnum erroCode = Error_Succeed;
- int retCode = 0;
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.colorType == 1)
- {
- m_pCapture->SetColor(COLORFUL);
- Dbg("SetColor(COLORFUL)");
- }
- else if(ctx->Req.colorType == 2)
- {
- m_pCapture->SetColor(GRAY);
- Dbg("SetColor(GRAY)");
- }
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.rotateType == 1)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 2)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 3)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 4)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT) returned %d.", erroCode);
- }
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.scanType == 1)
- {
- if(m_roiRate[ROI_DEFAULT] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_DEFAULT]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_DEFAULT]);
- } else {
- m_pCapture->SetROC(ROI_DEFAULT);
- Dbg("SetROC(ROI_DEFAULT)");
- }
- }
- else if(ctx->Req.scanType == 2)
- {
- if(m_roiRate[ROI_A4] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_A4]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_A4]);
- } else {
- m_pCapture->SetROC(ROI_A4);
- Dbg("SetROC(ROI_A4)");
- }
- }
- else if(ctx->Req.scanType == 3)
- {
- if(m_roiRate[ROI_IDCER] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_IDCER]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_IDCER]);
- } else {
- m_pCapture->SetROC(ROI_IDCER);
- Dbg("SetROC(ROI_IDCER)");
- }
- }
- ctx->Ans.retCode = retCode;
- ctx->Answer(Error_Succeed);
- return nRes;
- }
- int CPortableScannerFSM::SetWinPos(SpReqAnsContext<PortableScannerService_SetWinPos_Req,
- PortableScannerService_SetWinPos_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- int nRes = 0;
- ErrorCodeEnum erroCode = Error_Unexpect;
- bool bSetCustom = ctx->Req.type == 2;
- if(ctx->Req.type == 2) {
- Dbg("Start to SetViewPos x(%d), y(%d), w(%d), reserved(%d).",
- ctx->Req.pointX, ctx->Req.pointY, ctx->Req.nWidth, ctx->Req.reserved4);
- /*if((ctx->Req.nWidth == 0)) {
- if(!m_pCapture->IsWinHide()) {
- m_pCapture->SetWinShown(false);
- }
- } else*/ {
- m_pCapture->SetWinPosition(ctx->Req.pointX, ctx->Req.pointY,
- ctx->Req.nWidth, ctx->Req.nWidth * 9 / 16);
- /*if(m_pCapture->IsWinHide()) {
- m_pCapture->SetWinShown(true);
- }*/
- }
- erroCode = Error_Succeed;
- m_nRecX = ctx->Req.pointX;
- m_nRecY = ctx->Req.pointY;
- m_nRecW = ctx->Req.nWidth;
- }
- else
- {
- Dbg("SetWinShown(%d)", ctx->Req.type);
- m_pCapture->SetWinShown(ctx->Req.type == 0 ? false : true);
- erroCode = Error_Succeed;
- }
- ctx->Answer(erroCode);
- return nRes;
- }
- int CPortableScannerFSM::GetDevStatus(SpReqAnsContext<PortableScannerService_GetDevStatus_Req,
- PortableScannerService_GetDevStatus_Ans>::Pointer ctx)
- {
- int nRes = 0;
- ctx->Ans.connected = m_pCapture->IsAttached();
- ctx->Ans.previewing = m_pCapture->IsPreview();
- ctx->Ans.showing = (int)!m_pCapture->IsWinHide();
- ctx->Ans.devIndex = m_pCapture->GetCurScanID();
- ctx->Ans.type = (short)m_capCurrentType;
- ctx->Ans.checkcode = m_pCapture->GetCurDevName().c_str();
- ctx->Answer(Error_Succeed);
- return nRes;
- }
- int CPortableScannerFSM::GetDevInfo(SpReqAnsContext<PortableScannerService_GetDevInfo_Req,
- PortableScannerService_GetDevInfo_Ans>::Pointer ctx)
- {
- int nRes = 0;
- ctx->Ans.model = "CMB.PortableScanner";
- ctx->Ans.state = int(m_eDevStatus);
- if(m_pCapture->IsAttached()) {
- ctx->Ans.type = m_pCapture->GetCurDevName().c_str();
- }else {
- ctx->Ans.type = "not-known";
- }
- ctx->Ans.version = CSimpleStringA::Format("%s.%d", MOD_VERSION, (int)m_capCurrentType);
- ctx->Answer(Error_Succeed);
- return nRes;
- }
- BOOL CPortableScannerFSM::GetCurImageName(CSimpleStringA& csImagName, bool bExt /*= false*/)
- {
- //SYSTEMTIME st;
- //GetLocalTime(&st);
- //char szBuffer[MAX_PATH_SIZE] = {0};
- //sprintf_s(szBuffer, MAX_PATH_SIZE, "%02d%02d%02d%03d.jpg", st.wHour, st.wMinute, st.wSecond,
- // st.wMilliseconds);
- csImagName.Clear();
- if(bExt)
- {
- csImagName = "PortableMaterialImageExt.jpg";
- }
- else
- {
- csImagName = "PortableMaterialImage.jpg";
- }
- return (csImagName.GetLength() > 0);
- }
- BOOL CPortableScannerFSM::DelAndGetNewFileName(CSimpleStringA& csfileName)
- {
- LOG_FUNCTION();
- char fileName[MAX_PATH_SIZE] = {0};
- memset(fileName, 0, sizeof(char)*MAX_PATH_SIZE);
- strcpy_s(fileName, MAX_PATH_SIZE, (LPCTSTR)csfileName);
- if(strlen(fileName) == 0 || strchr(fileName, (int)'*') != NULL)
- {
- Dbg("Invalid or empty fileName occurs: (%s)", fileName);
- return FALSE;
- }
- CSimpleStringA strPath, strAimPath;
- ErrorCodeEnum erroCode = m_pEntity->GetFunction()->GetPath("Dep", strPath);
- strAimPath = strPath + "\\" + fileName;
- csfileName = strAimPath;
- return (DeleteFileIfExisted(strAimPath) == Error_Succeed);
- }
- int CPortableScannerFSM::OnDevExit()
- {
- ErrorCodeEnum erroCode = Error_Succeed;
- int nRes = 1;
- if(m_pCapture->IsAttached()) {
- /*无论是高拍仪还是普通摄像头,退回到主页后都释放掉*/
- if(true || m_capCurrentType != CAMTYPE_HSPS) {
- m_pCapture->CloseCamera();
- m_capCurrentType = CAMTYPE_UNKNOWN;
- }else {
- nRes = 0;
- if(m_pCapture->IsPreview()) {
- m_pCapture->TerminatePreview();
- }
- Dbg("Make the hsps camera occupied");
- }
- }
- return nRes;
- }
- ErrorCodeEnum CPortableScannerFSM::UnitTest()
- {
- #ifdef TWINKLE_LOCAL_DEBUG
- LOG_FUNCTION();
- CSimpleStringA strVendorName(true);
- BOOL res = RetrieveVendorName("nantian#doWnCamera", strVendorName);
- if(res != TRUE || strVendorName.Compare("nantian") != 0)
- return Error_NotImpl;
- res = RetrieveVendorName("doWnCamera", strVendorName);
- if(res == TRUE || !strVendorName.IsNullOrEmpty())
- return Error_NotInit;
- StuHSPSParam param;
- GetPortableResRatio("dfsklfdlf", param);
- if(param.resolutionX != 1920 || param.resolutionY != 1080 || param.framePerSec != 0)
- return Error_DataCheck;
- GetPortableResRatio("nantian#downcamera", param);
- if(param.resolutionX != 1920 || param.resolutionY != 1080 || param.framePerSec != 0)
- return Error_DataCheck;
- GetPortableResRatio("exIST#downcamera", param);
- if(param.resolutionX != 1024 || param.resolutionY != 1024 || param.framePerSec != 1024)
- return Error_NotIntegrated;
- if(GetPortableDevID("exIST#downcamera") != NORMAL_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("Eloam#DownCamera") != ELOAM_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("eloam#DownCamera") != ELOAM_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("KGR#DownCamera") != KGR_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("Hanvon#DownCamera") != HANVON_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("1434432434") != NORMAL_DEVICE_ID)
- return Error_NotIntegrated;
- if(GetPortableDevID("") != NORMAL_DEVICE_ID)
- return Error_NotIntegrated;
- const char* devicNames[] = {"",
- "Hanvon#DownCamera",
- "Unkonwn",
- "KGR#DownCamera",
- "Other#DownCamera",
- "Eloam#DownCamera"
- };
- for(int j=0; j<sizeof(devicNames)/sizeof(devicNames[0]); ++j)
- {
- m_cameraName = devicNames[j];
- Dbg("set camera name: %s", m_cameraName.GetData());
- for(int i=OPEN_CAMERA_FAILED; i<=RESET_CAMERE_FAILED; ++i)
- {
- FireLogEventMsg(i, CSimpleStringA::Format(
- "camera scans image ex failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- }
- }
- #endif
- return Error_Succeed;
- }
- // Default: 1920*1080 [7/15/2020 10:39 @Gifur]
- ErrorCodeEnum CPortableScannerFSM::GetHSPSParams(const CSimpleStringA& strSection, StuHSPSParam& param)
- {
- CSmartPointer<IEntityFunction> spFunction = this->GetEntityBase()->GetFunction();
- CSmartPointer<IConfigInfo> spRootConfig;
- ErrorCodeEnum ec = spFunction->OpenConfig(Config_Software, spRootConfig);
- const CSimpleStringA strSec = strSection.IsNullOrEmpty() ? "HSPS" : strSection;
- int ratioX = 0;
- int ratioY = 0;
- int fps = 0;
- Dbg("get vendor config: %s", strSec.GetData());
- if(ec == Error_Succeed)
- {
- spRootConfig->ReadConfigValueInt(strSec, "ResRatioX", ratioX);
- spRootConfig->ReadConfigValueInt(strSec, "ResRatioY", ratioY);
- spRootConfig->ReadConfigValueInt(strSec, "FPS", fps);
- } else {
- Dbg("OpenConfig for Software failed: %u", ec);
- }
- memset(¶m, 0, sizeof(StuHSPSParam));
- param.framePerSec = 0;
- param.resolutionX = 1920;
- param.resolutionY = 1080;
- if(ratioX != 0 && ratioY != 0) {
- Dbg("Overwrite resolution ratio x: %d", ratioX);
- param.resolutionX = ratioX;
- Dbg("Overwrite resolution ratio y: %d", ratioY);
- param.resolutionY = ratioY;
- }
- if(fps != 0) {
- Dbg("Overwrite frame per secondes: %d", fps);
- param.framePerSec = fps;
- }
- return ec;
- }
- void CPortableScannerFSM::SelfTest(EntityTestEnum eTestType,
- CSmartPointer<ITransactionContext> pTransactionContext)
- {
- pTransactionContext->SendAnswer(m_ecSelfTest);
- }
- void CPortableScannerFSM::OnStateTrans(int iSrcState, int iDstState)
- {
- Dbg("FSMState transfers from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
- m_nSrcState = iSrcState;
- }
- ErrorCodeEnum CPortableScannerFSM::SetPreview(SpReqAnsContext<PortableScannerService_StartPreview_Req,
- PortableScannerService_StartPreview_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- int nRes = 0;
- ErrorCodeEnum erroCode = Error_Succeed;
- int retCode = 0;
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.colorType == 1)
- {
- m_pCapture->SetColor(COLORFUL);
- Dbg("SetColor(COLORFUL)");
- }
- else if(ctx->Req.colorType == 2)
- {
- m_pCapture->SetColor(GRAY);
- Dbg("SetColor(GRAY)");
- }
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.rotateType == 1)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_NOANGLE) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 2)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_LEFT) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 3)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_MIRROR) returned %d.", erroCode);
- }
- else if(ctx->Req.rotateType == 4)
- {
- //erroCode = m_pHSPS->SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT);
- //Dbg("SetParam(HSPS_MODEL_ROTATE, HSPS_ROTATE_RIGHT) returned %d.", erroCode);
- }
- //////////////////////////////////////////////////////////////////////////
- if(ctx->Req.scanType == 1)
- {
- if(m_roiRate[ROI_DEFAULT] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_DEFAULT]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_DEFAULT]);
- } else {
- m_pCapture->SetROC(ROI_DEFAULT);
- Dbg("SetROC(ROI_DEFAULT)");
- }
- }
- else if(ctx->Req.scanType == 2)
- {
- if(m_roiRate[ROI_A4] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_A4]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_A4]);
- } else {
- m_pCapture->SetROC(ROI_A4);
- Dbg("SetROC(ROI_A4)");
- }
- }
- else if(ctx->Req.scanType == 3)
- {
- if(m_roiRate[ROI_IDCER] > 0) {
- m_pCapture->SetROCValue(m_roiRate[ROI_IDCER]);
- Dbg("SetROCValue(%f)", m_roiRate[ROI_IDCER]);
- } else {
- m_pCapture->SetROC(ROI_IDCER);
- Dbg("SetROC(ROI_IDCER)");
- }
- }
- ctx->Ans.retCode = retCode;
- ctx->Ans.reserved1 = 404040404;
- Dbg("retCode: %d, testCode: %d.", ctx->Ans.retCode, ctx->Ans.reserved1);
-
- if(retCode == 0) {
- return Error_Succeed;
- }
- return Error_DevCommFailed;
- }
- ErrorCodeEnum CPortableScannerFSM::DeleteFileIfExisted(LPCTSTR fileName)
- {
- ErrorCodeEnum erroCode = Error_Unexpect;
- WIN32_FIND_DATA findData;
- if(FindFirstFileA(fileName, &findData) != INVALID_HANDLE_VALUE)
- {
- if(DeleteFileA(fileName) != 0)
- {
- Dbg("DeleteFile(%s) suc.", fileName);
- return Error_Succeed;
- }
- else
- {
- Dbg("DeleteFile(%s) failed LastError(%d).", fileName, GetLastError());
- return Error_Unexpect;
- }
- }
- if(GetLastError() == ERROR_FILE_NOT_FOUND)
- {
- return Error_Succeed;
- }
- Dbg("DeleteFile(%s) Unexpect GetLastError(%d).", fileName, GetLastError());
- return Error_Unexpect;
- }
- int CPortableScannerFSM::BindDevice(SpReqAnsContext<PortableScannerService_BindCameraDevice_Req,
- PortableScannerService_BindCameraDevice_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- if(ctx->Req.devId == -1 && ctx->Req.devName.GetLength() <= 0) {
- ctx->Answer(Error_Param);
- Dbg("ctx::req param in BindDevice is invalid.");
- m_cameraName = "";
- return -1;
- }
- bool bOpen = false;
- bool bPrePreveiw = false;
- bool bShown = false;
- if(m_pCapture->IsAttached()) {
- bPrePreveiw = m_pCapture->IsPreview();
- bShown = !m_pCapture->IsWinHide();
- ctx->Ans.usedId = m_pCapture->GetCurScanID();
- ctx->Ans.checkcode = m_pCapture->GetCurDevName().c_str();
- if(ctx->Req.devName.GetLength() > 0) {
- if(!ctx->Ans.checkcode.Compare(ctx->Req.devName)) {
- Dbg("The required device is already attached, return previously.");
- ctx->Answer(Error_Succeed);
- m_cameraName = ctx->Req.devName;
- return 0;
- }
- }
- }else {
- ctx->Ans.usedId = -1;
- ctx->Ans.checkcode = "";
- }
- int nRes = -1;
- if(ctx->Req.devName.GetLength() > 0) {
- m_cameraName = ctx->Req.devName;
- Dbg("Try to OpenCamera(%s)", (LPCTSTR)m_cameraName);
- bOpen = m_pCapture->OpenCamera(ctx->Req.devName);
- if(!bOpen && ctx->Req.devId != -1) {
- bOpen = m_pCapture->Open(ctx->Req.devId);
- }
- }else {
- Dbg("Try to Open(%d)", ctx->Req.devId);
- bOpen = m_pCapture->Open(ctx->Req.devId);
- m_cameraName = "";
- }
- if(bOpen) {
- if(s_err_preview_times != 0) {
- Dbg("Reset error times");
- s_err_preview_times = 0;
- }
- m_capCurrentType = (CapTypeEnum)ctx->Req.type;
- m_pCapture->SetHSPSType(m_capCurrentType == CAMTYPE_HSPS);
- Dbg("Attach camera succ: devInfo: (%d)[%s]",
- (int)m_pCapture->GetCurScanID(), m_pCapture->GetCurDevName().c_str());
- nRes = 1;
- m_cameraName = m_pCapture->GetCurDevName().c_str();
- {
- StuHSPSParam ratio;
- GetPortableResRatio(m_cameraName, ratio);
- Dbg("Update resolution ratio: %d, %d, %d", ratio.resolutionX, ratio.resolutionY, ratio.framePerSec);
- m_pCapture->SetResoultionRatio(ratio.resolutionX, ratio.resolutionY, ratio.framePerSec);
- }
- ctx->Answer(Error_Succeed);
- if(bPrePreveiw && bShown) {
- if(m_pCapture->StartPreview()) {
- nRes = 6;
- }else {
- Dbg("StartPreview failed !!");
- nRes = -1;
- }
- }else if(bPrePreveiw) {
- //TODO:
- }
- } else {
- Dbg("Attach camera failed: %u", m_pCapture->GetError());
- ctx->Answer(Error_DevCommFailed);
- FireLogEventMsg(OPEN_CAMERA_FAILED, CSimpleStringA::Format(
- "Open camre failed %s, %d.", _GetFileName(__FUNCTION__), __LINE__));
- m_cameraName = "";
- }
- return nRes;
- }
- void CPortableScannerFSM::RetrieveROIFromRunTime()
- {
- for(int i=0; i<MAX_RIOMODE; ++i) {
- m_roiRate[i] = -1.0f;
- }
- CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum ec = spFunction->OpenConfig(Config_Run, spConfig);
- if (Error_Succeed == ec) {
- int nValue;
- ec = spConfig->ReadConfigValueInt("ROIMode", "Default", nValue);
- if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
- m_roiRate[ROI_DEFAULT] = 1.0f*nValue / 10;
- Dbg("Hey Default %f", m_roiRate[ROI_DEFAULT]);
- }
- ec = spConfig->ReadConfigValueInt("ROIMode", "A4", nValue);
- if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
- m_roiRate[ROI_A4] = 1.0f*nValue / 10;
- Dbg("Hey A4 %f", m_roiRate[ROI_A4]);
- }
- ec = spConfig->ReadConfigValueInt("ROIMode", "IDCer", nValue);
- if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
- m_roiRate[ROI_IDCER] = 1.0f*nValue / 10;
- Dbg("Hey IDCer %f", m_roiRate[ROI_IDCER]);
- }
- ec = spConfig->ReadConfigValueInt("ROIMode", "Custom", nValue);
- if(ISSUCCEEDED(ec) && nValue > 0 && nValue < 10) {
- m_roiRate[ROI_CUSTOM] = 1.0f*nValue / 10;
- Dbg("Hey Custom %f", m_roiRate[ROI_CUSTOM]);
- }
- }
- }
- void CPortableScannerFSM::GetPortableResRatio(LPCTSTR lpcszDevName, StuHSPSParam& param)
- {
- CSimpleStringA strVendorName(true);
- RetrieveVendorName(lpcszDevName, strVendorName);
- GetHSPSParams(strVendorName, param);
- }
- BOOL CPortableScannerFSM::RetrieveVendorName(LPCTSTR lpcszDevName, CSimpleStringA& strVendorName)
- {
- auto arrays = CSimpleStringA(lpcszDevName).Split('#');
- if(arrays.GetCount() == 2)
- {
- if(arrays[1].Compare("downcamera", true) == 0) {
- strVendorName = arrays[0];
- //Dbg("Retrive name: %s", strVendorName.GetData());
- return TRUE;
- }
- }
- strVendorName = "";
- return FALSE;
- }
- DWORD CPortableScannerFSM::GetPortableDevID(LPCTSTR lpcszDevName)
- {
- CSimpleStringA strVendorName(true);
- if(!RetrieveVendorName(lpcszDevName, strVendorName))
- {
- return NORMAL_DEVICE_ID;
- }
- if(strVendorName.Compare("Eloam", true) == 0)
- {
- return ELOAM_DEVICE_ID;
- } else if(strVendorName.Compare("KGR", true) == 0)
- {
- return KGR_DEVICE_ID;
- } else if(strVendorName.Compare("Hanvon", true) == 0)
- {
- return HANVON_DEVICE_ID;
- }
- return NORMAL_DEVICE_ID;
- }
- void CPortableScannerFSM::FireLogEventMsg(DWORD dwUserPlainCode, LPCTSTR lpcszMessage)
- {
- if(dwUserPlainCode != 0)
- {
- const DWORD dwComplUserCode = CombineEventCodeWithDevelID(
- CombineEventCodeWithDevID(GetPortableDevID(m_cameraName), dwUserPlainCode));
- LogWarn(Severity_Middle, Error_DevMedia, dwComplUserCode, lpcszMessage);
- }
- }
|