/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-07-11 00:13:35
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-08-02 16:06:03
 * @FilePath: /gsd/plugins/gsd_userApp.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 *
 *  gsd_userApp.cc
 *
 */

#include "gsd_userApp.h"
#include <iostream>
#include "Util/logger.h"
#include "Util/SqlPool.h"
#include "config.hpp"
#include "gsd_TcpPlugin.h"
#include "gsd_ExpelPlugin.h"
#include "gsd_HttpPlugin.h"
#include "gsd_MonitorPlugin.h"

using namespace std;
using namespace drogon;
using namespace gsd;
using namespace toolkit;

void userApp::initAndStart(const Json::Value &config)
{
    if(this->Init() != OK) {
        ErrorL << "userApp初始化失败" << endl;
        return;
    }
    this->StartTask();
}   

void userApp::shutdown() 
{
    
}

/**
 * @description: 初始化
 * @return {*}
 */
int8_t userApp::Init(){
    // 配置更新
    config::getPtr()->Update(); 
    
    #if defined(SUPPORT_DYNAMIC_TEMPLATE)
        SqlPool::Instance().Init("127.0.0.1",3306,"","root","sunwin2022"/*,character*/);
    #else
        //由于需要编译器对可变参数模板的支持，所以gcc5.0以下一般都不支持，否则编译报错
        ErrorL << "your compiler does not support variable parameter templates!" << endl;
        return -1;
    #endif //defined(SUPPORT_DYNAMIC_TEMPLATE)
    InfoL << "Starting Device, SN:" <<  config::getPtr()->getSimCode() << ",gsdApp:" <<  config::getPtr()->getApp() << ", Address:" <<  config::getPtr()->vpnIP << ", Version:" <<  config::getPtr()->getVersion() << ", Low:" <<  config::getPtr()->LowVersion << endl;
    
    NoticeCenter::Instance().addListener(0, NOTICE_DEVICEINFO,
        [&](std::string requestId,std::string json){
        if(app().getPlugin<TcpPlugin>()->getAlive()){
            json += "\r\n";
            app().getPlugin<TcpPlugin>()->sendRequest(requestId, NettyClientCommandEnum().device_info, json,[&](int status,std::string buf){});
        }
    });
    
    // TCP
    app().getPlugin<TcpPlugin>()->StartTask();
    app().getPlugin<ExpelPlugin>()->StartTask();
    app().getPlugin<MonitorPlugin>()->StartTask();
    app().getPlugin<HttpPlugin>()->StartTask();

    return OK;
}

/**
    * @description: 启动任务
    * @return {*}
    */    
void userApp::StartTask(){

    

}