mod_SalesRecorder.cpp 53 KB

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