src/DetailNode.cpp

Sat, 11 Feb 2023 15:48:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 11 Feb 2023 15:48:02 +0100
changeset 493
520306773450
parent 348
c5318497a0b6
child 495
6aa29aaa3f4d
permissions
-rw-r--r--

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.

/**
 * DetailNode.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 "DetailNode.h"
#include "ChartCarbonate.h"
#include "../ui/ui_DetailNode.h"
#include "global.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.
 */

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

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

    connect(ui->rebootButton, SIGNAL(clicked()), this, SLOT(control_reboot()));
    connect(ui->rebirthButton, SIGNAL(clicked()), this, SLOT(control_rebirth()));
    connect(parent, SIGNAL(updateNode(QString)), this, SLOT(refreshNode(QString)));
    emit refreshTable();
}


void DetailNode::refreshTable()
{
    QSqlQuery query;

    qDebug() << "refreshTable node rec:" << this->recno;

    query.prepare("SELECT * FROM mon_nodes WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.next()) {

	_node = query.value("node").toString();
	_group_id = query.value("group_id").toString();
	_uuid = query.value("uuid").toString();

	ui->uuidEdit->setText(_uuid);
	ui->systemEdit->setText(_node);
	ui->typeEdit->setText(_group_id);
	ui->firstEdit->setText(query.value("firstseen").toDateTime().toString("dd MMM yyyy HH:mm:ss"));
	ui->lastEdit->setText(query.value("lastseen").toDateTime().toString("dd MMM yyyy HH:mm:ss"));

	if (query.value("online").toInt()) {
	    ui->statusEdit->setText(tr("Online"));
	    ui->makerEdit->show();
	    ui->modelEdit->show();
	    ui->osEdit->show();
	    ui->fwEdit->show();
	    ui->makerLabel->show();
            ui->modelLabel->show();
            ui->osLabel->show();
            ui->fwLabel->show();
	    ui->intervalLabel->show();
            ui->intervalEdit->show();
	    ui->makerEdit->setText(query.value("hardwaremake").toString());
	    ui->modelEdit->setText(query.value("hardwaremodel").toString());
	    ui->osEdit->setText(query.value("os").toString()+QString(tr(" version: "))+query.value("os_version").toString());
	    ui->fwEdit->setText(query.value("firmware").toString());
	    ui->intervalEdit->setValue(query.value("up_interval").toInt());

	    if (query.value("temperature").toDouble() > 0) {
		ui->tempLabel->show();
                ui->tempEdit->show();
		ui->tempEdit->setValue(query.value("temperature").toDouble());
	    } else {
		ui->tempLabel->hide();
                ui->tempEdit->hide();
	    }
	    if (query.value("humidity").toDouble() > 0) {
		ui->humLabel->show();
                ui->humEdit->show();
                ui->humEdit->setValue(query.value("humidity").toDouble());
	    } else {
		ui->humLabel->hide();
            	ui->humEdit->hide();
	    }
	    if (query.value("barometer").toDouble() > 0) {
		ui->baroLabel->show();
                ui->baroEdit->show();
                ui->baroEdit->setValue(query.value("barometer").toDouble());
	    } else {
		ui->baroLabel->hide();
            	ui->baroEdit->hide();
	    }

	    ui->networkLabel->show();
	    ui->networkEdit->show();
	    ui->networkEdit->setText(query.value("net_ifname").toString()+" "+query.value("net_address").toString());

	    if (query.value("net_ssid").toString() != "") {
		ui->ssidLabel->show();
		ui->ssidEdit->show();
	    	ui->ssidEdit->setText(query.value("net_ssid").toString());
		ui->rssiLabel->show();
		ui->rssiEdit->show();
	    	if (query.value("net_rssi").toInt() < 0)
		    ui->rssiEdit->setValue(query.value("net_rssi").toInt());
	    } else {
		ui->ssidLabel->hide();
		ui->ssidEdit->hide();
		ui->rssiLabel->hide();
		ui->rssiEdit->hide();
	    }
	    if (_group_id == "fermenters") {
		/*
		 * Currently only fermenters support control commands.
		 */
		ui->rebootButton->show();
            	ui->rebirthButton->show();
	    } else {
		ui->rebootButton->hide();
                ui->rebirthButton->hide();
	    }

	} else {
	    /* Offline */
	    ui->statusEdit->setText(tr("Offline"));

	    ui->makerEdit->hide();
            ui->modelEdit->hide();
            ui->osEdit->hide();
            ui->fwEdit->hide();
            ui->makerLabel->hide();
            ui->modelLabel->hide();
            ui->osLabel->hide();
            ui->fwLabel->hide();
	    ui->intervalLabel->hide();
	    ui->intervalEdit->hide();

	    ui->tempLabel->hide();
	    ui->tempEdit->hide();
	    ui->humLabel->hide();
	    ui->humEdit->hide();
	    ui->baroLabel->hide();
	    ui->baroEdit->hide();

	    ui->networkLabel->hide();
            ui->networkEdit->hide();
	    ui->ssidLabel->hide();
	    ui->ssidEdit->hide();
	    ui->rssiLabel->hide();
            ui->rssiEdit->hide();

	    ui->rebootButton->hide();
	    ui->rebirthButton->hide();
	}
    }

}


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


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


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


void DetailNode::control_reboot()
{
    int rc = QMessageBox::warning(this, tr("Reboot application"), tr("Remote application is running, really reboot?"),
                                QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

    if (rc == QMessageBox::No)
	return;

    QString msg = QString("{\"node\":\""+_node+"\",\"group_id\":\""+_group_id+"\",\"control\":\"reboot\"}");
    qDebug() << msg;
    webSocket->sendTextMessage(msg);
}


void DetailNode::control_rebirth()
{
    QString msg = QString("{\"node\":\""+_node+"\",\"group_id\":\""+_group_id+"\",\"control\":\"rebirth\"}");
    qDebug() << msg;
    webSocket->sendTextMessage(msg);
}

mercurial