fileprocessdlg.cpp 13.9 KB
#include <QTimer>
#include "fileprocessdlg.h"
#include "ui_fileprocessdlg.h"
#include "fileprocessor.h"
#include "ovenstatics.h"
#include "stringer.h"
#include <QDebug>


#define ERROR_LOG_FILE_TOP  "/GasErrorHistoryTop.log"
#define ERROR_LOG_FILE_STEAM  "/GasErrorHistorySteam.log"
#define ERROR_LOG_FILE_BOTTOM  "/GasErrorHistoryBottom.log"
#define ERROR_LOG_FILE_TOTAL    "/TotalError.log"

FileProcessDlg::FileProcessDlg(QWidget *parent, ConfigType type, bool isDown) :
    QDialog(parent),
    ui(new Ui::FileProcessDlg)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint);

    ui->ctrWjProcess->setMinimum(0);
    ui->ctrWjProcess->setMaximum(100);

    if(isDown){
        QPixmap pxmap;
        pxmap.load(":/images/config/102_usb_upload_icon.png");
        ui->label_2->setPixmap(pxmap);
    }

    switch(type){
    case config_info_data_download:
        QTimer::singleShot(100,this,SLOT(infodataDownload()));
        break;
    case config_service_data_download:
        QTimer::singleShot(100,this,SLOT(servicedataDownload()));
        break;
    case config_program_download:
        QTimer::singleShot(100,this,SLOT(programDownload()));
        break;
    case config_program_upload:
        QTimer::singleShot(100,this,SLOT(programUpload()));
        break;
    case config_set_download:
        QTimer::singleShot(100,this,SLOT(configDownload()));
        break;
    case config_set_upload:
        QTimer::singleShot(100,this,SLOT(configUpload()));
        break;
    default:
        QTimer::singleShot(200,this,SLOT(deleteLater()));
        break;
    }
}

FileProcessDlg::~FileProcessDlg()
{
    delete ui;
}

void FileProcessDlg::on_ctrBtnCancel_clicked()
{
    close();
}

void FileProcessDlg::saveHistoryLineData(QTextStream &out, uint16_t i,  uint16_t fired_cnt, time_t first_fired, time_t last_fried){
    QString strLine;
    QDateTime dt;
    //for(int i =0;i<m_arrErrorMaxIdx[(uint16_t)type];i++){
   //     err_item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[type][i]]);
        strLine = tr("erro%1,").arg(i);
        if(first_fired == 0){
            strLine += "-,0,-";
        }
        else{
            dt.setTime_t(first_fired);
            strLine+= Stringer::DateTimeString(dt,Stringer::datetime_string_type_oneline) + ",";
            strLine += QString("%1").arg(fired_cnt) + ",";
            dt.setTime_t(last_fried);
            strLine += Stringer::DateTimeString(dt,Stringer::datetime_string_type_oneline) + ",";
        }
        out << strLine << "\n";
        qDebug() << strLine;
    //}
}

void FileProcessDlg::saveHistoryTotalData(QTextStream &out){
    int i = 0;
    error_item *item;
    time_t firsttimebuf=0,lasttimebuf=0;
    uint16_t firecntbuf=0;
    QString strLine;
    QDateTime dt;
    OvenStatistics *ovenst = OvenStatistics::getInstance();

    //01 상부 점화 장치 데이터 초기화
//    for(i=0;i<m_arrErrorMaxIdx[ERROR_HISTORY_UPPERBUNNER];i++){
//        item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_UPPERBUNNER][i]]);
//       if(i==0) {firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;}
//       else{
//            if( firsttimebuf > item->first_fired && item->first_fired != 0 ) firsttimebuf = item->first_fired;
//            if( lasttimebuf < item->last_fried ) lasttimebuf = item->last_fried;
//            firecntbuf += item->fired_cnt;
//       }

//    }
//    total_items[0].fired_cnt = firecntbuf;
//    total_items[0].first_fired = firsttimebuf;
//    total_items[0].last_fried = lasttimebuf;


}

void FileProcessDlg::infodataDownload(){
    QString strUsbPath;
    QString strFile;
    QFile file;
    error_item *item;
    OvenStatistics *ovenst = OvenStatistics::getInstance();
    time_t firsttimebuf=0,lasttimebuf=0;
    uint16_t firecntbuf=0;
    uint16_t erridx = 0;
    int i;

    if(FileProcessor::detectUSB(strUsbPath)){
        //Top
        strFile = strUsbPath + ERROR_LOG_FILE_TOP;
        qDebug() << strUsbPath;
        file.setFileName(strFile);
        if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){
            QTextStream out(&file);
            out << tr("Gas Error History\n") << "Top Ignition Box" <<"\n";
            out << tr("no,") << tr("First Appearance,") << tr("Counter,") << tr("Last Appearance\n");
            for(i =0;i<m_arrErrorMaxIdx[ERROR_HISTORY_UPPERBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_UPPERBUNNER][i]]);
                saveHistoryLineData(out,i+1,item->fired_cnt,item->first_fired,item->last_fried);
            }
            file.close();
        }
        ui->ctrWjProcess->setValue(25);
        ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 1초"));
        //Bottom
        strFile = strUsbPath + ERROR_LOG_FILE_BOTTOM;
        qDebug() << strUsbPath;
        file.setFileName(strFile);
        if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){
            QTextStream out(&file);
            out << tr("Gas Error History\n") << "Bottom Ignition Box" <<"\n";
            out << tr("no,") << tr("First Appearance,") << tr("Counter,") << tr("Last Appearance\n");
            for(i =0;i<m_arrErrorMaxIdx[ERROR_HISTORY_LOWERBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_LOWERBUNNER][i]]);
                saveHistoryLineData(out,i+1,item->fired_cnt,item->first_fired,item->last_fried);
            }
            file.close();
        }
        ui->ctrWjProcess->setValue(50);
        ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 1초"));
        //Steam
        strFile = strUsbPath + ERROR_LOG_FILE_STEAM;
        qDebug() << strUsbPath;
        file.setFileName(strFile);
        if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){
            QTextStream out(&file);
            out << tr("Gas Error History\n") << "Steam Ignition Box" <<"\n";
            out << tr("no,") << tr("First Appearance,") << tr("Counter,") << tr("Last Appearance\n");
            for(i =0;i<m_arrErrorMaxIdx[ERROR_HISTORY_STEAMBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_STEAMBUNNER][i]]);
                saveHistoryLineData(out,i+1,item->fired_cnt,item->first_fired,item->last_fried);
            }
            file.close();
        }
        ui->ctrWjProcess->setValue(75);
        ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 1초"));
        //Total
        strFile = strUsbPath + ERROR_LOG_FILE_TOTAL;
        qDebug() << strUsbPath;
        file.setFileName(strFile);
        if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){
            QTextStream out(&file);
            out << tr("Service Error History\n\n");
            out << tr("no,") << tr("First Appearance,") << tr("Counter,") << tr("Last Appearance\n");
            //01 상부 점화 장치 데이터 초기화
             firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            for(i=0;i<m_arrErrorMaxIdx[ERROR_HISTORY_UPPERBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_UPPERBUNNER][i]]);
               if(i==0) {firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;}
               else{
                    if( (firsttimebuf > item->first_fired && item->first_fired != 0) || firsttimebuf == 0 ) firsttimebuf = item->first_fired;
                    if( lasttimebuf < item->last_fried ) lasttimebuf = item->last_fried;
                    firecntbuf += item->fired_cnt;
               }
            }
            saveHistoryLineData(out,erridx+1, firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //02 스팀 점화 장치 데이터 초기화
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            for(i=0;i<m_arrErrorMaxIdx[ERROR_HISTORY_STEAMBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_STEAMBUNNER][i]]);
               if(i==0) {firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;}
               else{
                    if((firsttimebuf > item->first_fired && item->first_fired != 0) || firsttimebuf == 0 ) firsttimebuf = item->first_fired;
                    if( lasttimebuf < item->last_fried ) lasttimebuf = item->last_fried;
                    firecntbuf += item->fired_cnt;
               }
            }
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //03 하부 점화 장치 데이터 초기화
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            for(i=0;i<m_arrErrorMaxIdx[ERROR_HISTORY_LOWERBUNNER];i++){
                item = &(ovenst->srvdata->err_log.values[m_arrErrorIdxs[ERROR_HISTORY_LOWERBUNNER][i]]);
               if(i==0) {firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;}
               else{
                    if((firsttimebuf > item->first_fired && item->first_fired != 0) || firsttimebuf == 0 ) firsttimebuf = item->first_fired;
                    if( lasttimebuf < item->last_fried ) lasttimebuf = item->last_fried;
                    firecntbuf += item->fired_cnt;
               }
            }
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //04 WATER
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.inner_temp_fail);
            firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;
            item = &(ovenst->srvdata->err_log.items.qunching_temp_fail);
            if( firsttimebuf > item->first_fired && item->first_fired != 0 ) firsttimebuf = item->first_fired;
            if( lasttimebuf < item->last_fried ) lasttimebuf = item->last_fried;
            firecntbuf += item->fired_cnt;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //05 구성품
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //07 B1 센서 에러
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.inner_temp_high_alarm);
            firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //08 B2 센서 에러
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.qunching_temp_high_alarm);
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //10 B4 센서 에러
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.wall_temp1_high_alarm);
            firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            //11 B5 센서 에러
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.steam_gen_temp_high_alram);
            firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            firecntbuf = 0;firsttimebuf =  0; lasttimebuf=0;
            item = &(ovenst->srvdata->err_log.items.water_level_sensor_fail);
            firsttimebuf = item->first_fired;lasttimebuf = item->last_fried;firecntbuf=item->fired_cnt;
            saveHistoryLineData(out, erridx,firecntbuf,firsttimebuf,lasttimebuf);erridx++;
            file.close();
        }
        ui->ctrWjProcess->setValue(100);
        ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 완료"));
        QTimer::singleShot(1000,this,SLOT(close()));
    }
}

void FileProcessDlg::servicedataDownload(){


}

void FileProcessDlg::programDownload(){

}

void FileProcessDlg::programUpload(){

}

void FileProcessDlg::configDownload(){
    QString strUsbPath;
    if(FileProcessor::detectUSB(strUsbPath)){
        strUsbPath.append("/config.ini");
        qDebug() << strUsbPath;
        if(QFile::copy("/prime/config/config.ini", strUsbPath)){
            ui->ctrWjProcess->setValue(100);
            ui->ctrLbRemainTime->setText("남은 예상 시간 : 0초");
            ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 완료"));
            QTimer::singleShot(1000,this,SLOT(close()));
        }
        else{
            ui->ctrLbRemainTime->setText(tr("다운로드에 실패하였습니다."));
            QTimer::singleShot(1000,this,SLOT(close()));
        }
    }
    else{
        ui->ctrLbRemainTime->setText(tr("다운로드에 실패하였습니다."));
        QTimer::singleShot(1000,this,SLOT(close()));
    }
}

void FileProcessDlg::configUpload(){
    QString strUsbPath;
    if(FileProcessor::detectUSB(strUsbPath)){
        strUsbPath.append("/config.ini");
        qDebug() << strUsbPath;
        QFile file("/prime/config/config.ini");
        file.remove();
        if(QFile::copy( strUsbPath , "/prime/config/config.ini")){
            OvenStatistics* ovs = OvenStatistics::getInstance();
            ovs->srvdata->loadServiceData();
            ui->ctrWjProcess->setValue(100);
            ui->ctrLbRemainTime->setText(tr("남은 예상 시간 : 완료"));
            QTimer::singleShot(1000,this,SLOT(close()));
        }
        else{
            ui->ctrLbRemainTime->setText(tr("업로드에 실패하였습니다."));
            QTimer::singleShot(1000,this,SLOT(close()));
        }
    }
    else{
        ui->ctrLbRemainTime->setText(tr("업로드에 실패하였습니다."));
        QTimer::singleShot(1000,this,SLOT(close()));
    }
}