SystemGui/Sources/Chalet/CChalet.cpp

224 lines
5.0 KiB
C++

#include "CChalet.h"
#include "ChaletMasterCtrlInterface.h"
CChalet::CChalet(CChaletGui *ChaletGuiPtr)
{
mChaletGui = ChaletGuiPtr;
mChaletGui->mProgramHandle = this;
mNetworkInterface = new CChaletMasterCtrlInterface(this);
mChaletPollTimer = new QTimer();
mChaletPollTimer->setInterval(1000);
mChaletPollTimer->setSingleShot(false);
connect(mChaletPollTimer,SIGNAL(timeout()),this,SLOT(ChaletPollTimerExpired()));
}
CChalet::~CChalet()
{
delete mNetworkInterface;
delete mChaletPollTimer;
}
int CChalet::Start()
{
mNetworkInterface->ConnectToMasterCtrl();
mChaletPollTimer->start();
return RET_OK;
}
void CChalet::ChaletPollTimerExpired()
{
mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GENERAL_STATUS_REQUEST,QByteArray());
}
int CChalet::ChaletStatusReceived(CChaletMainStatus Status)
{
mChaletGui->UpdateChaletStatus(Status);
return RET_OK;
}
int CChalet::ChaletLogReceived(QByteArray *Log)
{
mChaletGui->UpdateChaletLogPlot(Log);
return RET_OK;
}
int CChalet::ChaletCommActivity()
{
return mChaletGui->ChaletCommActivity();
}
int CChalet::WiFiToggleButtonPressed(bool RequestedState)
{
QByteArray WiFiState;
WiFiState.clear();
if(RequestedState)
{
WiFiState.append((char)0x01);
}
else
{
WiFiState.append((char)0x00);
}
return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_SET_STATE_REQUEST,WiFiState);
}
int CChalet::InverterToggleButtonPressed(bool RequestedState)
{
QByteArray InverterState;
InverterState.clear();
if(RequestedState)
{
InverterState.append((char)0x01);
}
else
{
InverterState.append((char)0x00);
}
return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_AC_POWER_SET_STATE_REQUEST,InverterState);
}
int CChalet::DoHarakiriButtonClicked(bool Verified)
{
if(Verified)
{
return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_DO_HARAKIRI_REQUEST,QByteArray());
}
return RET_ERROR;
}
int CChalet::RebootCPUButtonPressed()
{
return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_REBOOT_CPU_REQUEST,QByteArray());
}
int CChalet::ConnectedToMaster(bool connected)
{
if(connected)
{
mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_TODAYS_DATA_LOG_REQUEST,QByteArray());
}
return RET_OK;
}
int CChalet::RequestChaletLogs(QDate StartDate)
{
if(StartDate > QDate::currentDate())
{
return false;
}
QByteArray StartDateData;
QDataStream Strm(&StartDateData,QIODevice::WriteOnly);
Strm.device()->seek(0);
Strm << StartDate;
return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_DATA_LOG_REQUEST,StartDateData);
}
int CChalet::RequestDeviceWifiParams()
{
mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_REQUEST,QByteArray());
return RET_OK;
}
int CChalet::DeviceWiFiParamsReceived(QByteArray *Data)
{
quint32 Add = 0;
char byte;
byte = Data->at(0);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(1);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(2);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(3);
Add += (quint32)(byte & 0x000000FF);
QHostAddress IP = QHostAddress(Add);
Add = 0;
byte = Data->at(4);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(5);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(6);
Add += (quint32)(byte & 0x000000FF);
Add <<= 8;
byte = Data->at(7);
Add += (quint32)(byte & 0x000000FF);
QHostAddress Gateway = QHostAddress(Add);
mChaletGui->UpdateDeviceWiFiParameters(IP, Gateway);
return RET_OK;
}
int CChalet::SetDeviceWifiParams(QString IP, QString Gateway)
{
QHostAddress DeviceIP;
QHostAddress DeviceGateway;
if(DeviceIP.setAddress(IP) == false)
{
return RET_ERROR;
}
if(DeviceGateway.setAddress(Gateway) == false)
{
return RET_ERROR;
}
QByteArray Buffer;
Buffer.resize(8);
quint32 Address = DeviceIP.toIPv4Address();
char byte = (char)(Address & 0x000000FF);
Buffer[3] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[2] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[1] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[0] = byte;
Address >>= 8;
Address = DeviceGateway.toIPv4Address();
byte = (char)(Address & 0x000000FF);
Buffer[7] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[6] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[5] = byte;
Address >>= 8;
byte = (char)(Address & 0x000000FF);
Buffer[4] = byte;
Address >>= 8;
mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_REQUEST,Buffer);
return RET_OK;
}