#include "ovenstatics.h"
#include <QtDebug>
#include <QApplication>
#include <QTimer>
#include "mainwindow.h"
#include "ovencontroller.h"
#include "engineermenuwindow.h"
#include "errorpopupdlg.h"
#include "historylistwindow.h"
#include "servicepassinputdlg.h"

OvenStatistics* OvenStatistics::p_singletonInstance=NULL;

OvenStatistics* OvenStatistics::getInstance(QObject *parent){
    if(!p_singletonInstance){
        p_singletonInstance = new OvenStatistics(parent);
        p_singletonInstance->udp = UdpHandler::getInstance();
        p_singletonInstance->oven = Oven::getInstance();
        //for singletone event debug
//        MainWindow* mw = (MainWindow*)parent;
//        connect(mw->pcombiButton, SIGNAL(clicked()),p_singtonInstance, SLOT(onDataChanged()));
        connect(p_singletonInstance->udp , SIGNAL(changed()), p_singletonInstance, SLOT(onDataChanged()));
        QTimer *timer = new QTimer(p_singletonInstance);
        connect(timer, SIGNAL(timeout()),p_singletonInstance,SLOT(oneSecTimerFired()));
        timer->start(1000); //1000ms fired
    }
    return p_singletonInstance;
}

void OvenStatistics::destroy(){
    if(!p_singletonInstance)
        delete p_singletonInstance;
}


OvenStatistics::OvenStatistics(QObject* parent) :QObject(parent)
{
    int i = 0;
    curdoorstate = false;
    curSysErrorState = 0;
    curCommErrorState = 0;
    curSensorErrorState = 0;
    bDataRefreshed = false;
    realdata.d32 = 0;
    m_bPopupShow = false;
    m_nLastPopupidx = MAX_ERROR_TYPE_CNT;

    for(i=0;i<MAX_LOG_SENSOR;i++){
        curSensorValue[i].utemp = 0;
    }

    curSensorValue[12].itemp = 100;

    srvdata = new ServiceData();

}

OvenStatistics::~OvenStatistics(){
    delete srvdata;
}


void OvenStatistics::onDataChanged()
{
    error_item *item;
    time_t ltime=0;
    uint16_t errstatetemp;
    udp->fillControl(control);
    udp->fillData(state);
    bDataRefreshed = true;
    time(&ltime);


    if(oven->cooking() && control.system){
        if(state.door_state ==1 && curdoorstate==0){
            //door error
            item = &(srvdata->err_log.items.door);
            item->fired_cnt+=1;
            if(item->first_fired==0)item->first_fired = ltime;
            item->last_fried = ltime;
        }
        curdoorstate = state.door_state;
    }

    if(state.system_error!=0 &&  state.system_error != curSysErrorState){
        processSystemError( state.system_error,ltime);
    }
    curSysErrorState = state.system_error;

    errstatetemp = state.communication_error;
    if(errstatetemp!=0 && errstatetemp != curCommErrorState ){
        processCommError( errstatetemp,ltime );
    }
    curCommErrorState = errstatetemp;

    errstatetemp = state.sensor_error;
    if(errstatetemp!=0 && errstatetemp != curSensorErrorState ){
        processSensorError(errstatetemp,ltime );
    }
    curSensorErrorState = errstatetemp;

    errstatetemp = state.controller_error;
    if(errstatetemp != 0 && errstatetemp != curControlErrorState){
        processStateError(errstatetemp,ltime);
    }
    curControlErrorState = errstatetemp;

}

void OvenStatistics::processSensorError(uint16_t errflag, time_t ltime){
    error_item *item;
    QString strMsg = "",strTitle="";
    uint8_t state;
    if( control.system==0 && !oven->cooking()) state = SYS_OFF_COOK_OFF;
    else if(control.system != 0 && !oven->cooking()) state = SYS_ON_COOK_OFF;
    else if(control.system !=0 && oven->cooking()) state = SYS_ON_COOK_ON;
    else state = SYS_ON_COOK_OFF;

    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_1)){
        item = &(srvdata->err_log.items.inner_temp_fail);
        strTitle = tr("내부 온도 이상 발생");
        strMsg  = tr("내부 온도 센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_1],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_2)){
        item = &(srvdata->err_log.items.qunching_temp_fail);
        strTitle  = tr("퀀칭 온도 이상 발생");
        strMsg  = tr("퀀칭 온도에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_2],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_3)){
        //Reserved allerrornoe 설정
        item = &(srvdata->err_log.items.qunching_temp_fail);
        strTitle  = tr("퀀칭 온도 이상 발생");
        strMsg  = tr("퀀칭 온도  센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_3],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_4)){
        item = &(srvdata->err_log.items.wall_temp1_fail);
        strTitle  = tr("벽면 온도 이상 발생");
        strMsg  = tr("벽면 온도 센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_4],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_5)){
        item = &(srvdata->err_log.items.steam_gen_temp_fail);
        strTitle  = tr("스팀제네레이터 온도 이상 발생");
        strMsg  = tr("스팀제네레이터 온도 센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_5],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_6)){
        item = &(srvdata->err_log.items.meatprove_temp1_fail);
        strTitle  = tr("미트프로브 온도 이상 발생");
        strMsg  = tr("미트프로브 온도 센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_6],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_7)){
       //Reseved
        item = &(srvdata->err_log.items.meatprove_temp2_fail);
        strTitle  = tr("미트프로브2 온도 이상 발생");
        strMsg  = tr("미트프로브2 온도 센서에 이상이 발생하였습니다.");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_7],strMsg , strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_8)){
        //Reserved
        item = &(srvdata->err_log.items.meatprove_temp3_fail);
        strMsg  = tr("미트프로브3 온도 센서에 이상이 발생하였습니다.");
        strTitle  = tr("미트프로브3 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_8],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_9)){
        //Reserved
        item = &(srvdata->err_log.items.meatprove_temp4_fail);
        strMsg  = tr("미트프로브4 온도 센서에 이상이 발생하였습니다.");
        strTitle  = tr("미트프로브4 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_9],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_10)){
        item = &(srvdata->err_log.items.pcb_temp_fail);
        strMsg  = tr("PCB 온도 센서에 이상이 발생하였습니다.");
        strTitle  = tr("PCB 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_10],strMsg,strTitle, ltime);
    }
}

void OvenStatistics::processCommError(uint16_t errflag, time_t ltime){
    error_item *item;
    QString strMsg = "",strTitle="";
    uint8_t state;
    if( control.system==0 && !oven->cooking()) state = SYS_OFF_COOK_OFF;
    else if(control.system != 0 && !oven->cooking()) state = SYS_ON_COOK_OFF;
    else if(control.system !=0 && oven->cooking()) state = SYS_ON_COOK_ON;
    else state = SYS_ON_COOK_OFF;

    if(errflag & MAKE_MASK(COMM_ERR_BUNNER1)){
        item = &(srvdata->err_log.items.upper_pan_fail);
        strMsg  = tr("상부 송풍기 통신 이상 발생");
        strTitle  = tr("상부 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER1],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_BUNNER2)){
        item = &(srvdata->err_log.items.lower_pan_fail);
        strMsg  = tr("하부 송풍기 통신 이상 발생");
        strTitle  = tr("하부 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_BUNNER3)){
        item = &(srvdata->err_log.items.steam_pan_fail);
        strMsg  = tr("스팀 송풍기 통신 이상 발생");
        strTitle  = tr("스팀 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER3],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_LOWERFAN)){
        item = &(srvdata->err_log.items.lower_motor_fail);
        strMsg  = tr("하부 FAN 컨트롤러 통신 이상 발생");
        strTitle  = tr("하부 FAN 컨트롤러 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_UPPERFAN)){
        item = &(srvdata->err_log.items.upper_motor_fail);
        strMsg  = tr("상부 FAN 컨트롤러 통신 이상 발생");
        strTitle  = tr("상부 FAN 컨트롤러 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
}

void OvenStatistics::processStateError(uint16_t errflag, time_t ltime){
    error_item *item;
    QString strMsg = "",strTitle="";
    uint8_t state;
    if( control.system==0 && !oven->cooking()) state = SYS_OFF_COOK_OFF;
    else if(control.system != 0 && !oven->cooking()) state = SYS_ON_COOK_OFF;
    else if(control.system !=0 && oven->cooking()) state = SYS_ON_COOK_ON;
    else state = SYS_ON_COOK_OFF;

    if(errflag & MAKE_MASK(STATE_ERR_BUNNER1)){
        item = &(srvdata->err_log.items.upper_pan_fail);
        strMsg  = tr("버너컨트롤러 1 이상 발생하였습니다.");
        strTitle  = tr("버너컨트롤러 1 이상 발생");
        processErrorItems(item,state_err_type_def[state][STATE_ERR_BUNNER1],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(STATE_ERR_BUNNER2)){
        item = &(srvdata->err_log.items.lower_pan_fail);
        strMsg  = tr("버너컨트롤러 2 이상  발생하였습니다.");
        strTitle  = tr("버너컨트롤러 2 이상 발생");
        processErrorItems(item,state_err_type_def[state][STATE_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(STATE_ERR_BUNNER3)){
        item = &(srvdata->err_log.items.steam_pan_fail);
        strMsg  = tr("버너컨트롤러 3 이상 발생하였습니다.");
        strTitle  = tr("버너컨트로러 3 이상 발생");
        processErrorItems(item,state_err_type_def[state][STATE_ERR_BUNNER3],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(STATE_ERR_LOWERFAN)){
        item = &(srvdata->err_log.items.lower_motor_fail);
        strMsg  = tr("하부 FAN 컨트롤러 이상 발생하였습니다.");
        strTitle  = tr("하부 FAN 컨트롤러 이상 발생");
        processErrorItems(item,state_err_type_def[state][STATE_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
    if(errflag & MAKE_MASK(STATE_ERR_UPPERFAN)){
        item = &(srvdata->err_log.items.upper_motor_fail);
        strMsg  = tr("상부 FAN 컨트롤러 이상 발생하였습니다.");
        strTitle  = tr("상부 FAN 컨트롤러 이상 발생");
        processErrorItems(item,state_err_type_def[state][STATE_ERR_BUNNER2],strMsg,strTitle, ltime);
    }
}

void OvenStatistics::processSystemError(uint16_t errflag,time_t ltime){
   error_item *item;
   QString strMsg = "",strTitle = "";
   uint8_t state;
   if( control.system==0 && !oven->cooking()) state = SYS_OFF_COOK_OFF;
   else if(control.system != 0 && !oven->cooking()) state = SYS_ON_COOK_OFF;
   else if(control.system !=0 && oven->cooking()) state = SYS_ON_COOK_ON;
   else state = SYS_ON_COOK_OFF;

    if( errflag & MAKE_MASK(SYS_ERR_FIRE_TRIGGER1)){
        //버너 컨트롤러 1 착화 이상
        item = &(srvdata->err_log.items.upper_fire_fail);
        strMsg  = tr("상부 버너 착하가 되지 않습니다.");
        strTitle = tr("상부 버너 착하 이상 발생");
        processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER1],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_FIRE_TRIGGER2)){
        //버너 컨트롤러 2 착화 이상
        item = &(srvdata->err_log.items.lower_fire_fail);
        strMsg  = tr("하부 버너 착하가 되지 않습니다.");
        strTitle  = tr("하부 버너 착하 이상 발생");
        processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER2],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_FIRE_TRIGGER3)){
        item = &(srvdata->err_log.items.steam_fire_fail);
        strMsg  = tr("스팀 버너 착하가 되지 않습니다.");
        strTitle  = tr("스팀 버너 착하 이상 발생");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER3],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_OVNE_TEMP_ALARM)){
        item = &(srvdata->err_log.items.inner_temp_high_alarm);
        strMsg  = tr("내부 온도가 과열되었습니다.");
        strTitle  = tr("내부 온도 과열 발생");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_OVNE_TEMP_ALARM],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_QUN_TEMP_ALARM)){
        //퀀칭 온도 센서 과열
        item = &(srvdata->err_log.items.qunching_temp_high_alarm);
        strTitle  = tr("퀀칭 온도 센서 이상 발생");
        strMsg  = tr("퀀칭 온도 센서가 과열되었습니다");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_QUN_TEMP_ALARM],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_CENTER_TEMP_ALARM)){
        //중심 온도 과열
        item = &(srvdata->err_log.items.meatprove_temp1_high_alarm);
        strTitle  = tr("미트프로브 온도 센서 이상 발생.");
        strMsg  = tr("미트프로브 온도 센서 과열되었습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_CENTER_TEMP_ALARM],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_WALL_TEMP_ALARM)){
        item = &(srvdata->err_log.items.wall_temp1_high_alarm);
        strTitle  = tr("벽면 온도 센서 이상 발생.");
        strMsg  = tr("벽면 온도 센서가 과열 되었습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WALL_TEMP_ALARM],strMsg,strTitle,ltime);
    }
   if(errflag & MAKE_MASK(SYS_ERR_STAM_TEMP_ALARM)){
       item = &(srvdata->err_log.items.steam_gen_temp_high_alram);
       strTitle  = tr("스팀제네레이터 온도 이상 발생.");
       strMsg  = tr("스팀제네레이터 온도 센서가 과열 되었습니다.");
        processErrorItems(item, sys_err_type_def[state][SYS_ERR_STAM_TEMP_ALARM],strMsg,strTitle,ltime);
   }
    if(errflag & MAKE_MASK(SYS_ERR_WATER_SPLY_FAIL)){
        //급수 이상 -> 유량 센서 이상
        item = &(srvdata->err_log.items.water_level_sensor_fail);
        strTitle  = tr("급수 이상 발생");
        strMsg  = tr("급수가 되지 않습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WATER_SPLY_FAIL],strMsg,strTitle,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_WATER_LEVEL_FAIL)){
        //급수 이상 -> 수위 센서 이상
        item = &(srvdata->err_log.items.water_level_sensor_fail);
        strTitle = tr("급수 이상 발생");
        strMsg  = tr("급수가 되지 않습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WATER_LEVEL_FAIL],strMsg,strTitle,ltime);
    }
}




bool OvenStatistics::getNeedErrorClear(){
    return bNeedErrorClear;
}

void OvenStatistics::clearNeedErrorClear(){
    //에러클리어 송신 후 flag clear
    bNeedErrorClear = false;
}

void OvenStatistics::oneSecTimerFired(void){
    static int nOneSecCnt=1;
    time_t ltime;

    ltime = time(NULL);

    nOneSecCnt++;
    if( (nOneSecCnt % 10) ==0 ) {
        srvdata->saveServiceData();
    }

    if(bDataRefreshed) bDataRefreshed= false;
    else{
        udp->fillControl(control);
        udp->fillData(state);
    }
    //가열부 시간 측정
    if( (state.onoff_state1 & LOAD_STATE1_STEAM_BUNNER) !=0){
        srvdata->use_log.items.steam_heat +=1;
    }
    if( (state.onoff_state1 & (LOAD_STATE1_UPPER_BUNNER | LOAD_STATE1_LOWER_FAN))!=0){
        srvdata->use_log.items.dry_heat+=1;
        if(state.onoff_state1 & (LOAD_STATE1_UPPER_BUNNER)){
            srvdata->use_log.items.upper_heat +=1;
        }
        if((state.onoff_state2 & LOAD_STATE1_LOWER_BUNNER)!=0){
            srvdata->use_log.items.lower_heat +=1;
        }
    }
    //모드 시간 계산
    if(state.cooking_mode !=0){
        if(state.cooking_humidity==0){
            //건열 모드
            srvdata->use_log.items.cook_dry_mode+=1;
        }
        else if(state.cooking_humidity ==100){
            //스팀모드
            srvdata->use_log.items.cook_steam_mode+=1;
        }
        else srvdata->use_log.items.cook_combi_mode+=1;
    }
    if(state.cleaning_mode !=0 && state.cleaning_sate !=0){
        switch (state.cleaning_mode) {
        case (uint16_t)wash_mode_nocleanser:
            srvdata->use_log.items.wash_mode_nocleanser+=1;
            break;
        case (uint16_t)wash_mode_simple:
            srvdata->use_log.items.wash_mode_simple+=1;
            break;
        case (uint16_t)wash_mode_standard:
            srvdata->use_log.items.wash_mode_standard+=1;
            break;
        case (uint16_t)wash_mode_strong:
            srvdata->use_log.items.wash_mode_strong+=1;
            break;
        case (uint16_t)wash_mode_highspeed:
            srvdata->use_log.items.wash_mode_speed+=1;
            break;
        default:
            break;
        }
    }
    if(control.cooldown ==1){
        srvdata->use_log.items.cooldown_mode+=1;
    }
    //부품
    if(state.door_state !=0){
        srvdata->use_log.items.door_open+=1;

    }


    if(state.onoff_state2 !=0){
           if( (state.onoff_state2 & LOAD_STATE2_DV)!=0){
               srvdata->use_log.items.dv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SSV)!=0){
               srvdata->use_log.items.ssv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_QNV)!=0){
               srvdata->use_log.items.qnv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SNV)!=0){
               srvdata->use_log.items.snv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SSP)!=0){
               srvdata->use_log.items.ssp_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_HDM)!=0){
               srvdata->use_log.items.hdm_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_DP)!=0){
               srvdata->use_log.items.dp_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_UNP) !=0){
               srvdata->use_log.items.unp_open+=1;
           }
    }

    //Real time Data Refresh

    realdata.b.door_open = state.door_state==0?0:1;
    realdata.b.dv_open = (state.onoff_state2&LOAD_STATE2_DV)==0?0:1;
    realdata.b.qnv_open = (state.onoff_state2&LOAD_STATE2_QNV)==0?0:1;
    realdata.b.sgnv_open = (state.onoff_state2&LOAD_STATE2_SGNV)==0?0:1;
    realdata.b.snv_open = (state.onoff_state2&LOAD_STATE2_SNV)==0?0:1;
    realdata.b.wsv_open = (state.onoff_state2&LOAD_STATE2_WSV)==0?0:1;
    realdata.b.ssv_open = (state.onoff_state2&LOAD_STATE2_SSV)==0?0:1;
    if( (state.reserved11 & 0x0018) !=0){
        if( (state.reserved11&0x0008) != 0){
            realdata.b.water_level = water_level_high;
        }
        else if( (state.reserved11 & 0x0010) !=0){
            realdata.b.water_level = water_level_low;
        }
    }
    else realdata.b.water_level = water_level_normal;

    if(srvdata->sensor_log.items.B1.high_temp.itemp < state.sensor1 ){
        srvdata->sensor_log.items.B1.high_temp.itemp = state.sensor1;
        srvdata->sensor_log.items.B1.last_high_time = ltime;
    }
    curSensorValue[0].itemp = state.sensor1;

    if(srvdata->sensor_log.items.B2.high_temp.itemp < state.sensor2 ){
        srvdata->sensor_log.items.B2.high_temp.itemp = state.sensor2;
        srvdata->sensor_log.items.B2.last_high_time = ltime;
    }
    curSensorValue[1].itemp = state.sensor2;

    if(srvdata->sensor_log.items.B3_1.high_temp.itemp < state.sensor6 ){
        srvdata->sensor_log.items.B3_1.high_temp.itemp = state.sensor6;
        srvdata->sensor_log.items.B3_1.last_high_time = ltime;
    }
    curSensorValue[5].itemp = state.sensor6;

    if(srvdata->sensor_log.items.B4.high_temp.itemp < state.sensor4 ){
        srvdata->sensor_log.items.B4.high_temp.itemp = state.sensor4;
        srvdata->sensor_log.items.B4.last_high_time = ltime;
    }
     curSensorValue[3].itemp = state.sensor4;

    if(srvdata->sensor_log.items.B5.high_temp.itemp < state.sensor5 ){
        srvdata->sensor_log.items.B5.high_temp.itemp = state.sensor5;
        srvdata->sensor_log.items.B5.last_high_time = ltime;
    }
    curSensorValue[4].itemp = state.sensor5;

    if(srvdata->sensor_log.items.B3_2.high_temp.itemp < state.sensor7 ){
        srvdata->sensor_log.items.B3_2.high_temp.itemp = state.sensor7;
        srvdata->sensor_log.items.B3_2.last_high_time = ltime;
    }
    curSensorValue[6].itemp = state.sensor7;

    if(srvdata->sensor_log.items.B3_3.high_temp.itemp < state.sensor8 ){
        srvdata->sensor_log.items.B3_3.high_temp.itemp = state.sensor8;
        srvdata->sensor_log.items.B3_3.last_high_time = ltime;
    }
    curSensorValue[7].itemp = state.sensor8;

    if(srvdata->sensor_log.items.B3_4.high_temp.itemp < state.sensor9 ){
        srvdata->sensor_log.items.B3_4.high_temp.itemp = state.sensor9;
        srvdata->sensor_log.items.B3_4.last_high_time = ltime;
    }
    curSensorValue[8].itemp = state.sensor9;

    if(srvdata->sensor_log.items.mbar1.high_temp.utemp < state.sensor11 ){
        srvdata->sensor_log.items.mbar1.high_temp.utemp = state.sensor11;
        srvdata->sensor_log.items.mbar1.last_high_time = ltime;
    }
    curSensorValue[10].utemp = state.sensor11;
    if(srvdata->sensor_log.items.mbar2.high_temp.utemp < state.sensor12 ){
        srvdata->sensor_log.items.mbar2.high_temp.utemp = state.sensor12;
        srvdata->sensor_log.items.mbar2.last_high_time = ltime;
    }
    curSensorValue[11].utemp = state.sensor12;

    if(srvdata->sensor_log.items.pcb.high_temp.itemp < state.sensor13 ){
        srvdata->sensor_log.items.pcb.high_temp.itemp = state.sensor13;
        srvdata->sensor_log.items.pcb.last_high_time = ltime;
    }
    curSensorValue[12].itemp = state.sensor13;

    if(srvdata->sensor_log.items.adc.high_temp.itemp < state.sensor14 ){
        srvdata->sensor_log.items.adc.high_temp.itemp = state.sensor14;
        srvdata->sensor_log.items.adc.last_high_time = ltime;
    }
    curSensorValue[13].itemp = state.sensor14;




}

void OvenStatistics::processErrorItems(error_item *item, error_exe_type errtype, const QString &MsgDesc, const QString &MsgTitle, time_t ltime){
   uint32_t erridx = (uint32_t) (item - &(srvdata->err_log.values[0]));
    qDebug() << (uint32_t) (item - &(srvdata->err_log.values[0]))  <<   "ERROR Fired!";
   if(m_pLastErrItem == item ){
        srvdata->err_log.items.repeat_error.fired_cnt =item->fired_cnt;
        srvdata->err_log.items.repeat_error.first_fired = item->first_fired;
        srvdata->err_log.items.repeat_error.last_fried = item->last_fried;
   }
   m_pLastErrItem = item;

    if(errtype >= error_type_onlychk){
        item->fired_cnt+=1;
        if(item->first_fired == 0) item->first_fired = ltime;
        item->last_fried = ltime;
    }

    if(errtype > error_type_clrsplit){
        //에러 클리어 명령 송신
       if(errtype == error_type_engclr){
            //확인 후 송신
            bNeedErrorClear = true;
        }
        else{
            //에러클리어 메시지 즉시 송신
            //udp->set(TG_ERROR_CLEAR,0xCECE);
            bNeedErrorClear=true;
        }
    }

    switch(errtype){
        case error_type_onlypop:
        case error_type_popclr:
        case error_type_popnonclr:
            {
                ErrorPopupDlg *dlg = new ErrorPopupDlg(QApplication::activeWindow(),MsgDesc,MsgTitle);
                dlg->exec();
                break;
            }
    case error_type_engclr:
        case error_type_engnonclr:
        case error_type_onlyeng:
            {
                //EngineerMenuWindow *w = new EngineerMenuWindow(0);
                //w->showFullScreen();
                ErrorPopupDlg *dlg = new ErrorPopupDlg(QApplication::activeWindow(),MsgDesc,MsgTitle);
                dlg->exec();
                ServicePassInputDlg *w = new ServicePassInputDlg(QApplication::activeWindow());
                w->exec();
                break;
            }
        default:
            break;
    }


}

realtime_data OvenStatistics::getPartsRealtimeData(void){
    return realdata;
}

uint8_t OvenStatistics::getSensorTypeByIdx(uint16_t idx){
    if(idx > MAX_LOG_SENSOR)  return 0;
    return sensorTypeInfo[idx];
}

type_temp OvenStatistics::getCurSensorValueByIdx(uint16_t idx){
    return curSensorValue[idx];
}

void OvenStatistics::onErrorPopupClosed(QWidget* parent){
    qDebug() << "Error Popup Closed!";
    if(bNeedErrorClear) {
        qDebug() << "send error clear command";
        bNeedErrorClear = false;
        udp->set(TG_ERROR_CLEAR,0xCECE);
    }
    if(m_mapPopupList.isEmpty()==false){
        popup_list_item item;
        item = m_mapPopupList.first();
        m_nLastPopupidx = m_mapPopupList.firstKey();
        bNeedErrorClear = item.bNeedClear;
        ErrorPopupDlg *dlg = new ErrorPopupDlg(parent,item.strDesc,item.strTitle);
        m_mapPopupList.remove(m_nLastPopupidx);
        connect(dlg,SIGNAL(closedPopup(QWidget*)),this, SLOT(onErrorPopupClosed(QWidget*)));
        dlg->showFullScreen();
    }
    else{
        m_bPopupShow = false;
        m_nLastPopupidx = MAX_ERROR_TYPE_CNT;
    }
}