# HG changeset patch # User Michiel Broek # Date 1656082222 -7200 # Node ID 79f100a84f65554c375ce6640ec6d14f468fd69e # Parent afd711e37f683fba8e1977c86c9dd8e14b96edcd Add database maintenance to the startup code. Start some work ok parsing websocket messages. diff -r afd711e37f68 -r 79f100a84f65 src/MainWindow.cpp --- a/src/MainWindow.cpp Fri Jun 24 11:07:34 2022 +0200 +++ b/src/MainWindow.cpp Fri Jun 24 16:50:22 2022 +0200 @@ -63,6 +63,7 @@ db->openDataBase(useDevelopOption); loadSetup(); + maintDataBase(); openWS(useDevelopOption); Acid a; @@ -141,6 +142,50 @@ } +/* + * On the server where bmsd runs, there is a crontask.php that does these checks + * too. Here we do some of the same commands so that we have the results sooner. + * Currently this takes 6 to 9 mSecs. + */ +void MainWindow::maintDataBase() +{ + QSqlQuery query; + + /* + * Upgrade package values. + */ + query.exec("UPDATE products SET package_volume = bottle_amount + keg_amount WHERE package_volume='0'"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to new package_volume value"; + query.exec("UPDATE products SET bottle_priming_water = bottle_amount * bottle_priming_amount / 500 WHERE bottle_priming_water = 0"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to new bottle_priming_water"; + query.exec("UPDATE products SET keg_priming_water = keg_amount * keg_priming_amount / 500 WHERE keg_priming_water = 0"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to new keg_priming_water"; + + /* + * Upgrade inventory_reduced value from old boolean to tiny integer value. + */ + query.exec("UPDATE products SET inventory_reduced=stage WHERE inventory_reduced = 1"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to new inventory_reduced value"; + + /* + * Update stages after packaging depending on the age. + */ + query.exec("UPDATE products SET stage=7 WHERE stage = 6 AND DATEDIFF(CURDATE(), package_date) > 0"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to stage 7 (Carbonation)"; + query.exec("UPDATE products SET stage=8 WHERE stage = 7 AND DATEDIFF(CURDATE(), package_date) > 13"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to stage 8 (Mature)"; + query.exec("UPDATE products SET stage=9 WHERE stage = 8 AND DATEDIFF(CURDATE(), package_date) > 41"); + if (query.numRowsAffected()) + qInfo() << "Updated" << query.numRowsAffected() << "products to stage 9 (Taste)"; +} + + bool MainWindow::openWS(bool develop) { QString server; @@ -178,7 +223,38 @@ void MainWindow::wsTextMessageReceived(QString message) { -// qDebug() << "WS received:" << message; + //qDebug() << "WS received:" << message; + + QJsonParseError parseError; + QJsonDocument jsonMessage = QJsonDocument::fromJson(message.toUtf8(), &parseError); + + if (parseError.error != QJsonParseError::NoError) { + qWarning() << "wsTextMessageReceived error:" << parseError.errorString() << "at" << parseError.offset ; + return; + } + + /* + * Two maingroups, nodes and devices. + * Node message are detected by the group_id object. + * Device messages are detected by the device object. + */ + QString device = jsonMessage.object()["device"].toString(); + QString group_id = jsonMessage.object()["group_id"].toString(); + if (device != "") { + if (device == "fermenters") { + qDebug() << "found fermenter"; + } else if (device == "co2meters") { + qDebug() << "found co2meter"; + } else if (device == "ispindels") { + qDebug() << "found iSpindel"; + } else { + qDebug() << "unknown device" << device; + } + } else if (group_id != "") { + qDebug() << "node" << jsonMessage.object()["node"].toString(); + } else { + qDebug() << "unknown WS message" << message; + } } diff -r afd711e37f68 -r 79f100a84f65 src/MainWindow.h --- a/src/MainWindow.h Fri Jun 24 11:07:34 2022 +0200 +++ b/src/MainWindow.h Fri Jun 24 16:50:22 2022 +0200 @@ -185,6 +185,11 @@ void loadSetup(); /** + * @brief Do database maintenance. + */ + void maintDataBase(); + + /** * @brief Open Websocket connection. * @param develop Is true if connect to develop server, else production. * @return Returns true if succes.