mod_SalesRecorder.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. #include "stdafx.h"
  2. #include "mod_SalesRecorder.h"
  3. #include "Event.h"
  4. #include "rvc_media_common.h"
  5. #include "fileutil.h"
  6. #include "array.h"
  7. #include <memutil.h>
  8. #include <algorithm>
  9. #include "y2k_time.h"
  10. #ifdef RVC_OS_WIN
  11. #include <Windows.h>
  12. #else
  13. #include <dirent.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/stat.h>
  16. #include <sys/fcntl.h>
  17. #endif
  18. #include "mod_customeraware/Event.h"
  19. #include "mod_facetracking/sysvar.h"
  20. #include "mod_mediacontroller/Event.h"
  21. #include "mod_interactivecontrol/Event.h"
  22. #include <assert.h>
  23. #include "EventCode.h"
  24. #include <string.h>
  25. #include "filecryption.h"
  26. #ifndef RVC_MIN_FILESIZE
  27. #define RVC_MIN_FILESIZE 10240
  28. #endif
  29. #ifndef MAX_LOG_LEN
  30. #define MAX_LOG_LEN 512
  31. #endif
  32. #define MAX_DISK_PERCENT 95 // 磁盘最大占用百分比
  33. static const char* record_failed_case_table[] = {
  34. "[RTA3L01] 启动录像失败,初始化失败",
  35. "[RTA3L02] 启动录像失败,字体为空,添加水印失败",
  36. "[RTA3L03] 开始录像失败,请稍后(约30秒)再试",
  37. "[RTA3L04] 录像失败,摄像头故障,获取不到视频,请联系厂商进行维修",
  38. "[RTA3L05] 录像失败,获取不到远端音频,请检查风险提示音是否正常播放且音量大小是否正常",
  39. "[RTA3L06] 录像失败,麦克风故障,获取不到本地音频,请联系厂商进行维修",
  40. "[RTA3L07] 录像失败,系统不支持当前音频采样率",
  41. "[RTA3L08] 录像失败,音频流写入失败,请稍后(约30秒)再试",
  42. "[RTA3L09] 录像失败,获取不到远端视频,请稍后(约30秒)再试"
  43. };
  44. static BOOL CheckDiskStatus(const char *szRoot, int nPercent, int *pFreeRatio)
  45. {
  46. //todo 调用resourcewatcher
  47. return TRUE;
  48. }
  49. static int hch2int(char hch)
  50. {
  51. if (hch >= '0' && hch <= '9')
  52. return hch-'0';
  53. else if (hch >= 'a' && hch <= 'f')
  54. return hch-'a'+10;
  55. else if (hch >= 'A' && hch <= 'F')
  56. return hch-'A'+10;
  57. return 0;
  58. }
  59. static long hexstr2int(const char *str, int len)
  60. {
  61. long result = 0;
  62. if (str && len <= strlen(str)) {
  63. for (int i = 0; i < len; ++i) {
  64. result += (hch2int(str[i]) << ((len-i-1) << 2));
  65. }
  66. }
  67. return result;
  68. }
  69. static CSimpleStringA VideoSerialID2TimeString(const char *videoserialid)
  70. {
  71. DWORD nTimeTicks = hexstr2int(videoserialid,8);
  72. return ((CSmallDateTime)nTimeTicks).ToTimeString();
  73. }
  74. static void CStringSplit(char* str, char** result, const char* del)
  75. {
  76. char* pdata = NULL;
  77. char* p = NULL;
  78. #ifdef RVC_OS_WIN
  79. p = strtok_s(str, del, &pdata);
  80. #else
  81. p = strtok_r(str, del, &pdata);
  82. #endif // RVC_OS_WIN
  83. while (p != NULL) {
  84. *result++ = p;
  85. #ifdef RVC_OS_WIN
  86. p = strtok_s(NULL, del, &pdata);
  87. #else
  88. p = strtok_r(NULL, del, &pdata);
  89. #endif
  90. }
  91. }
  92. static bool rvcMoveFile(const char* strSrcFile, const char* strDstFile)
  93. {
  94. bool bRet = false;
  95. if (NULL == strSrcFile || NULL == strDstFile) {
  96. return bRet;
  97. }
  98. #ifdef RVC_OS_WIN
  99. bRet = MoveFile(strSrcFile, strDstFile);
  100. #else
  101. if (0 == rename(strSrcFile, strDstFile)) {
  102. bRet = true;
  103. }
  104. #endif // RVC_OS_WIN
  105. return bRet;
  106. }
  107. static bool RvcDeleteFile(const char* strSrcFile)
  108. {
  109. bool bRet = false;
  110. if (NULL == strSrcFile) {
  111. return bRet;
  112. }
  113. #ifdef RVC_OS_WIN
  114. bRet = DeleteFile(strSrcFile);
  115. #else
  116. if (0 == remove(strSrcFile)) {
  117. bRet = true;
  118. }
  119. #endif // RVC_OS_WIN
  120. return bRet;
  121. }
  122. #ifdef RVC_OS_WIN
  123. // 寻找某目录下与通配符匹配的文件
  124. static bool FindMatchedFile(const char* pstrFindPath, const char* pstrFindFileName, uint32_t & uCountFile)
  125. {
  126. char sPath[MAX_PATH] = {0};
  127. char sFormatFileName[MAX_PATH + 2] = "*";
  128. WIN32_FIND_DATA FindFileData;
  129. HANDLE hFind;
  130. bool fFinished = false;
  131. strcpy(sFormatFileName, pstrFindPath);
  132. if (pstrFindPath[strlen(pstrFindPath) - 1] != SPLIT_SLASH)
  133. {
  134. strcat(sFormatFileName, SPLIT_SLASH_STR);
  135. strcat(sFormatFileName, "*");
  136. }
  137. else
  138. {
  139. strcat(sFormatFileName, "*");
  140. }
  141. strcat(sFormatFileName, pstrFindFileName);
  142. strcat(sFormatFileName, "*");
  143. hFind = FindFirstFile(sFormatFileName, &FindFileData);
  144. if (hFind == INVALID_HANDLE_VALUE)
  145. {
  146. return false;
  147. }
  148. else
  149. {
  150. while (!fFinished)
  151. {
  152. strcpy(sPath, pstrFindPath);
  153. if (sPath[strlen(sPath) - 1] != SPLIT_SLASH)
  154. {
  155. strcat(sPath, SPLIT_SLASH_STR);
  156. }
  157. strcat(sPath, FindFileData.cFileName);
  158. if (!(FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes))
  159. {
  160. ++uCountFile;
  161. }
  162. if (!FindNextFile(hFind, &FindFileData))
  163. {
  164. if (GetLastError() == ERROR_NO_MORE_FILES)
  165. {
  166. fFinished = true;
  167. }
  168. else
  169. {
  170. break;
  171. }
  172. }
  173. }
  174. FindClose(hFind);
  175. }
  176. return true;
  177. }
  178. #else
  179. static bool FindMatchedFile(const char* sFindPath, const char* sFindFileName, uint32_t& uCountFile)
  180. {
  181. bool bRet = false;
  182. DIR* pDir = NULL;
  183. struct dirent* pFile = NULL;
  184. CSimpleStringA tmpFindFileName = sFindFileName;
  185. pDir = opendir(sFindPath);
  186. if (pDir == NULL) {
  187. return bRet;
  188. }
  189. //linux不支持*查找,去掉后缀
  190. char strsuffix[MAX_PATH] = { 0 };
  191. snprintf(strsuffix, MAX_PATH, "*.%s", RECORD_MP4_SUFFIX);
  192. if (tmpFindFileName.IsEndWith(strsuffix)) {
  193. tmpFindFileName = tmpFindFileName.SubString(0, tmpFindFileName.GetLength() - 5);
  194. }
  195. while ((pFile = readdir(pDir)) != NULL) {
  196. if (pFile->d_type & DT_DIR) {
  197. continue;
  198. }
  199. else {
  200. if (0 == _strnicmp(pFile->d_name, tmpFindFileName.GetData(), tmpFindFileName.GetLength())) {
  201. ++uCountFile;
  202. bRet = true;
  203. }
  204. }
  205. }
  206. closedir(pDir);
  207. return bRet;
  208. }
  209. #endif
  210. static CSimpleStringA DecryptString(const char* lpszEncrpyted)
  211. {
  212. if (NULL == lpszEncrpyted) {
  213. return CSimpleStringA("");
  214. }
  215. int iEncrypt = 0;
  216. int len = strlen(lpszEncrpyted);
  217. CSimpleStringA csPlainTxt('\0', (len) / 2 + 1);
  218. int iCh = 0;
  219. for (int i = 0; i < len; i += 2) {
  220. sscanf(lpszEncrpyted + i, "%02X", &iCh);
  221. csPlainTxt[i / 2] = (char)(((char)iCh) ^ (128 | (iEncrypt++ & 127)));
  222. }
  223. return CSimpleStringA((LPCTSTR)csPlainTxt);
  224. }
  225. static unsigned long GetFileSize(const char* pfilename)
  226. {
  227. #ifdef RVC_OS_WIN
  228. unsigned long usize = 0;
  229. if (NULL == pfilename) {
  230. return usize;
  231. }
  232. FILE* pFile = fopen(pfilename, "rb");
  233. if (pFile) {
  234. fseek(pFile, 0, SEEK_END);
  235. usize = ftell(pFile);
  236. fclose(pFile);
  237. }
  238. return usize;
  239. #else
  240. struct stat statbuf;
  241. if (0 == stat(pfilename, &statbuf)) {
  242. return statbuf.st_size;
  243. }
  244. else {
  245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("errno info is %s.", strerror(errno));
  246. return 0;
  247. }
  248. #endif
  249. }
  250. static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
  251. {
  252. LOG_LEVEL_E eloglevel = LOG_LEVEL_DEBUG;
  253. if (FILECRYPT_LOG_INFO <= elevel) {
  254. eloglevel = LOG_LEVEL_INFO;
  255. }
  256. va_list arg;
  257. va_start(arg, fmt);
  258. int n = vsnprintf(NULL, 0, fmt, arg);
  259. if (n >= MAX_LOG_LEN) {
  260. char* buf = (char*)malloc((size_t)(n + 1));
  261. vsnprintf(buf, n + 1, fmt, arg);
  262. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", buf);
  263. free(buf);
  264. }
  265. else {
  266. char strlog[MAX_LOG_LEN] = { 0 };
  267. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  268. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", strlog);
  269. }
  270. va_end(arg);
  271. }
  272. void SalesRecordServiceSession::Handle_GetOFLVideoRecords( SpReqAnsContext<SalesRecorderSerVice_GetOFLVideoRecords_Req, SalesRecorderSerVice_GetOFLVideoRecords_Ans>::Pointer ctx )
  273. {
  274. DbgToBeidou(ctx->link, __FUNCTION__)();
  275. ctx->Answer(Error_Succeed);
  276. }
  277. void SalesRecordServiceSession::Handle_CheckVideoDiskStatus( SpReqAnsContext<SalesRecorderSerVice_CheckVideoDiskStatus_Req, SalesRecorderSerVice_CheckVideoDiskStatus_Ans>::Pointer ctx )
  278. {
  279. DbgToBeidou(ctx->link, __FUNCTION__)();
  280. int nFreeRatio = 0;
  281. int bSufficient = CheckDiskStatus(ctx->Req.DriveLetter.GetData(), m_pEntity->GetMaxDiskPercent(), &nFreeRatio);
  282. ctx->Ans.IsSufficient = bSufficient;
  283. ctx->Ans.FreeRatio = nFreeRatio;
  284. ctx->Answer(Error_Succeed);
  285. }
  286. void SalesRecordServiceSession::Handle_PlayVideo(SpReqAnsContext<SalesRecorderSerVice_PlayVideo_Req, SalesRecorderSerVice_PlayVideo_Ans>::Pointer ctx)
  287. {
  288. DbgToBeidou(ctx->link, __FUNCTION__)();
  289. ctx->Answer(Error_Succeed);
  290. }
  291. void SalesRecordServiceSession::Handle_SaveVideo(SpReqAnsContext<SalesRecorderSerVice_SaveVideo_Req, SalesRecorderSerVice_SaveVideo_Ans>::Pointer ctx)
  292. {
  293. DbgToBeidou(ctx->link, __FUNCTION__)();
  294. ErrorCodeEnum ErrorCode = m_pEntity->HandleSaveVideo();
  295. ctx->Answer(ErrorCode);
  296. }
  297. void SalesRecordServiceSession::Handle_StopRecord(SpReqAnsContext<SalesRecorderSerVice_StopRecord_Req, SalesRecorderSerVice_StopRecord_Ans>::Pointer ctx)
  298. {
  299. DbgToBeidou(ctx->link, __FUNCTION__)();
  300. ErrorCodeEnum ErrorCode = m_pEntity->HandleStopRecord(CSimpleStringW2A(ctx->Req.VideoName).GetData());
  301. ctx->Answer(ErrorCode);
  302. }
  303. void SalesRecordServiceSession::Handle_SetAudioTransFlag(SpReqAnsContext<SalesRecorderSerVice_SetAudioTransFlag_Req, SalesRecorderSerVice_SetAudioTransFlag_Ans>::Pointer ctx)
  304. {
  305. DbgToBeidou(ctx->link, __FUNCTION__)();
  306. ctx->Answer(Error_Succeed);
  307. }
  308. void SalesRecordServiceSession::Handle_StopShowVideo(SpReqAnsContext<SalesRecorderSerVice_StopShowVideo_Req, SalesRecorderSerVice_StopShowVideo_Ans>::Pointer ctx)
  309. {
  310. DbgToBeidou(ctx->link, __FUNCTION__)();
  311. ctx->Answer(Error_Succeed);
  312. }
  313. void SalesRecordServiceSession::Handle_PlaySalesRecord(SpReqAnsContext<SalesRecorderSerVice_PlaySalesRecord_Req, SalesRecorderSerVice_PlaySalesRecord_Ans>::Pointer ctx)
  314. {
  315. DbgToBeidou(ctx->link, __FUNCTION__)();
  316. ctx->Answer(Error_Succeed);
  317. }
  318. void SalesRecordServiceSession::Handle_StartRemoteRecord(SpReqAnsContext<SalesRecorderSerVice_StartRemoteRecord_Req, SalesRecorderSerVice_StartRemoteRecord_Ans>::Pointer ctx)
  319. {
  320. DbgToBeidou(ctx->link, __FUNCTION__)();
  321. ErrorCodeEnum ErrorCode = m_pEntity->HandleStartRecord(ctx->Req.VideoName.GetData(), true);
  322. ctx->Answer(ErrorCode);
  323. }
  324. void SalesRecordServiceSession::Handle_DeleteVideo(SpReqAnsContext<SalesRecorderSerVice_DeleteVideo_Req, SalesRecorderSerVice_DeleteVideo_Ans>::Pointer ctx)
  325. {
  326. DbgToBeidou(ctx->link, __FUNCTION__)();
  327. ErrorCodeEnum ErrorCode = m_pEntity->HandleDeleteVideo();
  328. ctx->Answer(ErrorCode);
  329. }
  330. void SalesRecordServiceSession::Handle_AppendWatermark(SpReqAnsContext<SalesRecorderSerVice_AppendWatermark_Req, SalesRecorderSerVice_AppendWatermark_Ans>::Pointer ctx)
  331. {
  332. DbgToBeidou(ctx->link, __FUNCTION__)();
  333. #ifdef RVC_OS_WIN
  334. ErrorCodeEnum ErrorCode = m_pEntity->HandleVideoAppendWatermark(CSimpleStringW2A(ctx->Req.VideoName).GetData(), CSimpleStringW2A(ctx->Req.Watermark).GetData());
  335. #else
  336. ErrorCodeEnum ErrorCode = m_pEntity->HandleVideoAppendWatermark(CSimpleStringW2A(ctx->Req.VideoName).GetData(), ctx->Req.Watermark.GetData());
  337. #endif
  338. ctx->Answer(ErrorCode);
  339. }
  340. void SalesRecordServiceSession::Handle_SetRecordCamera(SpReqAnsContext<SalesRecorderSerVice_SetRecordCamera_Req, SalesRecorderSerVice_SetRecordCamera_Ans>::Pointer ctx)
  341. {
  342. DbgToBeidou(ctx->link, __FUNCTION__)();
  343. ErrorCodeEnum ErrorCode = m_pEntity->SetRecordCamera(ctx->Req.iCamera);
  344. ctx->Answer(ErrorCode);
  345. }
  346. CServerSessionBase * CSalesRecorderEntity::OnNewSession( const char* pszRemoteEntityName, const char * pszClass )
  347. {
  348. return new SalesRecordServiceSession(this);
  349. }
  350. void CSalesRecorderEntity::OnPreStart( CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext )
  351. {
  352. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  353. pTransactionContext->SendAnswer(Error);
  354. }
  355. ErrorCodeEnum CSalesRecorderEntity::__OnStart( ErrorCodeEnum preOperationError )
  356. {
  357. //MessageBoxA(0,0,0,0);
  358. m_eDeviceType = eStand2sType;
  359. m_bNeedRestart = false;
  360. #ifdef RVC_OS_WIN
  361. m_xIdlePre = m_xKernelPre = m_xUserPre = 0;
  362. if (SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
  363. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Set Process(%d) RealTime Priority Success.", GetCurrentProcessId());
  364. }
  365. #endif // RVC_OS_WIN
  366. m_eDeviceType = RvcGetDeviceType();
  367. if (preOperationError != Error_Succeed) {
  368. return preOperationError;
  369. }
  370. ErrorCodeEnum Error = Error_Succeed;
  371. m_iActiveCamera = CAMERA_TYPE_ENV;
  372. m_iCameraState = 'N';
  373. m_iRecordCamera = CAMERA_TYPE_ENV;
  374. int i = 0;
  375. m_arrListener.Init(4);
  376. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, NULL, false);
  377. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_SALESRECORD_ENTITY_EXCEPTION, NULL, false);
  378. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_SALESRECORD_SECTION_FINISHED, NULL, false);
  379. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_SALESRECORD_WHOLE_FINISHED, NULL, false);
  380. GetFunction()->RegistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA,this);
  381. GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE,this);
  382. CSimpleStringA strValue;
  383. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  384. m_iCameraState = strValue[0];
  385. if (strValue[0] == 'E'){
  386. m_iActiveCamera = CAMERA_TYPE_OPT;
  387. }
  388. else{
  389. m_iActiveCamera = CAMERA_TYPE_ENV;
  390. }
  391. Error = GetFunction()->RegistSysVarEvent("SessionID", this);
  392. if (Error != Error_Succeed) {
  393. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "SessionID");
  394. }
  395. Error = GetFunction()->GetPath("Temp", m_TempDir);
  396. if (Error != Error_Succeed) {
  397. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record temp path failed!");
  398. }
  399. if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != SPLIT_SLASH) {
  400. m_TempDir += SPLIT_SLASH_STR;
  401. }
  402. Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
  403. if (Error != Error_Succeed) {
  404. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record save path failed!");
  405. }
  406. if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
  407. m_RecordSaveDir += SPLIT_SLASH_STR;
  408. }
  409. return Error;
  410. }
  411. void CSalesRecorderEntity::OnStarted()
  412. {
  413. m_eAudioOutQuality = eUltraHD;
  414. m_bIsAudioNsOn = false;
  415. m_iAudioNsPolicy = 2;
  416. m_iAudioChannels = 1;
  417. m_strHttpServerAPI = RVC_UPLOAD_VIDEORECORDING_HTTP_API;
  418. m_iHttpTimeOut = RVC_HTTPTIMEOUT;
  419. m_strHttpServerAddr = NULL;
  420. m_strAppVersion = NULL;
  421. m_strTerminalId = NULL;
  422. m_bPostOn = false;
  423. m_bWholeSection = false;
  424. m_bEncFlag = true;
  425. m_iRemoteRecordType = 0;
  426. CSystemStaticInfo si;
  427. ErrorCodeEnum Error = GetFunction()->GetSystemStaticInfo(si);
  428. if (Error == Error_Succeed) {
  429. m_strAppVersion = si.InstallVersion.ToString();
  430. m_strTerminalId = si.strTerminalID;
  431. }
  432. GetEntityConfig();
  433. #ifndef RVC_OS_WIN
  434. InitSalesRecorder();
  435. #endif
  436. LogEvent(Severity_Middle, LOG_EVT_MOD_SALESRECORDER_STARTED_SUCCESS, "sales recorder entity started successfully.");
  437. }
  438. void CSalesRecorderEntity::OnPreClose( EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext )
  439. {
  440. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  441. pTransactionContext->SendAnswer(Error);
  442. }
  443. ErrorCodeEnum CSalesRecorderEntity::__OnClose( ErrorCodeEnum preOperationError )
  444. {
  445. if (preOperationError != Error_Succeed) {
  446. return preOperationError;
  447. }
  448. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  449. for (int i = 0; i < m_arrListener.GetCount(); ++i) {
  450. spFunction->UnsubscribeLog(m_arrListener[i]);
  451. }
  452. GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
  453. GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
  454. StopRecord();
  455. #ifndef RVC_OS_WIN
  456. ReleaseSalesRecorder();
  457. #endif
  458. return Error_Succeed;
  459. }
  460. void CSalesRecorderEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  461. {
  462. if (Test_ShakeHand == eTestType)
  463. {
  464. if (m_bNeedRestart){
  465. pTransactionContext->SendAnswer(Error_Unexpect);
  466. }
  467. else{
  468. pTransactionContext->SendAnswer(Error_Succeed);
  469. }
  470. }
  471. }
  472. void CSalesRecorderEntity::Debug(record_loglevel elevel, const char* fmt, ...)
  473. {
  474. record_loglevel entitylevel = (m_lowestlevel > m_loglevel) ? m_lowestlevel : m_loglevel;
  475. if (entitylevel <= elevel) {
  476. va_list arg;
  477. va_start(arg, fmt);
  478. int n = vsnprintf(NULL, 0, fmt, arg);
  479. if (n >= MAX_PATH) {
  480. char* buf = (char*)malloc((size_t)(n + 1));
  481. vsnprintf(buf, n + 1, fmt, arg);
  482. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  483. free(buf);
  484. }
  485. else {
  486. char strlog[MAX_PATH] = { 0 };
  487. vsnprintf(strlog, MAX_PATH, fmt, arg);
  488. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  489. }
  490. va_end(arg);
  491. }
  492. }
  493. void CSalesRecorderEntity::vDebug(record_loglevel elevel, const char* str, va_list list)
  494. {
  495. record_loglevel entitylevel = (m_lowestlevel > m_loglevel) ? m_lowestlevel : m_loglevel;
  496. if (entitylevel <= elevel) {
  497. int n = vsnprintf(NULL, 0, str, list);
  498. if (n >= MAX_PATH) {
  499. char* buf = (char*)malloc((size_t)(n + 1));
  500. vsnprintf(buf, n + 1, str, list);
  501. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  502. free(buf);
  503. }
  504. else {
  505. char strlog[MAX_PATH] = { 0 };
  506. vsnprintf(strlog, MAX_PATH, str, list);
  507. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  508. }
  509. }
  510. }
  511. int CSalesRecorderEntity::GetRecordCamera()
  512. {
  513. int iRecordCamera = m_iRecordCamera;
  514. if (1 != m_iRemoteRecordType) {
  515. iRecordCamera = m_iActiveCamera;
  516. }
  517. else {
  518. if (0 == iRecordCamera) {
  519. LogWarn(Severity_Low, Error_Debug, LOG_EVT_SALESRECORD_USE_ENVCAMERA, "remote video record use env camera.");
  520. }
  521. else {
  522. LogWarn(Severity_Low, Error_Debug, LOG_EVT_SALESRECORD_USE_OPTCAMERA, "remote video record use opt camera.");
  523. }
  524. }
  525. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("remote record camera is %d.", iRecordCamera);
  526. return iRecordCamera;
  527. }
  528. int CSalesRecorderEntity::GetCameraState()
  529. {
  530. return m_iCameraState;
  531. }
  532. void CSalesRecorderEntity::OnRecordFailed(eRvcRecordFailedCase eCase, const char* pszMessage, bool bRecordDevFault)
  533. {
  534. #ifdef RVC_OS_WIN
  535. double fCpuUsage = GetSystemCpuUsage();
  536. if (fCpuUsage > 50.0){
  537. LogWarn(Severity_Low, Error_Debug, LOG_EVT_UI_RECORDFAILED_FOR_HIGHCPU, "more than 50 percent.");
  538. }
  539. #endif
  540. if (eCase < eDefault && eCase >= 0){
  541. LogEvent(Severity_Middle, LOG_EVT_UI_RECORDFAILED, record_failed_case_table[eCase]);
  542. }
  543. m_loglevel = RECORD_LOG_INFO;
  544. #ifdef RVC_OS_WIN
  545. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_FAILED, CSimpleStringA::Format("{%s} current memory usage is %d, and cpu usage is %f.", pszMessage ? pszMessage : " ", GetSystemMemoryUsage(), fCpuUsage).GetData());
  546. #else
  547. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_FAILED, CSimpleStringA::Format("%s", pszMessage ? pszMessage : " ").GetData());
  548. #endif
  549. if (eBeginFailed == eCase) {
  550. m_bNeedRestart = true;
  551. RealSelfCheck();
  552. }
  553. m_eBusinessStatus = eInterrupt;
  554. }
  555. void CSalesRecorderEntity::OnRecordEntityExcption()
  556. {
  557. LogEvent(Severity_High,LOG_EVT_SALESRECORD_ENTITY_EXCEPTION,"现场销售双录出现异常,请稍候重录,系统正在恢复中,预计60秒!");
  558. LogWarn(Severity_Low, Error_Debug, LOG_EVT_SALESRECORD_ENTITY_EXCEPTION, "sales record entity exception!");
  559. }
  560. void CSalesRecorderEntity::OnRecordFinished()
  561. {
  562. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_FINISHED, "现场销售双录已完成.");
  563. }
  564. void CSalesRecorderEntity::OnASectionFinished(const char* pszMessage, int iSerialNum, bool bfinished)
  565. {
  566. if (false == bfinished) {
  567. LogEvent(Severity_Middle, LOG_EVT_SALESRECORD_SECTION_FINISHED, pszMessage);
  568. }
  569. else {
  570. LogEvent(Severity_Middle, LOG_EVT_SALESRECORD_WHOLE_FINISHED, pszMessage);
  571. if (m_bEncFlag) {
  572. HandleEncryptVideoRecord(pszMessage);
  573. }
  574. }
  575. }
  576. void CSalesRecorderEntity::OnTimeout(DWORD dwTimerID)
  577. {
  578. }
  579. void CSalesRecorderEntity::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  580. {
  581. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  582. {
  583. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  584. m_iCameraState = pszValue[0];
  585. if (pszValue[0] == 'E')
  586. {
  587. m_iActiveCamera = CAMERA_TYPE_OPT;
  588. }
  589. else
  590. {
  591. m_iActiveCamera = CAMERA_TYPE_ENV;
  592. }
  593. }
  594. else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
  595. {
  596. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  597. if (m_iCameraState == 'N')
  598. {
  599. if (pszValue[0] == 'E')
  600. {
  601. m_iActiveCamera = CAMERA_TYPE_ENV;
  602. }
  603. else if (pszValue[0] == 'O')
  604. {
  605. m_iActiveCamera = CAMERA_TYPE_OPT;
  606. }
  607. }
  608. }
  609. }
  610. void CSalesRecorderEntity::OnLog( const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  611. {
  612. // 响应客户经理销售录像相关的事件
  613. switch (dwUserCode)
  614. {
  615. case LOG_EVT_UI_RETURNMENU:
  616. HandleReturnMenu();
  617. break;
  618. case LOG_EVT_SALESRECORD_ENTITY_EXCEPTION:
  619. HandleSalesRecordEntityException(pszMessage);
  620. break;
  621. case LOG_EVT_SALESRECORD_SECTION_FINISHED:
  622. {
  623. if (m_bEncFlag) {
  624. HandleEncryptVideoRecord(pszMessage);
  625. }
  626. }
  627. break;
  628. case LOG_EVT_SALESRECORD_WHOLE_FINISHED:
  629. {
  630. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("recv LOG_EVT_SALESRECORD_WHOLE_FINISHED EVENT.");
  631. }
  632. break;
  633. default:
  634. break;
  635. }
  636. }
  637. bool CSalesRecorderEntity::GetStandardQualityOnSiteSalesRecorder()
  638. {
  639. bool bRet = false;
  640. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE, REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE,
  641. REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE, REC_COMMON_AUDIO_SALES_SHM_QUEUE, REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE, REC_COMMON_REMOTEAUDIO_SHM_QUEUE);
  642. return bRet;
  643. }
  644. bool CSalesRecorderEntity::GetHighQualityOnSiteSalesRecorder()
  645. {
  646. bool bRet = false;
  647. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE, REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE,
  648. REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE, REC_COMMON_AUDIO_SALES_SHM_QUEUE, REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE, REC_COMMON_REMOTEAUDIO_SHM_QUEUE);
  649. return bRet;
  650. }
  651. void CSalesRecorderEntity::StartOnSiteSalesRecord(const int fps, const char* videofilename, int videoquality, SubtitleParam* subtitleParam, bool bWholeSection, bool bSessionManage, eRvcRecordType eRecordType)
  652. {
  653. #ifdef RVC_OS_WIN
  654. bool bRet = false;
  655. bRet = GetStandardQualityOnSiteSalesRecorder();
  656. if (false == bRet){
  657. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get onsite sales recorder failed.");
  658. return;
  659. }
  660. #endif
  661. Rvc_RecordAudioParam_t tAudioParams;
  662. tAudioParams.eRecordType = eRecordType;
  663. tAudioParams.eOutPutType = m_eAudioOutQuality;
  664. tAudioParams.bIsNsOn = m_bIsAudioNsOn;
  665. tAudioParams.iNsPolicy = m_iAudioNsPolicy;
  666. tAudioParams.iAudioChannels = m_iAudioChannels;
  667. tAudioParams.bMuteAudioMode = false;
  668. if (eStand2Agent == eRecordType) {
  669. tAudioParams.eOutPutType = eLowDefinition;
  670. tAudioParams.bIsNsOn = false;
  671. }
  672. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init lib video record success! and record type is %s and output audio quality is %s, audio noise suppression flag is %s. noise suppression policy is %d.", record_type_table[eRecordType], audio_quality_type_table[m_eAudioOutQuality], tAudioParams.bIsNsOn ? "true" : "false", tAudioParams.iNsPolicy);
  673. if (m_pRecorder->StartVideoRecord(fps, videoquality, eMP4, &tAudioParams, subtitleParam, bWholeSection, false, m_TempDir.GetData(),
  674. m_TempDir.GetLength(), videofilename, strlen(videofilename)))
  675. {
  676. m_eBusinessStatus = eSuccess;
  677. m_bStarted = true;
  678. }
  679. else {
  680. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Start VideoRecord failed!");
  681. m_eBusinessStatus = eFailed;
  682. if (m_bPostOn) {
  683. PostSalesRecordInfos();
  684. }
  685. }
  686. }
  687. void CSalesRecorderEntity::StartRecord(const char * pszVideoName, int videoquality, SubtitleParam *subtitleParam /* = NULL */, bool bWholeSection /* = false */, bool bSessionManage /* = false */, eRvcRecordType eRecordType /* = eSingleSide */)
  688. {
  689. StartOnSiteSalesRecord(10, pszVideoName, videoquality, subtitleParam, bWholeSection, bSessionManage, eRecordType);
  690. }
  691. ErrorCodeEnum CSalesRecorderEntity::StopRecord()
  692. {
  693. ErrorCodeEnum eCode = Error_Succeed;
  694. if (m_bStarted) {
  695. m_pRecorder->StopVideoRecord();
  696. #ifdef RVC_OS_WIN
  697. delete m_pRecorder;
  698. m_pRecorder = NULL;
  699. #endif
  700. m_bStarted = false;
  701. }
  702. else{
  703. eCode = Error_InvalidState;
  704. }
  705. return eCode;
  706. }
  707. void CSalesRecorderEntity::DeleteAllVideo( const char * pszVideoName)
  708. {
  709. if(!m_bStarted)
  710. {
  711. CSimpleStringA strPath;
  712. ErrorCodeEnum Error = GetFunction()->GetPath("Temp", strPath);
  713. if (Error == Error_Succeed) {
  714. if (strPath.GetLength() > 0 && strPath[strPath.GetLength()-1] != SPLIT_SLASH) {
  715. strPath += SPLIT_SLASH_STR;
  716. }
  717. uint32_t uVideoCount = 0;
  718. CSimpleStringA strFindFileName = CSimpleStringA::Format("S_%s*.%s", pszVideoName, RECORD_MP4_SUFFIX);
  719. bool bRet = FindMatchedFile(strPath.GetData(), strFindFileName.GetData(), uVideoCount);
  720. if(bRet)
  721. {
  722. strFindFileName = strFindFileName.SubString(0,strFindFileName.GetLength()-5);
  723. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("succeed to get sales record count while delete video!");
  724. CSimpleStringA fileName("");
  725. bool bDeleteSucc = true;
  726. for(int i = 0; i != uVideoCount; ++i)
  727. {
  728. fileName = CSimpleStringA::Format("%s%s_%d.%s", strPath.GetData(), strFindFileName.GetData(), i, RECORD_MP4_SUFFIX);
  729. bRet = RvcDeleteFile(fileName.GetData());
  730. if(!bRet) {
  731. bDeleteSucc = false;
  732. #ifdef RVC_OS_WIN
  733. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Error Code %lu while delete %s.", GetLastError(), fileName.GetData());
  734. #else
  735. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s(%d) while delete %s.", strerror(errno), errno, fileName.GetData());
  736. #endif // RVC_OS_WIN
  737. }
  738. }
  739. if (!bDeleteSucc) {
  740. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("failed to delete sales video!");
  741. }
  742. }
  743. }
  744. }
  745. }
  746. ErrorCodeEnum CSalesRecorderEntity::DeleteVideo( const char *videofilename)
  747. {
  748. ErrorCodeEnum ErrorCode = Error_Succeed;
  749. if(!m_bStarted)
  750. {
  751. CSimpleStringA strPath;
  752. ErrorCodeEnum Error = GetFunction()->GetPath("Temp", strPath);
  753. if (Error == Error_Succeed) {
  754. if (strPath.GetLength() > 0 && strPath[strPath.GetLength()-1] != SPLIT_SLASH) {
  755. strPath += SPLIT_SLASH_STR;
  756. }
  757. CSimpleStringA strFindFileName = CSimpleStringA::Format("%s*.%s", videofilename, RECORD_MP4_SUFFIX);
  758. uint32_t uVideoCount = 0;
  759. bool bRet = FindMatchedFile(strPath.GetData(), strFindFileName.GetData(), uVideoCount);
  760. if(bRet)
  761. {
  762. CSimpleStringA fileName;
  763. bool bDeleteSucc = true;
  764. for(int i = 0; i != uVideoCount; ++i)
  765. {
  766. fileName = CSimpleStringA::Format("%s%s_%d.%s", strPath.GetData(), videofilename, i, RECORD_MP4_SUFFIX);
  767. bRet = RvcDeleteFile(fileName.GetData());
  768. if(!bRet) {
  769. bDeleteSucc = false;
  770. #ifdef RVC_OS_WIN
  771. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Error Code %lu while delete %s ", GetLastError(), fileName.GetData());
  772. #else
  773. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s(%d) while delete %s ", strerror(errno), errno, fileName.GetData());
  774. #endif
  775. }
  776. }
  777. if (!bDeleteSucc) {
  778. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("delete videos failed!");
  779. }
  780. }
  781. else
  782. {
  783. ErrorCode = Error_NotExist;
  784. }
  785. }
  786. }
  787. else {
  788. ErrorCode = Error_NotImpl;
  789. }
  790. if (eInterrupt != m_eBusinessStatus) {
  791. m_eBusinessStatus = eCancel;
  792. }
  793. if (m_bPostOn) {
  794. PostSalesRecordInfos();
  795. }
  796. return ErrorCode;
  797. }
  798. /*
  799. SaveVideo包含两步,找到录像和移动录像
  800. Error_Succeed 提交上传成功
  801. Error_NotExist not exist
  802. Error_NotImpl 方法执行失败
  803. */
  804. ErrorCodeEnum CSalesRecorderEntity::SaveVideo( const char * videofilename)
  805. {
  806. ErrorCodeEnum ErrorCode = Error_Succeed;
  807. if (NULL == videofilename || 0 == strlen(videofilename)) {
  808. return Error_Param;
  809. }
  810. if(!m_bStarted)
  811. {
  812. CSimpleStringA sourPath;
  813. ErrorCodeEnum Error = GetFunction()->GetPath("Temp", sourPath);
  814. if(Error == Error_Succeed)
  815. {
  816. if (sourPath.GetLength() > 0 && sourPath[sourPath.GetLength()-1] != SPLIT_SLASH) {
  817. sourPath += SPLIT_SLASH_STR;
  818. }
  819. uint32_t uVideoCount = 0;
  820. CSimpleStringA strFindFileName = CSimpleStringA::Format("S_%s*.%s", videofilename, RECORD_MP4_SUFFIX);
  821. bool bRet = FindMatchedFile(sourPath.GetData(), strFindFileName.GetData(), uVideoCount);
  822. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("FindMatchedFile count is %d.", uVideoCount);
  823. if(bRet)
  824. {
  825. strFindFileName = strFindFileName.SubString(0,strFindFileName.GetLength()-5);
  826. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("strFindFileName is %s.", strFindFileName.GetData());
  827. CSimpleStringA destPath;
  828. Error = GetFunction()->GetPath("UploadVideo", destPath);
  829. if(Error == Error_Succeed)
  830. {
  831. if (destPath.GetLength() > 0 && destPath[destPath.GetLength()-1] != SPLIT_SLASH) {
  832. destPath += SPLIT_SLASH_STR;
  833. }
  834. CSimpleStringA sourFileName(""), destFileName("");
  835. bool bMoveSucc = false;
  836. for(int i = 0; i != uVideoCount; ++i)
  837. {
  838. sourFileName = CSimpleStringA::Format("%s%s_%d.%s", sourPath.GetData(), strFindFileName.GetData(), i, RECORD_MP4_SUFFIX);
  839. destFileName = CSimpleStringA::Format("%s%s_%d.%s", destPath.GetData(), strFindFileName.GetData(), i, RECORD_MP4_SUFFIX);
  840. unsigned long ufilesize = GetFileSize(sourFileName.GetData());
  841. char strhash[MAX_PATH] = { 0 };
  842. get_file_sm3digest(strhash, MAX_PATH, sourFileName.GetData());
  843. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_FILEINFO, CSimpleStringA::Format("%s file size is %u byte,sm3 digest is %s.", sourFileName.GetData(), ufilesize, strhash).GetData());
  844. bRet = rvcMoveFile(sourFileName.GetData(), destFileName.GetData());
  845. if (!bRet) {
  846. ErrorCode = Error_NotImpl;
  847. #ifdef RVC_OS_WIN
  848. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORD_SAVED_FAILED, CSimpleStringA::Format("Error Code %lu while move %s.", GetLastError(), sourFileName.GetData()).GetData());
  849. if (183 == GetLastError()) {
  850. ErrorCode = Error_InvalidState;
  851. }
  852. #else
  853. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORD_SAVED_FAILED, CSimpleStringA::Format("%s(%d) while move %s.", strerror(errno), errno, sourFileName.GetData()).GetData());
  854. #endif // RVC_OS_WIN
  855. }
  856. else {
  857. bMoveSucc = true;
  858. CSimpleStringA strVideoName = CSimpleStringA::Format("%s_%d.%s", strFindFileName.GetData(), i, RECORD_MP4_SUFFIX);
  859. if (m_bPostOn) {
  860. AddToSalesRecordList(destFileName.GetData(), strVideoName.GetData(), ufilesize);
  861. }
  862. }
  863. if (RVC_MIN_FILESIZE > ufilesize){
  864. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORD_INVALID_FILE, CSimpleStringA::Format("%s file size is %u.", sourFileName.GetData(), ufilesize).GetData());
  865. ErrorCode = Error_Failed;
  866. }
  867. }
  868. if (bMoveSucc) {
  869. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_SAVED_SUCCESS, CSimpleStringA::Format("succeed to save sales video, move it from %s to %s.", sourFileName.GetData(), destFileName.GetData()).GetData());
  870. if (m_bPostOn) {
  871. if (Error_Succeed != PostSalesRecordInfos()) {
  872. ErrorCode = Error_Failed;
  873. }
  874. }
  875. }
  876. }
  877. }
  878. else{
  879. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORD_SAVED_FAILED, "sales videos not exist or have been deleted");
  880. ErrorCode = Error_NotExist;
  881. }
  882. }
  883. }
  884. else{
  885. ErrorCode = Error_InvalidState;
  886. }
  887. return ErrorCode;
  888. }
  889. CSimpleStringA CSalesRecorderEntity::GetTerminalStage()
  890. {
  891. CSmartPointer<IEntityFunction> Func = GetFunction();
  892. CSimpleStringA strValue = "";
  893. Func->GetSysVar("TerminalStage", strValue);
  894. return strValue;
  895. }
  896. ErrorCodeEnum CSalesRecorderEntity::HandleStartRecord(const char *pszMessage, const bool bRemoteRecord)
  897. {
  898. ErrorCodeEnum Error = Error_Succeed;
  899. eRvcRecordType eRecordType = eSingleSide;
  900. if (NULL == pszMessage){
  901. return Error_Param;
  902. }
  903. size_t ulen = strlen(pszMessage);
  904. char *tmp = new char[ulen+1];
  905. memset(tmp, 0, ulen + 1);
  906. memcpy(tmp, pszMessage, ulen);
  907. char* result[16] = { 0 };
  908. #ifdef RVC_OS_WIN
  909. auto arr1 = CSimpleStringA2W(tmp).Split('@');
  910. auto arr2 = CAutoArray<CSimpleStringA>(arr1.GetCount());
  911. for (int i = 0; i < arr1.GetCount(); ++i){
  912. arr2[i] = CSimpleStringW2A(arr1[i]);
  913. result[i] = const_cast<char*>(arr2[i].GetData());
  914. }
  915. _snprintf(m_SalesVideoName, MAX_PATH, "%s", result[4]); //录像名:录像类型标志_录像流水号(如S_C13213EF)
  916. #else
  917. CStringSplit(tmp, result, "@");
  918. memset(m_SalesVideoName, 0, MAX_PATH);
  919. if (result[4]) {
  920. snprintf(m_SalesVideoName, MAX_PATH, "%s", result[4]); //录像名:录像类型标志_录像流水号(如S_C13213EF)
  921. }
  922. #endif
  923. int iPostionArr[4][2] = { 0 };
  924. for (int i = 0; i < 4; i++){
  925. sscanf(result[i], "%d|%d", &iPostionArr[i][0], &iPostionArr[i][1]);
  926. }
  927. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("local video frame size is (%d,%d).", iPostionArr[2][0], iPostionArr[3][0]);
  928. if (iPostionArr[2][0] >= iPostionArr[3][0]) {
  929. m_iRecordCamera = CAMERA_TYPE_ENV;
  930. }
  931. else {
  932. m_iRecordCamera = CAMERA_TYPE_OPT;
  933. }
  934. RecordSubTitle subTitle;
  935. memset(&subTitle, 0, sizeof(RecordSubTitle));
  936. if (result[5]) {
  937. CSimpleStringA strCardNo = DecryptString(result[5]);
  938. if (sizeof(subTitle.CustCardNo) > strCardNo.GetLength()) {
  939. _snprintf(subTitle.CustCardNo, sizeof(subTitle.CustCardNo), "%s", strCardNo.GetData());
  940. }
  941. else {
  942. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CustCardNo.");
  943. }
  944. }
  945. char* pStr = result[6];
  946. if (pStr) {
  947. CSimpleStringW szCustName = CSimpleStringA2W(pStr);
  948. if (szCustName.GetLength() <= 4) {
  949. strcpy(subTitle.CustName, pStr);
  950. }
  951. else {
  952. CSimpleStringW aa = szCustName.SubString(0, 4);
  953. _snprintf(subTitle.CustName, 32, "%s...", CSimpleStringW2A(aa).GetData());
  954. }
  955. }
  956. pStr = result[7];
  957. if (pStr) {
  958. int nProductCodeLen = strlen(pStr);
  959. if (nProductCodeLen <= 10) {
  960. strcpy(subTitle.ProductCode, pStr);
  961. }
  962. else {
  963. char aa[5] = { 0 }, bb[5] = { 0 };
  964. strncpy(aa, pStr, 4);
  965. strncpy(bb, pStr + nProductCodeLen - 4, 4);
  966. _snprintf(subTitle.ProductCode, 32, "%s...%s", aa, bb);
  967. }
  968. }
  969. pStr = result[8];
  970. if (pStr) {
  971. CSimpleStringW szProductName = CSimpleStringA2W(pStr);
  972. if (szProductName.GetLength() <= 5) {
  973. strcpy(subTitle.ProductName, pStr);
  974. }
  975. else {
  976. CSimpleStringW aa = szProductName.SubString(0, 3);
  977. CSimpleStringW bb = szProductName.SubString(szProductName.GetLength() - 2, 2);
  978. _snprintf(subTitle.ProductName, 128, "%s...%s", CSimpleStringW2A(aa).GetData(), CSimpleStringW2A(bb).GetData());
  979. }
  980. }
  981. if (result[9]) {
  982. strcpy(subTitle.SapID, result[9]);
  983. }
  984. else {
  985. subTitle.SapID[0] = '\0';
  986. }
  987. if (result[10]) {
  988. pStr = result[10];
  989. CSimpleStringW szCustManagerName = CSimpleStringA2W(pStr);
  990. if (szCustManagerName.GetLength() <= 4) {
  991. strcpy(subTitle.CustManagerName, result[10]);
  992. }
  993. else {
  994. CSimpleStringW aa = szCustManagerName.SubString(0,4);
  995. _snprintf(subTitle.CustManagerName, 32,"%s...",CSimpleStringW2A(aa).GetData());
  996. }
  997. } else {
  998. subTitle.CustManagerName[0] = '\0';
  999. }
  1000. delete[] tmp;
  1001. tmp = NULL;
  1002. SubtitleParam subtitleParam;
  1003. memset(&subtitleParam, 0, sizeof(SubtitleParam));
  1004. subtitleParam.bSubtitle = true;
  1005. subtitleParam.bSubtitleSection = true;
  1006. subtitleParam.topSubtitleData[0] = '\0';
  1007. #ifdef RVC_OS_WIN
  1008. if (strlen(subTitle.SapID)>0&&strlen(subTitle.CustManagerName)>0){
  1009. _snprintf(subtitleParam.bottomSubtitleData1, MAX_PATH, "%s %s %s", subTitle.CustCardNo, subTitle.CustName, subTitle.ProductCode);
  1010. _snprintf(subtitleParam.bottomSubtitleData2, MAX_PATH, "%s %s %s", subTitle.ProductName, subTitle.SapID, subTitle.CustManagerName);
  1011. }
  1012. else{
  1013. _snprintf(subtitleParam.bottomSubtitleData1, MAX_PATH, "%s %s", subTitle.CustCardNo, subTitle.CustName);
  1014. _snprintf(subtitleParam.bottomSubtitleData2, MAX_PATH, "%s %s", subTitle.ProductCode, subTitle.ProductName);
  1015. }
  1016. #else
  1017. if (strlen(subTitle.SapID)>0&&strlen(subTitle.CustManagerName)>0){
  1018. swprintf(subtitleParam.bottomSubtitleData1, MAX_PATH, L"%s %s %s", subTitle.CustCardNo, subTitle.CustName, subTitle.ProductCode);
  1019. swprintf(subtitleParam.bottomSubtitleData2, MAX_PATH, L"%s %s %s", subTitle.ProductName, subTitle.SapID, subTitle.CustManagerName);
  1020. }
  1021. else{
  1022. swprintf(subtitleParam.bottomSubtitleData1, MAX_PATH, L"%s %s", subTitle.CustCardNo, subTitle.CustName);
  1023. swprintf(subtitleParam.bottomSubtitleData2, MAX_PATH, L"%s %s", subTitle.ProductCode, subTitle.ProductName);
  1024. }
  1025. #endif
  1026. if (bRemoteRecord){
  1027. if (eStand2sType == m_eDeviceType || eStand1SPlusType == m_eDeviceType) { //如果是大机
  1028. eRecordType = eStand2Agent; //可视柜台大机的双向录像
  1029. }
  1030. }
  1031. StartRecord(m_SalesVideoName, 90, &subtitleParam, m_bWholeSection, false, eRecordType);
  1032. LogWarn(Severity_Low, Error_Debug, LOG_EVT_START_REMOTERECORD, CSimpleStringA::Format("start remote record %s.", m_SalesVideoName).GetData());
  1033. return Error;
  1034. }
  1035. ErrorCodeEnum CSalesRecorderEntity::HandleStopRecord(const char *pszMessage)
  1036. {
  1037. ErrorCodeEnum ErrorCode = StopRecord();
  1038. return ErrorCode;
  1039. }
  1040. ErrorCodeEnum CSalesRecorderEntity::HandleSaveVideo()
  1041. {
  1042. ErrorCodeEnum ErrorCode = Error_Succeed;
  1043. // 将视频从tmp移动到uploadvideo
  1044. ErrorCode = SaveVideo(m_SalesVideoName+2);
  1045. ZeroMemory(m_SalesVideoName,sizeof(m_SalesVideoName));
  1046. return ErrorCode;
  1047. }
  1048. ErrorCodeEnum CSalesRecorderEntity::HandleDeleteVideo()
  1049. {
  1050. ErrorCodeEnum ErrorCode = Error_Succeed;
  1051. // 删除tmp中的视频
  1052. ErrorCode = DeleteVideo(m_SalesVideoName);
  1053. ZeroMemory(m_SalesVideoName,sizeof(m_SalesVideoName));
  1054. return ErrorCode;
  1055. }
  1056. void CSalesRecorderEntity::HandleReturnMenu()
  1057. {
  1058. CSimpleStringA strValue;
  1059. GetFunction()->GetSysVar("DesktopType", strValue);
  1060. if (strValue != CSimpleStringA("U"))
  1061. {
  1062. if (m_bStarted) // 如果正在进行客户经理录像
  1063. {
  1064. // 停止录像
  1065. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("stop record and return menu!");
  1066. StopRecord();
  1067. }
  1068. // 删除tmp中的视频
  1069. if (strlen(m_SalesVideoName) > 0)
  1070. {
  1071. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("delete sales record!");
  1072. DeleteAllVideo(m_SalesVideoName+2);
  1073. }
  1074. ZeroMemory(m_SalesVideoName,sizeof(m_SalesVideoName));
  1075. }
  1076. }
  1077. #ifdef RVC_OS_WIN
  1078. ErrorCodeEnum CSalesRecorderEntity::HandleVideoAppendWatermark(const char* pszVideoName, const char* pszWaterMark)
  1079. {
  1080. ErrorCodeEnum Error = Error_Failed;
  1081. if (NULL == pszWaterMark) {
  1082. return Error_Param;
  1083. }
  1084. if (m_bStarted && (NULL != m_pRecorder)) {
  1085. if (m_pRecorder->SetRightVideoWaterMark(pszWaterMark)) {
  1086. Error = Error_Succeed;
  1087. }
  1088. }
  1089. return Error;
  1090. }
  1091. #else
  1092. ErrorCodeEnum CSalesRecorderEntity::HandleVideoAppendWatermark(const char* pszVideoName, const wchar_t* pszWaterMark)
  1093. {
  1094. ErrorCodeEnum Error = Error_Failed;
  1095. if (NULL == pszWaterMark){
  1096. return Error_Param;
  1097. }
  1098. if (m_bStarted && (NULL != m_pRecorder)){
  1099. if (m_pRecorder->SetRightVideoWaterMark(pszWaterMark)) {
  1100. Error = Error_Succeed;
  1101. }
  1102. }
  1103. return Error;
  1104. }
  1105. #endif
  1106. ErrorCodeEnum CSalesRecorderEntity::HandleSalesRecordEntityException(const char* pszMessage)
  1107. {
  1108. // 通知到业务中台
  1109. SalesRecordException evt;
  1110. evt.failedmsg = CSimpleStringA2W(pszMessage==NULL?"现场销售双录过程出现异常!":pszMessage);
  1111. SpSendBroadcast(GetFunction(), SP_MSG_OF(SalesRecordException), SP_MSG_SIG_OF(SalesRecordException), evt);
  1112. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[dbg] SalesRecord entity exception broadcast sent!");
  1113. //停止录像
  1114. StopRecord();
  1115. m_bNeedRestart = true;
  1116. //重启实体
  1117. RealSelfCheck();
  1118. return Error_Succeed;
  1119. }
  1120. #ifdef RVC_OS_LINUX
  1121. bool CSalesRecorderEntity::InitSalesRecorder()
  1122. {
  1123. bool bRet = false;
  1124. bRet = GetStandardQualityOnSiteSalesRecorder();
  1125. return bRet;
  1126. }
  1127. bool CSalesRecorderEntity::ReleaseSalesRecorder()
  1128. {
  1129. if (m_pRecorder) {
  1130. delete m_pRecorder;
  1131. m_pRecorder = NULL;
  1132. }
  1133. return true;
  1134. }
  1135. #endif
  1136. ErrorCodeEnum CSalesRecorderEntity::RealSelfCheck()
  1137. {
  1138. ErrorCodeEnum Error = Error_Succeed;
  1139. HealthManagerService_ClientBase* pHMClient = new HealthManagerService_ClientBase(this);
  1140. Error = pHMClient->Connect();
  1141. if (Error != Error_Succeed){
  1142. pHMClient->SafeDelete();
  1143. pHMClient = NULL;
  1144. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pHMClient connect fail!");
  1145. }
  1146. if (pHMClient)
  1147. {
  1148. HealthManagerService_RealCheck_Req req;
  1149. req.name = GetEntityName();
  1150. HealthManagerService_RealCheck_Ans ans;
  1151. DWORD Timeout = 500;
  1152. Error = pHMClient->RealCheck(req,ans,Timeout);
  1153. if (Error!=Error_Succeed){
  1154. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RealSelfcheck fail!");
  1155. }
  1156. }
  1157. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_ENTITY_RESTART, "sales record entity self restart.");
  1158. return Error;
  1159. }
  1160. #ifdef RVC_OS_WIN
  1161. bool IsMatchedVideo(WIN32_FIND_DATA fileData)
  1162. {
  1163. bool bRet = false;
  1164. if ('S' == fileData.cFileName[0] || 'W' == fileData.cFileName[0]){
  1165. SYSTEMTIME sysTime;
  1166. FileTimeToSystemTime(&fileData.ftCreationTime, &sysTime);
  1167. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("File %s create data info is %d %d %d.", fileData.cFileName, sysTime.wYear, sysTime.wMonth, sysTime.wDay);
  1168. if ((2020 == sysTime.wYear) && (sysTime.wMonth == 1)){
  1169. if (sysTime.wDay >= 3 && sysTime.wDay <= 10){
  1170. bRet = true;
  1171. }
  1172. }
  1173. }
  1174. return bRet;
  1175. }
  1176. void CSalesRecorderEntity::UploadTempPathVideos()
  1177. {
  1178. CSimpleStringA sourPath;
  1179. CSimpleStringA destPath;
  1180. ErrorCodeEnum Error = GetFunction()->GetPath("Temp", sourPath);
  1181. Error = GetFunction()->GetPath("UploadVideo", destPath);
  1182. if (sourPath.GetLength() > 0 && sourPath[sourPath.GetLength()-1] != SPLIT_SLASH) {
  1183. sourPath += SPLIT_SLASH_STR;
  1184. }
  1185. if (destPath.GetLength() > 0 && destPath[destPath.GetLength()-1] != SPLIT_SLASH) {
  1186. destPath += SPLIT_SLASH_STR;
  1187. }
  1188. if(Error == Error_Succeed)
  1189. {
  1190. char srcFilePath[MAX_PATH]={0};
  1191. WIN32_FIND_DATA FindFileData;
  1192. HANDLE hFind;
  1193. BOOL fFinished = FALSE;
  1194. sprintf_s(srcFilePath, MAX_PATH, "%s*.%s", sourPath, RECORD_MP4_SUFFIX);
  1195. hFind = FindFirstFile(srcFilePath, &FindFileData);
  1196. if (INVALID_HANDLE_VALUE != hFind)
  1197. {
  1198. while (!fFinished)
  1199. {
  1200. if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
  1201. {
  1202. continue;
  1203. }
  1204. if (IsMatchedVideo(FindFileData)){
  1205. CSimpleStringA sourFileName = CSimpleStringA::Format("%s%s", (LPCSTR)sourPath, FindFileData.cFileName);
  1206. CSimpleStringA destFileName = CSimpleStringA::Format("%sBAK_%s", (LPCSTR)destPath, FindFileData.cFileName);
  1207. if(!MoveFile((LPCSTR)sourFileName, (LPCSTR)destFileName)) {
  1208. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Error Code %lu while move %s ", GetLastError(), (LPCSTR)sourFileName);
  1209. }
  1210. }
  1211. else{
  1212. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File (%s) is not matched.", FindFileData.cFileName);
  1213. }
  1214. if (!FindNextFile(hFind, &FindFileData))
  1215. {
  1216. if (GetLastError() == ERROR_NO_MORE_FILES)
  1217. {
  1218. fFinished = TRUE;
  1219. }
  1220. else
  1221. {
  1222. break;
  1223. }
  1224. }
  1225. }
  1226. FindClose(hFind);
  1227. }
  1228. }
  1229. }
  1230. int CSalesRecorderEntity::GetSystemMemoryUsage()
  1231. {
  1232. // Use to convert bytes to KB
  1233. #define DIV 1024
  1234. MEMORYSTATUSEX statex;
  1235. statex.dwLength = sizeof (statex);
  1236. GlobalMemoryStatusEx (&statex);
  1237. return statex.dwMemoryLoad;
  1238. }
  1239. double CSalesRecorderEntity::GetSystemCpuUsage()
  1240. {
  1241. double ratio = 0;
  1242. #define _WIN32_WINNT 0x0601
  1243. FILETIME idleTime,kernelTime,userTime;
  1244. BOOL ret = GetSystemTimes(&idleTime,&kernelTime,&userTime);
  1245. if (ret == 0)
  1246. {
  1247. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("GetSystemCPUStatus.GetSystemTimes failed(%d).",GetLastError());
  1248. return 80.0;
  1249. }
  1250. __int64 xIdle,xKernel,xUser;
  1251. xIdle = idleTime.dwHighDateTime;
  1252. xIdle <<= 32;
  1253. xIdle |= idleTime.dwLowDateTime;
  1254. xKernel = kernelTime.dwHighDateTime;
  1255. xKernel <<= 32;
  1256. xKernel |= kernelTime.dwLowDateTime;
  1257. xUser = userTime.dwHighDateTime;
  1258. xUser <<= 32;
  1259. xUser |= userTime.dwLowDateTime;
  1260. if (m_xIdlePre != 0)
  1261. {
  1262. __int64 xI,xK,xU;
  1263. xI = xIdle - m_xIdlePre;
  1264. xK = xKernel - m_xKernelPre;
  1265. xU = xUser - m_xUserPre;
  1266. if ((xK +xU) != 0)
  1267. ratio = (xK - xI + xU) * 100 / (xK + xU);
  1268. }
  1269. m_xIdlePre = xIdle;
  1270. m_xKernelPre = xKernel;
  1271. m_xUserPre = xUser;
  1272. return ratio;
  1273. }
  1274. #else
  1275. /*
  1276. **time_t转SYSTEMTIME
  1277. */
  1278. SYSTEMTIME Time_tToSystemTime(time_t t)
  1279. {
  1280. tm temptm = *localtime(&t);
  1281. SYSTEMTIME st = { 1900 + temptm.tm_year,
  1282. 1 + temptm.tm_mon,
  1283. temptm.tm_wday,
  1284. temptm.tm_mday,
  1285. temptm.tm_hour,
  1286. temptm.tm_min,
  1287. temptm.tm_sec,
  1288. 0 };
  1289. return st;
  1290. }
  1291. bool IsMatchedVideo(LPCTSTR path, struct dirent* file)
  1292. {
  1293. bool bRet = false;
  1294. if ((file->d_reclen > 0) && ('S' == file->d_name[0] || 'W' == file->d_name[0])){
  1295. struct stat fileInfo;
  1296. char ttp[256];
  1297. bzero(ttp, sizeof(ttp));
  1298. strcat(ttp, path);
  1299. if (strcmp(ttp, "/") != 0)strcat(ttp, "/");//如果是根目录不需要加
  1300. strcat(ttp, file->d_name);
  1301. if (stat(ttp, &fileInfo) == 0) {
  1302. SYSTEMTIME sysTime;
  1303. sysTime = Time_tToSystemTime(fileInfo.st_mtime);
  1304. if ((2020 == sysTime.wYear) && (sysTime.wMonth == 1)) {
  1305. if (sysTime.wDay >= 3 && sysTime.wDay <= 10) {
  1306. bRet = true;
  1307. }
  1308. }
  1309. }
  1310. }
  1311. return bRet;
  1312. }
  1313. void CSalesRecorderEntity::UploadTempPathVideos() {
  1314. CSimpleStringA sourPath;
  1315. CSimpleStringA destPath;
  1316. ErrorCodeEnum Error = GetFunction()->GetPath("Temp", sourPath);
  1317. Error = GetFunction()->GetPath("UploadVideo", destPath);
  1318. if (sourPath.GetLength() > 0 && sourPath[sourPath.GetLength() - 1] != SPLIT_SLASH) {
  1319. sourPath += SPLIT_SLASH_STR;
  1320. }
  1321. if (destPath.GetLength() > 0 && destPath[destPath.GetLength() - 1] != SPLIT_SLASH) {
  1322. destPath += SPLIT_SLASH_STR;
  1323. }
  1324. if (Error == Error_Succeed)
  1325. {
  1326. char srcFilePath[MAX_PATH] = { 0 };
  1327. DIR* pDir = NULL;
  1328. struct dirent* pFile = NULL;
  1329. snprintf(srcFilePath, MAX_PATH, "%s*.%s", sourPath, RECORD_MP4_SUFFIX);
  1330. pDir = opendir(sourPath);
  1331. if (pDir == NULL) return ;
  1332. while ((pFile = readdir(pDir)) != NULL) {
  1333. if (pFile->d_type & DT_DIR) {
  1334. continue;
  1335. }
  1336. else {
  1337. if (IsMatchedVideo((LPCSTR)sourPath, pFile)) {
  1338. CSimpleStringA sourFileName = CSimpleStringA::Format("%s%s", (LPCSTR)sourPath, pFile->d_name);
  1339. CSimpleStringA destFileName = CSimpleStringA::Format("%sBAK_%s", (LPCSTR)destPath, pFile->d_name);
  1340. if (rename(sourFileName.GetData(), destFileName.GetData())) {
  1341. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Error Code %lu while move %s.", errno, sourFileName.GetData());
  1342. }
  1343. }
  1344. else {
  1345. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File (%s) is not matched.", pFile->d_name);
  1346. }
  1347. }
  1348. }
  1349. closedir(pDir);
  1350. }
  1351. }
  1352. #endif
  1353. ErrorCodeEnum CSalesRecorderEntity::SetRecordAudioQuality(int iAudioQuality)
  1354. {
  1355. ErrorCodeEnum eRet = Error_Succeed;
  1356. switch (iAudioQuality)
  1357. {
  1358. case 1:
  1359. m_eAudioOutQuality = eLowDefinition;
  1360. break;
  1361. case 2:
  1362. m_eAudioOutQuality = eStandardDefinition;
  1363. break;
  1364. case 3:
  1365. m_eAudioOutQuality = eHighDefinition;
  1366. break;
  1367. case 4:
  1368. m_eAudioOutQuality = eUltraHD;
  1369. break;
  1370. default:
  1371. m_eAudioOutQuality = eUltraHD;
  1372. break;
  1373. }
  1374. return eRet;
  1375. }
  1376. ErrorCodeEnum CSalesRecorderEntity::SetRecordAudioNsPolicy(int iNsPolicy)
  1377. {
  1378. ErrorCodeEnum eRet = Error_Succeed;
  1379. if (1 == iNsPolicy || 3 == iNsPolicy){
  1380. m_iAudioNsPolicy = iNsPolicy;
  1381. }
  1382. else{
  1383. m_iAudioNsPolicy = 2;
  1384. }
  1385. return eRet;
  1386. }
  1387. ErrorCodeEnum CSalesRecorderEntity::SetRecordAudioChannles(int iAudioChannles)
  1388. {
  1389. ErrorCodeEnum eRet = Error_Succeed;
  1390. m_iAudioChannels = 2;
  1391. return eRet;
  1392. }
  1393. DeviceTypeEnum CSalesRecorderEntity::RvcGetDeviceType()
  1394. {
  1395. DeviceTypeEnum eType = eStand2sType;
  1396. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1397. CSystemStaticInfo stStaticinfo;
  1398. spFunction->GetSystemStaticInfo(stStaticinfo);
  1399. m_terminalNo = stStaticinfo.strTerminalID;
  1400. if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
  1401. eType = eStand1SPlusType;
  1402. }
  1403. else if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
  1404. eType = eCardStore;
  1405. }
  1406. else {
  1407. eType = eStand2sType;
  1408. }
  1409. if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
  1410. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  1411. }
  1412. return eType;
  1413. }
  1414. ErrorCodeEnum CSalesRecorderEntity::AddToSalesRecordList(const char* videopath, const char* strfilename, unsigned long ufilesize)
  1415. {
  1416. ErrorCodeEnum Error = Error_Failed;
  1417. if (NULL == videopath || NULL == strfilename) {
  1418. return Error;
  1419. }
  1420. record_item_t* item = new record_item_t();
  1421. item->file_path = videopath;
  1422. item->file_length = (int)ufilesize;
  1423. item->file_name = strfilename;
  1424. m_vRecordList.push_back(item);
  1425. Error = Error_Succeed;
  1426. return Error;
  1427. }
  1428. ErrorCodeEnum CSalesRecorderEntity::PostSalesRecordInfos()
  1429. {
  1430. ErrorCodeEnum Error = Error_Failed;
  1431. char strtimenow[MAX_PATH] = { 0 };
  1432. y2k_time_t nowtime = y2k_time_now();
  1433. y2k_to_string(nowtime, strtimenow, MAX_PATH);
  1434. video_record_info_t video_params;
  1435. video_params.strServerURL = m_strHttpServerAddr;
  1436. video_params.strAPI = m_strHttpServerAPI;
  1437. video_params.strAppVersion = m_strAppVersion;
  1438. video_params.strRecordEndTime = strtimenow;
  1439. video_params.strTerminalNo = m_strTerminalId;
  1440. video_params.iBusinessStatus = (int)m_eBusinessStatus;
  1441. if (eFailed == m_eBusinessStatus) {
  1442. if (m_vRecordList.size() > 0) {
  1443. video_params.iBusinessStatus = eInterrupt;
  1444. }
  1445. }
  1446. video_params.strRecordID = m_SalesVideoName + strlen(RVC_SALES_RECORD_SUFFIX);
  1447. for (vector<record_item_t*>::iterator it = m_vRecordList.begin(); it < m_vRecordList.end(); ++it) {
  1448. video_params.vRecordList.push_back(*it);
  1449. }
  1450. unsigned int uposttime = 0;
  1451. CSimpleStringA strErrorMsg("");
  1452. if (0 == post_video_recordinfo_list(uposttime, strErrorMsg, &video_params, m_iHttpTimeOut, false)) {
  1453. LogWarn(Severity_Low, Error_Debug, LOG_EVT_POST_SALESRECORD_INFO_COST_TIME, CSimpleStringA::Format("post video record infos cost time is %ums.", uposttime).GetData());
  1454. Error = Error_Succeed;
  1455. }
  1456. else {
  1457. LogWarn(Severity_Middle, Error_Exception, LOG_EVT_POST_SALESRECORD_INFO_FAILED, strErrorMsg.GetData());
  1458. }
  1459. for (vector<record_item_t*>::iterator it = m_vRecordList.begin(); it < m_vRecordList.end(); ++it) {
  1460. if (NULL != *it) {
  1461. delete* it;
  1462. *it = NULL;
  1463. }
  1464. }
  1465. m_vRecordList.clear();
  1466. return Error;
  1467. }
  1468. int CSalesRecorderEntity::HandleEncryptVideoRecord(const char* videofilename)
  1469. {
  1470. int iRet = -1;
  1471. if (NULL == videofilename) {
  1472. return iRet;
  1473. }
  1474. if (!ExistsFile(videofilename)) {
  1475. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File %s is not exist.", videofilename);
  1476. return iRet;
  1477. }
  1478. filecryption_callback_t cb = { 0 };
  1479. cb.dbg = &rvcDbg;
  1480. char strOutFile[MAX_PATH] = { 0 };
  1481. int iresult = encryption_file(strOutFile, MAX_PATH, videofilename, &cb, eVerB);
  1482. if (0 != iresult) {
  1483. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_ENCRYPT_FAILED, CSimpleStringA::Format("encryption file %s failed, delete out temp file %s!", videofilename, strOutFile).GetData());
  1484. if (ExistsFile(strOutFile)) {
  1485. if (!RvcDeleteFile(strOutFile)) {
  1486. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  1487. }
  1488. }
  1489. return iRet;
  1490. }
  1491. bool bRet = RvcDeleteFile(videofilename);
  1492. if (!bRet) {
  1493. #ifdef RVC_OS_WIN
  1494. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_DELETE_FAILED, CSimpleStringA::Format("Error Code %lu while delete %s, delete out temp file[%s]!", GetLastError(), videofilename, strOutFile).GetData());
  1495. #else
  1496. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_DELETE_FAILED, CSimpleStringA::Format("%s(%d) while delete %s, delete out temp file[%s]!", strerror(errno), errno, videofilename, strOutFile).GetData());
  1497. #endif // RVC_OS_WIN
  1498. if (!RvcDeleteFile(strOutFile)) {
  1499. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  1500. }
  1501. return iRet;
  1502. }
  1503. else {
  1504. if (rvcMoveFile(strOutFile, videofilename)) {
  1505. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("rename %s to %s Success!", strOutFile, videofilename);
  1506. iRet = 0;
  1507. }
  1508. else {
  1509. #ifdef RVC_OS_WIN
  1510. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_RENAME_FAILED, CSimpleStringA::Format("Error Code %lu while rename %s.", GetLastError(), strOutFile).GetData());
  1511. #else
  1512. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_SALESRECORD_RENAME_FAILED, CSimpleStringA::Format("%s(%d) while rename %s.", strerror(errno), errno, strOutFile).GetData());
  1513. #endif // RVC_OS_WIN
  1514. }
  1515. }
  1516. #if 0
  1517. char strdecFile[MAX_PATH] = { 0 };
  1518. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("begin decrypt file %s.", videofilename);
  1519. iresult = decryption_file(strdecFile, MAX_PATH, videofilename, &cb, eVerB);
  1520. if (0 == iresult) {
  1521. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s success!", videofilename, strdecFile);
  1522. }
  1523. else {
  1524. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s failed!", videofilename, strdecFile);
  1525. }
  1526. #endif
  1527. return iRet;
  1528. }
  1529. ErrorCodeEnum CSalesRecorderEntity::SetRecordCamera(int iCamera)
  1530. {
  1531. ErrorCodeEnum Error = Error_Succeed;
  1532. if (CAMERA_TYPE_OPT == iCamera) {
  1533. m_iRecordCamera = CAMERA_TYPE_OPT;
  1534. }
  1535. else {
  1536. m_iRecordCamera = CAMERA_TYPE_ENV;
  1537. }
  1538. return Error;
  1539. }
  1540. int CSalesRecorderEntity::GetMaxDiskPercent()
  1541. {
  1542. return m_max_disk_percent;
  1543. }
  1544. ErrorCodeEnum CSalesRecorderEntity::GetEntityConfig()
  1545. {
  1546. ErrorCodeEnum Error = Error_Succeed;
  1547. int iAudioQuality = 3;
  1548. int iAudioNsPolicy = 2;
  1549. int iIsAudioNsOn = 0;
  1550. int iAudioChannels = 1;
  1551. int iLogLevel = 2;
  1552. int iLowestLevel = 1;
  1553. int iTimeOut = RVC_HTTPTIMEOUT;
  1554. CSimpleStringA strHttpServerAddr("");
  1555. int iPostOn = 0;
  1556. m_max_disk_percent = MAX_DISK_PERCENT;
  1557. int iStopEncflag = 0;
  1558. int iRemoteRecordType = 0;
  1559. CSmartPointer<IConfigInfo> spConfig;
  1560. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1561. if (spFunction->OpenConfig(Config_CenterSetting, spConfig) == Error_Succeed) {
  1562. spConfig->ReadConfigValueInt("SalesRecorder", "AudioNsPolicy", iAudioNsPolicy);
  1563. spConfig->ReadConfigValueInt("SalesRecorder", "IsAudioNsOn", iIsAudioNsOn);
  1564. spConfig->ReadConfigValueInt("SalesRecorder", "AudioQuality", iAudioQuality);
  1565. spConfig->ReadConfigValueInt("SalesRecorder", "AudioChannels", iAudioChannels);
  1566. spConfig->ReadConfigValueInt("SalesRecorder", "LogLevel", iLogLevel);
  1567. spConfig->ReadConfigValueInt("SalesRecorder", "LowestLevel", iLowestLevel);
  1568. spConfig->ReadConfigValueInt("SalesRecorder", "post_salesrecord_info_on", iPostOn);
  1569. spConfig->ReadConfigValue("Recorder", "http_video_record_addr", strHttpServerAddr);
  1570. spConfig->ReadConfigValueInt("Recorder", "http_timeout", iTimeOut);
  1571. spConfig->ReadConfigValueInt("SalesRecorder", "stopencflag", iStopEncflag);
  1572. spConfig->ReadConfigValueInt("InteractiveControl", "RemoteRecordType", iRemoteRecordType);
  1573. }
  1574. if (iLogLevel <= RECORD_LOG_ERROR && iLogLevel > 0) {
  1575. m_loglevel = (record_loglevel)iLogLevel;
  1576. }
  1577. if (iLowestLevel <= RECORD_LOG_ERROR && iLowestLevel > 0) {
  1578. m_lowestlevel = (record_loglevel)iLowestLevel;
  1579. }
  1580. if (strHttpServerAddr.GetLength() > 0) {
  1581. m_strHttpServerAddr = strHttpServerAddr;
  1582. }
  1583. if (iTimeOut > 0 && iTimeOut < 20 * RVC_HTTPTIMEOUT) {
  1584. m_iHttpTimeOut = iTimeOut;
  1585. }
  1586. if (1 == iPostOn) {
  1587. m_bPostOn = true;
  1588. }
  1589. SetRecordAudioQuality(iAudioQuality);
  1590. SetRecordAudioNsPolicy(iAudioNsPolicy);
  1591. SetRecordAudioChannles(iAudioChannels);
  1592. if (0 != iIsAudioNsOn) {
  1593. m_bIsAudioNsOn = true;
  1594. }
  1595. if (1 == iStopEncflag) {
  1596. m_bEncFlag = false;
  1597. }
  1598. if (1 == iRemoteRecordType) {
  1599. m_iRemoteRecordType = 1;
  1600. }
  1601. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_bEncFlag is %s, m_iRemoteRecordType is %d.", m_bEncFlag ? "true" : "false", m_iRemoteRecordType);
  1602. return Error;
  1603. }
  1604. SP_BEGIN_ENTITY_MAP()
  1605. SP_ENTITY(CSalesRecorderEntity)
  1606. SP_END_ENTITY_MAP()