//---------------------------------------------------------------------------


#pragma hdrstop

#include "CDSVmsCtlrF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

//---------------------------------------------------------------------------
TVmsCtlrManager *VmsCtlrManager = NULL;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int TVmsCtlrManager::Load()
{
    FLists.RemoveAll();

    String apiUri = g_AppCfg.sRestApiUri + "/api/vms/common/vms-list";
    TMcJsonItem *ObjList = NULL;
    try {
        ObjList = TRestObjectManager::RequestGet(apiUri);
        if (ObjList == NULL) {
            return 0;
        }
        for (int ii = 0; ii < ObjList->Count; ii++) {
            String sDelYn = TRestObjectManager::GetValue(ObjList->Items[ii], "del_yn");
            if (sDelYn == "Y") {
                continue;
            }

            TVmsCtlr *pObj = new TVmsCtlr();
            pObj->VMS_CTLR_NMBR     = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_nmbr");
            pObj->VMS_CTLR_ID       = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_id");
            pObj->VMS_NM            = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_nm");
            pObj->VMS_CTLR_IP       = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_ip");
            pObj->VMS_CTLR_PORT     = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_port");
            pObj->VMS_CTLR_LOCAL_NO = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_local_no");
            pObj->VMS_TYPE_CD       = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_type_cd");
            pObj->ISTL_LCTN_NM      = TRestObjectManager::GetValue(ObjList->Items[ii], "istl_lctn_nm");
            pObj->X_CRDN            = TRestObjectManager::GetValue(ObjList->Items[ii], "x_crdn");
            pObj->Y_CRDN            = TRestObjectManager::GetValue(ObjList->Items[ii], "y_crdn");
            pObj->WEB_CMRA_IP       = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_ip");
            pObj->WEB_CMRA_PORT     = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_port");
            pObj->WEB_CMRA_ID       = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_id");
            pObj->WEB_CMRA_PWD      = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_pwd");
            pObj->STRM_SESN_NM      = TRestObjectManager::GetValue(ObjList->Items[ii], "strm_sesn_nm");
            pObj->OPER_MODE         = TRestObjectManager::GetValue(ObjList->Items[ii], "oper_mode");
            pObj->VMS_MAX_PHSE_NUM  = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_max_phse_num");
            pObj->DEF_PHSE_CHNG_CYCL= TRestObjectManager::GetValue(ObjList->Items[ii], "def_phse_chng_cycl");
            pObj->PANL_ON_TIME      = TRestObjectManager::GetValue(ObjList->Items[ii], "panl_on_time");
            pObj->PANL_OFF_TIME     = TRestObjectManager::GetValue(ObjList->Items[ii], "panl_off_time");
            pObj->PANL_PWER_MODE    = TRestObjectManager::GetValue(ObjList->Items[ii], "panl_pwer_mode");
            pObj->FAN_MODE          = TRestObjectManager::GetValue(ObjList->Items[ii], "fan_mode");
            pObj->FAN_RUN_TMPR      = TRestObjectManager::GetValue(ObjList->Items[ii], "fan_run_tmpr");
            pObj->HETR_MODE         = TRestObjectManager::GetValue(ObjList->Items[ii], "hetr_mode");
            pObj->HETR_RUN_TMPR     = TRestObjectManager::GetValue(ObjList->Items[ii], "hetr_run_tmpr");
            pObj->BRGH_MODE         = TRestObjectManager::GetValue(ObjList->Items[ii], "brgh_mode");
            pObj->BRGH_CURR_STEP    = TRestObjectManager::GetValue(ObjList->Items[ii], "brgh_curr_step");
            pObj->BRGH_WEEK_STEP    = TRestObjectManager::GetValue(ObjList->Items[ii], "brgh_week_step");
            pObj->BRGH_NGHT_STEP    = TRestObjectManager::GetValue(ObjList->Items[ii], "brgh_nght_step");
            pObj->MODL_ERR_RATE     = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_err_rate");
            pObj->CMNC_FAIL_RATE    = TRestObjectManager::GetValue(ObjList->Items[ii], "cmnc_fail_rate");
            pObj->WEB_CMRA_CMPY     = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_cmpy");
            pObj->WEB_CMRA_MDL      = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_mdl");
            pObj->ISTL_LCTN_ADDR    = TRestObjectManager::GetValue(ObjList->Items[ii], "istl_lctn_addr");
            pObj->STRM_HTTP_ADDR    = TRestObjectManager::GetValue(ObjList->Items[ii], "strm_http_addr");
            pObj->STRM_RTSP_ADDR    = TRestObjectManager::GetValue(ObjList->Items[ii], "strm_rtsp_addr");
            pObj->DEL_YN = sDelYn;

            pObj->VMS_TYPE_DESC     = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_type_desc");    // "[VMP0] 2단10열"


            FLists.Push(pObj->VMS_CTLR_NMBR, pObj);
        }
    } __finally {
        if (ObjList) delete ObjList;
    }
    return FLists.Size();
}
//---------------------------------------------------------------------------

int TVmsCtlrManager::LoadStts()
{
    String apiUri = g_AppCfg.sRestApiUri + "/api/vms/monitoring/stts";
    TMcJsonItem *ObjList = NULL;
    try {
        ObjList = TRestObjectManager::RequestGet(apiUri);
        if (ObjList == NULL) {
            return 0;
        }
        for (int ii = 0; ii < ObjList->Count; ii++) {
            String VMS_CTLR_NMBR = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_nmbr");

            TVmsCtlr *pObj = FLists.Find(VMS_CTLR_NMBR);
            if (pObj == NULL) {
                continue;
            }

            pObj->VMS_WDTH  = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_wdth").ToIntDef(0);
            pObj->VMS_HGHT  = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_hght").ToIntDef(0);
            pObj->ROWS      = TRestObjectManager::GetValue(ObjList->Items[ii], "rows").ToIntDef(0);
            pObj->COLS      = TRestObjectManager::GetValue(ObjList->Items[ii], "cols").ToIntDef(0);
            pObj->MODL_ROWS = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_rows").ToIntDef(0);
            pObj->MODL_COLS = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_cols").ToIntDef(0);
            pObj->POWR_ROWS = TRestObjectManager::GetValue(ObjList->Items[ii], "powr_rows").ToIntDef(0);
            pObj->POWR_COLS = TRestObjectManager::GetValue(ObjList->Items[ii], "powr_cols").ToIntDef(0);

            pObj->OPER_MODE      = TRestObjectManager::GetValue(ObjList->Items[ii], "oper_mode");
            pObj->OPER_MODE_DESC = TRestObjectManager::GetValue(ObjList->Items[ii], "oper_mode_desc");

            pObj->PANL_ON_TIME   = TRestObjectManager::GetValue(ObjList->Items[ii], "panl_on_time");
            pObj->PANL_OFF_TIME  = TRestObjectManager::GetValue(ObjList->Items[ii], "panl_off_time");

            pObj->FAN_RUN_TMPR   = TRestObjectManager::GetValue(ObjList->Items[ii], "fan_run_tmpr");
            pObj->HETR_RUN_TMPR  = TRestObjectManager::GetValue(ObjList->Items[ii], "hetr_run_tmpr");

            //pObj->VMS_CMNC_ERR_BASS_VAL;
            //pObj->VMS_NGHT_BRGH_STEP;
            //pObj->VMS_WEEK_BRGH_STEP;
            //pObj->VMS_PHSE_CHNG_CYCL;
            //pObj->VMS_MODL_ERR_RATE;

            // 상태정보
            TVmsCtlrStts *STTS = pObj->STTS;

            pObj->MISS_STTS_YN = TRestObjectManager::GetValue(ObjList->Items[ii], "miss_stts_yn");

            STTS->UPDT_DT           = TRestObjectManager::GetValue(ObjList->Items[ii], "updt_dt");
            STTS->CMNC_STTS_CD      = TRestObjectManager::GetValue(ObjList->Items[ii], "cmnc_stts_cd");
            STTS->PWER_STTS_CD      = TRestObjectManager::GetValue(ObjList->Items[ii], "pwer_stts_cd");
            STTS->MODL_STTS_CD      = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_stts_cd");
            STTS->CBOX_DOOR_STTS_CD = TRestObjectManager::GetValue(ObjList->Items[ii], "cbox_door_stts_cd");
            STTS->FAN_STTS_CD       = TRestObjectManager::GetValue(ObjList->Items[ii], "fan_stts_cd");
            STTS->HETR_STTS_CD      = TRestObjectManager::GetValue(ObjList->Items[ii], "hetr_stts_cd");
            STTS->CBOX_TMPR         = TRestObjectManager::GetValue(ObjList->Items[ii], "cbox_tmpr").ToIntDef(0);
            STTS->BRGH_VAL          = TRestObjectManager::GetValue(ObjList->Items[ii], "brgh_val").ToIntDef(0);
            STTS->COMM_STTS_CD      = TRestObjectManager::GetValue(ObjList->Items[ii], "comm_stts_cd");
            STTS->MODL_STTS         = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_stts");
            STTS->PWER_STTS         = TRestObjectManager::GetValue(ObjList->Items[ii], "pwer_stts");

            STTS->CMNC_STTS_DESC      = TRestObjectManager::GetValue(ObjList->Items[ii], "cmnc_stts_desc");
            STTS->PWER_STTS_DESC      = TRestObjectManager::GetValue(ObjList->Items[ii], "pwer_stts_desc");
            STTS->MODL_STTS_DESC      = TRestObjectManager::GetValue(ObjList->Items[ii], "modl_stts_desc");
            STTS->CBOX_DOOR_STTS_DESC = TRestObjectManager::GetValue(ObjList->Items[ii], "cbox_door_stts_desc");
            STTS->FAN_STTS_DESC       = TRestObjectManager::GetValue(ObjList->Items[ii], "fan_stts_desc");
            STTS->HETR_STTS_DESC      = TRestObjectManager::GetValue(ObjList->Items[ii], "hetr_stts_desc");
            STTS->COMM_STTS_DESC      = TRestObjectManager::GetValue(ObjList->Items[ii], "comm_stts_desc");

            // 정보제공상태
            STTS->PROVIDE_TIME   = TRestObjectManager::GetValue(ObjList->Items[ii], "provide_time");
            STTS->PROVIDE_RESULT = TRestObjectManager::GetValue(ObjList->Items[ii], "provide_result");
            STTS->PROVIDE_SVC    = TRestObjectManager::GetValue(ObjList->Items[ii], "provide_svc");
            STTS->PROVIDE_CNT    = TRestObjectManager::GetValue(ObjList->Items[ii], "provide_cnt").ToIntDef(0);
            STTS->AUTO_TOTAL     = TRestObjectManager::GetValue(ObjList->Items[ii], "auto_total").ToIntDef(0);
            STTS->AUTO_USED      = TRestObjectManager::GetValue(ObjList->Items[ii], "auto_used").ToIntDef(0);
            STTS->FIXED_TOTAL    = TRestObjectManager::GetValue(ObjList->Items[ii], "fixed_total").ToIntDef(0);
            STTS->FIXED_USED     = TRestObjectManager::GetValue(ObjList->Items[ii], "fixed_used").ToIntDef(0);
            STTS->BASIC_TOTAL    = TRestObjectManager::GetValue(ObjList->Items[ii], "basic_total").ToIntDef(0);
            STTS->BASIC_USED     = TRestObjectManager::GetValue(ObjList->Items[ii], "basic_used").ToIntDef(0);
        }
    } __finally {
        if (ObjList) delete ObjList;
    }
    return FLists.Size();
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::Reset(String AUserId, String AVmsCtlrNmbr, int ACommand)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/reset/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;
        ObjList->Add("value")->AsInteger  = ACommand;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::PowerOnOff(String AUserId, String AVmsCtlrNmbr, int ACommand)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/power/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;
        ObjList->Add("value")->AsInteger  = ACommand;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::LuminanceSet(String AUserId, String AVmsCtlrNmbr, int ACommand, int ACurr, int AWeek, int ANght)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/luminance/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;
        ObjList->Add("value")->AsInteger  = ACommand;
        ObjList->Add("curr_step")->AsInteger = ACurr;
        ObjList->Add("week_step")->AsInteger = AWeek;
        ObjList->Add("nght_step")->AsInteger = ANght;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::OperModeSet(String AUserId, String AVmsCtlrNmbr, String ACommand)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/opr-mode/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;
        ObjList->Add("opr_mode")->AsString  = ACommand;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::ImmediatelyScnrDnld(String AUserId, String AVmsCtlrNmbr)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/imm-scnr-dnld/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TVmsCtlrManager::ParamSet(String AUserId, String AVmsCtlrNmbr, int AFanRunTmpr, int AHetrRunTmpr, String AOnTime, String AOffTime)
{
    String resultVal;
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/control/param/" + FApiKey + "/" + AVmsCtlrNmbr;
    TMcJsonItem *ObjList = new TMcJsonItem();
    try
    {
        ObjList->Add("user_id")->AsString = AUserId;
        ObjList->Add("fan_run_tmpr")->AsInteger = AFanRunTmpr;
        ObjList->Add("hetr_run_tmpr")->AsInteger = AHetrRunTmpr;
        ObjList->Add("panl_on_time")->AsString = AOnTime;
        ObjList->Add("panl_off_time")->AsString = AOffTime;

        bool result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }
        if (resultVal != "0") {
            return false;
        }
    }
    __finally
    {
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------


