src/DetailCO2meter.cpp

Tue, 27 Feb 2024 17:32:00 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 27 Feb 2024 17:32:00 +0100
changeset 518
6922856f4288
parent 492
c3a781b4d35b
permissions
-rw-r--r--

No carbonation beer selection before brewing the beer.

/**
 * DetailCO2meter.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 "DetailCO2meter.h"
#include "ChartCarbonate.h"
#include "../ui/ui_DetailCO2meter.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.
 */

DetailCO2meter::DetailCO2meter(int id, QWidget *parent) : QDialog(parent), ui(new Ui::DetailCO2meter)
{
    QSqlQuery query;

#ifdef DEBUG_MONITOR
    qDebug() << "DetailCO2meter record:" << id;
#endif

    ui->setupUi(this);
    this->recno = id;
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    setWindowTitle(tr("BMSapp - Details Carbonation"));

    ui->thermoMeter->setMaximum(40.0);
    ui->thermoMeter->setNominal(20.0);
    ui->thermoMeter->setCritical(25.0);
    ui->thermoMeter->setSuffix(QString("°C"));

    ui->barMeter->setMaximum(6.0);
    ui->barMeter->setNominal(1.0);
    ui->barMeter->setCritical(3.0);
    ui->barMeter->setSuffix(QString(" bar"));

    ui->codePick->addItem("Erase beer");
    query.exec("SELECT code,name FROM products WHERE 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, &DetailCO2meter::code_changed);
    connect(parent, SIGNAL(updateCO2meter(QString)), this, SLOT(refreshCO2meter(QString)));
    emit refreshTable();
}


void DetailCO2meter::refreshTable()
{
    QSqlQuery query, query2;
    QString sql = "";

    query.prepare("SELECT * FROM mon_co2meters 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();

	sql = "SELECT "
		"st_carb_min,st_carb_max,secondary_temp,primary_end_temp,bottle_carbonation_temp,"
		"bottle_carbonation"
		" FROM products WHERE code=:code AND name=:name";
	query2.prepare(sql);
        query2.bindValue(":code", _beercode);
	query2.bindValue(":name", _beername);
	query2.exec();
	if (query2.next()) {
	    /* Get highest fermentation temperature. */
	    double TSec = query2.value("secondary_temp").toDouble();
	    if (TSec < 1)
		TSec = query2.value("primary_end_temp").toDouble();
	    if (TSec < 1)
		TSec = 18;

	    double carbtemp = query2.value("bottle_carbonation_temp").toDouble();
	    double barmin  = Utils::GetPressureBar(Utils::CarbCO2toS(query2.value("st_carb_min").toDouble(), TSec, 1), carbtemp);
	    double barmax  = Utils::GetPressureBar(Utils::CarbCO2toS(query2.value("st_carb_max").toDouble(), TSec, 1), carbtemp);
	    double barthis = Utils::GetPressureBar(Utils::CarbCO2toS(query2.value("bottle_carbonation").toDouble(), TSec, 1), carbtemp);
	    ui->minVol->setValue(query2.value("st_carb_min").toDouble());
	    ui->maxVol->setValue(query2.value("st_carb_max").toDouble());
	    ui->thisVol->setValue(query2.value("bottle_carbonation").toDouble());
	    ui->minBar->setValue(barmin);
	    ui->maxBar->setValue(barmax);
	    ui->thisBar->setValue(barthis);
	    ui->barMeter->setNominal(barmin);
    	    ui->barMeter->setCritical(barmax);
	}

	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->modeEdit->setText(query.value("mode").toString());
	    ui->modeEdit->show();
	    ui->powerLED->setChecked(mode);
	    if (mode)
		ui->codePick->hide();
	    else
		ui->codePick->show();

	    if (query.value("temperature_address").toString().length() == 16) {
	    	ui->thermoBox->show();
		ui->logButton->show();
		if (! alarm && query.value("temperature_state").toString() == "OK") {
	    	    	ui->thermoMeter->setValue(query.value("temperature").toDouble());
	    	} else {
			ui->thermoMeter->setValue(NAN);
		}
	    	if (! alarm && query.value("pressure_state").toString() == "OK") {
                    	ui->barMeter->setValue(query.value("pressure_bar").toDouble());
            	} else {
			ui->barMeter->setValue(NAN);
		}
	    } else {
		ui->thermoBox->hide();
		ui->logButton->hide();
	    }

	} 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();
	}
    }

}


DetailCO2meter::~DetailCO2meter()
{
    delete ui;
    emit entry_changed();
}


/*
 * Receive signals destined for all co2meters.
 * Check if the signal is for us.
 */
void DetailCO2meter::refreshCO2meter(QString data)
{
    if (_node+"/"+_alias == data) {
	emit refreshTable();
    }
}


void DetailCO2meter::on_logButton_clicked()
{
    ChartCarbonate dialog(_beercode, _beername, this);
}


void DetailCO2meter::on_quitButton_clicked()
{
    this->close();
    this->setResult(1);
}


void DetailCO2meter::code_changed(int val)
{
    QJsonParseError parseError;
    QSqlQuery query;

    QString msg = QString("{\"device\":\"co2meters\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\",");
    if (val == 0) {
	msg.append(QString("\"beeruuid\":\"") + _uuid + "\",");
	msg.append(QString("\"beercode\":\"") + _alias.toUpper() + "\",");
	msg.append(QString("\"beername\":\"") + _alias + "\"}");
    } 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();
        }
	msg.append(QString("\"beeruuid\":\"") + query.value("uuid").toString() + "\",");
	msg.append(QString("\"beercode\":\"") + query.value("code").toString() + "\",");
	msg.append(QString("\"beername\":\"") + query.value("name").toString() + "\"}");
    }

#ifdef DEBUG_MONITOR
    qDebug() << "code_changed" << val << msg;
#endif
    webSocket->sendTextMessage(msg);
}

mercurial