Skip to content

Commit

Permalink
Done a Bunch of stuff for multiple device support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola02nb committed Dec 12, 2024
1 parent 119c709 commit 141fe31
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 356 deletions.
110 changes: 58 additions & 52 deletions src/DataTypes/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ void Device::updateDevice(const Device *new_device)

bool Device::updateDevice(const QList<Device *> &new_device_list)
{
for (int i = 0; i < new_device_list.length(); ++i) {
if (this != new_device_list.at(i)) {
this->updateDevice(new_device_list.at(i));
foreach (Device *new_device, new_device_list) {
if (*this == new_device) {
this->updateDevice(new_device);
return true;
}
}
Expand Down Expand Up @@ -145,69 +145,63 @@ QJsonObject Device::toJson() const
return json;
}

Device Device::fromJson(const QJsonObject &json)
Device *Device::fromJson(
const QJsonObject &json)
{
Device device;
device.device = json["device"].toString();
device.vendor = json["vendor"].toString();
device.product = json["product"].toString();
device.id_vendor = json["id_vendor"].toString();
device.id_product = json["id_product"].toString();

device.lights = json["lights"].toInt();
device.sidetone = json["sidetone"].toInt();
device.voice_prompts = json["voice_prompts"].toInt();
device.inactive_time = json["inactive_time"].toInt();
device.equalizer_preset = json["equalizer_preset"].toInt();
Device *device = new Device();
device->device = json["device"].toString();
device->vendor = json["vendor"].toString();
device->product = json["product"].toString();
device->id_vendor = json["id_vendor"].toString();
device->id_product = json["id_product"].toString();

device->lights = json["lights"].toInt();
device->sidetone = json["sidetone"].toInt();
device->voice_prompts = json["voice_prompts"].toInt();
device->inactive_time = json["inactive_time"].toInt();
device->equalizer_preset = json["equalizer_preset"].toInt();

QJsonArray curveArray = json["equalizer_curve"].toArray();
for (const auto &value : curveArray) {
device.equalizer_curve.append(value.toDouble());
device->equalizer_curve.append(value.toDouble());
}

device.volume_limiter = json["volume_limiter"].toInt();
device.rotate_to_mute = json["rotate_to_mute"].toInt();
device.mic_mute_led_brightness = json["mic_mute_led_brightness"].toInt();
device.mic_volume = json["mic_volume"].toInt();
device.bt_when_powered_on = json["bt_when_powered_on"].toInt();
device.bt_call_volume = json["bt_call_volume"].toInt();
device->volume_limiter = json["volume_limiter"].toInt();
device->rotate_to_mute = json["rotate_to_mute"].toInt();
device->mic_mute_led_brightness = json["mic_mute_led_brightness"].toInt();
device->mic_volume = json["mic_volume"].toInt();
device->bt_when_powered_on = json["bt_when_powered_on"].toInt();
device->bt_call_volume = json["bt_call_volume"].toInt();

return device;
}

void updateDeviceFromSource(QList<Device *> &devicesToUpdate, const Device *sourceDevice)
{
bool found = false;
for (Device *toUpdateDevice : devicesToUpdate) {
if (*toUpdateDevice == sourceDevice) {
// Update the saved device with connected device's information
*toUpdateDevice = *sourceDevice;
found = true;
}
};
if (!found) {
Device *toAppend = new Device();
*toAppend = *sourceDevice;
devicesToUpdate.append(toAppend);
}
}

void updateDevicesFromSource(QList<Device *> &devicesToUpdate, const QList<Device *> &sourceDevices)
{
for (Device *sourceDevice : sourceDevices) {
bool deviceFound = false;
for (Device *toUpdateDevice : devicesToUpdate) {
if (toUpdateDevice->id_vendor == sourceDevice->id_vendor
&& toUpdateDevice->id_product == sourceDevice->id_product) {
for (Device *toUpdateDevice : devicesToUpdate) {
for (Device *sourceDevice : sourceDevices) {
if (*toUpdateDevice == sourceDevice) {
// Update the connected device with saved device's information
toUpdateDevice->lights = sourceDevice->lights;
toUpdateDevice->sidetone = sourceDevice->sidetone;
toUpdateDevice->voice_prompts = sourceDevice->voice_prompts;
toUpdateDevice->inactive_time = sourceDevice->inactive_time;

toUpdateDevice->equalizer_preset = sourceDevice->equalizer_preset;
toUpdateDevice->equalizer_curve = sourceDevice->equalizer_curve;
toUpdateDevice->volume_limiter = sourceDevice->volume_limiter;

toUpdateDevice->rotate_to_mute = sourceDevice->rotate_to_mute;
toUpdateDevice->mic_mute_led_brightness = sourceDevice->mic_mute_led_brightness;
toUpdateDevice->mic_volume = sourceDevice->mic_volume;

toUpdateDevice->bt_when_powered_on = sourceDevice->bt_when_powered_on;
toUpdateDevice->bt_call_volume = sourceDevice->bt_call_volume;

deviceFound = true;
break;
*toUpdateDevice = *sourceDevice;
}
}

if (!deviceFound) {
// If the device wasn't found in saved devices, add it
devicesToUpdate.append(sourceDevice);
}
};
}

Expand All @@ -224,6 +218,7 @@ void serializeDevices(const QList<Device *> &devices, const QString &filePath)
file.write(doc.toJson());
file.close();
qDebug() << "Devices Serialized" << jsonArray;
qDebug();
}
}

Expand All @@ -237,11 +232,22 @@ QList<Device *> deserializeDevices(const QString &filePath)
QJsonArray jsonArray = doc.array();

for (const auto &value : jsonArray) {
Device *device = new Device(Device::fromJson(value.toObject()));
Device *device = Device::fromJson(value.toObject());
devices.append(device);
}

file.close();
qDebug() << "Devices Deserialized" << jsonArray;
qDebug();
}
return devices;
}

void deleteDevices(
QList<Device *> deviceList)
{
for (Device *device : deviceList) {
delete device;
}
deviceList.clear();
}
8 changes: 7 additions & 1 deletion src/DataTypes/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Device
Device();
Device(const QJsonObject &jsonObj, QString jsonData);

// Index
int index = -1;

// Status
QString status;

Expand Down Expand Up @@ -80,12 +83,15 @@ class Device
bool updateDevice(const QList<Device *> &new_device_list);

QJsonObject toJson() const;
static Device fromJson(const QJsonObject &json);
static Device *fromJson(const QJsonObject &json);
};

void updateDeviceFromSource(QList<Device *> &devicesToUpdate, const Device *sourceDevice);
void updateDevicesFromSource(QList<Device *> &devicesToUpdate, const QList<Device *> &sourceDevices);

void serializeDevices(const QList<Device *> &devices, const QString &filePath);
QList<Device *> deserializeDevices(const QString &filePath);

void deleteDevices(QList<Device *> deviceList);

#endif // DEVICE_H
2 changes: 2 additions & 0 deletions src/DataTypes/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Settings loadSettingsFromFile(const QString &filePath)
s.styleName = json["styleName"].toString();
}
qDebug() << "Settings Loaded:\t" << json;
qDebug();
}

return s;
Expand All @@ -67,4 +68,5 @@ void saveSettingstoFile(const Settings &settings, const QString &filePath)
file.write(doc.toJson());
file.close();
qDebug() << "Settings Saved:\t" << json;
qDebug();
}
Loading

0 comments on commit 141fe31

Please sign in to comment.