LedDisplay/LedDisplayController/leddisplaybackend.h

129 lines
4.3 KiB
C
Raw Normal View History

2020-10-08 20:06:57 +02:00
#ifndef OMOBIDISPLAYBACKEND_H
#define OMOBIDISPLAYBACKEND_H
#include <QObject>
2020-10-15 16:57:07 +02:00
#include <QTimer>
#include <QJsonDocument>
#include <QCryptographicHash>
#include <QSettings>
#include <QEventLoop>
#include <qbluetoothleuartclient.h>
2020-10-18 15:08:12 +02:00
#include <leddisplaytextmodel.h>
2020-10-08 20:06:57 +02:00
2020-10-18 15:08:12 +02:00
class LedDisplayBackend : public QObject
2020-10-08 20:06:57 +02:00
{
Q_OBJECT
Q_PROPERTY(QBluetoothLeUartClient* bleClient READ getBleClient NOTIFY bleClientChanged)
2020-10-11 15:50:13 +02:00
Q_PROPERTY(OmobiDisplayAppState state READ getState WRITE setState NOTIFY stateChanged)
2020-10-18 15:08:12 +02:00
Q_PROPERTY(LedDisplayTextModel* displayTextModel READ getDisplayTextModel NOTIFY displayTextModelChanged)
Q_PROPERTY(QVariantMap displayBrightness READ getDisplayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged)
2020-10-11 21:01:21 +02:00
2020-10-08 20:06:57 +02:00
public:
2020-10-18 15:08:12 +02:00
explicit LedDisplayBackend(QObject *parent = nullptr);
2020-10-08 20:06:57 +02:00
2020-10-11 15:50:13 +02:00
enum OmobiDisplayAppState {
Idle,
BluetoothOff,
LocationPermissionDenied,
2020-10-11 15:50:13 +02:00
Scanning,
ReadyToConnect,
Connecting,
2020-10-17 01:08:23 +02:00
AuthenticationRequired,
Authenticating,
Initing,
Connected,
Loading
2020-10-11 15:50:13 +02:00
};
Q_ENUM(OmobiDisplayAppState)
2020-10-08 20:06:57 +02:00
2020-10-18 15:08:12 +02:00
static void init() {
qmlRegisterType<LedDisplayBackend>("de.itsblue.LedDisplayController", 1, 0, "LedDisplayBackend");
qmlRegisterUncreatableType<LedDisplayTextModel>("de.itsblue.LedDisplayController", 1, 0, "LedDisplayTextModel", "LedDisplayTextModel cannot be created");
};
2020-10-08 20:06:57 +02:00
private:
enum OmobiDisplayCommand {
AuthenticateCommand = 0,
2020-10-15 16:57:07 +02:00
KeepAliveCommand = 1,
GetAllTextSetsCommand = 10,
GetTextSetParameterCommand = 11,
GetDisplayBrightnessCommand = 12,
SetTextSetParameterCommand = 20,
2020-10-17 03:22:37 +02:00
SetDisplayBrightnessCommand = 21,
SetDisplayCodeCommand = 22,
SetDisplayNameCommand = 23
};
enum OmobiDisplayTextSetParameter
{
TextParameter = 0,
ActiveParameter,
RuntimeParameter,
ColorParameter,
AlignmentParameter,
ScrollParameter,
ScrollDirectionParameter,
ScrollSpeedParameter,
ScrollCountParameter,
IndexParameter,
DisplayTextSetParameterCount /*!< Just for helping purposes */
};
enum OmobiDisplayStatusCode {
Success = 200,
DisplayControllerError = 501
};
2020-10-11 15:50:13 +02:00
OmobiDisplayAppState state;
QBluetoothLeUartClient *bleClient;
2020-10-15 16:57:07 +02:00
QTimer *keepAliveTimer;
2020-10-18 15:08:12 +02:00
LedDisplayTextModel* displayTextModel;
int waitingCommands;
QList<QMap<int, QVariant>> textSetsBuffer;
QVariantMap displayBrightness;
2020-10-08 20:06:57 +02:00
QSettings* settings;
QString lastDisplaySecret;
2020-10-09 00:03:50 +02:00
public slots:
2020-10-11 15:50:13 +02:00
Q_INVOKABLE void startScanning();
2020-10-17 01:08:23 +02:00
Q_INVOKABLE void authenticate(QString secret);
Q_INVOKABLE QBluetoothLeUartClient* getBleClient();
2020-10-11 15:50:13 +02:00
Q_INVOKABLE OmobiDisplayAppState getState();
2020-10-18 15:08:12 +02:00
Q_INVOKABLE LedDisplayTextModel* getDisplayTextModel();
Q_INVOKABLE QVariantMap getDisplayBrightness();
Q_INVOKABLE void setDisplayBrightness(QVariantMap brightness);
2020-10-17 03:22:37 +02:00
Q_INVOKABLE void setDisplayCode(QString code);
Q_INVOKABLE void setDisplayName(QString name);
2020-10-09 00:03:50 +02:00
2020-10-08 20:06:57 +02:00
private slots:
void handleBluetoothStateChange(QBluetoothLeUartClient::BluetoothLeUartClientState state);
2020-10-08 22:03:30 +02:00
void handleFoundNewDevice(QBluetoothLeUartDevice* device);
2020-10-09 00:03:50 +02:00
void handleBluetoothDeviceConected();
void handleBluetoothScanningError(QBluetoothLeUartClient::BluetoothScanError error);
2020-10-08 20:06:57 +02:00
void handleDisplayTextModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
void handleDisplayTextModelRowsInserted(const QModelIndex &parent, int first, int last);
void handleDisplayTextModelRowsRemoved(const QModelIndex &parent, int first, int last);
void sendBluetoothCommand(OmobiDisplayCommand command, QVariant data = QVariant());
2020-10-15 16:57:07 +02:00
void sendBluetoothKeepAlive();
void handleBluetoothDataReceived(QString s);
void updateDisplayTextSetParameter(int index, int parameter);
void updateDisplayTextSetParameter(int index, int parameter, QString value);
2020-10-11 21:01:21 +02:00
void refreshLoadingState();
2020-10-11 15:50:13 +02:00
void setState(OmobiDisplayAppState state);
2020-10-08 20:06:57 +02:00
signals:
2020-10-11 15:50:13 +02:00
void stateChanged();
void bleClientChanged();
2020-10-11 21:01:21 +02:00
void displayTextModelChanged();
void displayBrightnessChanged();
void commandFinished();
2020-10-08 20:06:57 +02:00
};
#endif // OMOBIDISPLAYBACKEND_H