gsd_ExpelPlugin.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "gsd_ExpelPlugin.h"
  2. #include "expel.h"
  3. using namespace drogon;
  4. using namespace gsd;
  5. void ExpelPlugin::initAndStart(const Json::Value &config)
  6. {
  7. /// Initialize and start the plugin
  8. DebugL;
  9. this->sockerUdp = Socket::createSocket();
  10. if(this->Init() != OK){
  11. ErrorL << "ExpelPlugin初始化失败" << endl;
  12. }
  13. }
  14. void ExpelPlugin::shutdown()
  15. {
  16. /// Shutdown the plugin
  17. }
  18. /**
  19. * @description: 初始化
  20. * @return {*}
  21. */
  22. int8_t ExpelPlugin::Init(){
  23. this->sockerUdp->bindUdpSock(7777);
  24. this->sockerUdp->setOnRead([&](const Buffer::Ptr &buf, struct sockaddr *addr , int){
  25. this->UdpRecData(buf);
  26. });
  27. return OK;
  28. }
  29. /**
  30. * @description: 启动任务
  31. * @return {*}
  32. */
  33. void ExpelPlugin::StartTask(){
  34. }
  35. /**
  36. * @description:
  37. * @param {sockaddr} *out
  38. * @param {char} *ip
  39. * @param {uint16_t} port
  40. * @return {*}
  41. */
  42. void ExpelPlugin::makeAddr(struct sockaddr *out,const char *ip,uint16_t port){
  43. struct sockaddr_in &servaddr = *((struct sockaddr_in *)out);
  44. servaddr.sin_family = AF_INET;
  45. servaddr.sin_port = htons(port);
  46. servaddr.sin_addr.s_addr = inet_addr(ip);
  47. bzero(&(servaddr.sin_zero), sizeof servaddr.sin_zero);
  48. }
  49. /**
  50. * @description: UDP数据接收
  51. * @param {Ptr} &buf
  52. * @return {*}
  53. */
  54. void ExpelPlugin::UdpRecData(const Buffer::Ptr &buf){
  55. static std::shared_ptr<Ticker> ticker = nullptr;
  56. string json = buf->toString();
  57. std::shared_ptr<CNStreamInferData> cnstreamInferData = nullptr;
  58. cnstreamInferData = std::make_shared<CNStreamInferData>();
  59. DebugL << json << endl;
  60. if(cnstreamInferData->jsonToObject(json)){
  61. vector<vector<std::string>> result;
  62. SqlWriter sqlSelete("SELECT DeviceId,DeviceToken,DeviceType, \
  63. ServerIp,ServerCname,ServerCpwd FROM gsdDB.`DeviceList` WHERE ISDELETE = 0");
  64. sqlSelete << result;
  65. Expel::getPtr()->UpdateDeviceList(result);
  66. InfineFilter::getPtr()->bboxSize = config::getPtr()->bboxSize;
  67. // 过滤小目标
  68. InfineFilter::getPtr()->MinBBoxFilter(cnstreamInferData);
  69. if(cnstreamInferData->Objects.size()){
  70. if(ticker != nullptr) ticker->resetTime();
  71. if(this -> m_DevPowerState == false){
  72. // 控制驱鸟设备
  73. Expel::getPtr() -> AllFire();
  74. // 更新状态
  75. this -> m_DevPowerState = true;
  76. }
  77. }else{
  78. if(ticker == nullptr) ticker = std::make_shared<Ticker>();
  79. int time = config::getPtr()->ExpelInterval;
  80. if(ticker->elapsedTime() > time && this->m_DevPowerState){
  81. this->m_DevPowerState = false;
  82. ticker = nullptr;
  83. Expel::getPtr() -> AllClose();
  84. }
  85. }
  86. }
  87. }