2020-10-15 16:57:07 +02:00
|
|
|
#include "omobidisplaybackend.h"
|
2020-10-08 20:06:57 +02:00
|
|
|
|
|
|
|
OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
this->ble = new QBluetoothLeUart();
|
2020-10-15 16:57:07 +02:00
|
|
|
this->ble->setUUIDs("92fecb20-1406-426a-afa5-cd5c1f306462", "92fecb21-1406-426a-afa5-cd5c1f306462", "92fecb22-1406-426a-afa5-cd5c1f306462");
|
2020-10-11 21:01:21 +02:00
|
|
|
this->displayTextModel = new OmobiDisplayTextModel(this);
|
2020-10-14 23:54:12 +02:00
|
|
|
this->textSetsBuffer.clear();
|
|
|
|
this->displayBrightness = -1;
|
|
|
|
this->waitingCommands = 0;
|
2020-10-08 20:06:57 +02:00
|
|
|
|
2020-10-15 16:57:07 +02:00
|
|
|
this->keepAliveTimer = new QTimer(this);
|
|
|
|
this->keepAliveTimer->setInterval(5000);
|
|
|
|
this->keepAliveTimer->setSingleShot(false);
|
|
|
|
connect(this->keepAliveTimer, &QTimer::timeout, this, &OmobiDisplayBackend::sendBluetoothKeepAlive);
|
|
|
|
|
2020-10-08 20:06:57 +02:00
|
|
|
connect(this->ble, &QBluetoothLeUart::stateChanged, this, &OmobiDisplayBackend::handleBluetoothStateChange);
|
|
|
|
connect(this->ble, &QBluetoothLeUart::foundNewDevice, this, &OmobiDisplayBackend::handleFoundNewDevice);
|
2020-10-14 23:54:12 +02:00
|
|
|
connect(this->ble, &QBluetoothLeUart::dataReceived, this, &OmobiDisplayBackend::handleBluetoothDataReceived);
|
2020-10-09 00:03:50 +02:00
|
|
|
connect(this->ble, &QBluetoothLeUart::connectedToDevice, this, &OmobiDisplayBackend::handleBluetoothDeviceConected);
|
2020-10-11 21:01:21 +02:00
|
|
|
connect(this->displayTextModel, &OmobiDisplayTextModel::dataChanged, this, &OmobiDisplayBackend::handleDisplayTextModelDataChanged);
|
2020-10-14 23:54:12 +02:00
|
|
|
connect(this->displayTextModel, &OmobiDisplayTextModel::rowsInserted, this, &OmobiDisplayBackend::handleDisplayTextModelRowsInserted);
|
|
|
|
connect(this->displayTextModel, &OmobiDisplayTextModel::rowsRemoved, this, &OmobiDisplayBackend::handleDisplayTextModelRowsRemoved);
|
2020-10-11 15:50:13 +02:00
|
|
|
|
|
|
|
this->setState(Idle);
|
|
|
|
this->ble->startScanningForDevices();
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::startScanning() {
|
|
|
|
this->ble->startScanningForDevices();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){
|
|
|
|
switch(state){
|
2020-10-11 15:50:13 +02:00
|
|
|
case QBluetoothLeUart::Idle: {
|
|
|
|
this->setState(Idle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Scanning: {
|
|
|
|
this->setState(Scanning);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ScanFinished: {
|
|
|
|
this->setState(ReadyToConnect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Connecting: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ScanningForService: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ServiceFound: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Connected:
|
|
|
|
{
|
2020-10-08 20:06:57 +02:00
|
|
|
break;
|
2020-10-11 15:50:13 +02:00
|
|
|
}
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
2020-10-15 16:57:07 +02:00
|
|
|
|
|
|
|
if(state == QBluetoothLeUart::Connected)
|
|
|
|
this->keepAliveTimer->start();
|
|
|
|
else if(this->keepAliveTimer->isActive())
|
|
|
|
this->keepAliveTimer->stop();
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 00:03:50 +02:00
|
|
|
void OmobiDisplayBackend::handleBluetoothDeviceConected() {
|
2020-10-14 23:54:12 +02:00
|
|
|
this->setState(Initing);
|
2020-10-11 21:01:21 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
// tell display to send over existing model data
|
|
|
|
this->sendBluetoothCommand(GetAllTextSetsCommand);
|
|
|
|
this->sendBluetoothCommand(GetDisplayBrightnessCommand);
|
2020-10-09 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 22:03:30 +02:00
|
|
|
void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) {
|
2020-10-08 20:06:57 +02:00
|
|
|
qDebug() << "Found a device: name: " << device->getName() << " address: " << device->getAddress();
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::handleDisplayTextModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-15 16:57:07 +02:00
|
|
|
qDebug() << "Data changed: topLeft: " << topLeft << " bottomRight: " << bottomRight << " roles: " << roles;
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-15 16:57:07 +02:00
|
|
|
for(int role : roles)
|
|
|
|
this->updateDisplayTextSetParameter(topLeft.row(), role);
|
2020-10-14 23:54:12 +02:00
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::handleDisplayTextModelRowsInserted(const QModelIndex &parent, int first, int last) {
|
|
|
|
qDebug() << "Rows inserted: parent: " << parent << " first: " << first << " last " << last;
|
|
|
|
for(int i = 0; i < OmobiDisplayTextModel::OmobiDisplayTextModelRoleCount; i++) {
|
|
|
|
this->updateDisplayTextSetParameter(first, i);
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::handleDisplayTextModelRowsRemoved(const QModelIndex &parent, int first, int last) {
|
|
|
|
qDebug() << "Rows removed: parent: " << parent << " first: " << first << " last " << last;
|
|
|
|
// Setting Text to "" will delete the item
|
|
|
|
this->updateDisplayTextSetParameter(first, OmobiDisplayTextModel::TextRole, "");
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::sendBluetoothCommand(OmobiDisplayCommand command, QVariant data) {
|
|
|
|
QVariantMap commandMap = {
|
|
|
|
{"header", command},
|
|
|
|
{"data", data}
|
|
|
|
};
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(commandMap);
|
|
|
|
|
|
|
|
qDebug() << "Sending command: \n" << qPrintable(doc.toJson(QJsonDocument::Indented));
|
|
|
|
|
|
|
|
this->waitingCommands ++;
|
|
|
|
|
|
|
|
if(this->state == Connected)
|
|
|
|
this->setState(Loading);
|
|
|
|
|
|
|
|
this->ble->sendData(doc.toJson(QJsonDocument::Compact));
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:57:07 +02:00
|
|
|
void OmobiDisplayBackend::sendBluetoothKeepAlive() {
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(QVariantMap{{"header", KeepAliveCommand}});
|
|
|
|
|
|
|
|
qDebug() << "Sending keep alive: \n" << qPrintable(doc.toJson(QJsonDocument::Indented));
|
|
|
|
|
|
|
|
this->ble->sendData(doc.toJson(QJsonDocument::Compact));
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::handleBluetoothDataReceived(QString s){
|
|
|
|
qDebug() << "New data: \n" << qPrintable(s);
|
2020-10-11 21:01:21 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
QJsonParseError parseError;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(s.toUtf8(), &parseError);
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
if(parseError.error != QJsonParseError::NoError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OmobiDisplayCommand header = OmobiDisplayCommand(doc.toVariant().toMap()["header"].toInt());
|
|
|
|
QVariantMap data = doc.toVariant().toMap()["data"].toMap();
|
|
|
|
OmobiDisplayStatusCode status = OmobiDisplayStatusCode(doc.toVariant().toMap()["status"].toInt());
|
|
|
|
|
|
|
|
switch (header) {
|
2020-10-15 16:57:07 +02:00
|
|
|
case AuthorizeSessionCommand: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KeepAliveCommand: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
case GetAllTextSetsCommand: {
|
|
|
|
// indicates that all existing txt sets have been sent over after GetAllTextSetsCommand was called
|
|
|
|
if(status != Success)
|
|
|
|
// TODO: handle error
|
|
|
|
break;
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
this->displayTextModel->maximumTextLength = data["maximumTextLength"].toInt();
|
|
|
|
this->displayTextModel->maximumTextSets = data["maximumTextSets"].toInt();
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
this->displayTextModel->setTexts(this->textSetsBuffer);
|
|
|
|
this->textSetsBuffer.clear();
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
this->refreshLoadingState();
|
|
|
|
|
|
|
|
break;
|
2020-10-13 01:58:14 +02:00
|
|
|
}
|
2020-10-14 23:54:12 +02:00
|
|
|
case GetTextSetParameterCommand: {
|
|
|
|
|
|
|
|
int index = data["index"].toInt();
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
if(index < 0)
|
|
|
|
return;
|
|
|
|
|
2020-10-15 12:52:19 +02:00
|
|
|
while(this->textSetsBuffer.length() <= index)
|
2020-10-14 23:54:12 +02:00
|
|
|
this->textSetsBuffer.append(QMap<int, QVariant>());
|
|
|
|
|
|
|
|
int parameter = data["parameter"].toInt();
|
2020-10-13 01:58:14 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
if(!this->textSetsBuffer[index].contains(parameter))
|
|
|
|
this->textSetsBuffer[index].insert(parameter, QVariant());
|
2020-10-13 17:00:25 +02:00
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
this->textSetsBuffer[index][parameter] = data["value"].toString();
|
|
|
|
|
|
|
|
if(this->state != Initing)
|
|
|
|
this->refreshLoadingState();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GetDisplayBrightnessCommand: {
|
|
|
|
this->setDisplayBrightness(data["displayBrightness"].toInt());
|
|
|
|
this->refreshLoadingState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SetTextSetParameterCommand: {
|
|
|
|
// TODO: Error handling
|
|
|
|
this->refreshLoadingState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SetDisplayBrightnessCommand: {
|
|
|
|
// TODO: Error handling
|
|
|
|
this->refreshLoadingState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::updateDisplayTextSetParameter(int index, int parameter) {
|
|
|
|
this->updateDisplayTextSetParameter(index, parameter, this->displayTextModel->data(index, parameter).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::updateDisplayTextSetParameter(int index, int parameter, QString value) {
|
|
|
|
if(this->state == Initing)
|
|
|
|
return;
|
|
|
|
qDebug() << "Updating data at index: " << index << " parameter: " << parameter << " and value: " << value;
|
|
|
|
|
|
|
|
QVariantMap dataMap = {
|
|
|
|
{"index", index},
|
|
|
|
{"parameter", parameter},
|
|
|
|
{"value", value}
|
|
|
|
};
|
|
|
|
|
|
|
|
this->sendBluetoothCommand(SetTextSetParameterCommand, dataMap);
|
2020-10-11 21:01:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 00:03:50 +02:00
|
|
|
QBluetoothLeUart* OmobiDisplayBackend::getBleController() {
|
|
|
|
return this->ble;
|
|
|
|
}
|
2020-10-11 15:50:13 +02:00
|
|
|
|
2020-10-11 21:01:21 +02:00
|
|
|
|
|
|
|
OmobiDisplayTextModel* OmobiDisplayBackend::getDisplayTextModel() {
|
|
|
|
return this->displayTextModel;
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:50:13 +02:00
|
|
|
OmobiDisplayBackend::OmobiDisplayAppState OmobiDisplayBackend::getState() {
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
void OmobiDisplayBackend::refreshLoadingState() {
|
|
|
|
if(this->state != Initing && this->state != Loading)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qDebug() << "Refreshing loading state! Waiting: " << this->waitingCommands;
|
|
|
|
|
|
|
|
if(this->waitingCommands <= 1) {
|
|
|
|
this->waitingCommands = 0;
|
|
|
|
this->setState(Connected);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this->waitingCommands--;
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:50:13 +02:00
|
|
|
void OmobiDisplayBackend::setState(OmobiDisplayAppState state) {
|
|
|
|
if(state == this->state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->state = state;
|
|
|
|
emit this->stateChanged();
|
|
|
|
|
2020-10-14 23:54:12 +02:00
|
|
|
qDebug() << "Now in " << state << " state";
|
|
|
|
|
2020-10-11 15:50:13 +02:00
|
|
|
if(this->state == Idle)
|
|
|
|
this->ble->startScanningForDevices();
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
|
|
|
|
int OmobiDisplayBackend::getDisplayBrightness() {
|
|
|
|
return this->displayBrightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::setDisplayBrightness(int brightness) {
|
|
|
|
if(brightness == this->displayBrightness)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->displayBrightness = brightness;
|
|
|
|
|
2020-10-15 17:21:31 +02:00
|
|
|
this->sendBluetoothCommand(SetDisplayBrightnessCommand, QVariantMap{{"displayBrightness",this->displayBrightness}});
|
2020-10-13 01:58:14 +02:00
|
|
|
|
|
|
|
emit this->displayBrightnessChanged();
|
|
|
|
}
|