realtimesensorwindow.cpp 6.03 KB
#include <QKeyEvent>
#include "realtimesensorwindow.h"
#include "ui_realtimesensorwindow.h"
#include "ovenstatics.h"
#include "stringer.h"
#include "QDateTime"
#include "soundplayer.h"

RealtimeSensorWindow::RealtimeSensorWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::RealtimeSensorWindow)
{
    OvenStatistics* ovs = OvenStatistics::getInstance();
    int i = 0;

    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    foreach (QPushButton *button, findChildren<QPushButton *>())
        connect(button, &QPushButton::pressed, SoundPlayer::playClick);

    m_arrLbState[0][0] = ui->m_ctrlLbListState_1_1;
    m_arrLbState[0][1] = ui->m_ctrlLbListState_1_2;
    m_arrLbState[0][2] = ui->m_ctrlLbListState_1_3;
    m_arrLbState[0][3] = ui->m_ctrlLbListState_1_4;
    m_arrLbState[0][4] = ui->m_ctrlLbListState_1_5;
    m_arrLbState[0][5] = ui->m_ctrlLbListState_1_6;
    m_arrLbState[0][6] = ui->m_ctrlLbListState_1_7;
    m_arrLbState[0][7] = ui->m_ctrlLbListState_1_8;
    m_arrLbState[0][8] = ui->m_ctrlLbListState_1_9;

    m_arrLbState[1][0] = ui->m_ctrlLbListState_2_1;
    m_arrLbState[1][1] = ui->m_ctrlLbListState_2_2;
    m_arrLbState[1][2] = ui->m_ctrlLbListState_2_3;
    m_arrLbState[1][3] = ui->m_ctrlLbListState_2_4;
    m_arrLbState[1][4] = ui->m_ctrlLbListState_2_5;
    m_arrLbState[1][5] = ui->m_ctrlLbListState_2_6;
    m_arrLbState[1][6] = ui->m_ctrlLbListState_2_7;
    m_arrLbState[1][7] = ui->m_ctrlLbListState_2_8;
    m_arrLbState[1][8] = ui->m_ctrlLbListState_2_9;

    m_arrLbState[2][0] = ui->m_ctrlLbListState_3_1;
    m_arrLbState[2][1] = ui->m_ctrlLbListState_3_2;
    m_arrLbState[2][2] = ui->m_ctrlLbListState_3_3;
    m_arrLbState[2][3] = ui->m_ctrlLbListState_3_4;
    m_arrLbState[2][4] = ui->m_ctrlLbListState_3_5;
    m_arrLbState[2][5] = ui->m_ctrlLbListState_3_6;
    m_arrLbState[2][6] = ui->m_ctrlLbListState_3_7;
    m_arrLbState[2][7] = ui->m_ctrlLbListState_3_8;
    m_arrLbState[2][8] = ui->m_ctrlLbListState_3_9;

    m_arrLbState[3][0] = ui->m_ctrlLbListState_4_1;
    m_arrLbState[3][1] = ui->m_ctrlLbListState_4_2;
    m_arrLbState[3][2] = ui->m_ctrlLbListState_4_3;
    m_arrLbState[3][3] = ui->m_ctrlLbListState_4_4;
    m_arrLbState[3][4] = ui->m_ctrlLbListState_4_5;
    m_arrLbState[3][5] = ui->m_ctrlLbListState_4_6;
    m_arrLbState[3][6] = ui->m_ctrlLbListState_4_7;
    m_arrLbState[3][7] = ui->m_ctrlLbListState_4_8;
    m_arrLbState[3][8] = ui->m_ctrlLbListState_4_9;

    for(i=0;i<REALSENSOR_CONTROL_LIST_ITEM_MAX;i++){
        m_arrSensorItems[i] = &(ovs->srvdata->sensor_log.values[m_arrSensorItemIdxs[i]]);
    }
    reloadUi();
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),this,SLOT(oneSecTimerFired()));
    timer->start(1000); //1000ms fired
}

RealtimeSensorWindow::~RealtimeSensorWindow()
{
}

void RealtimeSensorWindow::on_backButton_clicked()
{
    close();
}

uint16_t i_temp1,i_temp2;
type_temp temp_temp;

void RealtimeSensorWindow::reloadUi(void){
    char strTime[64];
    QString strTemp="";
    struct tm *timeptr;
    int i = 0;
    OvenStatistics* ovs = OvenStatistics::getInstance();
    if(ovs!= NULL){
        for(i=0;i<REALSENSOR_CONTROL_LIST_ITEM_MAX;i++){
            i_temp1 = i;
            i_temp2 = ovs->getSensorTypeByIdx(m_arrSensorItemIdxs[i]);
            temp_temp = ovs->getCurSensorValueByIdx(m_arrSensorItemIdxs[i]);

            if(ovs->getSensorTypeByIdx(m_arrSensorItemIdxs[i]) == 2 )
                    strTemp = Stringer::temperature(ovs->getCurSensorValueByIdx(m_arrSensorItemIdxs[i]).itemp/10);// strTemp.sprintf("%d℃",ovs->getCurSensorValueByIdx(m_arrSensorItemIdxs[i]).itemp/10);
            else if(ovs->getSensorTypeByIdx(m_arrSensorItemIdxs[i]) == 1 )
                    strTemp = Stringer::temperature(ovs->getCurSensorValueByIdx(m_arrSensorItemIdxs[i]).utemp);
            else  strTemp = Stringer::temperature(ovs->getCurSensorValueByIdx(m_arrSensorItemIdxs[i]).itemp);
            m_arrLbState[0][i]->setText(strTemp);
        }

        for(i=0;i<REALSENSOR_CONTROL_LIST_ITEM_MAX;i++){
            if(ovs->getSensorTypeByIdx(m_arrSensorItemIdxs[i]) == 2 )
                    strTemp = Stringer::temperature(m_arrSensorItems[i]->high_temp.itemp/10);
            else if(ovs->getSensorTypeByIdx(m_arrSensorItemIdxs[i]) == 1)
                    strTemp = Stringer::temperature(m_arrSensorItems[i]->high_temp.utemp);
            else  strTemp= Stringer::temperature(m_arrSensorItems[i]->high_temp.itemp);
            m_arrLbState[1][i]->setText(strTemp);
        }

        for(i=0;i<REALSENSOR_CONTROL_LIST_ITEM_MAX;i++){
            strTemp = Stringer::temperature(m_arrCriticalValue[i]);
            m_arrLbState[2][i]->setText(strTemp);
        }


        for(i=0;i<REALSENSOR_CONTROL_LIST_ITEM_MAX;i++){
            if(m_arrSensorItems[i]->last_high_time !=0){
                QDateTime dt_tm;
                dt_tm.setTime_t(m_arrSensorItems[i]->last_high_time);
                //timeptr = localtime(&(m_arrSensorItems[i]->last_high_time));
                //strftime(strTime,64,"%y-%m-%d\n%H:%M:%S",timeptr);
                m_arrLbState[3][i]->setText(Stringer::DateTimeString(dt_tm));
            }
            else{
                m_arrLbState[3][i]->setText("-");
            }
        }




    }

}

void RealtimeSensorWindow::oneSecTimerFired(void){
    reloadUi();
}

void RealtimeSensorWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        focusPreviousChild();
        break;
    case 0x01000031:    // Push
        break;
    case 0x01000030:    // Turn right
        focusNextChild();
        break;
    }
}

void RealtimeSensorWindow::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        focusPreviousChild();
        break;
    case 0x01000031:    // Push
    {
        QPushButton *btn = qobject_cast<QPushButton*>(focusWidget());
        if(btn != NULL){
            btn->click();
        }
        break;
    }
    case 0x01000030:    // Turn right
        focusNextChild();
        break;
    }
}