Move divecomputer configuration code to different files

This splits the code in configuredivecomputer.cpp into multiple files.
The read and write threads are moved to configuredivecomputerthreads.h/cpp,
and the device details class is moved to devicedetails.h/.cpp

Signed-off-by: Joseph W. Joshua <joejoshw@gmail.com>
Signed-off-by: Thiago Macieira <thiago@macieira.org>
This commit is contained in:
Joseph W. Joshua 2014-06-10 15:03:26 +03:00 committed by Thiago Macieira
parent 2432350064
commit 4fc16b1674
10 changed files with 403 additions and 225 deletions

View file

@ -4,19 +4,21 @@
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
QObject(parent),
readThread(0),
writeThread(0)
writeThread(0),
m_deviceDetails(0)
{
setState(INITIAL);
}
void ConfigureDiveComputer::readSettings(device_data_t *data)
void ConfigureDiveComputer::readSettings(DeviceDetails *deviceDetails, device_data_t *data)
{
setState(READING);
m_deviceDetails = deviceDetails;
if (readThread)
readThread->deleteLater();
readThread = new ReadSettingsThread(this, data);
readThread = new ReadSettingsThread(this, deviceDetails, data);
connect (readThread, SIGNAL(finished()),
this, SLOT(readThreadFinished()), Qt::QueuedConnection);
connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
@ -24,19 +26,9 @@ void ConfigureDiveComputer::readSettings(device_data_t *data)
readThread->start();
}
void ConfigureDiveComputer::setDeviceName(device_data_t *data, QString newName)
void ConfigureDiveComputer::saveDeviceDetails()
{
writeSettingToDevice(data, "Name", newName);
}
void ConfigureDiveComputer::setDeviceDateAndTime(device_data_t *data, QDateTime dateAndTime)
{
writeSettingToDevice(data, "DateAndTime", dateAndTime);
}
void ConfigureDiveComputer::setDeviceBrightness(device_data_t *data, int brighnessLevel)
{
writeSettingToDevice(data, "Brightness", brighnessLevel);
}
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)
@ -45,20 +37,6 @@ void ConfigureDiveComputer::setState(ConfigureDiveComputer::states 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;
@ -68,7 +46,7 @@ void ConfigureDiveComputer::setError(QString err)
void ConfigureDiveComputer::readThreadFinished()
{
setState(DONE);
emit deviceSettings(readThread->result);
emit readFinished();
}
void ConfigureDiveComputer::writeThreadFinished()
@ -79,108 +57,3 @@ void ConfigureDiveComputer::writeThreadFinished()
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 (m_settingName == "Name") {
switch (dc_device_get_type(data->device)) {
case DC_FAMILY_HW_OSTC3:
supported = true;
result = hw_ostc3_device_customtext(data->device, m_settingValue.toByteArray().data());
break;
case DC_FAMILY_HW_FROG:
supported = true;
result = hw_frog_device_customtext(data->device, m_settingValue.toByteArray().data());
break;
}
}
if (m_settingName == "DateAndTime") {
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();
switch (dc_device_get_type(data->device)) {
case DC_FAMILY_HW_OSTC3:
supported = true;
result = hw_ostc3_device_clock(data->device, &time);
break;
case DC_FAMILY_HW_OSTC:
supported = true;
result = hw_ostc_device_clock(data->device, &time);
break;
case DC_FAMILY_HW_FROG:
supported = true;
result = hw_frog_device_clock(data->device, &time);
break;
}
}
if (m_settingName == "Brightness") {
switch (dc_device_get_type(data->device)) {
case DC_FAMILY_HW_OSTC3:
qDebug() << "Brightness";
supported = true;
unsigned char packet[1] = { m_settingValue.toInt() };
result = hw_ostc3_device_config_write(data->device, 0x2D, packet, sizeof (packet));
break;
}
}
qDebug() << result;
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);
}
}