SystemGui/Sources/Chalet/ChaletGui.cpp

315 lines
8.8 KiB
C++

#include "ChaletGui.h"
#include "ui_ChaletGui.h"
#include "CChalet.h"
#include <QWidget>
#include <QDate>
CChaletGui::CChaletGui(QWidget *parent) :
QWidget(parent),
ui(new Ui::CChaletGui)
{
ui->setupUi(this);
ui->mDoHarakiriButton->setEnabled(false);
ui->mLogStartDateEdit->setDate(QDate::currentDate());
connect(ui->mWiFiModuleONBtn,SIGNAL(clicked()),this,SLOT(WiFiONButtonClicked()));
connect(ui->mWiFiModuleOFFBtn,SIGNAL(clicked(bool)),this,SLOT(WiFiOFFButtonClicked()));
connect(ui->mInverterRelayOFFBtn,SIGNAL(clicked()),this,SLOT(InverterPowerOFFButtonClicked()));
connect(ui->mInverterRelayONBtn,SIGNAL(clicked(bool)),this,SLOT(InverterPowerONButtonClicked()));
connect(ui->mRebootCPUBtn,SIGNAL(clicked(bool)),this,SLOT(RebootCPUButtonClicked()));
connect(ui->mDoHarakiriButton,SIGNAL(clicked(bool)),this,SLOT(DoHarakiriButtonClicked()));
connect(ui->mEnableHarakiriChkBx,SIGNAL(clicked(bool)),this,SLOT(EnableHarakiriClicked(bool)));
connect(ui->mGetChaletLogButton,SIGNAL(clicked(bool)),this,SLOT(GetChaletLogsBtnClicked()));
mBatteryPlotWidget = new QCustomPlot(ui->mPlotWidget);
mBatteryPlotWidget->resize(ui->mPlotWidget->size());
// create graph and assign data to it:
mBatteryPlotWidget->addGraph();
// give the axes some labels:
mBatteryPlotWidget->xAxis->setLabel("time");
mBatteryPlotWidget->yAxis->setLabel("Volts");
double now = QDateTime::currentDateTime().toSecsSinceEpoch();
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("hh:mm:ss\ndd MMM");
mBatteryPlotWidget->xAxis->setTicker(dateTicker);
mBatteryPlotWidget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
QDateTime Now = QDateTime::currentDateTime().toLocalTime();
QDateTime midnight = Now;
midnight.setTime(QTime(0,0,0));
QDateTime eod = Now;
eod.setTime(QTime(23,59,0));
//mBatteryPlotWidget->xAxis->setRange(0/*QCPAxisTickerDateTime::dateTimeToKey(midnight)*/,QCPAxisTickerDateTime::dateTimeToKey(eod));
// mBatteryPlotWidget->xAxis->setRange(now, now+(2*3600));
mBatteryPlotWidget->xAxis->setRange(midnight.toSecsSinceEpoch(), eod.toSecsSinceEpoch());
mBatteryPlotWidget->yAxis->setRange(12,15);
mBatteryPlotWidget->graph(0)->addData(now,13.5);
mBatteryPlotWidget->replot();
}
CChaletGui::~CChaletGui()
{
delete ui;
delete mBatteryPlotWidget;
}
int CChaletGui::UpdateChaletStatus(CChaletMainStatus Status)
{
static bool LastToggle = false;
QString text;
int Delta = QDateTime::currentDateTime().toSecsSinceEpoch() - Status.mLastLoraStatus.toSecsSinceEpoch();
ui->mLasCommRequestReceivedLbl->setText(QString("Last response: %1 | %2 seconds ago...").arg(Status.mLastLoraStatus.toString("yyyy-MM-dd-hh:mm:ss")).arg(Delta));
if(Status.mIsOnline == false)
{
ui->mChaletOnlineStatusLbl->setText("OFFLINE");
QPalette pal = QPalette(ui->mChaletOnlineStatusLbl->palette());
pal.setColor(QPalette::WindowText,QColor(Qt::red));
ui->mChaletOnlineStatusLbl->setPalette(pal);
ui->mWiFiModuleStatusLabel->setText("Unknown");
ui->mInverterRlyStatusLabel->setText("Unknown");
ui->mCurrentSensorStateLbl->setText("Current Sensor: Unknown state");
ui->mBatteryVoltageLabel->setText("Battery Voltage: Unknown");
ui->mBatterySOCLabel->setText("Battery SOC: Unknown");
ui->mSolarPanelCurrentLabel->setText("Solar Panel Current: Unknown");
ui->mLostReqPercentLbl->setText("N/A");
ui->mChaletTemperatureLbl->setText("Temperature: -100)");
return RET_OK;
}
else if(ui->mChaletOnlineStatusLbl->text() == "OFFLINE")
{
ui->mChaletOnlineStatusLbl->setText("ONLINE");
QPalette pal = QPalette(ui->mChaletOnlineStatusLbl->palette());
pal.setColor(QPalette::WindowText,QColor(Qt::darkGreen));
ui->mChaletOnlineStatusLbl->setPalette(pal);
}
switch(Status.mWiFiModuleStatus)
{
case WIFI_CONNECTED_STATE:
{
text = "Connected";
break;
}
case WIFI_MODULE_OFF_STATE:
{
text = "Module OFF";
break;
}
case WIFI_DISCONNECTED_STATE:
{
text = "Disconnected";
break;
}
case WIFI_INIT_ERROR_STATE:
{
text = "Init. Error";
break;
}
case WIFI_UNKNOWN_STATE:
default:
{
text = "WiFi: Unknown state";
break;
}
}
ui->mWiFiModuleStatusLabel->setText(text);
switch(Status.mInverterRelayStatus)
{
case CHALET_POWER_RELAY_OFF_STATE:
{
text = "OFF";
break;
}
case CHALET_POWER_RELAY_ON_STATE:
{
text = "ON";
break;
}
case CHALET_POWER_RELAY_UNKNOWN_STATE:
default:
{
text = "Unknown";
break;
}
}
ui->mInverterRlyStatusLabel->setText(text);
switch(Status.mCurrentSensorStatus)
{
case CHALET_CURRENT_SENSOR_DEFECT_STATE:
{
text = "Current Sensor: DEFECTIVE";
break;
}
case CHALET_CURRENT_SENSOR_OK_STATE:
{
text = "Current Sensor: OK";
break;
}
case CHALET_CURRENT_SENSOR_UNKNOWN_STATE:
default:
{
text = "Current Sensor: Unknown state";
break;
}
}
ui->mCurrentSensorStateLbl->setText(text);
QString Voltage = QString("Battery Voltage: %1").arg(Status.mBatteryVoltage);
ui->mBatteryVoltageLabel->setText(Voltage);
ui->mVoltageLCD->display(Voltage);
QString Current = QString("Solar Panel Current: %1").arg(Status.mBatteryCurrent);
ui->mSolarPanelCurrentLabel->setText(Current);
QString SOC = QString("Battery SOC: %1").arg(Status.mBatterySOC);
ui->mBatterySOCLabel->setText(SOC);
QString Percent = QString("Lost Requests: %1%").arg(Status.mLostRequestPercentage,0,'f',2);
ui->mLostReqPercentLbl->setText(Percent);
if(Status.mStatusToggleBit != LastToggle)
{
LastToggle = Status.mStatusToggleBit;
if(ui->mChaletCommActivityLbl->isEnabled())
{
ui->mChaletCommActivityLbl->setEnabled(false);
}
else
{
ui->mChaletCommActivityLbl->setEnabled(true);
}
double CurTime = QDateTime::currentDateTime().toSecsSinceEpoch();
mBatteryPlotWidget->graph(0)->addData(CurTime,Status.mBatteryVoltage);
mBatteryPlotWidget->replot();
}
QString Temperature = QString("Temperature: %1").arg(Status.mChaletTemperature);
ui->mChaletTemperatureLbl->setText(Temperature);
return RET_OK;
}
int CChaletGui::UpdateChaletLogPlot(QByteArray *Log)
{
int NbRecords;
CChaletMainStatus CurData;
QVector<double> x,y;
QDataStream Strm(Log,QIODevice::ReadOnly);
Strm.device()->seek(0);
Strm >> NbRecords;
if(NbRecords > 0)
{
for(int i = 0; i < NbRecords; i++)
{
Strm >> CurData;
x.append(CurData.mThisStatusDateTime.toSecsSinceEpoch());
y.append(CurData.mBatteryVoltage);
}
}
if(x.size() == 0 || y.size() == 0)
return RET_ERROR;
mBatteryPlotWidget->graph(0)->data().clear();
mBatteryPlotWidget->graph(0)->setData(x,y);
mBatteryPlotWidget->xAxis->setRange(x.first(),x.last());
mBatteryPlotWidget->replot();
return RET_OK;
}
void CChaletGui::WiFiONButtonClicked()
{
mProgramHandle->WiFiToggleButtonPressed(true);
}
void CChaletGui::WiFiOFFButtonClicked()
{
mProgramHandle->WiFiToggleButtonPressed(false);
}
void CChaletGui::InverterPowerONButtonClicked()
{
mProgramHandle->InverterToggleButtonPressed(true);
}
void CChaletGui::InverterPowerOFFButtonClicked()
{
mProgramHandle->InverterToggleButtonPressed(false);
}
void CChaletGui::DoHarakiriButtonClicked()
{
mProgramHandle->DoHarakiriButtonClicked(true);
}
void CChaletGui::RebootCPUButtonClicked()
{
mProgramHandle->RebootCPUButtonPressed();
}
void CChaletGui::EnableHarakiriClicked(bool checked)
{
if(checked)
{
ui->mDoHarakiriButton->setEnabled(true);
}
else
{
ui->mDoHarakiriButton->setEnabled(false);
}
}
int CChaletGui::ChaletCommActivity()
{
if(ui->mChaletCommActivityLbl->isEnabled())
{
ui->mChaletCommActivityLbl->setEnabled(false);
}
else
{
ui->mChaletCommActivityLbl->setEnabled(true);
}
return RET_OK;
}
void CChaletGui::GetChaletLogsBtnClicked()
{
QDate StartDate = ui->mLogStartDateEdit->date();
if(StartDate > QDate::currentDate())
{
QMessageBox::critical(this,"Error","Date is in the future!");
return;
}
mProgramHandle->RequestChaletLogs(StartDate);
}