# HG changeset patch # User Michiel Broek # Date 1719048494 -7200 # Node ID 48de0f61e5ea3b1846a30ffb30e865e7a915804c # Parent 3d327db12cefc1e020ac7a705da0d38fd83d07fc The iSpindel monitor, detail and chart screens try to use the polynomial calibration data to show the SG. Fallback to already logged data if this fails. diff -r 3d327db12cef -r 48de0f61e5ea src/ChartiSpindel.cpp --- a/src/ChartiSpindel.cpp Fri Jun 14 08:45:35 2024 +0200 +++ b/src/ChartiSpindel.cpp Sat Jun 22 11:28:14 2024 +0200 @@ -23,10 +23,11 @@ ChartiSpindel::ChartiSpindel(QString code, QString name, QWidget *parent) : QDialog(parent) { QSqlQuery query; - double timestamp; + double timestamp, poly[4], plato, angle; double sg_min = 1.2, sg_max = 0.8, mg, sg; double temp_min = 100, temp_max = 0, temp; double batt_min = 4.5, batt_max = 3.0, batt; + bool polyvalid = false; qDebug() << "ChartiSpindel:" << code << name; @@ -48,6 +49,33 @@ buttonBox->setStandardButtons(QDialogButtonBox::Ok); buttonBox->addButton(saveButton,QDialogButtonBox::ActionRole); + /* + * Try to load the polynomial calibration data. + * This only succeeds for the current brew. + * For historical brews we need another way to load the correct record. + */ + query.prepare("SELECT calibrate FROM mon_ispindels WHERE beercode=:beercode AND beername=:beername"); + query.bindValue(":beercode", code); + query.bindValue(":beername", name); + query.exec(); + if (query.next()) { + QJsonParseError parseError; + const auto& json = query.value("calibrate").toString(); + if (!json.trimmed().isEmpty()) { + const auto& formattedJson = QString("%1").arg(json); + QJsonDocument jsonResponse = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); + QJsonObject jsonObj = jsonResponse.object(); + QJsonArray polyData = jsonObj.value("polyData").toArray(); + if (polyData.size() == 4) { + for (int i = 0; i < polyData.size(); i++) { + poly[i] = polyData.at(i).toDouble(); + } + polyvalid = true; + } + } + } +// qDebug() << "polyvalid" << polyvalid; + temperature = new QLineSeries(); density = new QLineSeries(); battery = new QLineSeries(); @@ -57,8 +85,14 @@ query.exec(); while (query.next()) { timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000; - - mg = query.value("sg").toDouble(); + if (polyvalid) { + angle = query.value("angle").toDouble(); + plato = (poly[0] * pow(angle, 3)) + (poly[1] * pow(angle, 2)) + (poly[2] * angle) + poly[3]; + mg = Utils::plato_to_sg(plato); +// qDebug() << "sg" << query.value("sg").toDouble() << "mg" << mg; + } else { + mg = query.value("sg").toDouble(); + } temp = query.value("temperature").toDouble(); if (ceil(temp) > temp_max) temp_max = ceil(temp); diff -r 3d327db12cef -r 48de0f61e5ea src/DetailiSpindel.cpp --- a/src/DetailiSpindel.cpp Fri Jun 14 08:45:35 2024 +0200 +++ b/src/DetailiSpindel.cpp Sat Jun 22 11:28:14 2024 +0200 @@ -64,6 +64,7 @@ void DetailiSpindel::refreshTable() { QSqlQuery query; + double angle, plato, sg, poly[4]; query.prepare("SELECT * FROM mon_ispindels WHERE record = :recno"); query.bindValue(":recno", this->recno); @@ -87,30 +88,50 @@ ui->systemEdit->setText(_node+"/"+_alias); ui->codePick->setItemText(0, _alias.toUpper()+" - "+_alias); ui->alarmLED->setChecked(alarm); + ui->lastEdit->setText(query.value("lastseen").toDateTime().toString("dd MMM HH:mm:ss")); + ui->codeEdit->setText(_beercode+" - "+_beername); + ui->powerLED->setChecked(mode); + ui->modeEdit->show(); + ui->modeEdit->setCurrentIndex(mode ? 1:0); 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(); + ui->modeEdit->setEnabled(true); if (mode) { - ui->modeEdit->setCurrentIndex(1); ui->codePick->hide(); } else { - ui->modeEdit->setCurrentIndex(0); ui->codePick->show(); } + plato = query.value("gravity").toDouble(); + angle = query.value("angle").toDouble(); + /* + * Try to use the calibrate poly to recalcultate the density. + */ + QJsonParseError parseError; + const auto& json = query.value("calibrate").toString(); + if (!json.trimmed().isEmpty()) { + const auto& formattedJson = QString("%1").arg(json); + QJsonDocument jsonResponse = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); + QJsonObject jsonObj = jsonResponse.object(); + QJsonArray polyData = jsonObj.value("polyData").toArray(); + if (polyData.size() == 4) { + for (int i = 0; i < polyData.size(); i++) { + poly[i] = polyData.at(i).toDouble(); + } + /* New plato from current polynomial */ + plato = (poly[0] * pow(angle, 3)) + (poly[1] * pow(angle, 2)) + (poly[2] * angle) + poly[3]; + } + } + sg = Utils::plato_to_sg(plato); + 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->tiltEdit->setText(QString("%1").arg(angle, 6, 'f', 5, '0')); + ui->platoEdit->setText(QString("%1").arg(plato, 4, 'f', 3, '0')); ui->sgEdit->setText(QString("%1").arg(sg, 5, 'f', 4, '0')); - ui->lastEdit->setText(query.value("lastseen").toDateTime().toString("dd MMM HH:mm:ss")); if (query.value("gravity").toDouble()) { double o_plato = query.value("og_gravity").toDouble(); @@ -158,10 +179,9 @@ /* 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->modeEdit->setEnabled(false); ui->thermoBox->hide(); ui->logButton->hide(); } diff -r 3d327db12cef -r 48de0f61e5ea src/MoniSpindels.cpp --- a/src/MoniSpindels.cpp Fri Jun 14 08:45:35 2024 +0200 +++ b/src/MoniSpindels.cpp Sat Jun 22 11:28:14 2024 +0200 @@ -74,8 +74,9 @@ void MoniSpindels::refreshTable() { QTableWidgetItem *item; + double plato, poly[4]; - QSqlQuery query("SELECT record,alias,node,online,mode,temperature,gravity,beercode,beername FROM mon_ispindels ORDER BY alias"); + QSqlQuery query("SELECT record,alias,node,online,mode,temperature,gravity,angle,calibrate,beercode,beername FROM mon_ispindels ORDER BY alias"); const QStringList labels({tr("Unit"), tr("Node"), tr("Status"), tr("Beer"), tr("Temperature"), tr("SG"), tr("Cal"), tr("Details")}); this->tableiSpindels->setColumnCount(8); @@ -114,10 +115,31 @@ } if (query.value("online").toInt()) { + /* + * Try to use the calibrate poly to recalcultate the SG. + */ + plato = query.value("gravity").toDouble(); + QJsonParseError parseError; + const auto& json = query.value("calibrate").toString(); + if (!json.trimmed().isEmpty()) { + const auto& formattedJson = QString("%1").arg(json); + QJsonDocument jsonResponse = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); + QJsonObject jsonObj = jsonResponse.object(); + QJsonArray polyData = jsonObj.value("polyData").toArray(); + if (polyData.size() == 4) { + for (int i = 0; i < polyData.size(); i++) { + poly[i] = polyData.at(i).toDouble(); + } + double angle = query.value("angle").toDouble(); + /* New plato from current polynomial */ + plato = (poly[0] * pow(angle, 3)) + (poly[1] * pow(angle, 2)) + (poly[2] * angle) + poly[3]; + } + } + item = new QTableWidgetItem(QString("%1°C").arg(query.value("temperature").toDouble(), 4, 'f', 3, '0')); item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); this->tableiSpindels->setItem(i, 4, item); - double sg = Utils::plato_to_sg(query.value("gravity").toDouble()); + double sg = Utils::plato_to_sg(plato); item = new QTableWidgetItem(QString("%1").arg(sg, 5, 'f', 4, '0')); item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); this->tableiSpindels->setItem(i, 5, item); diff -r 3d327db12cef -r 48de0f61e5ea translations/bmsapp_en.ts --- a/translations/bmsapp_en.ts Fri Jun 14 08:45:35 2024 +0200 +++ b/translations/bmsapp_en.ts Sat Jun 22 11:28:14 2024 +0200 @@ -299,79 +299,79 @@ ChartiSpindel - + BMSapp - iSpindel - + Save - + Temperature - + SG - + Battery - + Date - + Temperature °C - + Battery volt - - Save Image - - - - - Image (*.png) - - - - - Save File - - - - - No image file selected. - - - - - %1 -Temperature %2°C - - - + Save Image + + + + + Image (*.png) + + + + + Save File + + + + + No image file selected. + + + + + %1 +Temperature %2°C + + + + %1 Density %2 SG - + %1 Battery %2 volt Capacity %3% @@ -1143,12 +1143,12 @@ - + Online - + Offline @@ -10359,49 +10359,49 @@ - + Unit - + Node - + Status - + Beer - + Temperature - + SG - - + + Details - - + + Cal - + Total items: %1 diff -r 3d327db12cef -r 48de0f61e5ea translations/bmsapp_nl.ts --- a/translations/bmsapp_nl.ts Fri Jun 14 08:45:35 2024 +0200 +++ b/translations/bmsapp_nl.ts Sat Jun 22 11:28:14 2024 +0200 @@ -359,7 +359,7 @@ ChartiSpindel - + BMSapp - iSpindel BMSapp - iSpindel @@ -368,76 +368,76 @@ Temp °C - + Save Bewaar - + Temperature Temperatuur - + SG SG - + Battery Batterij - + Date Datum - + Temperature °C Temperatuur °C - + Battery volt Batterij volt - + Save Image Opslaan plaatje - + Image (*.png) Plaatje (*.png) - + Save File Bestand opslaan - + No image file selected. Geen plaatjes bestand gekozen. - + %1 Temperature %2°C %1 Temperatuur %2°C - + %1 Density %2 SG %1 Dichtheid %2 SG - + %1 Battery %2 volt Capacity %3% @@ -1257,12 +1257,12 @@ BMSapp - Detail iSpindel - + Online Online - + Offline Offline @@ -11559,49 +11559,49 @@ Terug - + Unit Eenheid - + Node Systeem - + Status Status - + Beer Bier - + Temperature Temperatuur - + SG SG - - + + Details Details - - + + Cal Cal - + Total items: %1 Totaal items: %1