mod_SalesRecorder.cpp 54 KB

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