#include "omobidisplaybackend.h" OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent) { this->ble = new QBluetoothLeUart(); this->displayTextModel = new OmobiDisplayTextModel(this); connect(this->ble, &QBluetoothLeUart::stateChanged, this, &OmobiDisplayBackend::handleBluetoothStateChange); connect(this->ble, &QBluetoothLeUart::foundNewDevice, this, &OmobiDisplayBackend::handleFoundNewDevice); connect(this->ble, &QBluetoothLeUart::dataReceived, this, &OmobiDisplayBackend::DataHandler); connect(this->ble, &QBluetoothLeUart::connectedToDevice, this, &OmobiDisplayBackend::handleBluetoothDeviceConected); connect(this->displayTextModel, &OmobiDisplayTextModel::dataChanged, this, &OmobiDisplayBackend::handleDisplayTextModelDataChanged); this->setState(Idle); this->ble->startScanningForDevices(); } void OmobiDisplayBackend::startScanning() { this->ble->startScanningForDevices(); } void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){ switch(state){ 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: { break; } } } void OmobiDisplayBackend::handleBluetoothDeviceConected() { this->ble->sendData("GET_TEXTS"); this->ble->sendData("GET_BRIGHTNESS"); // get the existing model data! // TODO: implement some communication! QString currentDisplayText = "[[],[]]"; this->setState(Connected); } void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) { qDebug() << "Found a device: name: " << device->getName() << " address: " << device->getAddress(); } void OmobiDisplayBackend::DataHandler(QString s){ qDebug() << "New data: " << s; if(s.startsWith("GET_TEXTS:")) { QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(s.replace("GET_TEXTS:", "").toUtf8(), &parseError); if(parseError.error != QJsonParseError::NoError) return; QList> texts; for(QVariant textMap : doc.toVariant().toList()) { QMap text; static const QMap keyTranslations { { "text", OmobiDisplayTextModel::TextRole }, { "active", OmobiDisplayTextModel::ActiveRole }, { "runtime", OmobiDisplayTextModel::RuntimeRole }, { "color", OmobiDisplayTextModel::ColorRole }, { "alignment", OmobiDisplayTextModel::AlignmentRole }, { "scroll", OmobiDisplayTextModel::ScrollRole }, { "scrollSpeed", OmobiDisplayTextModel::ScrollSpeedRole }, { "scrollCount", OmobiDisplayTextModel::ScrollCountRole }, { "index", OmobiDisplayTextModel::IndexRole } }; for(QString key : textMap.toMap().keys()) { if(keyTranslations.contains(key)) text.insert(keyTranslations[key], textMap.toMap()[key]); } texts.append(text); } this->displayTextModel->setTexts(texts); } else if(s.startsWith("GET_BRIGHTNESS:")) { this->displayBrightness = s.replace("GET_BRIGHTNESS:", "").toInt(); emit this->displayBrightnessChanged(); } } void OmobiDisplayBackend::handleDisplayTextModelDataChanged() { QVariantList textList; for(QMap text : this->displayTextModel->getTexts()) { QVariantHash textMap; for(int key : text.keys()) { textMap.insert(this->displayTextModel->roleNames()[key], text[key]); } textList.append(textMap); } QJsonDocument doc = QJsonDocument::fromVariant(textList); this->ble->sendData("SET_TEXTS:" + doc.toJson(QJsonDocument::Compact)); } QBluetoothLeUart* OmobiDisplayBackend::getBleController() { return this->ble; } OmobiDisplayTextModel* OmobiDisplayBackend::getDisplayTextModel() { return this->displayTextModel; } OmobiDisplayBackend::OmobiDisplayAppState OmobiDisplayBackend::getState() { return this->state; } void OmobiDisplayBackend::setState(OmobiDisplayAppState state) { if(state == this->state) return; this->state = state; emit this->stateChanged(); if(this->state == Idle) this->ble->startScanningForDevices(); } int OmobiDisplayBackend::getDisplayBrightness() { return this->displayBrightness; } void OmobiDisplayBackend::setDisplayBrightness(int brightness) { if(brightness == this->displayBrightness) return; this->displayBrightness = brightness; this->ble->sendData("SET_BRIGHTNESS:" + QString::number(this->displayBrightness)); emit this->displayBrightnessChanged(); }