ovenstatics.cpp 14.9 KB
#include "ovenstatics.h"
#include <QtDebug>
#include <QApplication>
#include <QTimer>
#include "mainwindow.h"
#include "ovencontroller.h"
#include "popupwindow.h"
#include "engineermenuwindow.h"
#include "errorpopupdlg.h"

OvenStatics* OvenStatics::p_singtonInstance=NULL;

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

void OvenStatics::destroy(){
    if(!p_singtonInstance)
        delete p_singtonInstance;
}


OvenStatics::OvenStatics(QObject* parent) :QObject(parent)
{
    curdoorstate = false;
    curSysErrorState = 0;
    curCommErrorState = 0;
    curSensorErrorState = 0;
    bDataRefreshed = false;
    srvdatas = new servicedatas();
    srvdatas->loadServiceDatas();
}

OvenStatics::~OvenStatics(){
    delete srvdatas;
}


void OvenStatics::onDataChanged()
{
    error_item *item;
    time_t ltime=0;
    uint16_t errstatetemp;
    qDebug() << "statics event fired";
    return;
    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 = &(srvdatas->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.reserved2f;
    if(errstatetemp!=0 && errstatetemp != curCommErrorState ){
        processCommError( errstatetemp,ltime );
    }
    curCommErrorState = errstatetemp;

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


}

void OvenStatics::processSensorError(uint16_t errflag, time_t ltime){
    error_item *item;
    QString strMsg = "";
    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 = &(srvdatas->err_log.items.inner_temp_fail);
        strMsg  = tr("내부 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_1],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_2)){
        item = &(srvdatas->err_log.items.qunching_temp_fail);
        strMsg  = tr("퀀칭 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_2],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_3)){
        //Reserved allerrornoe 설정
        item = &(srvdatas->err_log.items.qunching_temp_fail);
        strMsg  = tr("퀀칭 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_3],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_4)){
        item = &(srvdatas->err_log.items.wall_temp1_fail);
        strMsg  = tr("벽면 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_4],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_5)){
        item = &(srvdatas->err_log.items.steam_gen_temp_fail);
        strMsg  = tr("스팀제네레이터 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_5],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_6)){
        item = &(srvdatas->err_log.items.meatprove_temp1_fail);
        strMsg  = tr("미트프로브 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_6],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_7)){
       //Reseved
        item = &(srvdatas->err_log.items.meatprove_temp2_fail);
        strMsg  = tr("미트프로브2 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_7],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_8)){
        //Reserved
        item = &(srvdatas->err_log.items.meatprove_temp3_fail);
        strMsg  = tr("미트프로브3 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_8],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_9)){
        //Reserved
        item = &(srvdatas->err_log.items.meatprove_temp4_fail);
        strMsg  = tr("미트프로브4 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_9],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SENSOR_ERR_SENSOR_10)){
        item = &(srvdatas->err_log.items.pcb_temp_fail);
        strMsg  = tr("PCB 온도 이상 발생");
        processErrorItems(item,sensor_err_type_def[state][SENSOR_ERR_SENSOR_10],strMsg,ltime);
    }
}

void OvenStatics::processCommError(uint16_t errflag, time_t ltime){
    error_item *item;
    QString strMsg = "";
    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 = &(srvdatas->err_log.items.upper_pan_fail);
        strMsg  = tr("상부 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER1],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_BUNNER2)){
        item = &(srvdatas->err_log.items.lower_pan_fail);
        strMsg  = tr("하부 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_BUNNER3)){
        item = &(srvdatas->err_log.items.steam_pan_fail);
        strMsg  = tr("스팀 송풍기 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER3],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_LOWERPAN)){
        item = &(srvdatas->err_log.items.lower_motor_fail);
        strMsg  = tr("상부 모터 컨트롤러 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(COMM_ERR_UPPERFAN)){
        item = &(srvdatas->err_log.items.upper_motor_fail);
        strMsg  = tr("상부 모터 컨트롤러 이상 발생");
        processErrorItems(item,comm_err_type_def[state][COMM_ERR_BUNNER2],strMsg,ltime);
    }
}

void OvenStatics::processSystemError(uint16_t errflag,time_t ltime){
   error_item *item;
   QString strMsg = "";
   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 = &(srvdatas->err_log.items.upper_fire_fail);
        strMsg  = tr("상부 버너 착하가 되지 않습니다.");
        processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER1],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_FIRE_TRIGGER2)){
        //버너 컨트롤러 2 착화 이상
        item = &(srvdatas->err_log.items.lower_fire_fail);
        strMsg  = tr("하부 버너 착하가 되지 않습니다.");
        processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER2],strMsg,ltime);
    }
    if(errflag & SYS_ERR_FIRE_TRIGGER3){
        item = &(srvdatas->err_log.items.steam_fire_fail);
        strMsg  = tr("스팀 버너 착하가 되지 않습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_FIRE_TRIGGER3],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_OVNE_TEMP_ALARM)){
        item = &(srvdatas->err_log.items.inner_temp_high_alarm);
        strMsg  = tr("내부 온도 과열 발생.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_OVNE_TEMP_ALARM],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_QUN_TEMP_ALARM)){
        //퀀칭 온도 센서 과열
        item = &(srvdatas->err_log.items.qunching_temp_high_alarm);
        strMsg  = tr("퀀칭 온도 센서 이상 발생.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_QUN_TEMP_ALARM],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_CENTER_TEMP_ALARM)){
        //중심 온도 과열
        item = &(srvdatas->err_log.items.meatprove_temp1_high_alarm);
        strMsg  = tr("미트프로브 온도 센서 이상 발생.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_CENTER_TEMP_ALARM],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_WALL_TEMP_ALARM)){
        item = &(srvdatas->err_log.items.wall_temp1_high_alarm);
        strMsg  = tr("벽면 온도 센서 이상 발생.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WALL_TEMP_ALARM],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_WATER_SPLY_FAIL)){
        //급수 이상 -> 유량 센서 이상
        item = &(srvdatas->err_log.items.water_level_sensor_fail);
        strMsg  = tr("급수가 되지 않습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WATER_SPLY_FAIL],strMsg,ltime);
    }
    if(errflag & MAKE_MASK(SYS_ERR_WATER_LEVEL_FAIL)){
        //급수 이상 -> 수위 센서 이상
        item = &(srvdatas->err_log.items.water_level_sensor_fail);
        strMsg  = tr("급수가 되지 않습니다.");
         processErrorItems(item, sys_err_type_def[state][SYS_ERR_WATER_LEVEL_FAIL],strMsg,ltime);
    }
}




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

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

void OvenStatics::oneSecTimerFired(void){
    if(bDataRefreshed) bDataRefreshed= false;
    else{
        udp->fillControl(control);
        udp->fillData(state);
    }
    //가열부 시간 측정
    if( (state.onoff_state1 & LOAD_STATE1_STEAM_BUNNER) !=0){
        srvdatas->use_log.items.steam_heat +=1;
    }
    if( (state.onoff_state1 & (LOAD_STATE1_UPPER_BUNNER | LOAD_STATE1_LOWER_FAN))!=0){
        srvdatas->use_log.items.dry_heat+=1;
        if(state.onoff_state1 & (LOAD_STATE1_UPPER_BUNNER)){
            srvdatas->use_log.items.upper_heat +=1;
        }
        if((state.onoff_state2 & LOAD_STATE1_LOWER_BUNNER)!=0){
            srvdatas->use_log.items.lower_heat +=1;
        }
    }
    //모드 시간 계산
    if(state.cooking_mode !=0){
        if(state.cooking_humidity==0){
            //건열 모드
            srvdatas->use_log.items.cook_dry_mode+=1;
        }
        else if(state.cooking_humidity ==100){
            //스팀모드
            srvdatas->use_log.items.cook_steam_mode+=1;
        }
        else srvdatas->use_log.items.cook_combi_mode+=1;
    }
    if(state.cleaning_mode !=0){
        switch (state.cleaning_mode) {
        case (uint16_t)wash_mode_nocleanser:
            srvdatas->use_log.items.wash_mode_nocleanser+=1;
            break;
        case (uint16_t)wash_mode_simple:
            srvdatas->use_log.items.wash_mode_simple+=1;
            break;
        case (uint16_t)wash_mode_standard:
            srvdatas->use_log.items.wash_mode_standard+=1;
            break;
        case (uint16_t)wash_mode_strong:
            srvdatas->use_log.items.wash_mode_strong+=1;
            break;
        case (uint16_t)wash_mode_highspeed:
            srvdatas->use_log.items.wash_mode_speed+=1;
            break;
        default:
            break;
        }
    }
    if(control.cooldown ==1){
        srvdatas->use_log.items.cooldown_mode+=1;
    }
    //부품
    if(state.door_state ==1){
        srvdatas->use_log.items.door_open+=1;
    }
    if(state.onoff_state2 !=0){
           if( (state.onoff_state2 & LOAD_STATE2_DV)!=0){
               srvdatas->use_log.items.dv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SSV)!=0){
               srvdatas->use_log.items.ssv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_QNV)!=0){
               srvdatas->use_log.items.qnv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SNV)!=0){
               srvdatas->use_log.items.snv_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_SSP)!=0){
               srvdatas->use_log.items.ssp_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_HDM)!=0){
               srvdatas->use_log.items.hdm_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_DP)!=0){
               srvdatas->use_log.items.dp_open+=1;
           }
           if((state.onoff_state2 & LOAD_STATE2_UNP) !=0){
               srvdatas->use_log.items.unp_open+=1;
           }
    }
}

void OvenStatics::processErrorItems(error_item *item, error_exe_type errtype, const QString &MsgDesc, time_t ltime){

    if(errtype >= error_type_onlychk){
        item->fired_cnt+=1;
        if(item->first_fired == 0) item->first_fired = ltime;
        item->last_fried = ltime;
    }
    switch(errtype){
        case error_type_onlypop:
        case error_type_popclr:
        case error_type_popnonclr:
            {
                errorPopupDlg *dlg = new errorPopupDlg(QApplication::activeWindow(),MsgDesc);
                dlg->exec();
                break;
            }
        case error_type_engclr:
        case error_type_engnonclr:
        case error_type_onlyeng:
            {
                engineermenuwindow *w = new engineermenuwindow(QApplication::activeWindow());
                w->showFullScreen();
                break;
            }
        default:
            break;
    }
    if(errtype > error_type_clrsplit){
        //에러 클리어 명령 송신
        qDebug() << "send error clear command";
        if(errtype == error_type_engclr){
            //확인 후 송신
            bNeedErrorClear = true;
        }
        else{
            //에러클리어 메시지 즉시 송신
        }
    }

}