src/DetailFermenter.cpp

Thu, 30 Jun 2022 21:05:30 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 30 Jun 2022 21:05:30 +0200
changeset 323
d053ffbbf3e9
parent 318
ff02aca2b63c
child 324
c1bb6b197763
permissions
-rw-r--r--

Started with change and signal functions. Implemented mode change for test. The MainWindow webSocket is now global so the Detail screens can send websocket messages.

/**
 * DetailFermenter.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 "DetailFermenter.h"
#include "../ui/ui_DetailFermenter.h"
#include "global.h"
#include "MainWindow.h"


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

    qDebug() << "DetailFermenter record:" << id;
    ui->setupUi(this);
    this->recno = id;
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    setWindowTitle(tr("BMSapp - Details Fermenter"));

    ui->airThermo->setMaximum(45.0);
    ui->airThermo->setNominal(15.0);
    ui->airThermo->setCritical(20.0);
    ui->airThermo->setSuffix(QString("°C"));

    ui->beerThermo->setMaximum(45.0);
    ui->beerThermo->setNominal(15.0);
    ui->beerThermo->setCritical(20.0);
    ui->beerThermo->setSuffix(QString("°C"));

    ui->modeEdit->addItem("OFF");
    ui->modeEdit->addItem("NONE");
    ui->modeEdit->addItem("FRIDGE");
    ui->modeEdit->addItem("BEER");
    ui->modeEdit->addItem("PROFILE");

    ui->stageEdit->addItem("PRIMARY");
    ui->stageEdit->addItem("SECONDARY");
    ui->stageEdit->addItem("TERTIARY");
    ui->stageEdit->addItem("CARBONATION");

    ui->codePick->addItem("Free - Dummy");	// Will be replaced later
    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->loEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &DetailFermenter::lo_changed);
    connect(ui->hiEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &DetailFermenter::hi_changed);
    connect(ui->heatSwitch, SIGNAL(clicked()), this, SLOT(heat_switched()));
    connect(ui->coolSwitch, SIGNAL(clicked()), this, SLOT(cool_switched()));
    connect(ui->fanSwitch, SIGNAL(clicked()), this, SLOT(fan_switched()));
    connect(ui->modeEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetailFermenter::mode_changed);
    connect(parent, SIGNAL(updateFermenter(QString)), this, SLOT(refreshFermenter(QString)));
    emit refreshTable();
}


void DetailFermenter::refreshTable()
{
    QSqlQuery query;

    qDebug() << "refreshTable fermenter" << this->recno;

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

	ui->uuidEdit->setText(query.value("uuid").toString());
	ui->systemEdit->setText(_node+"/"+_alias);
	ui->codePick->setItemText(0, _alias.toUpper()+" - "+_alias);

	if (query.value("online").toInt()) {
	    ui->statusEdit->setText(tr("Online"));
            ui->codeEdit->setText(query.value("beercode").toString()+" - "+query.value("beername").toString());
	    ui->airThermo->setNominal(query.value("yeast_lo").toDouble());
	    ui->airThermo->setCritical(query.value("yeast_hi").toDouble());
	    ui->beerThermo->setNominal(query.value("yeast_lo").toDouble());
            ui->beerThermo->setCritical(query.value("yeast_hi").toDouble());

	    if (query.value("mode").toString() == "OFF")
	    	ui->modeEdit->setCurrentIndex(0);
            else if (query.value("mode").toString() == "NONE")
	    	ui->modeEdit->setCurrentIndex(1);
	    else if (query.value("mode").toString() == "FRIDGE")
            	ui->modeEdit->setCurrentIndex(2);
	    else if (query.value("mode").toString() == "BEER")
            	ui->modeEdit->setCurrentIndex(3);
	    else if (query.value("mode").toString() == "PROFILE")
            	ui->modeEdit->setCurrentIndex(4);

	    if (query.value("stage").toString() == "PRIMARY")
		ui->stageEdit->setCurrentIndex(0);
	    else if (query.value("stage").toString() == "SECONDARY")
                ui->stageEdit->setCurrentIndex(1);
	    else if (query.value("stage").toString() == "TERTIARY")
                ui->stageEdit->setCurrentIndex(2);
	    else if (query.value("stage").toString() == "CARBONATION")
                ui->stageEdit->setCurrentIndex(3);

	    if (query.value("door_address").toString() != "") {
		ui->doorLED->show();
                ui->doorLabel->show();
		ui->doorLED->setChecked((query.value("door_state").toInt() != 0) ? true:false);
	    } else {
		ui->doorLED->hide();
		ui->doorLabel->hide();
	    }

	    if (query.value("light_address").toString() != "") {
                ui->lightLED->show();
                ui->lightLabel->show();
                ui->lightLED->setChecked((query.value("light_state").toInt() != 0) ? true:false);
            } else {
                ui->lightLED->hide();
                ui->lightLabel->hide();
            }

	    if (query.value("mode").toString() == "OFF") {
	    	ui->powerLED->setChecked(false);
		ui->codePick->show();
	    } else {
	    	ui->powerLED->setChecked(true);
		ui->codePick->hide();
	    }
	    ui->alarmLED->setChecked((query.value("alarm").toInt() != 0) ? true:false);

	    ui->tempsetBox->show();
	    if ((query.value("mode").toString() == "FRIDGE") || (query.value("mode").toString() == "BEER")) {
		ui->loEdit->setReadOnly(false);
		ui->loEdit->setButtonSymbols(QAbstractSpinBox::UpDownArrows);
		ui->hiEdit->setReadOnly(false);
                ui->hiEdit->setButtonSymbols(QAbstractSpinBox::UpDownArrows);
	    } else {
		ui->loEdit->setReadOnly(true);
		ui->loEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
		ui->hiEdit->setReadOnly(true);
                ui->hiEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
	    }
	    ui->loEdit->setValue(query.value("setpoint_low").toDouble());
	    ui->hiEdit->setValue(query.value("setpoint_high").toDouble());

	    ui->switchBox->show();
	    ui->heatLED->setChecked((query.value("heater_state").toInt() != 0) ? true:false);
	    ui->coolLED->setChecked((query.value("cooler_state").toInt() != 0) ? true:false);
	    ui->fanLED->setChecked((query.value("fan_state").toInt() != 0) ? true:false);

	    if (query.value("mode").toString() == "NONE") {
		ui->heatSwitch->show();
		ui->coolSwitch->show();
		ui->fanSwitch->show();
	    } else {
		ui->heatSwitch->hide();
                ui->coolSwitch->hide();
                ui->fanSwitch->hide();
	    }

	    //webSocket->sendTextMessage(QString("dd"));

	    ui->thermoBox->show();
	    if (query.value("air_state").toString() == "OK") {
	    	ui->airThermo->setValue(query.value("air_temperature").toDouble());
	    }
	    if (query.value("beer_state").toString() == "OK") {
                ui->beerThermo->setValue(query.value("beer_temperature").toDouble());
            }

	} else {
	    /* Offline */
	    ui->statusEdit->setText(tr("Offline"));
	    ui->powerLED->setChecked(false);
	    ui->alarmLED->setChecked(true);
	    ui->codePick->hide();
	    ui->modeEdit->hide();
	    ui->stageEdit->hide();
	    ui->thermoBox->hide();
	    ui->tempsetBox->hide();
	    ui->switchBox->hide();
	}
    }

}


DetailFermenter::~DetailFermenter()
{
    qDebug() << "DetailFermenter done";
    delete ui;
    emit entry_changed();
}


/*
 * Receive signals destined for all fermenters.
 * Check if the signal is for us.
 */
void DetailFermenter::refreshFermenter(QString data)
{
    if (_node+"/"+_alias == data) {
	qDebug() << "for us";
	emit refreshTable();
    }
}


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


void DetailFermenter::lo_changed(double val)
{
    qDebug() << "lo_changed" << val;
}


void DetailFermenter::hi_changed(double val)
{
    qDebug() << "hi_changed" << val;
}


void DetailFermenter::heat_switched()
{
    qDebug() << "heat_switched" << heat_state;
}


void DetailFermenter::cool_switched()
{
    qDebug() << "cool_switched" << cool_state;
}


void DetailFermenter::fan_switched()
{
    qDebug() << "fan_switched" << fan_state;
}


void DetailFermenter::mode_changed(int val)
{
    qDebug() << "mode_changed" << val;
    QStringList mode ({ "OFF", "NONE", "FRIDGE", "BEER", "PROFiLE" });
    QString msg = QString("{\"device\":\"fermenters\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\",\"mode\":\"" + mode[val] + "\"}");
    qDebug() << msg;
    webSocket->sendTextMessage(msg);
}

mercurial