cumsgbuffer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "cumsgbuffer.h"
  2. cumsgbuffer::cumsgbuffer()
  3. {
  4. }
  5. void cumsgbuffer::addmsg(int id, qint64 ntime, std::string strVIN, std::string strqueryMD5, std::string strctrlMD5, std::vector<char> *pxdata,bool bImportant,int nkeeptime)
  6. {
  7. qDebug("ntime is %lld",ntime);
  8. mMutex.lock();
  9. iv::cumsg * pmsg = 0;
  10. int nsize = mvectormsg.size();
  11. int i;
  12. for(i=0;i<nsize;i++)
  13. {
  14. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  15. {
  16. pmsg = &mvectormsg[i];
  17. break;
  18. }
  19. }
  20. if(pmsg == 0)
  21. {
  22. iv::cumsg cmsg;
  23. cmsg.id = id;
  24. cmsg.ntime = ntime;
  25. cmsg.strVIN = strVIN;
  26. cmsg.strqueryMD5 = strqueryMD5;
  27. cmsg.strctrlMD5 = strctrlMD5;
  28. if(pxdata->size() > 0)
  29. {
  30. cmsg.xdata.resize(pxdata->size());
  31. memcpy(cmsg.xdata.data(),pxdata->data(),pxdata->size());
  32. }
  33. cmsg.mlastuptime = QDateTime::currentMSecsSinceEpoch();
  34. cmsg.mbImportant = bImportant;
  35. cmsg.mkeeptime = nkeeptime;
  36. cmsg.mbhavequery = false;
  37. mvectormsg.push_back(cmsg);
  38. mMutex.unlock();
  39. return;
  40. }
  41. if((pmsg->mbImportant != true)||(pmsg->mbhavequery)||((QDateTime::currentMSecsSinceEpoch() - pmsg->mlastuptime)>=pmsg->mkeeptime))
  42. {
  43. pmsg->id = id;
  44. pmsg->ntime = ntime;
  45. pmsg->mbImportant = bImportant;
  46. pmsg->mbhavequery = false;
  47. pmsg->mlastuptime = QDateTime::currentMSecsSinceEpoch();
  48. pmsg->mkeeptime = nkeeptime;
  49. pmsg->strqueryMD5 = strqueryMD5;
  50. pmsg->strctrlMD5 = strctrlMD5;
  51. pmsg->xdata.clear();
  52. if(pxdata->size()>0)
  53. {
  54. pmsg->xdata.resize(pxdata->size());
  55. memcpy(pmsg->xdata.data(),pxdata->data(),pxdata->size());
  56. }
  57. }
  58. mMutex.unlock();
  59. }
  60. int cumsgbuffer::getmsg(std::string strVIN,std::string strqueryMD5, qint64 nlasttime, int &id, qint64 &ntime, std::vector<char> *pxdata)
  61. {
  62. mMutex.lock();
  63. iv::cumsg * pmsg = 0;
  64. int nsize = mvectormsg.size();
  65. int i;
  66. for(i=0;i<nsize;i++)
  67. {
  68. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  69. {
  70. pmsg = &mvectormsg[i];
  71. break;
  72. }
  73. }
  74. if(pmsg == 0)
  75. {
  76. std::cout<<" no this vin data"<<std::endl;;
  77. mMutex.unlock();
  78. return -1;
  79. }
  80. if(strqueryMD5 != pmsg->strqueryMD5)
  81. {
  82. std::cout<<" query error."<<std::endl;
  83. mMutex.unlock();
  84. return -2;
  85. }
  86. pmsg->mbhavequery = true;
  87. if(nlasttime == pmsg->ntime)
  88. {
  89. mMutex.unlock();
  90. return 0;
  91. }
  92. id = pmsg->id;
  93. ntime = pmsg->ntime;
  94. pxdata->clear();
  95. int ndatasize = pmsg->xdata.size();
  96. pxdata->resize(ndatasize);
  97. memcpy(pxdata->data(),pmsg->xdata.data(),ndatasize);
  98. mMutex.unlock();
  99. return 1;
  100. }
  101. void cumsgbuffer::addPicData(std::string strVIN, const char *strdata, const unsigned int ndatasize,
  102. qint64 npictime, int nCamPos,qint64 nLatency)
  103. {
  104. mMutex.lock();
  105. iv::cumsg * pmsg = 0;
  106. int nsize = mvectormsg.size();
  107. int i;
  108. for(i=0;i<nsize;i++)
  109. {
  110. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  111. {
  112. pmsg = &mvectormsg[i];
  113. break;
  114. }
  115. }
  116. if(pmsg == 0)
  117. {
  118. std::cout<<" Not Found buf. Need Data Prepare."<<std::endl;
  119. }
  120. else
  121. {
  122. if((nCamPos<0)||(nCamPos>= CAM_NUM))
  123. {
  124. std::cout<<"Camera Postion error . nCamPos = "<<nCamPos<<std::endl;
  125. }
  126. else
  127. {
  128. iv::PicData xPicData;
  129. xPicData.mdata_ptr = std::shared_ptr<char>(new char[ndatasize]);
  130. memcpy(xPicData.mdata_ptr.get(),strdata,ndatasize);
  131. xPicData.mdatasize = ndatasize;
  132. xPicData.mnMsgTime = npictime;
  133. pmsg->mpicbuf[nCamPos].AddData(xPicData);
  134. pmsg->mpicbuf[nCamPos].SetLatency(nLatency);
  135. }
  136. }
  137. mMutex.unlock();
  138. }
  139. int cumsgbuffer::getPicData(std::string strVIN, std::string strqueryMD5,std::shared_ptr<char> &pdata_ptr,
  140. unsigned int &ndatasize,std::string strclientid, int nCamPos,qint64 & nPicTime,qint64 & nPicLatency,int & nFrameRate)
  141. {
  142. mMutex.lock();
  143. iv::cumsg * pmsg = 0;
  144. int nsize = mvectormsg.size();
  145. int i;
  146. for(i=0;i<nsize;i++)
  147. {
  148. if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
  149. {
  150. pmsg = &mvectormsg[i];
  151. break;
  152. }
  153. }
  154. if(pmsg == 0)
  155. {
  156. std::cout<<" no this vin data"<<std::endl;;
  157. mMutex.unlock();
  158. return -1;
  159. }
  160. if(strqueryMD5 != pmsg->strqueryMD5)
  161. {
  162. std::cout<<" query error."<<std::endl;
  163. mMutex.unlock();
  164. return -2;
  165. }
  166. if((nCamPos<0)||(nCamPos>= CAM_NUM))
  167. {
  168. std::cout<<"cumsgbuffer::getPicData Camera Postion error . nCamPos = "<<nCamPos<<std::endl;
  169. mMutex.unlock();
  170. return -3;
  171. }
  172. qint64 nLastPicTime = 0;
  173. if(mclienttime.GetClientCamLastTime(strclientid,nCamPos,nLastPicTime) < 0)
  174. {
  175. std::cout<<"cumsgbuffer::getPicData Can't Get Camera LastTime"<<std::endl;
  176. mMutex.unlock();
  177. return -4;
  178. }
  179. iv::PicData xPicData;
  180. int nrtn = pmsg->mpicbuf[nCamPos].GetData(nLastPicTime,xPicData);
  181. if(nrtn == 1)
  182. {
  183. pdata_ptr = xPicData.mdata_ptr;
  184. ndatasize = xPicData.mdatasize;
  185. nPicTime = nLastPicTime;
  186. }
  187. nPicLatency = pmsg->mpicbuf[nCamPos].GetLatency();
  188. nFrameRate = pmsg->mpicbuf[nCamPos].GetFrameRate();
  189. mclienttime.SetClientCamLastTime(strclientid,nCamPos,nLastPicTime);
  190. mMutex.unlock();
  191. return nrtn;
  192. }