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

#include <vcl.h>
#pragma hdrstop

#include "VMSCommLibF.h"

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

#pragma package(smart_init)

SYSTEM_INFORMATION *g_SysInfo;
bool                LOAD_DB = true;

APP_CONFIG  g_AppCfg;
LOG_INFO    g_LogCfg;
TITSLog    *ITSLog = NULL;

TItsNetQ    g_logQ;
TItsNetQ    g_dbmQ;
TItsNetQ    g_dbmReadQ;
TItsNetQ    g_jobQ;

TItsQBuff<IPC_LOG_MESSAGE, MAX_LOGQ_BUFF> g_logBuff;
TItsQBuff<IPC_DBM_MESSAGE, MAX_DBMQ_BUFF> g_dbmBuff;
TItsQBuff<IPC_DBM_MESSAGE, MAX_DBMQ_BUFF> g_dbmReadBuff;
TItsQBuff<IPC_JOB_MESSAGE, MAX_JOBQ_BUFF> g_jobBuff;

String          g_sAppDir  = "";       // Application Directory
String          g_sAppName = "";       // Program name
String          g_sCfgDir  = "";       // Program Config Directory
String          g_sLogDir  = "";       // Program Log Directory
String          g_sTempDir = "";       // Program Temp Directory
String          g_sImgDir = "";        // Program Image Directory

String          g_sFtpHomeDir   = "";  // FTP Home Directory
String          g_sFtpFormDir   = "";  // FTP Form Directory
String          g_sFtpVideoDir  = "";  // FTP Video Directory
String          g_sFtpStaticDir = "";  // FTP 정적이미지 Directory
String          g_sFtpImageDir  = "";  // FTP 이미지 Directory

GRADE_TEXT      FIGURE;
GRADE_TEXT      TEXT;


int APP_LogWrite(void* ALogObj, int ALogKind, int ATarget, int AFrom, char *AFmt, ...)
{
    va_list ap;
    int     cnt = 0;

    String  sLogKind = " [XXX] ";
    bool bLog = false;
    switch(ALogKind)
    {
    case eLOG_INFO   : bLog = g_LogCfg.Info;    sLogKind = " [INF] "; break;
    case eLOG_DATA   : bLog = g_LogCfg.Data;    sLogKind = " [DAT] "; break;
    case eLOG_ERROR  : bLog = g_LogCfg.Error;   sLogKind = " [ERR] "; break;
    case eLOG_WARNING: bLog = g_LogCfg.Warning; sLogKind = " [WAN] "; break;
    case eLOG_DEBUG  : bLog = g_LogCfg.Debug;   sLogKind = " [DBG] "; break;
    case eLOG_DETAIL : bLog = g_LogCfg.Detail;  sLogKind = " [DET] "; break;
    }
    if (!bLog) return -1;

    try
    {
        IPC_LOG_MESSAGE *pLog = g_logBuff.GetBuff();
        if (pLog)
        {
            memset(pLog->Msg, 0x00, sizeof(pLog->Msg));

            va_start(ap, AFmt);
            cnt = vsprintf(pLog->Msg, AFmt, ap);
            va_end(ap);

            pLog->Target = AFrom;
            pLog->From   = ATarget;
            pLog->Kind   = ALogKind;
            pLog->Flag   = 0;
            pLog->Write  = true;
            pLog->Tm     = Now();
            pLog->Type   = 0;
            pLog->Obj    = ALogObj;
            pLog->Ctlr   = NULL;
            pLog->Len    = strlen(pLog->Msg);

            g_logQ.PushTimeout((DWORD)pLog, 2000);
#if 0
            SingleLogThread->AddSysLogd(Now().FormatString("hh:nn:ss") + sLogKind + String(pLog->Msg));
#endif
        }
    }
    catch(Exception &e)
    {
    }
    return cnt;
}
//---------------------------------------------------------------------------

int APP_PostDbThreadWriteMessage(e_dbm_command ACommand, int ASize, void *AData)
{
    IPC_DBM_MESSAGE *pDbm = g_dbmBuff.GetBuff();
    if (pDbm)
    {
        pDbm->Command = ACommand;
        pDbm->Size    = ASize;
        if (ASize > 0)
        {
            memcpy(pDbm->U.Data, AData, ASize);
        }
        else
        {
            pDbm->U.Data[0] = 0x00;
        }

        g_dbmQ.PushTimeout((DWORD)pDbm, 2000);
    }
    return 0;
}
//---------------------------------------------------------------------------

int APP_PostDbThreadReadMessage(e_dbm_command ACommand, int ASize, void *AData)
{
    IPC_DBM_MESSAGE *pDbm = g_dbmReadBuff.GetBuff();
    if (pDbm)
    {
        pDbm->Command = ACommand;
        pDbm->Size    = ASize;
        if (ASize > 0)
        {
            memcpy(pDbm->U.Data, AData, ASize);
        }
        else
        {
            pDbm->U.Data[0] = 0x00;
        }

        g_dbmReadQ.PushTimeout((DWORD)pDbm, 2000);
    }
    return 0;
}
//---------------------------------------------------------------------------
AnsiString __fastcall APP_GetDbJobName(int nCommand)
{
    switch(nCommand)
    {
    case dbm_save_unit_stts:        return "dbm_save_unit_stts";
    case dbm_save_ctlr_stts:        return "dbm_save_ctlr_stts";
    case dbm_param_res:             return "dbm_param_res";
    case dbm_save_ctlr_stts_all:    return "dbm_save_ctlr_stts_all";
    case dbm_load_onoff_time:       return "dbm_load_onoff_time";
    case dbm_load_download_job:     return "dbm_load_download_job";
    case dbm_save_form_download:    return "dbm_save_form_download";
    case dbm_dnld_form_init:        return "dbm_dnld_form_init";
    case dbm_dnld_form_cycle:       return "dbm_dnld_form_cycle";
    case dbm_dnld_form_mode:        return "dbm_dnld_form_mode";
    case dbm_dnld_form_eveh:        return "dbm_dnld_form_eveh";
    case dbm_provide_result:        return "dbm_provide_result";
    case dbm_master_symbol_image:   return "dbm_master_symbol_image";
    case dbm_master_symbol_traffic: return "dbm_master_symbol_traffic";
    case dbm_master_form:           return "dbm_master_form";
    case dbm_master_vms_ifsc:       return "dbm_master_vms_ifsc";
    case dbm_check_db_alive:        return "dbm_check_db_alive";
    case dbm_initialize:            return "dbm_initialize";

    case dbm_job_start:             return "dbm_job_start";
    case dbm_status:                return "dbm_status";
    case dbm_save_unit_stts_hs:     return "dbm_save_unit_stts_hs";
    case dbm_save_ctlr_stts_hs:     return "dbm_save_ctlr_stts_hs";
    case dbm_check_evehicle_alarm:  return "dbm_check_evehicle_alarm";
    default:
        break;
    }
    return AnsiString(nCommand);
}
//---------------------------------------------------------------------------


