summaryrefslogtreecommitdiffstats
path: root/configuredivecomputer.cpp
blob: addc2a728e4cfc2515eaee5697d3064effbff7e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "configuredivecomputer.h"
#include "libdivecomputer/hw.h"
#include <QDebug>
#include <QFile>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxslt/transform.h>
#include <QStringList>
#include <QXmlStreamWriter>

ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
	QObject(parent),
	readThread(0),
	writeThread(0)
{
	setState(INITIAL);
}

void ConfigureDiveComputer::readSettings(device_data_t *data)
{
	setState(READING);

	if (readThread)
		readThread->deleteLater();

	readThread = new ReadSettingsThread(this, data);
	connect(readThread, SIGNAL(finished()),
		this, SLOT(readThreadFinished()), Qt::QueuedConnection);
	connect(readThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
	connect(readThread, SIGNAL(devicedetails(DeviceDetails*)), this,
		SIGNAL(deviceDetailsChanged(DeviceDetails*)));

	readThread->start();
}

void ConfigureDiveComputer::saveDeviceDetails(DeviceDetails *details, device_data_t *data)
{
	setState(WRITING);

	if (writeThread)
		writeThread->deleteLater();

	writeThread = new WriteSettingsThread(this, data);
	connect(writeThread, SIGNAL(finished()),
		this, SLOT(writeThreadFinished()), Qt::QueuedConnection);
	connect(writeThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));

	writeThread->setDeviceDetails(details);
	writeThread->start();
}

bool ConfigureDiveComputer::saveXMLBackup(QString fileName, DeviceDetails *details, device_data_t *data)
{
	QString xml = "";
	QString vendor = data->vendor;
	QString product = data->product;
	QXmlStreamWriter writer(&xml);
	writer.setAutoFormatting(true);

	writer.writeStartDocument();
	writer.writeStartElement("DiveComputerSettingsBackup");
	writer.writeStartElement("DiveComputer");
	writer.writeTextElement("Vendor", vendor);
	writer.writeTextElement("Product", product);
	writer.writeEndElement();
	writer.writeStartElement("Settings");
	writer.writeTextElement("CustomText", details->customText());
	//Add gasses
	QString gas1 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->gas1().oxygen),
			     QString::number(details->gas1().helium),
			     QString::number(details->gas1().type),
			     QString::number(details->gas1().depth)
			     );
	QString gas2 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->gas2().oxygen),
			     QString::number(details->gas2().helium),
			     QString::number(details->gas2().type),
			     QString::number(details->gas2().depth)
			     );
	QString gas3 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->gas3().oxygen),
			     QString::number(details->gas3().helium),
			     QString::number(details->gas3().type),
			     QString::number(details->gas3().depth)
			     );
	QString gas4 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->gas4().oxygen),
			     QString::number(details->gas4().helium),
			     QString::number(details->gas4().type),
			     QString::number(details->gas4().depth)
			     );
	QString gas5 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->gas5().oxygen),
			     QString::number(details->gas5().helium),
			     QString::number(details->gas5().type),
			     QString::number(details->gas5().depth)
			     );
	writer.writeTextElement("Gas1", gas1);
	writer.writeTextElement("Gas2", gas2);
	writer.writeTextElement("Gas3", gas3);
	writer.writeTextElement("Gas4", gas4);
	writer.writeTextElement("Gas5", gas5);
	//
	//Add dil values
	QString dil1 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->dil1().oxygen),
			     QString::number(details->dil1().helium),
			     QString::number(details->dil1().type),
			     QString::number(details->dil1().depth)
			     );
	QString dil2 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->dil2().oxygen),
			     QString::number(details->dil2().helium),
			     QString::number(details->dil2().type),
			     QString::number(details->dil2().depth)
			     );
	QString dil3 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->dil3().oxygen),
			     QString::number(details->dil3().helium),
			     QString::number(details->dil3().type),
			     QString::number(details->dil3().depth)
			     );
	QString dil4 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->dil4().oxygen),
			     QString::number(details->dil4().helium),
			     QString::number(details->dil4().type),
			     QString::number(details->dil4().depth)
			     );
	QString dil5 = QString("%1,%2,%3,%4")
			.arg(QString::number(details->dil5().oxygen),
			     QString::number(details->dil5().helium),
			     QString::number(details->dil5().type),
			     QString::number(details->dil5().depth)
			     );
	writer.writeTextElement("Dil1", dil1);
	writer.writeTextElement("Dil2", dil2);
	writer.writeTextElement("Dil3", dil3);
	writer.writeTextElement("Dil4", dil4);
	writer.writeTextElement("Dil5", dil5);
	//
	//Add set point values
	QString sp1 = QString("%1,%2")
			.arg(QString::number(details->sp1().sp),
			     QString::number(details->sp1().depth)
			     );
	QString sp2 = QString("%1,%2")
			.arg(QString::number(details->sp2().sp),
			     QString::number(details->sp2().depth)
			     );
	QString sp3 = QString("%1,%2")
			.arg(QString::number(details->sp3().sp),
			     QString::number(details->sp3().depth)
			     );
	QString sp4 = QString("%1,%2")
			.arg(QString::number(details->sp4().sp),
			     QString::number(details->sp4().depth)
			     );
	QString sp5 = QString("%1,%2")
			.arg(QString::number(details->sp5().sp),
			     QString::number(details->sp5().depth)
			     );
	writer.writeTextElement("SetPoint1", sp1);
	writer.writeTextElement("SetPoint2", sp2);
	writer.writeTextElement("SetPoint3", sp3);
	writer.writeTextElement("SetPoint4", sp4);
	writer.writeTextElement("SetPoint5", sp5);

	//Other Settings
	writer.writeTextElement("DiveMode", QString::number(details->diveMode()));
	writer.writeTextElement("Saturation", QString::number(details->saturation()));
	writer.writeTextElement("Desaturation", QString::number(details->desaturation()));
	writer.writeTextElement("LastDeco", QString::number(details->lastDeco()));
	writer.writeTextElement("Brightness", QString::number(details->brightness()));
	writer.writeTextElement("Units", QString::number(details->units()));
	writer.writeTextElement("SamplingRate", QString::number(details->samplingRate()));
	writer.writeTextElement("Salinity", QString::number(details->salinity()));
	writer.writeTextElement("DiveModeColor", QString::number(details->diveModeColor()));
	writer.writeTextElement("Language", QString::number(details->language()));
	writer.writeTextElement("DateFormat", QString::number(details->dateFormat()));
	writer.writeTextElement("CompassGain", QString::number(details->compassGain()));

	// Suunto vyper settings.
	writer.writeTextElement("Altitude", QString::number(details->altitude()));
	writer.writeTextElement("PersonalSafety", QString::number(details->personalSafety()));
	writer.writeTextElement("TimeFormat", QString::number(details->timeFormat()));

	writer.writeStartElement("Light");
	writer.writeAttribute("enabled", QString::number(details->lightEnabled()));
	writer.writeCharacters(QString::number(details->light()));
	writer.writeEndElement();

	writer.writeStartElement("AlarmTime");
	writer.writeAttribute("enabled", QString::number(details->alarmTimeEnabled()));
	writer.writeCharacters(QString::number(details->alarmTime()));
	writer.writeEndElement();

	writer.writeStartElement("AlarmDepth");
	writer.writeAttribute("enabled", QString::number(details->alarmDepthEnabled()));
	writer.writeCharacters(QString::number(details->alarmDepth()));
	writer.writeEndElement();

	writer.writeEndElement();
	writer.writeEndElement();

	writer.writeEndDocument();
	QFile file(fileName);
	if (!file.open(QIODevice::WriteOnly)) {
		lastError = tr("Could not save the backup file %1. Error Message: %2")
				.arg(fileName, file.errorString());
		return false;
	}
	//file open successful. write data and save.
	QTextStream out(&file);
	out << xml;

	file.close();
	return true;
}

bool ConfigureDiveComputer::restoreXMLBackup(QString fileName, DeviceDetails *details)
{
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly)) {
		lastError = tr("Could not open backup file: %1").arg(file.errorString());
		return false;
	}

	QString xml = file.readAll();

	QXmlStreamReader reader(xml);
	while (!reader.atEnd()) {
		if (reader.isStartElement()) {
			QString settingName = reader.name().toString();
			QXmlStreamAttributes attributes = reader.attributes();
			reader.readNext();
			QString keyString = reader.text().toString();

			if (settingName == "CustomText")
				details->setCustomText(keyString);

			if (settingName == "Gas1") {
				QStringList gasData = keyString.split(",");
				gas gas1;
				gas1.oxygen = gasData.at(0).toInt();
				gas1.helium = gasData.at(1).toInt();
				gas1.type = gasData.at(2).toInt();
				gas1.depth = gasData.at(3).toInt();
				details->setGas1(gas1);
			}

			if (settingName == "Gas2") {
				QStringList gasData = keyString.split(",");
				gas gas2;
				gas2.oxygen = gasData.at(0).toInt();
				gas2.helium = gasData.at(1).toInt();
				gas2.type = gasData.at(2).toInt();
				gas2.depth = gasData.at(3).toInt();
				details->setGas1(gas2);
			}

			if (settingName == "Gas3") {
				QStringList gasData = keyString.split(",");
				gas gas3;
				gas3.oxygen = gasData.at(0).toInt();
				gas3.helium = gasData.at(1).toInt();
				gas3.type = gasData.at(2).toInt();
				gas3.depth = gasData.at(3).toInt();
				details->setGas3(gas3);
			}

			if (settingName == "Gas4") {
				QStringList gasData = keyString.split(",");
				gas gas4;
				gas4.oxygen = gasData.at(0).toInt();
				gas4.helium = gasData.at(1).toInt();
				gas4.type = gasData.at(2).toInt();
				gas4.depth = gasData.at(3).toInt();
				details->setGas4(gas4);
			}

			if (settingName == "Gas5") {
				QStringList gasData = keyString.split(",");
				gas gas5;
				gas5.oxygen = gasData.at(0).toInt();
				gas5.helium = gasData.at(1).toInt();
				gas5.type = gasData.at(2).toInt();
				gas5.depth = gasData.at(3).toInt();
				details->setGas5(gas5);
			}

			if (settingName == "Dil1") {
				QStringList dilData = keyString.split(",");
				gas dil1;
				dil1.oxygen = dilData.at(0).toInt();
				dil1.helium = dilData.at(1).toInt();
				dil1.type = dilData.at(2).toInt();
				dil1.depth = dilData.at(3).toInt();
				details->setDil1(dil1);
			}

			if (settingName == "Dil2") {
				QStringList dilData = keyString.split(",");
				gas dil2;
				dil2.oxygen = dilData.at(0).toInt();
				dil2.helium = dilData.at(1).toInt();
				dil2.type = dilData.at(2).toInt();
				dil2.depth = dilData.at(3).toInt();
				details->setDil1(dil2);
			}

			if (settingName == "Dil3") {
				QStringList dilData = keyString.split(",");
				gas dil3;
				dil3.oxygen = dilData.at(0).toInt();
				dil3.helium = dilData.at(1).toInt();
				dil3.type = dilData.at(2).toInt();
				dil3.depth = dilData.at(3).toInt();
				details->setDil3(dil3);
			}

			if (settingName == "Dil4") {
				QStringList dilData = keyString.split(",");
				gas dil4;
				dil4.oxygen = dilData.at(0).toInt();
				dil4.helium = dilData.at(1).toInt();
				dil4.type = dilData.at(2).toInt();
				dil4.depth = dilData.at(3).toInt();
				details->setDil4(dil4);
			}

			if (settingName == "Dil5") {
				QStringList dilData = keyString.split(",");
				gas dil5;
				dil5.oxygen = dilData.at(0).toInt();
				dil5.helium = dilData.at(1).toInt();
				dil5.type = dilData.at(2).toInt();
				dil5.depth = dilData.at(3).toInt();
				details->setDil5(dil5);
			}

			if (settingName == "SetPoint1") {
				QStringList spData = keyString.split(",");
				setpoint sp1;
				sp1.sp = spData.at(0).toInt();
				sp1.depth = spData.at(1).toInt();
				details->setSp1(sp1);
			}

			if (settingName == "SetPoint2") {
				QStringList spData = keyString.split(",");
				setpoint sp2;
				sp2.sp = spData.at(0).toInt();
				sp2.depth = spData.at(1).toInt();
				details->setSp2(sp2);
			}

			if (settingName == "SetPoint3") {
				QStringList spData = keyString.split(",");
				setpoint sp3;
				sp3.sp = spData.at(0).toInt();
				sp3.depth = spData.at(1).toInt();
				details->setSp3(sp3);
			}

			if (settingName == "SetPoint4") {
				QStringList spData = keyString.split(",");
				setpoint sp4;
				sp4.sp = spData.at(0).toInt();
				sp4.depth = spData.at(1).toInt();
				details->setSp4(sp4);
			}

			if (settingName == "SetPoint5") {
				QStringList spData = keyString.split(",");
				setpoint sp5;
				sp5.sp = spData.at(0).toInt();
				sp5.depth = spData.at(1).toInt();
				details->setSp5(sp5);
			}

			if (settingName == "Saturation")
				details->setSaturation(keyString.toInt());

			if (settingName == "Desaturation")
				details->setDesaturation(keyString.toInt());

			if (settingName == "DiveMode")
				details->setDiveMode(keyString.toInt());

			if (settingName == "LastDeco")
				details->setLastDeco(keyString.toInt());

			if (settingName == "Brightness")
				details->setBrightness(keyString.toInt());

			if (settingName == "Units")
				details->setUnits(keyString.toInt());

			if (settingName == "SamplingRate")
				details->setSamplingRate(keyString.toInt());

			if (settingName == "Salinity")
				details->setSalinity(keyString.toInt());

			if (settingName == "DiveModeColour")
				details->setDiveModeColor(keyString.toInt());

			if (settingName == "Language")
				details->setLanguage(keyString.toInt());

			if (settingName == "DateFormat")
				details->setDateFormat(keyString.toInt());

			if (settingName == "CompassGain")
				details->setCompassGain(keyString.toInt());

			if (settingName == "Altitude")
				details->setAltitude(keyString.toInt());

			if (settingName == "PersonalSafety")
				details->setPersonalSafety(keyString.toInt());

			if (settingName == "TimeFormat")
				details->setTimeFormat(keyString.toInt());

			if (settingName == "Light") {
				if (attributes.hasAttribute("enabled"))
					details->setLightEnabled(attributes.value("enabled").toString().toInt());
				details->setLight(keyString.toInt());
			}

			if (settingName == "AlarmDepth") {
				if (attributes.hasAttribute("enabled"))
					details->setAlarmDepthEnabled(attributes.value("enabled").toString().toInt());
				details->setAlarmDepth(keyString.toInt());
			}

			if (settingName == "AlarmTime") {
				if (attributes.hasAttribute("enabled"))
					details->setAlarmTimeEnabled(attributes.value("enabled").toString().toInt());
				details->setAlarmTime(keyString.toInt());
			}
		}
		reader.readNext();
	}

	return true;
}

void ConfigureDiveComputer::startFirmwareUpdate(QString fileName, device_data_t *data)
{

}

void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
{
	currentState = newState;
	emit stateChanged(currentState);
}

void ConfigureDiveComputer::setError(QString err)
{
	lastError = err;
	emit error(err);
}

void ConfigureDiveComputer::readThreadFinished()
{
	setState(DONE);
	emit readFinished();
}

void ConfigureDiveComputer::writeThreadFinished()
{
	setState(DONE);
	if (writeThread->lastError.isEmpty()) {
		//No error
		emit message(tr("Setting successfully written to device"));
	}
}