gsd_MonitorPlugin.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. *
  3. * gsd_MonitorPlugin.cc
  4. *
  5. */
  6. #include "gsd_MonitorPlugin.h"
  7. #include "HttpClient.h"
  8. using namespace drogon;
  9. using namespace gsd;
  10. void MonitorPlugin::initAndStart(const Json::Value &config)
  11. {
  12. #ifdef KAFKA
  13. // Initialize and start the plugin
  14. this->monitor = std::make_shared<Monitor>();
  15. if(this->Init() != OK){
  16. ErrorL << "MonitorPlugin初始化失败" << endl;
  17. }
  18. #endif
  19. }
  20. void MonitorPlugin::shutdown()
  21. {
  22. this->stop_ = true;
  23. }
  24. /**
  25. * @description: 初始化
  26. * @return {*}
  27. */
  28. int8_t MonitorPlugin::Init(){
  29. if(!monitor->Init()) {
  30. ErrorL << "monitor初始化失败" << endl;
  31. return ERR;
  32. }
  33. this->pool = std::make_shared<ThreadPool>(1,ThreadPool::PRIORITY_HIGHEST, false);
  34. return OK;
  35. }
  36. /**
  37. * @description: 启动任务
  38. * @return {*}
  39. */
  40. void MonitorPlugin::StartTask(){
  41. // 寒武纪加速卡版本
  42. #ifdef KAFKA
  43. this->pool->async([&](){
  44. while(!this->stop_){
  45. this->MonitorProThrd();
  46. }
  47. });
  48. this->pool->start();
  49. #endif
  50. }
  51. /**
  52. * @description: 处理任务
  53. * @return {*}
  54. */
  55. void MonitorPlugin::MonitorProThrd(){
  56. FrameInferData::Ptr result = std::make_shared<FrameInferData>();
  57. if(!this->ConsumeData(result)) return;
  58. if(result->Num == 0) return;
  59. if(!app().getPlugin<TcpPlugin>()->getAlive()){
  60. result->uuid = uuid::generate();
  61. std::string sql = result->ObjectToSql();
  62. SqlWriter insert(sql.c_str());
  63. vector<vector<std::string>> ret;
  64. insert << ret;
  65. InfoL << "Save inferential Data, uuid:" << result->uuid << endl;
  66. return;
  67. }
  68. config::Ptr m_config = config::getPtr();
  69. // 发送鸟情数据至后端
  70. SendBird sendBird;
  71. sendBird.birdImageBase64 = "";
  72. if(!config::getPtr()->debug){
  73. sendBird.birdImageBase64 = result->ImageBase64;
  74. }
  75. sendBird.birdNum = result->Num;
  76. sendBird.birdType = (NettyClientCommandEnum().bird_info);
  77. if(!config::getPtr()->LowVersion) sendBird.deviceUuid = m_config->SimCode;
  78. else sendBird.deviceUuid = m_config->vpnIP;
  79. sendBird.degree = 2;
  80. sendBird.remark = result->Remark;
  81. sendBird.heightRange = "0";
  82. sendBird.warningTime = result->WarningTime;
  83. sendBird.attr4 = result->VideoPath;
  84. sendBird.attr5 = "";
  85. HistoryVideo historyVideo;
  86. historyVideo.fileName = result->VideoPath;
  87. historyVideo.UpdateTime = result->WarningTime;
  88. this->SaveVideoRecord(historyVideo);
  89. NettyClientResultMsg <SendBird> nettyClientResultMsg;
  90. std::string RequestId = uuid::generate();
  91. result->uuid = RequestId;
  92. nettyClientResultMsg.setRequestId(RequestId);
  93. nettyClientResultMsg.setData(sendBird);
  94. nettyClientResultMsg.setDataType((NettyClientCommandEnum().bird_info));
  95. std::string json;
  96. nettyClientResultMsg.objectToJson(json);
  97. json += "\r\n";
  98. std::string sql = result->ObjectToSql();
  99. if(app().getPlugin<TcpPlugin>()->getAlive()){
  100. app().getPlugin<TcpPlugin>()->sendRequest(RequestId, NettyClientCommandEnum().bird_info, json, [&, sql](int status,std::string pbuf){
  101. if(!config::getPtr()->LowVersion){
  102. if(status == 200) return;
  103. SqlWriter insert(sql.c_str());
  104. vector<vector<std::string>> ret;
  105. insert << ret;
  106. }
  107. });
  108. }
  109. }
  110. /**
  111. * @description: 构建请求
  112. * @param {CNStreamInferData::Ptr} data
  113. * @return {*}
  114. */
  115. bool MonitorPlugin::sendRequest(CNStreamInferData::Ptr data){
  116. FrameInferData::Ptr result = std::make_shared<FrameInferData>();
  117. InfineFilter::Ptr infineFilter = InfineFilter::getPtr();
  118. config::Ptr m_config = config::getPtr();
  119. if(!config::getPtr()->debug){
  120. infineFilter->bboxSize = m_config->bboxSize;
  121. // 设置
  122. infineFilter->setSensitivity(m_config->sensitivity);
  123. infineFilter->setInterval(m_config->interval);
  124. infineFilter->setTimeOut(m_config->TimeOut);
  125. if(infineFilter != nullptr) {
  126. if(infineFilter->judgementResult(data) != OK){
  127. return false;
  128. }
  129. }
  130. }
  131. rapidjson::StringBuffer buffer;
  132. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  133. writer.StartArray();
  134. for(auto &obj : data->Objects)
  135. {
  136. writer.StartObject();
  137. writer.Key("x");
  138. writer.Double(obj.BBox.x * 1920);
  139. writer.Key("y");
  140. writer.Double(obj.BBox.y * 1080);
  141. writer.Key("width");
  142. writer.Double(obj.BBox.w * 1920);
  143. writer.Key("height");
  144. writer.Double(obj.BBox.h * 1080);
  145. writer.EndObject();
  146. }
  147. writer.EndArray();
  148. // 获取时间
  149. char ctime[80];
  150. time_t rawtime;
  151. struct tm *info;
  152. time(&rawtime);
  153. info = localtime(&rawtime);
  154. strftime(ctime, 80, "%Y-%m-%d %H:%M:%S", info);
  155. result->ClassId = 1;
  156. result->ImageBase64 = data->ImageBase64;
  157. result->Remark = buffer.GetString();
  158. result->Num = data->Objects.size();
  159. result->WarningTime = ctime;
  160. result->VideoPath = data->videoPath;
  161. if(result->Num == 0) return false;
  162. if(!app().getPlugin<TcpPlugin>()->getAlive()){
  163. result->uuid = uuid::generate();
  164. std::string sql = result->ObjectToSql();
  165. SqlWriter insert(sql.c_str());
  166. vector<vector<std::string>> ret;
  167. insert << ret;
  168. return true;
  169. }
  170. // 发送鸟情数据至后端
  171. SendBird sendBird;
  172. sendBird.birdImageBase64 = "";
  173. if(!config::getPtr()->debug){
  174. sendBird.birdImageBase64 = result->ImageBase64;
  175. }
  176. sendBird.birdNum = result->Num;
  177. sendBird.birdType = (NettyClientCommandEnum().bird_info);
  178. sendBird.deviceUuid = "";
  179. if(!config::getPtr()->LowVersion) sendBird.deviceUuid = config::getPtr()->getSimCode();
  180. else sendBird.deviceUuid = config::getPtr()->vpnIP;
  181. sendBird.degree = 2;
  182. sendBird.remark = result->Remark;
  183. sendBird.heightRange = "0";
  184. sendBird.warningTime = result->WarningTime;
  185. sendBird.attr4 = result->VideoPath;
  186. sendBird.attr5 = "";
  187. HistoryVideo historyVideo;
  188. historyVideo.fileName = result->VideoPath;
  189. historyVideo.UpdateTime = result->WarningTime;
  190. this->SaveVideoRecord(historyVideo);
  191. NettyClientResultMsg <SendBird> nettyClientResultMsg;
  192. std::string RequestId = uuid::generate();
  193. result->uuid = RequestId;
  194. nettyClientResultMsg.setRequestId(RequestId);
  195. nettyClientResultMsg.setData(sendBird);
  196. nettyClientResultMsg.setDataType((NettyClientCommandEnum().bird_info));
  197. std::string json;
  198. nettyClientResultMsg.objectToJson(json);
  199. json += "\r\n";
  200. std::string sql = result->ObjectToSql();
  201. if(app().getPlugin<TcpPlugin>()->getAlive()){
  202. app().getPlugin<TcpPlugin>()->sendRequest(RequestId, NettyClientCommandEnum().bird_info, json, [&, sql](int status,std::string pbuf){
  203. if(!config::getPtr()->debug){
  204. if(status == 200) return;
  205. SqlWriter insert(sql.c_str());
  206. vector<vector<std::string>> ret;
  207. insert << ret;
  208. }
  209. });
  210. }else ErrorL;
  211. return true;
  212. }
  213. /**
  214. * @description:
  215. * @param {Ptr&} result
  216. * @return {*}
  217. */
  218. bool MonitorPlugin::ConsumeData(FrameInferData::Ptr& result){
  219. return monitor->ConsumeData(result);
  220. }
  221. /**
  222. * @description: 保存视频记录
  223. * @param {HistoryVideo&} historyVideo
  224. * @return {*}
  225. */
  226. bool MonitorPlugin::SaveVideoRecord(HistoryVideo& historyVideo){
  227. vector<vector<std::string>> result;
  228. SqlWriter sqlSelect("SELECT Id FROM gsdDB.`HistoryVideo` where fileName = '?' and ISDELETE = 0");
  229. sqlSelect << historyVideo.fileName << result;
  230. if(result.empty()){
  231. char ctime[80];
  232. HttpClient::getDataTime(ctime);
  233. historyVideo.UpdateTime = ctime;
  234. std::string sql = historyVideo.ObjectToSql();
  235. SqlWriter write(sql.c_str());
  236. write << result;
  237. }
  238. return true;
  239. }