Skip to content

Commit

Permalink
implement commands via secondary instance
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianRudolph committed Jun 20, 2021
1 parent 58899d4 commit a73a7d2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "singleapplication"]
path = singleapplication
url = git@github.com:itay-grudev/SingleApplication.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Bluetooth)

add_executable(${PROJECT_NAME} main.cpp MainWindow.cpp Protocol.cpp Statistics.cpp)

set(QAPPLICATION_CLASS QApplication CACHE STRING "Inheritance class for SingleApplication")
add_subdirectory(singleapplication)
target_link_libraries(${PROJECT_NAME} SingleApplication::SingleApplication)

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
33 changes: 28 additions & 5 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void MainWindow::handleSend() {
if (state != CONNECTED) return;
if (!sendQueue.empty()) {
service->writeCharacteristic(writeChar, sendQueue.front());
qDebug() << "SEND" << sendQueue.front().toHex();
//qDebug() << "SEND" << sendQueue.front().toHex();
sendQueue.pop_front();
}
}
Expand Down Expand Up @@ -400,11 +400,11 @@ void MainWindow::serviceStateChanged(QLowEnergyService::ServiceState newState) {

void MainWindow::characteristicChanged(const QLowEnergyCharacteristic &c, const QByteArray &value){
if (c.uuid().toUInt16() != 0xfe01) return;
qDebug() << "Characteristic Changed" << c.uuid() << value.toHex(' ');
//qDebug() << "Characteristic Changed" << c.uuid() << value.toHex(' ');
using namespace Pad;
auto parsed = parseMessage(value);
if (auto info = std::get_if<Info>(&parsed)) {
qDebug("Info: state %u, speed %u, mode %u, time %u, distance %u, steps %u", info->state, info->speed, info->mode, info->time, info->distance, info->steps);
//qDebug("Info: state %u, speed %u, mode %u, time %u, distance %u, steps %u", info->state, info->speed, info->mode, info->time, info->distance, info->steps);
auto time = QDateTime::currentMSecsSinceEpoch();
if (info->mode < 3 && (time - setModeTime) > 1000) {
modeButtons[info->mode]->setChecked(true);
Expand All @@ -420,11 +420,11 @@ void MainWindow::characteristicChanged(const QLowEnergyCharacteristic &c, const
auto s = info->state;
startButton->setText(s == 0 || s == 5 ? "Start" : "Stop");
} else if (auto params = std::get_if<Params>(&parsed)) {
qDebug("Params: goalType %u, goal %u, regulate %u, maxSpeed %u, startSpeed %u, startMode %u, sensitivity %u, display %x, lock %u, unit %u", params->goalType, params->goal, params->regulate, params->maxSpeed, params->startSpeed, params->startMode, params->sensitivity, params->display, params->lock, params->unit);
//qDebug("Params: goalType %u, goal %u, regulate %u, maxSpeed %u, startSpeed %u, startMode %u, sensitivity %u, display %x, lock %u, unit %u", params->goalType, params->goal, params->regulate, params->maxSpeed, params->startSpeed, params->startMode, params->sensitivity, params->display, params->lock, params->unit);
queriedParams = true; // only query params once
setStartSpeedWidgets(params->startSpeed);
} else if (auto record = std::get_if<Record>(&parsed)) {
qDebug("Record: onTime %u, startTime %u, duration %u, distance %u, steps %u, remaining %u", record->onTime, record->startTime, record->duration, record->distance, record->steps, record->remainingRecords);
//qDebug("Record: onTime %u, startTime %u, duration %u, distance %u, steps %u, remaining %u", record->onTime, record->startTime, record->duration, record->distance, record->steps, record->remainingRecords);
remainingRecords = record->remainingRecords;
if (record->duration) {
stats.addRecord(*record);
Expand Down Expand Up @@ -506,3 +506,26 @@ void MainWindow::updateStatsLabel() {
.arg(dat.distance/1000., w, 'f', 2));
}

void MainWindow::receivedMessage(int instanceId, QByteArray message) {
if (state != CONNECTED) return;
auto args = message.split(' ');
if (args.empty()) return;
auto cmd = args[0];
if (cmd == "start") {
send(Pad::start());
} else {
if (args.size() < 2) return;
bool ok;
int val = args[1].toInt(&ok);
if (!ok) return;
if (cmd == "setSpeed" && val >= 0 && val <= 60) { //TODO max speed
send(Pad::setSpeed(val));
} else if (cmd == "addSpeed") {
auto speed = currentSpeed + val;
if (speed >= 0 && speed <= 60) {
send(Pad::setSpeed(speed));
}
}
}
}

3 changes: 3 additions & 0 deletions MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class MainWindow : public QMainWindow {
public:
MainWindow();

public slots:
void receivedMessage(int instanceId, QByteArray message);

protected:
void showEvent(QShowEvent *event) override;

Expand Down
25 changes: 18 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
#include "MainWindow.h"
#include <QApplication>
#include <QStyleFactory>
#include <QLoggingCategory>
#include <singleapplication.h>

int main(int argc, char *argv[]) {
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
//QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));

QCoreApplication::setOrganizationName("Dorian Rudolph");
QCoreApplication::setOrganizationDomain("dorianrudolph.com");
QCoreApplication::setApplicationName("QWalkingPad");

if (!Settings().getUseSystemTheme()) {
if (argc == 1 && !Settings().getUseSystemTheme()) {
QApplication::setStyle(QStyleFactory::create("Fusion"));
QIcon::setThemeName("Fusion");
}

QApplication a(argc, argv);
MainWindow win;
win.show();
return QApplication::exec();
SingleApplication app(argc, argv, true);

if (app.isPrimary()) {
if (argc > 1) return 1;
MainWindow win;
QObject::connect(&app, &SingleApplication::receivedMessage, &win, &MainWindow::receivedMessage);
win.show();
return QApplication::exec();
} else {
auto args = SingleApplication::arguments();
args.pop_front();
if (args.empty()) return 1;
app.sendMessage(args.join(' ').toUtf8());
}

}
1 change: 1 addition & 0 deletions singleapplication
Submodule singleapplication added at c557da

0 comments on commit a73a7d2

Please sign in to comment.