mod_SalesRecorder.cpp 54 KB

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