src/ImportBrew.cpp

Thu, 18 Aug 2022 20:34:15 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 18 Aug 2022 20:34:15 +0200
changeset 401
583148eb6e01
parent 387
7945bf3be1f9
permissions
-rw-r--r--

Init est_carb field for new products.

/**
 * ImportBrew.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 "ImportBrew.h"
#include "../ui/ui_ImportBrew.h"
#include "global.h"
#include "Utils.h"
#include "database/db_product.h"
#include "MainWindow.h"


ImportBrew::ImportBrew(QWidget *parent) : QDialog(parent), ui(new Ui::ImportBrew)
{
    qDebug() << "ImportBrew start";
    ui->setupUi(this);
    WindowTitle();
    ui->progressBar->setValue(0);
    connect(ui->quitButton, SIGNAL(clicked()), parent, SLOT(fromImportBrew()));
}


ImportBrew::~ImportBrew()
{
    qDebug() << "ImportBrew done";
    delete ui;
}


void ImportBrew::WindowTitle()
{
    QString txt = QString(tr("BMSapp - Import Brewlog"));
    setWindowTitle(txt);
}


void ImportBrew::on_openButton_clicked()
{
    QSqlQuery query;
    QString   log, sql, code, name, uuid;
    int total = 0, errors = 0, updates = 0;

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath() + "/*.json", tr("Files (*.json)"));
    if (fileName == 0) {
        QMessageBox::warning(this, tr("Open File"), tr("No Brewlog JSON file selected."));
        return;
    }
    ui->fileEdit->setText(fileName);

    QFile file(fileName);
    qint64 fsize = file.size();

    log = "Import Brewlog file `" + fileName + "`\n\n"; 
    qInfo() << "Import Brewlog" << fileName << "length" << fsize << "bytes";

    file.open(QIODevice::ReadOnly);
    QByteArray val = file.readAll();
    file.close();

    QJsonParseError parseError;
    QJsonDocument brewlog = QJsonDocument::fromJson(val, &parseError);
    if (parseError.error != QJsonParseError::NoError) {
	qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
    } else {
	QJsonObject root = brewlog.object();
	QJsonValue value = root.value(QString("brew"));

	/*
	 * The logfile is a array with just 1 item.
	 */
	QJsonArray array = value.toArray();
	foreach (const QJsonValue & v, array) {
	    /*
	     * 4 items in a object is a complete brewlog.
	     */
	    QString Recipe = v.toObject().value("Recipe").toString();
	    QDateTime Date = QDateTime::fromString(v.toObject().value("Date").toString());
	    QJsonArray data = v.toObject().value("brewdata").toArray();
	    QJsonArray events = v.toObject().value("annotations").toArray();
	    qDebug() << Recipe << Date << data.size() << events.size();
	    code = Recipe.split(" ").at(0);
	    name = Recipe.remove(0, code.size() + 1);
	    log.append(QString(tr("Brew: ") + code + " " + name + "\n"));

	    sql = "SELECT uuid FROM products WHERE code='" + code + "'";
	    query.exec(sql);
	    if (query.first()) {
		uuid = query.value(0).toString();
		qDebug() << uuid;
	    } else {
		uuid = "";
		errors++;
		qWarning() << "ImportBrew" << code << "product not found";
		continue;
	    }

	    sql = "DELETE FROM log_brews WHERE code='" + code + "'";
	    query.exec(sql);
	    if (query.numRowsAffected() > 0) {
		log.append(QString(tr("Deleted %1 old records\n")).arg(query.numRowsAffected()));
	    	qDebug() << "ImportBrew deleted" << query.numRowsAffected();
	    }

	    for (int j = 0; j < data.size(); j++) {
		
		QJsonObject obj = data.at(j).toObject();

		/*
		 * It's fucking crazy that despite all Googling this cannot be
		 * done with an internal function. All examples are not working
		 * for this very very simple problem, add the time string converted
		 * to QTime to a QDateTime.
		 *
		 * Solution that always works, split the string, and do the math.
		 */
		QStringList Times = obj["Label"].toString().split(":");
		int seconds = Times[0].toInt() * 3600 + Times[1].toInt() * 60;
		QDateTime logDate = Date.addSecs(seconds);
		QString dt = logDate.toString("yyyy-MM-dd HH:mm:ss");

		sql = "INSERT INTO log_brews SET version='2', datetime='" + dt + "'";
		sql.append(", uuid='" + uuid + "'");
		sql.append(", code='" + code + "'");
		sql.append(", name='" + name + "'");
		sql.append(", pv_mlt='" + obj["MLT_pv"].toString() + "'");
		sql.append(", sp_mlt='" + obj["MLT_sp"].toString() + "'");
		sql.append(", pwm_mlt='" + obj["MLT_pwm"].toString() + "'");
		if (obj["HLT_pv"].toString().size()) {
		    sql.append(", pv_hlt='" + obj["HLT_pv"].toString() + "'");
		    sql.append(", sp_hlt='" + obj["HLT_sp"].toString() + "'");
		    sql.append(", pwm_hlt='" + obj["HLT_pwm"].toString() + "'");
		}
		//qDebug() << sql;

		query.exec(sql);
		if (query.lastError().isValid()) {
        	    qWarning() << "ImportBrew" << query.lastError();
		    errors++;
		}
		total++;

		/*
		 * The first 90% is for the normal data
		 */
		ui->progressBar->setValue((total * 90) / data.size());
	    }
	    log.append(QString(tr("Inserted %1 new records\n")).arg(total - errors));

	    /*
	     * Update events, they are annotations in the json file.
	     */
	    for (int j = 0; j < events.size(); j++) {

                QJsonObject obj = events.at(j).toObject();

		QStringList Times = obj["value"].toString().split(":");
                int seconds = Times[0].toInt() * 3600 + Times[1].toInt() * 60;
                QDateTime logDate = Date.addSecs(seconds);
                QString dt = logDate.toString("yyyy-MM-dd HH:mm:ss");
		QJsonObject label = obj["label"].toObject();
		QString evt = label["content"].toString();

		sql = "UPDATE log_brews SET event='" + evt + "' WHERE datetime='" + dt + "' AND uuid='" + uuid + "'";
		//qDebug() << sql;
		query.exec(sql);
                if (query.lastError().isValid()) {
                    qWarning() << "ImportBrew" << query.lastError();
                    errors++;
                } else {
		    updates++;
		}

		/*
		 * The last 10% is for the updates.
		 */
		ui->progressBar->setValue(((updates * 10) / events.size()) + 90);
	    }
	    log.append(QString(tr("Updated %1 records with events\n")).arg(updates));

	    if (errors == 0) {
		sql = "UPDATE products SET log_brew=1 WHERE code='" + code + "'";
		query.exec(sql);
		if (query.numRowsAffected())
		    log.append(QString(tr("Marked brew graph available in product\n")));
	    }
	}

    }

    ui->logEdit->setPlainText(log);
    qInfo() << "Import" << total << "records, " << total - errors << "ok, " << errors << "errors";

    ui->progressBar->setValue(100);
}

mercurial