
/***** (C) Copyright, Shenzhen SUNWIN Intelligent Co.,Ltd. ******source file****
* File name          : monitor.cpp
* Author             : JinLiang Yang 
* Brief              : 
********************************************************************************
* modify
* Version   Date                Author              Described
* V2.00     2021/04/08          ShengYin Li         Created
*******************************************************************************/
#include "monitor.h"
#include "config.hpp"

using namespace std;

Monitor::Monitor()
{
}

Monitor::~Monitor()
{      
  InfoL << "Release Monitor resources!";
}

/**
 * @description: 初始化
 * @param {*}
 * @return {*}
 */
bool Monitor::Init()
{
    #ifdef KAFKA
        std::string brokers = "192.168.31.223";
        std::string topic_str = "deepstream";
        std::string group_id = "cnstream-group";

        // kafka
        this->m_KafkaConsumer = std::make_shared<kafka_consumer_client>(brokers, topic_str, group_id);
        if(this->m_KafkaConsumer->initClient()){
          InfoL << "KafkaServe initialization success."; 
        }else{
          ErrorL << "KafkaServe initialization failed.";
          return false;
        }
    #endif
    return true;
}

/**
 * @description: 消费数据
 * @param {*}
 * @return {*}
 */
bool Monitor::ConsumeData(FrameInferData::Ptr& result)
{
  #ifdef KAFKA
      std::shared_ptr<CNStreamInferData> cnstreamInferData = std::make_shared<CNStreamInferData>();
      std::string json = "";
      // 消费数据
      this->m_KafkaConsumer->ConsumeData(json, 5000);
      if(json != ""){
        cnstreamInferData->Objects.clear();
        // 获取到数据
        if(cnstreamInferData->jsonToObject(json)){
          // 过滤器
          InfineFilter::Ptr infineFilter = InfineFilter::getPtr();
          config::Ptr m_config = config::getPtr();

          infineFilter->bboxSize = m_config->bboxSize;
          // 设置
          infineFilter->setSensitivity(m_config->sensitivity);
          infineFilter->setInterval(m_config->interval);
          infineFilter->setTimeOut(m_config->TimeOut);

          if(infineFilter != nullptr) {
            if(infineFilter->judgementResult(cnstreamInferData) != OK){
                return false;
            }
          }
          
          // 解析数据成功
          rapidjson::StringBuffer buffer;
          rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
          writer.StartArray();
          for(auto &obj : cnstreamInferData->Objects)
          {
            writer.StartObject();
            writer.Key("x");
            writer.Double(obj.BBox.x * 1920);
            writer.Key("y");
            writer.Double(obj.BBox.y * 1080);
            writer.Key("width");
            writer.Double(obj.BBox.w * 1920);
            writer.Key("height");
            writer.Double(obj.BBox.h * 1080);
            writer.EndObject();
          }
          writer.EndArray();

          // 获取时间
          char ctime[80];
          time_t rawtime;
          struct tm *info;
          time(&rawtime);
          info = localtime(&rawtime);
          strftime(ctime, 80, "%Y-%m-%d %H:%M:%S", info);

          result->ClassId = 1;
          result->ImageBase64 = cnstreamInferData->ImageBase64;
          result->Remark = buffer.GetString();
          result->Num = cnstreamInferData->Objects.size();
          result->WarningTime = ctime;
          result->VideoPath = cnstreamInferData->videoPath;
          return true;
        }
      }
  #endif
  return false;
}


/******** (C) Copyright, Shenzhen SUNWIN Intelligent Co.,Ltd. ******** End *****/
