imstorage_manager.cpp 6.0 KB

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