/**
 * 摄像头微服务主程序
 * */

// #include "register.h"
// using namespace ns_register;

// #include "https_sn.h"
// using namespace ns_https_sn;

#include <signal.h>
#include "myqueue.h"

#include "EngineManager.h"
using namespace ai_matrix;

#include "AppCommon.h"
#include "ToWeb.h"

std::atomic_bool app_flag(true);
//bool g_bNoDealStepTwoFlag(false);                   //不做第二步处理标识
//bool g_bHaveTrainFlag(false);                       //模型检测有车标识
//std::atomic_uint64_t g_i64ReRunTimeStamp(0);        //停止后再次行驶的时间戳
//std::atomic_uint32_t g_iReRunOrigFrameId(0);        //停止后再次行驶的原始帧号
//std::atomic_uint32_t g_iReRunFrameId(0);            //停止后再次行驶的帧号
//std::atomic_uint32_t g_iReRunOrigChkDateFid(0);     //停止后再次行驶的定检期原始帧号
//std::atomic_uint32_t g_iReRunOrigContainerFid(0);   //停止后再次行驶的集装箱原始帧号

std::atomic<int> g_identify_type(0);
std::atomic<int> g_come_direction(0);
std::atomic<int> g_car_type(0);

void SigHandler(int iSigno)
{
    if (iSigno == SIGINT)
    {
        app_flag = false;
    }
}
//定义配置文件地址
std::string strConfigYamlPath = "./config/config.yaml";
std::string strModelConfigYamlPath = "./config/model_config.yaml";
std::string strEngineYamlPath = "./config/matrix.yaml";

int main(int argc, const char *argv[])
{
    if (argc > 1)
    {
        strConfigYamlPath = argv[1];
        printf("configpath:%s\n", strConfigYamlPath.c_str());
    }

    LogInfo << "configpath:" << strConfigYamlPath;
    //加载配置文件
    int iRetYaml = Config::getins()->readYaml(strConfigYamlPath);
    if (-1 == iRetYaml)
    {
        LogError << "read config.yaml file error";
        return -1;
    }

    iRetYaml = Config::getins()->readModelYaml(strModelConfigYamlPath);
    if (-1 == iRetYaml)
    {
        LogError << "read model_config.yaml file error";
        return -1;
    }

    ai_matrix::BaseConfig baseConfig = Config::getins()->getBaseConfig();
    ai_matrix::LogConfig logConfig = Config::getins()->getLogConfig();

    //设置日志信息
    MatrixAILog::Log::SetLogLevel(logConfig.strOutLevel);
    MatrixAILog::Log::SetLogPath(baseConfig.strLogPath);

    if (!FileUtil::getins()->createDirPath("./logs"))
    {
        LogError << "日志目录创建失败";
        return -1;
    }

    ToWeb::getins()->setConfig(Config::getins()->getHttpServerConfig());

    //捕获信号
    if (signal(SIGINT, SigHandler) == SIG_ERR)
    {
        LogError << "cannot catch SIGINT.";
        return -1;
    }

    //acl初始化
    EngineManager engineManager;
    int ret = engineManager.Init();
    if (ret != APP_ERR_OK)
    {
        LogError << "engineManager init error";
        return -1;
    }
    ret = engineManager.load_yaml_config(strEngineYamlPath);
    if (ret != APP_ERR_OK)
    {
        LogError << "load matrix.yaml error";
        return -1;
    }

    engineManager.RunAllEngine();
    while (app_flag)
    {
        sleep(1);
    }
    //acl去初始化
    engineManager.StopAllEngine();
    engineManager.DeInit();
    //停止特殊线程
    LogInfo << "app end";
    return 0;
}
