|
1 /** |
|
2 * DetailiSpindel.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 "DetailiSpindel.h" |
|
18 #include "../ui/ui_DetailiSpindel.h" |
|
19 #include "global.h" |
|
20 #include "Utils.h" |
|
21 #include "MainWindow.h" |
|
22 |
|
23 |
|
24 /* |
|
25 * Results are available via MySQL and websockets. Because we initialize using |
|
26 * MySQL we only use that for the results and up to date status. |
|
27 * Commands are send via websockets only. |
|
28 */ |
|
29 |
|
30 DetailiSpindel::DetailiSpindel(int id, QWidget *parent) : QDialog(parent), ui(new Ui::DetailiSpindel) |
|
31 { |
|
32 QSqlQuery query; |
|
33 |
|
34 qDebug() << "DetailiSpindel record:" << id; |
|
35 ui->setupUi(this); |
|
36 this->recno = id; |
|
37 setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); |
|
38 setWindowTitle(tr("BMSapp - Details iSpindel")); |
|
39 |
|
40 ui->thermoMeter->setMaximum(40.0); |
|
41 ui->thermoMeter->setNominal(20.0); |
|
42 ui->thermoMeter->setCritical(25.0); |
|
43 ui->thermoMeter->setSuffix(QString("°C")); |
|
44 |
|
45 ui->modeEdit->addItem("OFF"); |
|
46 ui->modeEdit->addItem("ON"); |
|
47 |
|
48 ui->codePick->addItem("Free - Dummy"); // Will be replaced later |
|
49 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"); |
|
50 while (query.next()) { |
|
51 ui->codePick->addItem(query.value("code").toString()+" - "+query.value("name").toString()); |
|
52 } |
|
53 |
|
54 connect(ui->codePick, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetailiSpindel::code_changed); |
|
55 connect(ui->modeEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DetailiSpindel::mode_changed); |
|
56 connect(parent, SIGNAL(updateiSpindel(QString)), this, SLOT(refreshiSpindel(QString))); |
|
57 emit refreshTable(); |
|
58 } |
|
59 |
|
60 |
|
61 void DetailiSpindel::refreshTable() |
|
62 { |
|
63 QSqlQuery query; |
|
64 |
|
65 qDebug() << "refreshTable ispindel rec:" << this->recno; |
|
66 |
|
67 query.prepare("SELECT * FROM mon_ispindels WHERE record = :recno"); |
|
68 query.bindValue(":recno", this->recno); |
|
69 query.exec(); |
|
70 if (query.next()) { |
|
71 |
|
72 const QSignalBlocker blocker1(ui->codePick); |
|
73 const QSignalBlocker blocker2(ui->modeEdit); |
|
74 |
|
75 _node = query.value("node").toString(); |
|
76 _alias = query.value("alias").toString(); |
|
77 _uuid = query.value("uuid").toString(); |
|
78 _beercode = query.value("beercode").toString(); |
|
79 _beername = query.value("beername").toString(); |
|
80 |
|
81 ui->uuidEdit->setText(_uuid); |
|
82 ui->systemEdit->setText(_node+"/"+_alias); |
|
83 ui->codePick->setItemText(0, _alias.toUpper()+" - "+_alias); |
|
84 |
|
85 if (query.value("online").toInt()) { |
|
86 ui->statusEdit->setText(tr("Online")); |
|
87 ui->codeEdit->setText(_beercode+" - "+_beername); |
|
88 ui->modeEdit->show(); |
|
89 if (query.value("mode").toString() == "OFF") { |
|
90 ui->powerLED->setChecked(false); |
|
91 ui->modeEdit->setCurrentIndex(0); |
|
92 ui->codePick->show(); |
|
93 } else { |
|
94 ui->powerLED->setChecked(true); |
|
95 ui->modeEdit->setCurrentIndex(1); |
|
96 ui->codePick->hide(); |
|
97 } |
|
98 ui->alarmLED->setChecked((query.value("alarm").toInt() != 0) ? true:false); |
|
99 |
|
100 ui->voltEdit->setText(QString("%1").arg(query.value("battery").toDouble(), 4, 'f', 3, '0')); |
|
101 ui->tiltEdit->setText(QString("%1").arg(query.value("angle").toDouble(), 6, 'f', 5, '0')); |
|
102 ui->platoEdit->setText(QString("%1").arg(query.value("gravity").toDouble(), 4, 'f', 3, '0')); |
|
103 double sg = Utils::plato_to_sg(query.value("gravity").toDouble()); |
|
104 ui->sgEdit->setText(QString("%1").arg(sg, 5, 'f', 4, '0')); |
|
105 |
|
106 if (query.value("gravity").toDouble()) { |
|
107 double og = Utils::plato_to_sg(query.value("og_gravity").toDouble()); |
|
108 double svg = Utils::calc_svg(og, sg); |
|
109 |
|
110 ui->ogVal->setText(QString("%1").arg(og, 5, 'f', 4, '0')); |
|
111 ui->ogVal2->setText(QString("%1°P").arg(query.value("og_gravity").toDouble(), 4, 'f', 3, '0')); |
|
112 ui->sgVal->setText(QString("%1").arg(sg, 5, 'f', 4, '0')); |
|
113 ui->sgVal2->setText(QString("%1°P").arg(query.value("gravity").toDouble(), 4, 'f', 3, '0')); |
|
114 ui->svgVal->setText(QString("%1%").arg(svg, 2, 'f', 1, '0')); |
|
115 ui->svgBar->setValue(svg); |
|
116 ui->abvVal->setText(QString("%1%").arg(Utils::abvol(og, sg), 3, 'f', 2, '0')); |
|
117 } else { |
|
118 ui->ogVal->setText(""); |
|
119 ui->ogVal2->setText(""); |
|
120 ui->sgVal->setText(""); |
|
121 ui->sgVal2->setText(""); |
|
122 ui->svgVal->setText(""); |
|
123 ui->abvVal->setText(""); |
|
124 ui->batVal->setText(""); |
|
125 } |
|
126 |
|
127 double batt = query.value("battery").toDouble() - 3.064; // 0% voltage |
|
128 if (batt < 0) |
|
129 batt = 0; |
|
130 batt = round(batt / 1.17875 * 100.0); // 100% range |
|
131 if (batt > 100) |
|
132 batt = 100; |
|
133 ui->batVal->setText(QString("%1%").arg(batt, 2, 'f', 1, '0')); |
|
134 |
|
135 ui->thermoBox->show(); |
|
136 ui->thermoMeter->setValue(query.value("temperature").toDouble()); |
|
137 |
|
138 } else { |
|
139 /* Offline */ |
|
140 ui->statusEdit->setText(tr("Offline")); |
|
141 ui->powerLED->setChecked(false); |
|
142 ui->alarmLED->setChecked(true); |
|
143 ui->codePick->hide(); |
|
144 ui->modeEdit->hide(); |
|
145 ui->thermoBox->hide(); |
|
146 ui->logButton->hide(); |
|
147 } |
|
148 } |
|
149 |
|
150 } |
|
151 |
|
152 |
|
153 DetailiSpindel::~DetailiSpindel() |
|
154 { |
|
155 qDebug() << "DetailiSpindel done"; |
|
156 delete ui; |
|
157 emit entry_changed(); |
|
158 } |
|
159 |
|
160 |
|
161 /* |
|
162 * Receive signals destined for all co2meters. |
|
163 * Check if the signal is for us. |
|
164 */ |
|
165 void DetailiSpindel::refreshiSpindel(QString data) |
|
166 { |
|
167 if (_node+"/"+_alias == data) { |
|
168 emit refreshTable(); |
|
169 } |
|
170 } |
|
171 |
|
172 |
|
173 void DetailiSpindel::on_quitButton_clicked() |
|
174 { |
|
175 this->close(); |
|
176 this->setResult(1); |
|
177 } |
|
178 |
|
179 |
|
180 void DetailiSpindel::mode_changed(int val) |
|
181 { |
|
182 QStringList mode ({ "OFF", "ON" }); |
|
183 QString msg = QString("{\"device\":\"ispindels\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\",\"mode\":\"" + mode[val] + "\"}"); |
|
184 qDebug() << "mode_changed" << val << msg; |
|
185 webSocket->sendTextMessage(msg); |
|
186 } |
|
187 |
|
188 |
|
189 void DetailiSpindel::code_changed(int val) |
|
190 { |
|
191 QJsonParseError parseError; |
|
192 QSqlQuery query; |
|
193 |
|
194 QString msg = QString("{\"device\":\"ispindels\",\"node\":\"" + _node + "\",\"unit\":\"" + _alias + "\","); |
|
195 if (val == 0) { |
|
196 msg.append(QString("\"beeruuid\":\"") + _uuid + "\","); |
|
197 msg.append(QString("\"beercode\":\"") + _alias.toUpper() + "\","); |
|
198 msg.append(QString("\"beername\":\"") + _alias + "\"}"); |
|
199 } else { |
|
200 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"); |
|
201 for (int i = 0; i < val; i++) { |
|
202 query.next(); |
|
203 } |
|
204 msg.append(QString("\"beeruuid\":\"") + query.value("uuid").toString() + "\","); |
|
205 msg.append(QString("\"beercode\":\"") + query.value("code").toString() + "\","); |
|
206 msg.append(QString("\"beername\":\"") + query.value("name").toString() + "\"}"); |
|
207 } |
|
208 |
|
209 qDebug() << "code_changed" << val << msg; |
|
210 webSocket->sendTextMessage(msg); |
|
211 } |
|
212 |