Interface Ispindel + dev ChaletInterface

This commit is contained in:
2021-12-12 12:21:21 -05:00
parent 1dadad4b09
commit 8aafc96dbd
23 changed files with 849 additions and 30 deletions
+54
View File
@@ -0,0 +1,54 @@
#include "IspindelData.h"
CIspindelData::CIspindelData()
{
mIspindelID = mRSSI = mInterval = 0;
mIspindelName = mTemperatureUnits = "";
mAngle = mBattery = mGravity = mTemperature = 0.0;
mSampleDateTime = QDateTime::currentDateTime();
}
QDataStream &operator<<(QDataStream &out, const CIspindelData &source)
{
out << source.mAngle
<< source.mBattery
<< source.mGravity
<< source.mInterval
<< source.mIspindelID
<< source.mIspindelName
<< source.mRSSI
<< source.mTemperature
<< source.mTemperatureUnits
<< source.mSampleDateTime;
return out;
}
QDataStream &operator>>(QDataStream &in, CIspindelData &dest)
{
in >> dest.mAngle
>> dest.mBattery
>> dest.mGravity
>> dest.mInterval
>> dest.mIspindelID
>> dest.mIspindelName
>> dest.mRSSI
>> dest.mTemperature
>> dest.mTemperatureUnits
>> dest.mSampleDateTime;
return in;
}
QByteArray CIspindelData::ToByteArray()
{
QByteArray Array;
QDataStream Strm(&Array,QIODevice::WriteOnly | QIODevice::Unbuffered);
Strm << *this;
return Array;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef CISPINDELDATA_H
#define CISPINDELDATA_H
#include <QString>
#include <QDataStream>
#include <QDateTime>
class CIspindelData
{
public:
CIspindelData();
int mIspindelID, mRSSI, mInterval;
QString mIspindelName, mTemperatureUnits;
double mAngle, mBattery, mGravity, mTemperature;
QDateTime mSampleDateTime;
QByteArray ToByteArray();
};
QDataStream &operator<<(QDataStream &out, const CIspindelData &source);
QDataStream &operator>>(QDataStream &in, CIspindelData &dest);
#endif // CISPINDELDATA_H
+102
View File
@@ -0,0 +1,102 @@
#include "IspindelDataLogger.h"
#include <QFile>
#include <QDataStream>
#include <GlobalDefine.h>
CIspindelDataLogger::CIspindelDataLogger(QObject *parent) : QObject(parent)
{
}
int CIspindelDataLogger::WriteLogToFile(QList<CIspindelData *> *Data, bool OnlyLastItem)
{
QFile *LogFile;
QString LogFileName(ISPINDEL_LOG_DIR);
LogFileName.append("BrewLog");
LogFileName.append(".blg");
LogFile = new QFile(LogFileName);
if(LogFile->open(QIODevice::ReadWrite|QIODevice::Unbuffered) == false)
{
delete LogFile;
qDebug("Cannot open Ispindel log file for writing...");
return RET_ERROR;
}
QDataStream Strm(LogFile);
//Write header
int FileVer = ISPINDEL_LOG_FILE_VERSION;
int NbItems = Data->size();
LogFile->seek(0);
Strm << FileVer << NbItems;
if(OnlyLastItem)
{
//seek to the end of the file
LogFile->seek(LogFile->size());
//write the last item
Strm << *Data->last();
}
else
{
for(int i = 0; i < Data->size(); i++)
{
Strm << *Data->at(i);
}
}
LogFile->close();
delete LogFile;
return RET_OK;
}
int CIspindelDataLogger::LoadLogData(QList<CIspindelData *> *DataList)
{
QFile *LogFile;
QString LogFileName(ISPINDEL_LOG_DIR);
LogFileName.append("BrewLog");
LogFileName.append(".blg");
LogFile = new QFile(LogFileName);
if(LogFile->open(QIODevice::ReadOnly|QIODevice::Unbuffered) == false)
{
delete LogFile;
qDebug("Cannot open Ispindel log file for loading...");
return RET_ERROR;
}
QDataStream Strm(LogFile);
DataList->clear();
int FileVersion, NbItems;
Strm >> FileVersion;
Strm >> NbItems;
for(int i = 0; i < NbItems; i++)
{
CIspindelData *Data = new CIspindelData;
Strm >> *Data;
DataList->append(Data);
}
LogFile->close();
delete LogFile;
qDebug("Ispindel loaded %d readings",DataList->size());
for(int i = 0; i < DataList->size(); i++)
{
qDebug(qPrintable(DataList->at(i)->mSampleDateTime.toString("yyyy-MM-dd hh:mm:ss")));
}
return RET_OK;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef ISPINDELDATALOGGER_H
#define ISPINDELDATALOGGER_H
#include <QObject>
#include <QList>
#include "IspindelData.h"
#define ISPINDEL_LOG_FILE_VERSION 1
#define ISPINDEL_LOG_DIR "D:/Main/Brew/Ispindel Logs/"
class CIspindelDataLogger : public QObject
{
Q_OBJECT
public:
explicit CIspindelDataLogger(QObject *parent = 0);
int WriteLogToFile(QList<CIspindelData *> *Data, bool OnlyLastItem = true);
int LoadLogData(QList<CIspindelData *> *DataList);
signals:
public slots:
};
#endif // ISPINDELDATALOGGER_H
+128
View File
@@ -0,0 +1,128 @@
#include "IspindelDevice.h"
#include <QJsonDocument>
#include <QJsonObject>
CIspindelDevice::CIspindelDevice()
{
mISpindelServer = new QTcpServer;
connect(mISpindelServer,SIGNAL(newConnection()),this,SLOT(IspindelClientConnected()));
mISpindelServer->listen(QHostAddress::Any,80);
mIspindelLog.clear();
mDataLogger.LoadLogData(&mIspindelLog);
}
void CIspindelDevice::IspindelClientConnected()
{
mIspindelClientConnection = mISpindelServer->nextPendingConnection();
connect(mIspindelClientConnection,SIGNAL(readyRead()),this,SLOT(IspindelClientDataAvail()));
qDebug("Ispindel connected");
}
void CIspindelDevice::IspindelClientDataAvail()
{
CIspindelData *NewData = new CIspindelData;
QByteArray IspindelData;
if(mIspindelClientConnection->bytesAvailable() > 1)
{
IspindelData = QByteArray(mIspindelClientConnection->readAll());
}
else
{
return;
}
QJsonDocument JsonReply = QJsonDocument::fromJson(IspindelData);
QJsonObject JsonObject = JsonReply.object();
QStringList Keys = JsonObject.keys();
if(Keys.isEmpty())
return;
NewData->mIspindelID = JsonObject["ID"].toInt();
NewData->mRSSI = JsonObject["RSSI"].toInt();
NewData->mAngle = JsonObject["angle"].toDouble();
NewData->mBattery = JsonObject["battery"].toDouble();
NewData->mGravity = JsonObject["gravity"].toDouble();
NewData->mInterval = JsonObject["interval"].toInt();
NewData->mIspindelName = JsonObject["name"].toString();
NewData->mTemperatureUnits = JsonObject["temp_units"].toString();
NewData->mTemperature = JsonObject["temperature"].toDouble();
mIspindelLog.append(NewData);
qDebug("ID : %i",NewData->mIspindelID);
qDebug("RSSI : %i",NewData->mRSSI);
qDebug("Angle : %f",NewData->mAngle);
qDebug("Battery : %f",NewData->mBattery);
qDebug("Gravity : %f",NewData->mGravity);
qDebug("Interval (s): %i",NewData->mInterval);
qDebug("Name : %s",qPrintable(NewData->mIspindelName));
qDebug("Units : %s",qPrintable(NewData->mTemperatureUnits));
qDebug("Temperature : %f",NewData->mTemperature);
mDataLogger.WriteLogToFile(&mIspindelLog);
emit NewIspindelDataReceived();
// "ID" 6913415 QJsonValue (Number)
// "RSSI" -65 QJsonValue (Number)
// "angle" 89.59756 QJsonValue (Number)
// "battery" 4.301355 QJsonValue (Number)
// "gravity" 33.36325 QJsonValue (Number)
// "interval" 10 QJsonValue (Number)
// "name" "iSpindel001" QJsonValue (String)
// "temp_units" "C" QJsonValue (String)
// "temperature" 20.625 QJsonValue (Number)
}
void CIspindelDevice::IspindelClientDisconnected()
{
}
CIspindelDevice::~CIspindelDevice()
{
mISpindelServer->close();
for(int i = 0; i < mIspindelLog.size(); i++)
{
delete mIspindelLog[i];
mIspindelLog.clear();
}
}
CIspindelData *CIspindelDevice::GetLastestIspindelData()
{
if(mIspindelLog.size() > 0)
return mIspindelLog.last();
else
return 0;
}
QByteArray CIspindelDevice::GetAllDataBuffer()
{
QByteArray Array;
QDataStream Strm (&Array,QIODevice::WriteOnly | QIODevice::Unbuffered);
int Size = mIspindelLog.size();
Strm << Size;
for(int i = 0; i < Size; i++)
{
Strm << *mIspindelLog.at(i);
}
return Array;
}
QByteArray CIspindelDevice::GetLastestIspindelDataBuffer()
{
QByteArray Buf;
if(mIspindelLog.size() > 1)
{
Buf= mIspindelLog.last()->ToByteArray();
}
return Buf;
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef ISPINDELDEVICE_H
#define ISPINDELDEVICE_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include "IspindelData.h"
#include "IspindelDataLogger.h"
#include <QList>
class CIspindelDevice: public QObject
{
Q_OBJECT
public:
CIspindelDevice();
~CIspindelDevice();
QTcpServer *mISpindelServer;
QTcpSocket *mIspindelClientConnection;
QList<CIspindelData*> mIspindelLog;
CIspindelDataLogger mDataLogger;
CIspindelData *GetLastestIspindelData();
QList<CIspindelData*> *GetAllData(){return &mIspindelLog;}
QByteArray GetAllDataBuffer();
QByteArray GetLastestIspindelDataBuffer();
public slots:
void IspindelClientConnected();
void IspindelClientDataAvail();
void IspindelClientDisconnected();
signals:
void NewIspindelDataReceived();
};
#endif // ISPINDELDEVICE_H
+44
View File
@@ -0,0 +1,44 @@
#include "IspindelInterface.h"
#include "ProtocolDefs.h"
#include "IspindelData.h"
CIspindelInterface::CIspindelInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CIspindelDevice *DevicePtr):
CNetworkDevice(ID_ISPINDEL_INTERFACE,Address,NetworkInterface)
{
mISpindelDevice = DevicePtr;
connect(mISpindelDevice,SIGNAL(NewIspindelDataReceived()),this,SLOT(SendLastIspindelData()));
}
int CIspindelInterface::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{
Q_UNUSED(DeviceID)
Q_UNUSED(DeviceAddress)
Q_UNUSED(DataSize)
Q_UNUSED(Data)
switch(MessageID)
{
case ISPINDLE_LATEST_DATA_REQUEST:
{
QByteArray Buf;
break;
}
case ISPINDEL_GET_FULL_DATA_BUFFER_REQUEST:
{
QByteArray Buf = mISpindelDevice->GetAllDataBuffer();
mNetworkInterfacePtr->SendNetworkMessage(ID_ISPINDEL_INTERFACE,mDeviceAddress,ISPINDEL_GET_FULL_DATA_BUFFER_RESPONSE,Buf.size(),&Buf);
break;
}
}
return RET_OK;
}
void CIspindelInterface::SendLastIspindelData()
{
QByteArray Buf = mISpindelDevice->GetLastestIspindelDataBuffer();
mNetworkInterfacePtr->SendNetworkMessage(ID_ISPINDEL_INTERFACE,mDeviceAddress,ISPINDLE_LATEST_DATA_RESPONSE,Buf.size(),&Buf);
qDebug("Sent latest Ispindel data to interface");
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef ISPINDELINTERFACE_H
#define ISPINDELINTERFACE_H
#include "NetworkDevice.h"
#include "IspindelDevice.h"
class CIspindelInterface: public QObject, public CNetworkDevice
{
Q_OBJECT
public:
CIspindelInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CIspindelDevice *DevicePtr);
virtual int NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data);
private:
CIspindelDevice *mISpindelDevice;
public slots:
void SendLastIspindelData();
};
#endif // ISPINDELINTERFACE_H