/*
 * @Author: your name
 * @Date: 2023-07-25 23:55:04
 * @LastEditors: your name
 * @LastEditTime: 2023-07-25 23:55:05
 * @Description: file content
 * @FilePath: /shigetai_lirs/nvidia_ascend_engine/common_engine/FilterEngine/FilterTrainStepOneEngine.h
 * 
 * Copyright © 2022 <Shandong Matrix Software Engineering Co., Ltd>
 */
/**
 * 车号第一步过滤Engine
 * */

#ifndef FILTERTRAINSTEPONEENGINE_H
#define FILTERTRAINSTEPONEENGINE_H

#include "AppCommon.h"
#include "MyYaml.h"
#include "EngineBase.h"
#include "EngineFactory.h"
#include "json/json.h"

class FilterTrainStepOneEngine : public ai_matrix::EngineBase
{
public:
    FilterTrainStepOneEngine();
    ~FilterTrainStepOneEngine();

    APP_ERROR Init() override;
    APP_ERROR DeInit() override;
    APP_ERROR Process() override;

private:
    //参数初始化(列车结束时需调用)
    void InitParam();
    //push数据到队列，队列满时则休眠一段时间再push
    void PushData(const std::string &strPort, const std::shared_ptr<ProcessData> &pProcessData);

    void CalculateDirection(std::shared_ptr<ProcessData> pProcessData);
    void SetDirection(std::vector<Step1Location> &vecLocation, std::shared_ptr<ProcessData> pProcessData);

    int GetTrainStatus(std::shared_ptr<ProcessData> pProcessData);

    //根据当前帧数据，处理上一帧数据
    void DealProcessDataPre(std::shared_ptr<ProcessData> pProcessData);

    void AddBackInfo(std::shared_ptr<ProcessData> pProcessData);
    bool IsEndDealBackInfo(std::shared_ptr<ProcessData> pProcessData);

    //计算方向新
    void CalculateDirectionNew(std::shared_ptr<ProcessData> pProcessData);

    void sendComeTrain(const std::string strTrainDate, const std::string strTrainName, const int iDirection);

    std::string strPort0_;
    std::string strResultPath_;
    int iChkStopPX_;
    int iChkStopCount_;
    int iDirection_;            //方向
    int iPushDirection_;        //需要识别的方向
    int rightFirst_;            // 向右行驶的在前大框类型
    int leftFirst_;             // 向左行驶的在前大框类型
    int iPartitionFrameNum_;    //满足跨车厢的帧间隔
    int iPlitFrameSpanPX_;      //相连帧 同种大框的跨度最大值 
    std::map<int, PostData> mapPostDataFrist_;                                  //[key-数据源id, value-第一步识别信息]
    std::map<int, std::map<int, std::vector<Step1Location>>> mapMapStep1Info_;  //[key-数据源id, value-[key-识别目标, value-识别框集合]]

    std::map<int, std::shared_ptr<ProcessData>> mapProcessDataPre_;
    std::map<int, int> mapHeadContinueCnt_;
    std::map<int, int> mapProContinueCnt_;
    std::map<int, int> mapNumContinueCnt_;
    std::map<int, int> mapSpaceContinueCnt_;
    std::map<int, int> mapTrainSpaceContinueCnt_;
    std::map<int, bool> mapDataSourceIsEnd_;                      //[key-数据源id, value-数据是否结束]

    int iNotChgCount_ = 0; //第一步无变化帧数

    //PostData postDataPre_;                  //前一帧第一步后处理结果
    //int iNoTargetCnt_;                      //无目标数据个数
    // int iSpaceIntervalCnt_;                 //上一节车厢间隔目标间隔个数
    // int iSpaceFrameBegin_;                  //当节车厢间隔目标出现的开始帧
    // int iSpaceFrameLast_;                   //最后出现的车厢间隔目标框帧号
    // bool bStopFlag_;                        //停车标识
    // uint32_t iReRunOrigFrameId_;            //停止后再次行驶的原始帧号
    // int iNoTargetReRunCnt_;                 //停车再行驶次数(无框)
    // int iTargetReRunCnt_;                   //停车再行驶次数(有框)

    int iTrainStatus_;                      //列车状态标识
    std::queue<PostData> quePostData_;      //保存3帧信息，用于实时判断方向
    std::stack<TrainBackInfo> stackBackInfo_;
    std::map<int, std::string> mapTargetStr_;
    std::map<int, std::map<int, std::vector<CalculateInfo>>> mapCalDirection_;
    ai_matrix::DataSourceConfig mainCfg_;   //主摄像头配置信息
};

ENGINE_REGIST(FilterTrainStepOneEngine)

#endif
