/****************************************************************************
** File name    :  busi_database.cpp
** Description  :  define iec 61850 dat file parser api
** Create date  :  2019.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyrigth By:  xi'an huatek, Inc Co., Ltd
** Update record:
** DATE         AUTHER   DESC
** -------------------------------------------------------------------------
** 2019.09.01   Liuyx    first build
****************************************************************************/
#include "HTGlobal.h"
static const char *_FILE_ = "HTBusiDatabase.cpp";

/*******************************************************************************
** Function Name :  vLoadCommunicationConf
** Description   :  加载通信参数
** Parameter In  :  None
**   param name  :  
** Parameter Out :  None
**   param name  :  
** Return Code   :  void
**        RET_OK :  Successed
**      RET_FAIL :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:17 10:56
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/17          : caeated
********************************************************************************/
void vLoadCommunicationConf()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;
    ST_IED_ADDR stIed;
    sprintf(szSql, "select protocol,serial_number,baud_rate,check_bit,data_bit,"
        "stop_bit,ip_addr,port,password from busi_communication_parameters where communication_interface='0' ");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        if (row[0] && strlen(row[0]) > 0) {
            if (atoi(row[0]) == 1)  g_TConfig.setCommProtocol(1);  // 通信模式 1:modbus
            if (atoi(row[0]) == 2)  g_TConfig.setCommProtocol(2);  // 通信模式 2:61850
        }
        if (row[1] && strlen(row[1]) > 0) {
            g_TConfig.setModbusRtuPort(row[1]);     // COMx
        }
        if (row[2] && strlen(row[2]) > 0) {
            g_TConfig.setModbusBaud(atoi(row[2]));     // baud
        }
        if (row[3] && strlen(row[3]) > 0) {
            g_TConfig.setModbusParity(row[3][0]);     // parity bit N=无 O=奇 E=偶
        }
        if (row[4] && strlen(row[4]) > 0) {
            g_TConfig.setModbusDataBit(atoi(row[4])); // data bit 6,7,8
        }
        if (row[5] && strlen(row[5]) > 0) {
            g_TConfig.setModbusStopBit(atoi(row[5])); // stop bit 1,2
        }
        if (row[6] && strlen(row[6]) > 0) {
            g_TConfig.setModbusTcpAddr(row[6]);       // tcp_ip addr for 61850
            strcpy(stIed.IedAddr, row[6]);
        }
        if (row[7] && strlen(row[7]) > 0) {
            g_TConfig.setModbusTcpPort(atoi(row[7])); // tcp_port for 61850
            stIed.port = atoi(row[7]);
        }
        if (row[8] && strlen(row[8]) > 0) {
            g_TConfig.setWifiPasswd(row[8]);          // wifi passwd
        }
        if (strlen(stIed.IedAddr) > 0 && stIed.port > 0) {
            g_TConfig.setIEDAddress(0, stIed);
            memset(&stIed, 0x00, sizeof(ST_IED_ADDR));
        }
    }
    pdbHandle->FreeRecord(res);
    return;
}

// 加载传感器基本信息
void vLoadBaseDeviceInfo()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_DEVICE_INFO>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;

    sprintf(szSql, "select m.id,m.system_code,m.device_name,s.id as sid,s.slaveid,s.sensor_code,s.device_type,"
        "s.side,s.phase,s.in_out,s.sub_device_name from busi_main_device m,busi_sub_device s "
        "where m.id = s.main_id and m.state = 'Y' ");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_DEVICE_INFO stThres;
        memset(&stThres, 0x00, sizeof(ST_DEVICE_INFO));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stThres.szMID, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            strcpy(stThres.szSysCode, row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stThres.szMDevName, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stThres.szSID, row[3]);
        }
        if (row[4] && strlen(row[4]) > 0) {
            stThres.cSlaveID = atoi(row[4]);
        }
        if (row[5] && strlen(row[5]) > 0) {
            strcpy(stThres.szSersorID, row[5]);
            strKey = stThres.szSersorID;    // key
        }
        if (row[6] && strlen(row[6]) > 0) {
            stThres.cDevType = row[6][0];
        }
        if (row[7] && strlen(row[7]) > 0) {
            stThres.cSide = row[7][0];
        }
        if (row[8] && strlen(row[8]) > 0) {
            stThres.cPhase = row[8][0];
        }
        if (row[9] && strlen(row[9]) > 0) {
            stThres.cInOut = row[9][0];
        }
        if (row[10] && strlen(row[10]) > 0) {
            strcpy(stThres.szSubName, row[10]);
        }
        mutex_lock(g_map_device_mutex);
        m_pIter = g_map_device.find((char*)strKey.c_str());
        if (m_pIter == g_map_device.end())
        {
            // 新配置数据
            g_map_device.insert(map<string, ST_DEVICE_INFO>::value_type(strKey, stThres));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stThres, sizeof(ST_DEVICE_INFO));
        }
        mutex_unlock(g_map_device_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}
/*******************************************************************************
** Function Name :  vLoadAmpliDeviceThreshold
** Description   :  加载振动装置阈值配置信息
** Parameter In  :  None
**   param name  :  
** Parameter Out :  None
**   param name  :  
** Return Code   :  void
**        RET_OK :  Successed
**      RET_FAIL :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:17 11:18
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/17          : created
********************************************************************************/
void vLoadAmpliDeviceThreshold()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_THRES_CONF>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;

    sprintf(szSql, "select m.id as mid,m.system_code,m.device_name,s.id as sid,s.sub_device_name,"
        "s.device_type,s.sensor_code, s.side, s.phase, s.in_out, f.frequency, t.amplitude "
        "from busi_sub_device s, busi_main_device m, busi_frequency_conf f, busi_ampli_threshold_conf t "
        "where s.main_id = m.id and f.id = t.frequencyid and t.sub_id = s.id "
        "ORDER BY s.sensor_code, cast(f.frequency as unsigned INTEGER)");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_THRES_CONF stThres;
        memset(&stThres, 0x00, sizeof(ST_THRES_CONF));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stThres.szMID, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            strcpy(stThres.szSysCode, row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stThres.szMDevName, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stThres.szSubID, row[3]);
        }
        if (row[4] && strlen(row[4]) > 0) {
            strcpy(stThres.szSDevName, row[4]); 
        }
        if (row[5] && strlen(row[5]) > 0) {
            stThres.bDevType = row[5][0];
        }
        if (row[6] && strlen(row[6]) > 0) {
            strcpy(stThres.szSensorID, row[6]);
            strKey = stThres.szSensorID;
        }
        if (row[7] && strlen(row[7]) > 0) {
            stThres.bSide = row[7][0];
        }
        if (row[8] && strlen(row[8]) > 0) {
            stThres.bPhase = row[8][0];
        }
        if (row[9] && strlen(row[9]) > 0) {
            stThres.bInOut = row[9][0];
        }
        if (row[10] && strlen(row[10]) > 0) {
            strcpy(stThres.szFrequency, row[10]);
            strKey.append("-");
            strKey.append(row[10]);
        }
        if (row[11] && strlen(row[11]) > 0) {
            stThres.dThreshold = atof(row[11]); 
        }

        mutex_lock(g_thres_conf_mutex);
        m_pIter = g_thres_conf.find((char*)strKey.c_str());
        if (m_pIter == g_thres_conf.end())
        {
            // 新配置数据
            g_thres_conf.insert(map<string, ST_THRES_CONF>::value_type(strKey, stThres));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stThres, sizeof(ST_THRES_CONF));
        }
        mutex_unlock(g_thres_conf_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}

/*******************************************************************************
** Function Name :  vLoadNoiseDeviceThreshold
** Description   :  加载噪声装置阈值配置信息
** Parameter In  :  None
**   param name  :
** Parameter Out :  None
**   param name  :
** Return Code   :  void
**        RET_OK :  Successed
**      RET_FAIL :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:17 11:18
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/17          : created
********************************************************************************/
void vLoadNoiseDeviceThreshold()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_THRES_CONF>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;

    sprintf(szSql, "select m.id as mid,m.system_code,m.device_name,s.id,s.sub_device_name,s.device_type,s.sensor_code,c.noise "
        "from busi_main_device m, busi_sub_device s, busi_noise_threshold_conf c "
        "where m.id = s.main_id and s.id = c.sub_id and m.id = c.main_id ");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_THRES_CONF stThres;
        memset(&stThres, 0x00, sizeof(ST_THRES_CONF));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stThres.szMID, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            strcpy(stThres.szSysCode, row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stThres.szMDevName, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stThres.szSubID, row[3]);
        }
        if (row[4] && strlen(row[4]) > 0) {
            strcpy(stThres.szSDevName, row[4]);
        }
        if (row[5] && strlen(row[5]) > 0) {
            stThres.bDevType = row[5][0];
        }
        if (row[6] && strlen(row[6]) > 0) {
            strcpy(stThres.szSensorID, row[6]);
            strKey = stThres.szSensorID;
        }
        if (row[7] && strlen(row[7]) > 0) {
            stThres.dThreshold = atof(row[7]);
        }
                
        mutex_lock(g_thres_conf_mutex);
        m_pIter = g_thres_conf.find((char*)stThres.szSensorID);
        if (m_pIter == g_thres_conf.end())
        {
            // 新配置数据
            g_thres_conf.insert(map<string, ST_THRES_CONF>::value_type(strKey, stThres));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stThres, sizeof(ST_THRES_CONF));
        }
        mutex_unlock(g_thres_conf_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}

/*******************************************************************************
** Function Name :  vLoadCenterDeviceThreshold
** Description   :  加载中心点装置阈值配置信息
** Parameter In  :  None
**   param name  :
** Parameter Out :  None
**   param name  :
** Return Code   :  void
**        RET_OK :  Successed
**      RET_FAIL :  Failed
** Create by     :  Liuyx
** Create date   :  2020:7:17 11:18
** History modify:  Liuyx
** Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
**-----------------------------------------------------------------------
** Modify by     :  Date            : Context
** Liuyx  :  2020/07/17          : created
********************************************************************************/
void vLoadCenterDeviceThreshold()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_THRES_CONF>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;

    sprintf(szSql, "select m.id as mid, m.system_code,m.device_name,s.id,s.sub_device_name,s.device_type,s.sensor_code,c.negative "
        "from busi_main_device m, busi_sub_device s, busi_center_threshold_conf c "
        "where m.id = s.main_id and s.id = c.sub_id and m.id = c.main_id ");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_THRES_CONF stThres;
        memset(&stThres, 0x00, sizeof(ST_THRES_CONF));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stThres.szMID, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            strcpy(stThres.szSysCode, row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stThres.szMDevName, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stThres.szSubID, row[3]);
        }
        if (row[4] && strlen(row[4]) > 0) {
            strcpy(stThres.szSDevName, row[4]);
        }
        if (row[5] && strlen(row[5]) > 0) {
            stThres.bDevType = row[5][0];
        }
        if (row[6] && strlen(row[6]) > 0) {
            strcpy(stThres.szSensorID, row[6]);
            strKey = stThres.szSensorID;
        }
        if (row[7] && strlen(row[7]) > 0) {
            stThres.dThreshold = atof(row[7]);
        }
        mutex_lock(g_thres_conf_mutex);
        m_pIter = g_thres_conf.find((char*)stThres.szSensorID);
        if (m_pIter == g_thres_conf.end())
        {
            // 新配置数据
            g_thres_conf.insert(map<string, ST_THRES_CONF>::value_type(strKey, stThres));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stThres, sizeof(ST_THRES_CONF));
        }
        mutex_unlock(g_thres_conf_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}

// 加载控制参数
void vLoadDeviceCtrlParam()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_CONTROL_CONF>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;

    sprintf(szSql, "select m.id as mid,m.system_code,s.id as sid,s.sensor_code,s.sub_device_name,"
        "c.isenable, c.intervals, c.gather_time, c.smpprod, c.alone_test "
        "from busi_main_device m, busi_sub_device s, busi_control_conf c "
        "where c.main_id = s.main_id and m.id = c.main_id and s.id = c.sub_id "
        "ORDER BY c.main_id, c.sub_id");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_CONTROL_CONF stCtrl;
        memset(&stCtrl, 0x00, sizeof(ST_CONTROL_CONF));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stCtrl.szMID, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            strcpy(stCtrl.szSysCode, row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stCtrl.szSubID, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stCtrl.szSensorID, row[3]);
            strKey = stCtrl.szSensorID;
        }
        if (row[4] && strlen(row[4]) > 0) {
            strcpy(stCtrl.szSubName, row[4]);
        }
        if (row[5] && strlen(row[5]) > 0) {
            stCtrl.isEnable = (atoi(row[5]) == 1 ? 1:0);
        }
        if (row[6] && strlen(row[6]) > 0) {
            stCtrl.SmpInt = atoi(row[6]);
        }
        if (row[7] && strlen(row[7]) > 0) {
            stCtrl.StartTime = atoi(row[7]);
        }
        if (row[8] && strlen(row[8]) > 0) {
            stCtrl.SmpProd = atoi(row[8]);
        }
        if (row[9] && strlen(row[9]) > 0) {
            stCtrl.alone = (atoi(row[9]) == 1 ? 1:0);
        }
        mutex_lock(g_ctrl_conf_mutex);
        m_pIter = g_ctrl_conf.find((char*)stCtrl.szSensorID);
        if (m_pIter == g_ctrl_conf.end())
        {
            // 新配置数据
            g_ctrl_conf.insert(map<string, ST_CONTROL_CONF>::value_type(strKey, stCtrl));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stCtrl, sizeof(ST_CONTROL_CONF));
        }
        mutex_unlock(g_ctrl_conf_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}

// 加载ICD配置节点与传感器绑定关系
void vLoadICDParameterBind()
{
    char szSql[DEF_BUFFER_1K] = { 0 };
    map<string, ST_PARAM_BIND>::iterator m_pIter;
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) return;
    // 监测类型: 1=振动监测值 2=振动告警 3=噪声监测值 4=噪声告警 5=中心点监测 6=中心点告警
    sprintf(szSql, "select  b.sensor_code, b.nodeid,b.frequency,b.nodename,b.monitype,p.nodetype from busi_iedparam p,busi_param_bind b "
        "where p.nodename=b.nodename");
    MYSQL_RES *res = pdbHandle->SelectRecord(szSql);
    if (!res)  return;

    MYSQL_ROW row = NULL;
    while (row = pdbHandle->GetRecord(res))
    {
        string strKey = "";
        ST_PARAM_BIND stBind;
        memset(&stBind, 0x00, sizeof(ST_PARAM_BIND));

        if (row[0] && strlen(row[0]) > 0) {
            strcpy(stBind.sensorid, row[0]);
        }
        if (row[1] && strlen(row[1]) > 0) {
            stBind.nodeid = atoi(row[1]);
        }
        if (row[2] && strlen(row[2]) > 0) {
            strcpy(stBind.szFrequency, row[2]);
        }
        if (row[3] && strlen(row[3]) > 0) {
            strcpy(stBind.nodename, row[3]);
            strKey = stBind.nodename;
        }
        if (row[4] && strlen(row[4]) > 0) {
            stBind.monitype = row[4][0]; // 监测类型: 1=振动监测值 2=振动告警 3=噪声监测值 4=噪声告警 5=中心点监测 6=中心点告警
        }
        if (row[5] && strlen(row[5]) > 0) {
            strcpy(stBind.nodetype, row[5]); // 节点类型 MX,ST,SE
        }
        mutex_lock(g_map_parambind_mutex);
        m_pIter = g_map_parambind.find((char*)stBind.nodename);
        if (m_pIter == g_map_parambind.end())
        {
            // 新配置数据
            g_map_parambind.insert(map<string, ST_PARAM_BIND>::value_type(strKey, stBind));
        }
        else { // 更新配置
            memcpy(&m_pIter->second, &stBind, sizeof(ST_PARAM_BIND));
        }
        mutex_unlock(g_map_parambind_mutex);
    }
    pdbHandle->FreeRecord(res);
    return;
}


// 添加Ied不能连接的告警消息
void vSetIedDevicesWarnMesg(char *Ied_ip, int port, time_t altime)
{
    char szTime[32] = { 0 };
    char szSql[DEF_BUFFER_1K] = { 0 };
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) {
        return;
    }
    vTranHostTimeFmt(altime, szTime);
    _snprintf(szSql, sizeof(szSql), "INSERT INTO busi_warning(id,sub_id,warn_time,warn_type,load_type,warn_desc,warn_level,state) "
        "VALUES(guuid(), '%s', '%s','1', '1','%s:%d,Ied链接异常断开,请检查!', '0','1')",
        "", szTime, Ied_ip, port);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "insert Warning: %s", szSql);
    if (!pdbHandle->InsertRecord((const char *)szSql)) {
        vPrtLogMsg(LOG_ERROR, RET_FAIL, "INSERT INTO warning message failed,SQL:%s", szSql);
    }
}
// 删除Ied服务不能连接的告警消息
void vDelIedDevicesWarnMesg(char *Ied_ip, int port, time_t altime)
{
    int i = 0;
    char szSql[DEF_BUFFER_1K] = { 0 }, szTime[32] = { 0 };
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) {
        return;
    }
    vTranHostTimeFmt(altime, szTime);
    _snprintf(szSql, sizeof(szSql), "update busi_warning set process='network check', process_time=sysdate(),"
        "process_user='system' where warn_type='1' and warn_level='0' and  state = '1' and warn_time='%s' "
        "warn_desc = '%s:%d,Ied链接异常断开,请检查!' ", szTime, Ied_ip, port);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "update Warning: %s", szSql);
    if (!pdbHandle->InsertRecord((const char *)szSql)) {
        vPrtLogMsg(LOG_ERROR, RET_FAIL, "update warning message failed,SQL:%s", szSql);
    }
}


// 插入数据
void vSetRealDevicesMoniMesg(ST_DB_BODY *pData)
{
    int i = 0;
    char szSql[DEF_BUFFER_1K] = { 0 }, szTime[32] = { 0 };
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) {
        return;
    }
    vTranHostTimeFmt(pData->dtime, szTime);
    // 1:振动装置 2 : 噪声装置 3 : 中心点装置
    if (pData->szDevType == 1) {
        _snprintf(szSql, sizeof(szSql), "insert into busi_ampli_real(id, file_id,main_id,sub_id,side,phase,in_out,"
            "frequency,max_amplitude,ins_time) "
            "values(guuid(), '%s', '%s', '%s', '%c', '%c', '%c', '%s', '%.4f', '%s') ",
            pData->file_id, pData->m_id, pData->s_id, pData->side, pData->phase, pData->in_out,
            pData->szFrequency, pData->value, szTime);
        vPrtLogMsg(LOG_DEBUG, RET_OK, "insert busi_ampli_real: %s", szSql);
    }
    else if (pData->szDevType == 2) {
        _snprintf(szSql, sizeof(szSql), "insert into busi_noise_real(id, main_id,max_amplitude,ins_time) "
            "values(guuid(), '%s','%.4f', '%s') ",
            pData->m_id, pData->value, szTime);
    }
    else if (pData->szDevType == 3) {
        _snprintf(szSql, sizeof(szSql), "insert into busi_center_real(id, main_id,max_amplitude,ins_time) "
            "values(guuid(), '%s','%.4f', '%s') ",
            pData->m_id, pData->value, szTime);
    }
    if (!pdbHandle->InsertRecord((const char *)szSql)) {
        vPrtLogMsg(LOG_ERROR, RET_FAIL, "insert busi_ampli_real message failed,SQL:%s", szSql);
    }
}

// 传感器上传的告警消息入库
void vSetRealDevicesWarnMesg(ST_DB_WARN &stWarn)
{
    char szTime[32] = { 0 };
    char szSql[DEF_BUFFER_1K] = { 0 };
    CDBMySQL *pdbHandle = CDBMySQL::Instance();
    if (!pdbHandle) {
        return;
    }
    vTranHostTimeFmt(stWarn.almtime, szTime);
    _snprintf(szSql, sizeof(szSql), "INSERT INTO busi_warning(id,sub_id,warn_time,warn_type,warn_desc,warn_level,state,sample_value,value_unit) "
        "VALUES(guuid(), '%s', '%s','%d', '%s', '%d','%d','%.4f','%s')",
        "", stWarn.s_id, szTime, stWarn.warn_type, stWarn.szDesc,stWarn.level,stWarn.state,stWarn.value,stWarn.valunit);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "insert Warning: %s", szSql);
    if (!pdbHandle->InsertRecord((const char *)szSql)) {
        vPrtLogMsg(LOG_ERROR, RET_FAIL, "INSERT INTO warning message failed,SQL:%s", szSql);
    }
}









// 显示阈值配置信息
void vPrtDeviceThresholdConf()
{
    int i = 0;
    map<string, ST_THRES_CONF>::iterator m_pIter;
    mutex_lock(g_thres_conf_mutex);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "---------Print device threshold Table Config ----------");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " Index SensorID\t\t\tM_sys_code\tSub_id\t\t\t     Device_name      DType   Values  Frequ  Phase  Side  Inout");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " ----------------------------------------------------------------------------------------------------------------------------------------");
    m_pIter = g_thres_conf.begin();
    while (m_pIter != g_thres_conf.end())
    {
        vPrtLogMsg(LOG_DEBUG, RET_OK, "-->%3d  %-18s %-18s %-33s %-16s %3s   %.4f   %-4s   %c    %4s  %4s", i + 1,
            (m_pIter->second).szSensorID,
            (m_pIter->second).szSysCode,
            (m_pIter->second).szSubID,
            (m_pIter->second).szSDevName,
            ((m_pIter->second).bDevType == '1' ? "AMP" : ((m_pIter->second).bDevType == '2' ? "NOS":"CEN")),
            (m_pIter->second).dThreshold,
            ((m_pIter->second).szFrequency),
            (m_pIter->second).bPhase == 0 ? '-' : (m_pIter->second).bPhase,
            ((m_pIter->second).bSide == 'P' ? "原边" : ((m_pIter->second).bSide == 'S' ? "副边":"-")),
            ((m_pIter->second).bInOut == 'U' ? "上侧" : ((m_pIter->second).bInOut == 'D' ? "下侧" : "-")));
        m_pIter++; i++;
    }
    vPrtLogMsg(LOG_DEBUG, RET_OK, "--------------- Print device threshold_size:%d-----------", g_thres_conf.size());
    mutex_unlock(g_thres_conf_mutex);
}
// 显示传感器控制参数
void vPrtDeviceCtrlParam()
{
    int i = 0;
    map<string, ST_CONTROL_CONF>::iterator m_pIter;
    mutex_lock(g_ctrl_conf_mutex);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "---------Print device control parameter Config ----------");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " Index SensorID\t\t\tM_sys_code\tSub_id\t\t\t     Device_name       Enable   KHz  Intg  Stime  Test");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " ----------------------------------------------------------------------------------------------------------------------------------------");
    m_pIter = g_ctrl_conf.begin();
    while (m_pIter != g_ctrl_conf.end())
    {
        vPrtLogMsg(LOG_DEBUG, RET_OK, "-->%3d  %-18s %-18s %-33s %-16s %4s   %4d %3d   %3d      %4s", 
            i + 1,
            (m_pIter->second).szSensorID,
            (m_pIter->second).szSysCode,
            (m_pIter->second).szSubID,
            (m_pIter->second).szSubName,
            ((m_pIter->second).isEnable == true ? "TRUE" : "FLAS"),
            (m_pIter->second).SmpProd,
            (m_pIter->second).SmpInt,
            (m_pIter->second).StartTime,
            ((m_pIter->second).alone == true ? "TRUE" : "FLAS"));  //0:分 1 : 合
        m_pIter++; i++;
    }
    vPrtLogMsg(LOG_DEBUG, RET_OK, "--------------- Print device control parameter_size:%d-----------", g_ctrl_conf.size());
    mutex_unlock(g_ctrl_conf_mutex);
}

// 显示传感器控制参数
void vPrtICDParameterBind()
{
    int i = 0;
    map<string, ST_CONTROL_CONF>::iterator m_pIter;
    mutex_lock(g_ctrl_conf_mutex);
    vPrtLogMsg(LOG_DEBUG, RET_OK, "---------Print device control parameter Config ----------");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " Index SensorID\t\t\tM_sys_code\tSub_id\t\t\t     Device_name       Enable   KHz  Intg  Stime  Test");
    vPrtLogMsg(LOG_DEBUG, RET_OK, " ----------------------------------------------------------------------------------------------------------------------------------------");
    m_pIter = g_ctrl_conf.begin();
    while (m_pIter != g_ctrl_conf.end())
    {
        vPrtLogMsg(LOG_DEBUG, RET_OK, "-->%3d  %-18s %-18s %-33s %-16s %4s   %4d %3d   %3d      %4s",
            i + 1,
            (m_pIter->second).szSensorID,
            (m_pIter->second).szSysCode,
            (m_pIter->second).szSubID,
            (m_pIter->second).szSubName,
            ((m_pIter->second).isEnable == true ? "TRUE" : "FLAS"),
            (m_pIter->second).SmpProd,
            (m_pIter->second).SmpInt,
            (m_pIter->second).StartTime,
            ((m_pIter->second).alone == true ? "TRUE" : "FLAS"));  //0:分 1 : 合
        m_pIter++; i++;
    }
    vPrtLogMsg(LOG_DEBUG, RET_OK, "--------------- Print device control parameter_size:%d-----------", g_ctrl_conf.size());
    mutex_unlock(g_ctrl_conf_mutex);
}
