Wed, 18 Oct 2023 16:00:06 +0200
Added application icon
387 | 1 | /** |
2 | * ImportBrew.cpp is part of bmsapp. | |
3 | * | |
4 | * bmsapp is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * bmsapp is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | #include "ImportBrew.h" | |
18 | #include "../ui/ui_ImportBrew.h" | |
19 | #include "global.h" | |
20 | #include "Utils.h" | |
21 | #include "database/db_product.h" | |
22 | #include "MainWindow.h" | |
23 | ||
24 | ||
25 | ImportBrew::ImportBrew(QWidget *parent) : QDialog(parent), ui(new Ui::ImportBrew) | |
26 | { | |
27 | qDebug() << "ImportBrew start"; | |
28 | ui->setupUi(this); | |
29 | WindowTitle(); | |
30 | ui->progressBar->setValue(0); | |
31 | connect(ui->quitButton, SIGNAL(clicked()), parent, SLOT(fromImportBrew())); | |
32 | } | |
33 | ||
34 | ||
35 | ImportBrew::~ImportBrew() | |
36 | { | |
37 | qDebug() << "ImportBrew done"; | |
38 | delete ui; | |
39 | } | |
40 | ||
41 | ||
42 | void ImportBrew::WindowTitle() | |
43 | { | |
44 | QString txt = QString(tr("BMSapp - Import Brewlog")); | |
45 | setWindowTitle(txt); | |
46 | } | |
47 | ||
48 | ||
49 | void ImportBrew::on_openButton_clicked() | |
50 | { | |
51 | QSqlQuery query; | |
52 | QString log, sql, code, name, uuid; | |
53 | int total = 0, errors = 0, updates = 0; | |
54 | ||
55 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath() + "/*.json", tr("Files (*.json)")); | |
56 | if (fileName == 0) { | |
57 | QMessageBox::warning(this, tr("Open File"), tr("No Brewlog JSON file selected.")); | |
58 | return; | |
59 | } | |
60 | ui->fileEdit->setText(fileName); | |
61 | ||
62 | QFile file(fileName); | |
63 | qint64 fsize = file.size(); | |
64 | ||
65 | log = "Import Brewlog file `" + fileName + "`\n\n"; | |
66 | qInfo() << "Import Brewlog" << fileName << "length" << fsize << "bytes"; | |
67 | ||
68 | file.open(QIODevice::ReadOnly); | |
69 | QByteArray val = file.readAll(); | |
70 | file.close(); | |
71 | ||
72 | QJsonParseError parseError; | |
73 | QJsonDocument brewlog = QJsonDocument::fromJson(val, &parseError); | |
74 | if (parseError.error != QJsonParseError::NoError) { | |
75 | qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; | |
76 | } else { | |
77 | QJsonObject root = brewlog.object(); | |
78 | QJsonValue value = root.value(QString("brew")); | |
79 | ||
80 | /* | |
81 | * The logfile is a array with just 1 item. | |
82 | */ | |
83 | QJsonArray array = value.toArray(); | |
84 | foreach (const QJsonValue & v, array) { | |
85 | /* | |
86 | * 4 items in a object is a complete brewlog. | |
87 | */ | |
88 | QString Recipe = v.toObject().value("Recipe").toString(); | |
89 | QDateTime Date = QDateTime::fromString(v.toObject().value("Date").toString()); | |
90 | QJsonArray data = v.toObject().value("brewdata").toArray(); | |
91 | QJsonArray events = v.toObject().value("annotations").toArray(); | |
92 | qDebug() << Recipe << Date << data.size() << events.size(); | |
93 | code = Recipe.split(" ").at(0); | |
94 | name = Recipe.remove(0, code.size() + 1); | |
95 | log.append(QString(tr("Brew: ") + code + " " + name + "\n")); | |
96 | ||
97 | sql = "SELECT uuid FROM products WHERE code='" + code + "'"; | |
98 | query.exec(sql); | |
99 | if (query.first()) { | |
100 | uuid = query.value(0).toString(); | |
101 | qDebug() << uuid; | |
102 | } else { | |
103 | uuid = ""; | |
104 | errors++; | |
105 | qWarning() << "ImportBrew" << code << "product not found"; | |
106 | continue; | |
107 | } | |
108 | ||
109 | sql = "DELETE FROM log_brews WHERE code='" + code + "'"; | |
110 | query.exec(sql); | |
111 | if (query.numRowsAffected() > 0) { | |
112 | log.append(QString(tr("Deleted %1 old records\n")).arg(query.numRowsAffected())); | |
113 | qDebug() << "ImportBrew deleted" << query.numRowsAffected(); | |
114 | } | |
115 | ||
116 | for (int j = 0; j < data.size(); j++) { | |
117 | ||
118 | QJsonObject obj = data.at(j).toObject(); | |
119 | ||
120 | /* | |
121 | * It's fucking crazy that despite all Googling this cannot be | |
122 | * done with an internal function. All examples are not working | |
123 | * for this very very simple problem, add the time string converted | |
124 | * to QTime to a QDateTime. | |
125 | * | |
126 | * Solution that always works, split the string, and do the math. | |
127 | */ | |
128 | QStringList Times = obj["Label"].toString().split(":"); | |
129 | int seconds = Times[0].toInt() * 3600 + Times[1].toInt() * 60; | |
130 | QDateTime logDate = Date.addSecs(seconds); | |
131 | QString dt = logDate.toString("yyyy-MM-dd HH:mm:ss"); | |
132 | ||
133 | sql = "INSERT INTO log_brews SET version='2', datetime='" + dt + "'"; | |
134 | sql.append(", uuid='" + uuid + "'"); | |
135 | sql.append(", code='" + code + "'"); | |
136 | sql.append(", name='" + name + "'"); | |
137 | sql.append(", pv_mlt='" + obj["MLT_pv"].toString() + "'"); | |
138 | sql.append(", sp_mlt='" + obj["MLT_sp"].toString() + "'"); | |
139 | sql.append(", pwm_mlt='" + obj["MLT_pwm"].toString() + "'"); | |
140 | if (obj["HLT_pv"].toString().size()) { | |
141 | sql.append(", pv_hlt='" + obj["HLT_pv"].toString() + "'"); | |
142 | sql.append(", sp_hlt='" + obj["HLT_sp"].toString() + "'"); | |
143 | sql.append(", pwm_hlt='" + obj["HLT_pwm"].toString() + "'"); | |
144 | } | |
145 | //qDebug() << sql; | |
146 | ||
147 | query.exec(sql); | |
148 | if (query.lastError().isValid()) { | |
149 | qWarning() << "ImportBrew" << query.lastError(); | |
150 | errors++; | |
151 | } | |
152 | total++; | |
153 | ||
154 | /* | |
155 | * The first 90% is for the normal data | |
156 | */ | |
157 | ui->progressBar->setValue((total * 90) / data.size()); | |
158 | } | |
159 | log.append(QString(tr("Inserted %1 new records\n")).arg(total - errors)); | |
160 | ||
161 | /* | |
162 | * Update events, they are annotations in the json file. | |
163 | */ | |
164 | for (int j = 0; j < events.size(); j++) { | |
165 | ||
166 | QJsonObject obj = events.at(j).toObject(); | |
167 | ||
168 | QStringList Times = obj["value"].toString().split(":"); | |
169 | int seconds = Times[0].toInt() * 3600 + Times[1].toInt() * 60; | |
170 | QDateTime logDate = Date.addSecs(seconds); | |
171 | QString dt = logDate.toString("yyyy-MM-dd HH:mm:ss"); | |
172 | QJsonObject label = obj["label"].toObject(); | |
173 | QString evt = label["content"].toString(); | |
174 | ||
175 | sql = "UPDATE log_brews SET event='" + evt + "' WHERE datetime='" + dt + "' AND uuid='" + uuid + "'"; | |
176 | //qDebug() << sql; | |
177 | query.exec(sql); | |
178 | if (query.lastError().isValid()) { | |
179 | qWarning() << "ImportBrew" << query.lastError(); | |
180 | errors++; | |
181 | } else { | |
182 | updates++; | |
183 | } | |
184 | ||
185 | /* | |
186 | * The last 10% is for the updates. | |
187 | */ | |
188 | ui->progressBar->setValue(((updates * 10) / events.size()) + 90); | |
189 | } | |
190 | log.append(QString(tr("Updated %1 records with events\n")).arg(updates)); | |
191 | ||
192 | if (errors == 0) { | |
193 | sql = "UPDATE products SET log_brew=1 WHERE code='" + code + "'"; | |
194 | query.exec(sql); | |
195 | if (query.numRowsAffected()) | |
196 | log.append(QString(tr("Marked brew graph available in product\n"))); | |
197 | } | |
198 | } | |
199 | ||
200 | } | |
201 | ||
202 | ui->logEdit->setPlainText(log); | |
203 | qInfo() << "Import" << total << "records, " << total - errors << "ok, " << errors << "errors"; | |
204 | ||
205 | ui->progressBar->setValue(100); | |
206 | } | |
207 | ||
208 |