/**
 *
 *  gsd_HttpPlugin.cc
 *
 */

#include "gsd_HttpPlugin.h"

using namespace drogon;
using namespace gsd;

void HttpPlugin::initAndStart(const Json::Value &config)
{
    /// Initialize and start the plugin
    if(this->Init() != OK){
        ErrorL << "HttpPlugin 初始化失败" << endl;
    }
}

void HttpPlugin::shutdown() 
{
    /// Shutdown the plugin
} 

/**
 * @description: 初始化
 * @return {*}
 */
int8_t HttpPlugin::Init(){
    if(!config::getPtr()->debug) {
        if(!config::getPtr()->LowVersion){
            if(this->m_httpClient == nullptr) this->m_httpClient = HttpClient::CreateNew();
            // HTTP NETTY登录
            if(this->m_httpClient->Init( config::getPtr()->usr,  config::getPtr()->NettyIP, 8860) == OK){
                InfoL << "The Netty login succeeds. Procedure" << endl;
            }else{
                WarnL << "Netty login failed. Procedure" << endl;
            }
        }
    }
    return OK;
}

/**
 * @description: 启动任务
 * @return {*}
 */
void HttpPlugin::StartTask(){
    this->timer0 = std::make_shared<Timer>(2.0f, [&](){
        if(!config::getPtr()->debug) {
            if(!config::getPtr()->LowVersion){
                if(!this->m_httpClient->alive()){
                    this->m_httpClient->LoginNetty();
                }else{
                    this->getHttpInfo();
                }
            }
        }
        return true;
    }, nullptr);
}   

/**
 * @description: 获取数据
 * @param {*}
 * @return {*}
 */
void HttpPlugin::getHttpInfo()
{
    try
    {
        NettyHttpResultMsg<NettyExpelDevInfo,NettyHttpNull> nettyHttpResultMsg;
        NettyHttpResultMsg<NettyHttpDicCode,NettyHttpNull> nettyHttpResultMsg1;
        ExpelDevice expelDevice;
        if(this->m_httpClient->getHttpDicCode("peyServerIp", nettyHttpResultMsg1) == OK){
            if(!nettyHttpResultMsg1.datas.empty()){
                auto iter = nettyHttpResultMsg1.datas.begin();
                expelDevice.ServerIp = iter->attr1;
                nettyHttpResultMsg1.datas.clear();
            }
        }
        if(this->m_httpClient->getHttpDicCode("peyServerCname", nettyHttpResultMsg1) == OK){
            if(!nettyHttpResultMsg1.datas.empty()){
                 auto iter = nettyHttpResultMsg1.datas.begin();
                expelDevice.ServerCname = iter->attr1;
                nettyHttpResultMsg1.datas.clear();
            }
        }
        if(this->m_httpClient->getHttpDicCode("peyServerCpwd", nettyHttpResultMsg1) == OK){
             if(!nettyHttpResultMsg1.datas.empty()){
                 auto iter = nettyHttpResultMsg1.datas.begin();
                expelDevice.ServerCpwd = iter->attr1;
                nettyHttpResultMsg1.datas.clear();
             }
        }
        config::Ptr aconfig = config::getPtr();
        if(this->m_httpClient->getExpelInfo(aconfig->SimCode, nettyHttpResultMsg) == OK){
            char ctime[80];
            HttpClient::getDataTime(ctime);
            std::string updateTime = ctime;
            int i = 1;
            vector<vector<std::string>> ret;
            // 同步数据
            for(auto iter = nettyHttpResultMsg.datas.begin(); iter != nettyHttpResultMsg.datas.end(); iter++){
                expelDevice.DeviceId = iter->birdDeviceCode;
                switch ((int)iter->deviceControlType)
                {
                case 2:
                    // 上海民航1 
                    expelDevice.deviceType = DeviceGasV1;
                    expelDevice.deviceToken = serial;
                    break;
                case 3:
                    // 上海民航2
                    expelDevice.deviceType = DeviceUlt;
                    expelDevice.deviceToken = serial;
                    break;
                case 4:
                    // 兰州
                    expelDevice.deviceType = DeviceGasV2;
                    expelDevice.deviceToken = serial;
                    break;

                case 5:
                    // 派尔云
                    expelDevice.deviceType = DevicePlayer;
                    expelDevice.deviceToken = http;
                    break;
                default:
                    // 协议不存在
                    return;
                    break;
                }
                expelDevice.UpdateTime = updateTime;
                expelDevice.Id = i;
                std::string sql = expelDevice.ObjectToSql();
                SqlWriter writer(sql.c_str());
                writer << ret;
                i++;
            }
            SqlWriter Update("UPDATE gsdDB.`DeviceList` SET ISDELETE = 1 WHERE UpdateTime != '?'");
            Update << updateTime << ret;
        }
    }
    catch(const std::exception& e)
    {
        ErrorL << e.what() << '\n';
    }
}