summaryrefslogtreecommitdiffstats
path: root/qt-ui/configuredivecomputer.cpp
blob: ae71ea627432ab51ae3d87b3e63d864f3319d804 (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
#include "configuredivecomputer.h"
#include "libdivecomputer/hw.h"
#include <QDebug>
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)));

	readThread->start();
}

void ConfigureDiveComputer::setDeviceName(device_data_t *data, QString newName)
{
	writeSettingToDevice(data, "Name", newName);
}

void ConfigureDiveComputer::setDeviceDateAndTime(device_data_t *data, QDateTime dateAndTime)
{
	writeSettingToDevice(data, "DateAndTime", dateAndTime);
}

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

void ConfigureDiveComputer::writeSettingToDevice(device_data_t *data, QString settingName, QVariant settingValue)
{
	setState(READING);

	if (writeThread)
		writeThread->deleteLater();

	writeThread = new WriteSettingsThread(this, data, settingName, settingValue);
	connect (writeThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
	connect (writeThread, SIGNAL(finished()), this, SLOT(writeThreadFinished()));

	writeThread->start();
}

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

void ConfigureDiveComputer::readThreadFinished()
{
	setState(DONE);
	emit deviceSettings(readThread->result);
}

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

ReadSettingsThread::ReadSettingsThread(QObject *parent, device_data_t *data)
	: QThread(parent), data(data)
{

}

void ReadSettingsThread::run()
{
	dc_status_t rc;
	rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname);
	if (rc == DC_STATUS_SUCCESS) {
		if (dc_device_get_type(data->device) == DC_FAMILY_HW_OSTC3) {
			unsigned char hw_data[10];
			hw_ostc3_device_version(data->device, hw_data, 10);
			QTextStream (&result) << "Device Version: " << hw_data; //just a test. I will work on decoding this
		} else {
			lastError = tr("This feature is not yet available for the selected dive computer.");
			emit error(lastError);
		}
		dc_device_close(data->device);
	} else {
		lastError = tr("Could not a establish connection to the dive computer.");
		emit error(lastError);
	}
}

WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data, QString settingName, QVariant settingValue)
	: QThread(parent), data(data), m_settingName(settingName), m_settingValue(settingValue)
{

}

void WriteSettingsThread::run()
{
	bool supported = false;
	dc_status_t rc;
	rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname);
	if (rc == DC_STATUS_SUCCESS) {
		dc_status_t result;
		if (dc_device_get_type(data->device) == DC_FAMILY_HW_OSTC3) {
			if (m_settingName == "Name") {
				supported = true;
				result = hw_ostc3_device_customtext(data->device, m_settingValue.toByteArray().data());
			}
		} else if ( dc_device_get_type(data->device) == DC_FAMILY_HW_FROG ) {
			if (m_settingName == "Name") {
				supported = true;
				result = hw_frog_device_customtext(data->device, m_settingValue.toByteArray().data());
			}
		}
		if ( dc_device_get_type(data->device) == DC_FAMILY_HW_OSTC3 && m_settingName == "DateTime" ) {
			supported = true;
			QDateTime timeToSet = m_settingValue.toDateTime();
			dc_datetime_t time;
			time.year = timeToSet.date().year();
			time.month = timeToSet.date().month();
			time.day = timeToSet.date().day();
			time.hour = timeToSet.time().hour();
			time.minute = timeToSet.time().minute();
			time.second = timeToSet.time().second();
			result = hw_ostc_device_clock(data->device, &time); //Toto fix error here
		}
		if (result !=  DC_STATUS_SUCCESS) {
			qDebug() << result;
			lastError = tr("An error occurred while sending data to the dive computer.");
			//Todo Update this message to change depending on actual result.

			emit error(lastError);
		}
		dc_device_close(data->device);
	} else {
		lastError = tr("Could not a establish connection to the dive computer.");
		emit error(lastError);
	}
	if (!supported) {
		lastError = tr("This feature is not yet available for the selected dive computer.");
		emit error(lastError);
	}
}