Skip to content

Commit

Permalink
Simplify CommandMessage
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Bakker <jellebakker@jb-audio.com>
  • Loading branch information
xioTechnologies and JelleBakker authored Dec 20, 2024
1 parent 314f98a commit def5f99
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
21 changes: 13 additions & 8 deletions x-IMU3-GUI/Source/CommandMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class CommandMessage
{
public:
std::string json;
juce::String key;
juce::var value;
std::optional<juce::String> error;
std::string key;
std::string value;
std::optional<std::string> error;

CommandMessage(const juce::String& key_, const juce::var& value_)
: json("{" + key_.quoted().toStdString() + ":" + juce::JSON::toString(value_).toStdString() + "}"),
key(key_),
value(value_)
: json("{\"" + key_.toStdString() + "\":" + juce::JSON::toString(value_).toStdString() + "}"),
key(key_.toStdString()),
value(juce::JSON::toString(value_).toStdString())
{
}

Expand All @@ -24,7 +24,7 @@ class CommandMessage
const auto commandMessage = ximu3::XIMU3_command_message_parse(json.c_str());
json = commandMessage.json;
key = commandMessage.key;
value = juce::JSON::fromString(commandMessage.value);
value = commandMessage.value;
if (std::strlen(commandMessage.error) > 0)
{
error = commandMessage.error;
Expand All @@ -33,14 +33,19 @@ class CommandMessage

bool operator==(const CommandMessage& other) const
{
return key.isNotEmpty() && key == other.key; // empty key in response indicates no response
return key.empty() == false && key == other.key; // empty key in response indicates no response
}

operator const std::string&() const
{
return json;
}

juce::var getValue() const
{
return juce::JSON::fromString(value);
}

static juce::String normaliseKey(const juce::String& key)
{
return std::regex_replace(key.toLowerCase().toStdString(), std::regex("[^0-9a-z]"), "");
Expand Down
4 changes: 2 additions & 2 deletions x-IMU3-GUI/Source/ConnectionPanel/ConnectionPanelHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ void ConnectionPanelHeader::updateTitle(const std::vector<CommandMessage>& respo

if (CommandMessage::normaliseKey(response.key) == CommandMessage::normaliseKey("device_name"))
{
updateTitle(response.value, serialNumber);
updateTitle(response.getValue(), serialNumber);
}
else if (CommandMessage::normaliseKey(response.key) == CommandMessage::normaliseKey("serial_number"))
{
updateTitle(deviceName, response.value);
updateTitle(deviceName, response.getValue());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions x-IMU3-GUI/Source/DeviceSettings/DeviceSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ void DeviceSettings::setValue(const CommandMessage& response)
}

juce::ScopedValueSetter _(ignoreCallback, true);
if (setting[DeviceSettingsIDs::value] != response.value)
if (setting[DeviceSettingsIDs::value] != response.getValue())
{
setting.setProperty(DeviceSettingsIDs::status, (int) Setting::Status::modified, nullptr);
setting.setProperty(DeviceSettingsIDs::statusTooltip, "Modified but Not Written to Device", nullptr);
}
setting.setProperty(DeviceSettingsIDs::value, response.value, nullptr);
setting.setProperty(DeviceSettingsIDs::value, response.getValue(), nullptr);
setting.sendPropertyChangeMessage(DeviceSettingsIDs::value);
}

Expand Down
6 changes: 3 additions & 3 deletions x-IMU3-GUI/Source/Dialogs/SendCommandDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SendCommandDialog::SendCommandDialog(const juce::String& title, const std::optio
stringValue.setVisible(type == Type::string);
numberValue.setVisible(type == Type::number);

setOkButton((keyValue.isEmpty() == false) && (juce::JSON::parse(commandValue.getText()).isVoid() == false));
setOkButton((keyValue.isEmpty() == false) && (CommandMessage(commandValue.getText().toStdString()).json.empty() == false));
};

selectCommand(recentCommands.getChild(0));
Expand Down Expand Up @@ -128,11 +128,11 @@ juce::String SendCommandDialog::toString(const Type type)

juce::String SendCommandDialog::createCommand(const juce::String& key, const Type type, const juce::String& string, const juce::String& number)
{
juce::String text = "{" + key.quoted() + ":";
juce::String text = "{\"" + key + "\":";
switch (type)
{
case Type::string:
text += string.quoted();
text += "\"" + string + "\"";
break;
case Type::number:
text += number;
Expand Down
2 changes: 1 addition & 1 deletion x-IMU3-GUI/Source/Dialogs/SendingCommandDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ SendingCommandDialog::SendingCommandDialog(const CommandMessage& command, const
else
{
row_->state = Row::State::complete;
row_->response = responses[0].value.isVoid() ? "" : juce::JSON::toString(responses[0].value, true);
row_->response = responses[0].value;
}

table.updateContent();
Expand Down
2 changes: 1 addition & 1 deletion x-IMU3-GUI/Source/Dialogs/UpdatingFirmwareDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ UpdatingFirmwareDialog::UpdatingFirmwareDialog(std::unique_ptr<ximu3::Connection
showError("Unable to read hardware version.");
return;
}
const auto hardwareVersion = CommandMessage(responses[0]).value.toString();
const auto hardwareVersion = CommandMessage(responses[0]).getValue().toString();

// Check compatibility
const auto firmwareIsV2 = hexFile.getFileName().startsWith("x-IMU3-Firmware-v2.");
Expand Down
4 changes: 2 additions & 2 deletions x-IMU3-GUI/Source/Windows/DeviceSettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ DeviceSettingsWindow::DeviceSettingsWindow(const juce::ValueTree& windowLayout_,

deviceSettings.setValue(*response);

if (CommandMessage::normaliseKey(command.key) == CommandMessage::normaliseKey("firmware_version") && response->value != Firmware::version)
if (CommandMessage::normaliseKey(command.key) == CommandMessage::normaliseKey("firmware_version") && response->getValue() != Firmware::version)
{
deviceSettings.setStatus(command.key, Setting::Status::warning, "Unexpected Firmware Version");
continue;
Expand Down Expand Up @@ -92,7 +92,7 @@ DeviceSettingsWindow::DeviceSettingsWindow(const juce::ValueTree& windowLayout_,
juce::DynamicObject object;
for (const auto& command : deviceSettings.getWriteCommands(false))
{
object.setProperty(command.key, command.value);
object.setProperty(juce::String(command.key), command.getValue());
}

juce::FileOutputStream stream(fileChooser->getResult());
Expand Down

0 comments on commit def5f99

Please sign in to comment.