imstorage_manager.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <io.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include "imstorage_manager.h"
  5. //namespace graft_gcv
  6. //{
  7. CRITICAL_SECTION g_cs;
  8. CRITICAL_SECTION g_cs_pcd;
  9. void getFiles( string path, vector<string>& files, time_t clean_time )
  10. {
  11. //文件句柄
  12. long hFile = 0;
  13. //文件信息
  14. struct _finddata_t fileinfo;
  15. string p;
  16. if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
  17. {
  18. do
  19. {
  20. //如果是目录,迭代之
  21. //如果不是,加入列表
  22. if((fileinfo.attrib & _A_SUBDIR))
  23. {
  24. if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
  25. getFiles( p.assign(path).append("\\").append(fileinfo.name), files ,clean_time);
  26. }
  27. else
  28. {
  29. if(clean_time>fileinfo.time_create){
  30. files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
  31. }
  32. }
  33. }while(_findnext(hFile, &fileinfo) == 0);
  34. _findclose(hFile);
  35. }
  36. }
  37. bool g_thread_run=false;
  38. bool g_thread_run_saver=true;
  39. bool g_thread_run_saver_pcd = true;
  40. int WINAPI TheadFuncClearn(LPVOID lpParam)
  41. {
  42. ThreadParamClean* threadParam = (ThreadParamClean *) lpParam;
  43. string folder =threadParam->folder;
  44. int store_days = threadParam->store_days;
  45. bool* state = threadParam->state;
  46. int cnt=0;
  47. __time64_t dtime = store_days*86400;
  48. //cout<<"folder: "<<folder<<endl;
  49. time_t scan_time = time(NULL);
  50. while(folder.size()>0)
  51. {
  52. if(!(*state)){break;}
  53. if(time(NULL)-scan_time < 3600000){
  54. Sleep(500);
  55. continue;
  56. }
  57. vector<string>filenames;
  58. time_t clean_time = time(NULL) - dtime;
  59. getFiles(folder,filenames,clean_time);
  60. scan_time = time(NULL);
  61. for(size_t i=0;i<filenames.size();++i){
  62. //delete the file
  63. remove(filenames[i].c_str());
  64. }
  65. }
  66. return 0;
  67. }
  68. int WINAPI TheadFuncSave(LPVOID lpParam)
  69. {
  70. ThreadParamSave* threadParam = (ThreadParamSave *) lpParam;
  71. int nIndex = threadParam->nIndex;
  72. queue<ImgParam>* imgQ =threadParam->imgQ;
  73. while(true)
  74. {
  75. if(!g_thread_run_saver){break;}
  76. if(imgQ->size()==0){
  77. Sleep(50);
  78. continue;
  79. }
  80. try{
  81. EnterCriticalSection(&g_cs);
  82. ImgParam& im_info = imgQ->front();
  83. cv::imwrite(im_info.img_name,im_info.image);
  84. //cout<<im_info.img_name<<"\t"<<imgQ->size()<<endl;
  85. imgQ->pop();
  86. }
  87. catch(...){
  88. //int tes = 0;
  89. }
  90. LeaveCriticalSection(&g_cs);
  91. }
  92. return 0;
  93. }
  94. //int WINAPI TheadFuncSavePcd(LPVOID lpParam)
  95. //{
  96. // ThreadParamSavePcd* threadParam = (ThreadParamSavePcd *)lpParam;
  97. // int nIndex = threadParam->nIndex;
  98. // queue<PcdParam>* pcdQ = threadParam->pcdQ;
  99. // while (true)
  100. // {
  101. // if (!g_thread_run_saver_pcd) { break; }
  102. // if (pcdQ->size() == 0) {
  103. // Sleep(50);
  104. // continue;
  105. // }
  106. // try {
  107. // EnterCriticalSection(&g_cs_pcd);
  108. // PcdParam& pcd_info = pcdQ->front();
  109. // pcl::io::savePLYFile(pcd_info.pcd_name, *pcd_info.pcd, true);
  110. // //cout<<im_info.img_name<<"\t"<<imgQ->size()<<endl;
  111. // pcdQ->pop();
  112. // }
  113. // catch (...) {
  114. // //int tes = 0;
  115. // }
  116. // LeaveCriticalSection(&g_cs_pcd);
  117. // }
  118. // return 0;
  119. //}
  120. CImStoreManager::CImStoreManager()
  121. :m_storeDays(7)
  122. ,m_storeDir("")
  123. ,m_workHandle(0)
  124. ,m_workHandleSave(0)
  125. //,m_workHandleSavePcd(0)
  126. {
  127. InitializeCriticalSection(&g_cs);
  128. ThreadParamSave paramSave;
  129. paramSave.nIndex = 0;
  130. //paramSave.state = &g_thread_run;
  131. paramSave.imgQ = &m_images;
  132. m_workHandleSave = CreateThread(
  133. NULL,
  134. 0,
  135. (LPTHREAD_START_ROUTINE)TheadFuncSave,
  136. (LPVOID)&paramSave,
  137. NULL,
  138. NULL);
  139. InitializeCriticalSection(&g_cs_pcd);
  140. //ThreadParamSavePcd paramSavePcd;
  141. //paramSavePcd.nIndex = 0;
  142. //paramSave.state = &g_thread_run;
  143. //paramSavePcd.pcdQ = &m_pcds;
  144. /*m_workHandleSavePcd = CreateThread(
  145. NULL,
  146. 0,
  147. (LPTHREAD_START_ROUTINE)TheadFuncSavePcd,
  148. (LPVOID)&paramSavePcd,
  149. NULL,
  150. NULL);*/
  151. Sleep(500);
  152. }
  153. CImStoreManager::~CImStoreManager(){
  154. g_thread_run=false;
  155. g_thread_run_saver=false;
  156. g_thread_run_saver_pcd = false;
  157. HANDLE handles[2];
  158. handles[0]=m_workHandle;
  159. handles[1]=m_workHandleSave;
  160. //handles[2] = m_workHandleSavePcd;
  161. WaitForMultipleObjects(2,handles,TRUE,500);
  162. DeleteCriticalSection(&g_cs);
  163. //DeleteCriticalSection(&g_cs_pcd);
  164. }
  165. void CImStoreManager::restart_start_worker()
  166. {
  167. ThreadParamClean param_clean;
  168. param_clean.folder = m_storeDir;
  169. param_clean.store_days = m_storeDays;
  170. param_clean.state = &g_thread_run;
  171. g_thread_run=false;
  172. if(m_workHandle){
  173. WaitForSingleObject(m_workHandle,INFINITE);
  174. CloseHandle(m_workHandle);
  175. }
  176. g_thread_run=true;
  177. m_workHandle = CreateThread(
  178. NULL,
  179. 0,
  180. (LPTHREAD_START_ROUTINE)TheadFuncClearn,
  181. (LPVOID)&param_clean,
  182. NULL,
  183. NULL);
  184. Sleep(500);
  185. }
  186. int CImStoreManager::setStoreDir(string& folder)
  187. {
  188. int stat = _access(folder.c_str(),0);
  189. if (stat==0){
  190. m_storeDir = folder;
  191. g_thread_run=false;
  192. restart_start_worker();
  193. }
  194. else{
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. void CImStoreManager::getStoreDir(string& folder)
  200. {
  201. folder = m_storeDir;
  202. }
  203. void CImStoreManager::setStoreDays(int days)
  204. {
  205. if(days>0){
  206. m_storeDays = days;
  207. g_thread_run=false;
  208. restart_start_worker();
  209. }
  210. }
  211. bool CImStoreManager::is_valid_folder()
  212. {
  213. int stat = _access(m_storeDir.c_str(),0);
  214. if(stat==0){return true;}
  215. else{return false;}
  216. }
  217. int CImStoreManager::saveImage(cv::Mat&img,string name_id)
  218. {
  219. if(!is_valid_folder()){return 1;}
  220. if(img.empty()){return 1;}
  221. string tar_file = m_storeDir+"/"+name_id+".jpg";
  222. ImgParam imgp;
  223. imgp.img_name=tar_file;
  224. imgp.image = img.clone();
  225. EnterCriticalSection(&g_cs);
  226. m_images.push(imgp);
  227. //cout<<"=========="<<imgp.img_name<<endl;
  228. LeaveCriticalSection(&g_cs);
  229. return 0;
  230. }
  231. //int CImStoreManager::saveBinPly(
  232. // pcl::PointCloud<pcl::PointXYZ>::Ptr pcd,
  233. // string name_id)
  234. //{
  235. // if (!is_valid_folder()) { return 1; }
  236. // if (pcd->points.size()==0) { return 1; }
  237. //
  238. // EnterCriticalSection(&g_cs_pcd);
  239. // string tar_file = m_storeDir + "/" + name_id + ".ply";
  240. // PcdParam pcdp;
  241. // pcdp.pcd.reset(new pcl::PointCloud<pcl::PointXYZ>);
  242. // pcdp.pcd_name = tar_file;
  243. // pcl::copyPointCloud(*pcd, *pcdp.pcd);
  244. //
  245. // m_pcds.push(pcdp);
  246. // //cout<<"=========="<<pcdp.pcd_name<<endl;
  247. // LeaveCriticalSection(&g_cs_pcd);
  248. //
  249. // return 0;
  250. //}
  251. //};