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

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

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

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

#include "EngineManager.h"
using namespace ai_matrix;

#include "AppCommon.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);   //停止后再次行驶的集装箱原始帧号

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

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

    //加载配置文件
    int iRetYaml = MyYaml::GetIns()->ReadYaml(strConfigYamlPath);
    if (-1 == iRetYaml)
    {
        printf("-read yaml file error\n");
        return -1;
    }

    //设置日志信息
    MatrixAILog::Log::SetLogLevel(MyYaml::GetIns()->GetStringValue("gc_log_level"));
    MatrixAILog::Log::SetLogFile(MyYaml::GetIns()->GetStringValue("gc_log_logfile"),
                                 MyYaml::GetIns()->GetStringValue("gc_log_logfile_bakpath"));

    // The string pointed to by argv[0] represents the program name
    std::string path(argv[0], argv[0] + strlen(argv[0]));
    APP_ERROR ret = ChangeDir(path.c_str()); // Change the directory to the excute file
    if (ret != APP_ERR_OK)
    {
        LogError << "Failed to change directory to " << path.c_str();
        return ret;
    }

    // if (MyYaml::GetIns()->GetBoolValue("gc_check_register"))
    // {
    //     //检查授权文件是否正确
    //     bool bIsRegister = check_xml(strRegisterPath.c_str());
    //     if (!bIsRegister)
    //     {
    //         LogError << "not register";
    //         return -1;
    //     }
    //     LogDebug << "register ok";
    // }

    //实例化myutils
    MyUtils::getins();

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

    //acl初始化
    EngineManager engineManager;
    ret = engineManager.Init();
    if (ret != APP_ERR_OK)
    {
        LogError << "engineManager init error";
        return -1;
    }
    ret = engineManager.load_yaml_config("./config/matrix.yaml");
    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;
}
