Sat, 11 Feb 2023 15:48:02 +0100
Monitor iSpindels: use global variables instead of repeated expensive MySQL calls. Use the yeast temperature ranges for the colors on the thermometer scale. Don't show SVG and ABV if the OG is not yet known. Turn statusfield red if offline. Extra mon_ispindles fields yeast_lo and yeast_hi. Need at least bmsd version 0.3.42. If a websocket message is received that cannot be parsed, show the whole received message.
/** * DetailiSpindel.cpp is part of bmsapp. * * bmsapp is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * bmsapp is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "DetailiSpindel.h" #include "ChartiSpindel.h" #include "../ui/ui_DetailiSpindel.h" #include "global.h" #include "Utils.h" #include "MainWindow.h" /* * Results are available via MySQL and websockets. Because we initialize using * MySQL we only use that for the results and up to date status. * Commands are send via websockets only. */ DetailiSpindel::DetailiSpindel(int id, QWidget *parent) : QDialog(parent), ui(new Ui::DetailiSpindel) { QSqlQuery query; #ifdef DEBUG_MONITOR qDebug() << "DetailiSpindel record:" << id; #endif ui->setupUi(this); this->recno = id; setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); setWindowTitle(tr("BMSapp - Details iSpindel")); ui->thermoMeter->setMaximum(40.0); ui->thermoMeter->setNominal(20.0); ui->thermoMeter->setCritical(25.0); ui->thermoMeter->setSuffix(QString("°C")); ui->modeEdit->addItem("OFF"); ui->modeEdit->addItem("ON"); ui->codePick->addItem("Erase beer"); query.exec("SELECT code,name FROM products WHERE stage='1' OR stage='2' OR stage='3' OR stage='4' OR stage='5' OR stage='6' OR stage='7' ORDER BY code"); while (query.next()) { ui->codePick->addItem(query.value("code").toString()+" - "+query.value("name").toString()); } connect(ui->codePick, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetailiSpindel::code_changed); connect(ui->modeEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetailiSpindel::mode_changed); connect(parent, SIGNAL(updateiSpindel(QString)), this, SLOT(refreshiSpindel(QString))); emit refreshTable(); } void DetailiSpindel::refreshTable() { QSqlQuery query; qDebug() << "DetailiSpindel::refreshTable()"; query.prepare("SELECT * FROM mon_ispindels WHERE record = :recno"); query.bindValue(":recno", this->recno); query.exec(); if (query.next()) { const QSignalBlocker blocker1(ui->codePick); const QSignalBlocker blocker2(ui->modeEdit); _node = query.value("node").toString(); _alias = query.value("alias").toString(); _uuid = query.value("uuid").toString(); _beercode = query.value("beercode").toString(); _beername = query.value("beername").toString(); bool alarm = (query.value("alarm").toInt() != 0) ? true:false; bool online = (query.value("online").toInt() != 0) ? true:false; bool mode = (query.value("mode").toString() == "ON") ? true:false; ui->uuidEdit->setText(_uuid); ui->systemEdit->setText(_node+"/"+_alias); ui->codePick->setItemText(0, _alias.toUpper()+" - "+_alias); ui->alarmLED->setChecked(alarm); if (online) { ui->statusEdit->setText(tr("Online")); ui->statusEdit->setStyleSheet(""); ui->codeEdit->setText(_beercode+" - "+_beername); ui->thermoMeter->setNominal(query.value("yeast_lo").toDouble()); ui->thermoMeter->setCritical(query.value("yeast_hi").toDouble()); ui->powerLED->setChecked(mode); ui->modeEdit->show(); if (mode) { ui->modeEdit->setCurrentIndex(1); ui->codePick->hide(); } else { ui->modeEdit->setCurrentIndex(0); ui->codePick->show(); } ui->voltEdit->setText(QString("%1").arg(query.value("battery").toDouble(), 4, 'f', 3, '0')); ui->tiltEdit->setText(QString("%1").arg(query.value("angle").toDouble(), 6, 'f', 5, '0')); ui->platoEdit->setText(QString("%1").arg(query.value("gravity").toDouble(), 4, 'f', 3, '0')); double sg = Utils::plato_to_sg(query.value("gravity").toDouble()); ui->sgEdit->setText(QString("%1").arg(sg, 5, 'f', 4, '0')); if (query.value("gravity").toDouble()) { double o_plato = query.value("og_gravity").toDouble(); double og = Utils::plato_to_sg(o_plato); double svg = Utils::calc_svg(og, sg); ui->ogVal->setText(QString("%1").arg(og, 5, 'f', 4, '0')); ui->ogVal2->setText(QString("%1°P").arg(o_plato, 4, 'f', 3, '0')); ui->sgVal->setText(QString("%1").arg(sg, 5, 'f', 4, '0')); ui->sgVal2->setText(QString("%1°P").arg(query.value("gravity").toDouble(), 4, 'f', 3, '0')); if (o_plato > 0.1) { ui->svgVal->setText(QString("%1%").arg(svg, 2, 'f', 1, '0')); ui->svgBar->setValue(svg); ui->abvVal->setText(QString("%1%").arg(Utils::abvol(og, sg), 3, 'f', 2, '0')); } else { /* o_plato is 0 if a new beer is selected. */ ui->svgVal->setText(""); ui->svgBar->setValue(0); ui->abvVal->setText(""); } } else { ui->ogVal->setText(""); ui->ogVal2->setText(""); ui->sgVal->setText(""); ui->sgVal2->setText(""); ui->svgVal->setText(""); ui->abvVal->setText(""); ui->batVal->setText(""); } double batt = query.value("battery").toDouble() - 3.064; // 0% voltage if (batt < 0) batt = 0; batt = round(batt / 1.17875 * 100.0); // 100% range if (batt > 100) batt = 100; ui->batVal->setText(QString("%1%").arg(batt, 2, 'f', 1, '0')); ui->thermoBox->show(); ui->thermoMeter->setValue(query.value("temperature").toDouble()); } else { /* Offline */ ui->statusEdit->setText(tr("Offline")); ui->statusEdit->setStyleSheet("background-color: red"); ui->powerLED->setChecked(false); ui->alarmLED->setChecked(true); ui->codePick->hide(); ui->modeEdit->hide(); ui->thermoBox->hide(); ui->logButton->hide(); } } } DetailiSpindel::~DetailiSpindel() { delete ui; emit entry_changed(); } /* * Receive signals destined for all co2meters. * Check if the signal is for us. */ void DetailiSpindel::refreshiSpindel(QString data) { if (_node+"/"+_alias == data) { emit refreshTable(); } } void DetailiSpindel::on_logButton_clicked() { ChartiSpindel dialog(_beercode, _beername, this); } void DetailiSpindel::on_quitButton_clicked() { this->close(); this->setResult(1); } void DetailiSpindel::mode_changed(int val) { QStringList mode ({ "OFF", "ON" }); QString msg = QString("{\"device\":\"ispindels\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\",\"mode\":\"" + mode[val] + "\"}"); #ifdef DEBUG_MONITOR qDebug() << "mode_changed" << val << msg; #endif webSocket->sendTextMessage(msg); } void DetailiSpindel::code_changed(int val) { QJsonParseError parseError; QSqlQuery query; QString msg = QString("{\"device\":\"ispindels\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\","); if (val == 0) { msg.append(QString("\"beeruuid\":\"") + _uuid + "\","); msg.append(QString("\"beercode\":\"") + _alias.toUpper() + "\","); msg.append(QString("\"beername\":\"") + _alias + "\","); msg.append(QString("\"yeast_lo\":20.0,")); msg.append(QString("\"yeast_hi\":25.0}")); } else { query.exec("SELECT code,name,uuid,stage,json_yeasts FROM products WHERE stage='1' OR stage='2' OR stage='3' OR stage='4' OR stage='5' OR stage='6' OR stage='7' ORDER BY code"); for (int i = 0; i < val; i++) { query.next(); } double yl = 0; double yh = 40; const auto& y_json = query.value("json_yeasts").toString(); if (! y_json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(y_json); QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; } else if (yeasts.isArray()) { for (int i = 0; i < yeasts.array().size(); i++) { QJsonObject obj = yeasts.array().at(i).toObject(); if (obj["y_use"].toInt() == 0) { // Primary yeast if (obj["y_min_temperature"].toDouble() > yl) yl = obj["y_min_temperature"].toDouble(); if (obj["y_max_temperature"].toDouble() < yh) yh = obj["y_max_temperature"].toDouble(); } } } } msg.append(QString("\"beeruuid\":\"") + query.value("uuid").toString() + "\","); msg.append(QString("\"beercode\":\"") + query.value("code").toString() + "\","); msg.append(QString("\"beername\":\"") + query.value("name").toString() + "\","); msg.append(QString("\"yeast_lo\":%1,").arg(yl)); msg.append(QString("\"yeast_hi\":%1}").arg(yh)); } #ifdef DEBUG_MONITOR qDebug() << "code_changed" << val << msg; #endif webSocket->sendTextMessage(msg); }