mod_SalesRecorder.cpp 55 KB

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