* @brief Compiles and runs the transmit function with the user's input value. */
| 144 | * @brief Compiles and runs the transmit function with the user's input value. |
| 145 | */ |
| 146 | void DataModel::TransmitTestDialog::evaluate() |
| 147 | { |
| 148 | const auto input = m_userInput->text(); |
| 149 | if (input.isEmpty()) |
| 150 | return; |
| 151 | |
| 152 | if (m_hexCheckBox->isChecked() && !validateHexInput(input)) { |
| 153 | QMessageBox::warning(this, |
| 154 | tr("Invalid Hex Input"), |
| 155 | tr("Please enter valid hexadecimal bytes.\n\nValid format: 01 A2 FF 3C")); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (m_transmitCode.trimmed().isEmpty()) { |
| 160 | displayOutput({}, tr("No transmit function code to evaluate.")); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | DataModel::FrameBuilder::instance().refreshTableStoreFromProjectModel(); |
| 165 | |
| 166 | QJSEngine engine; |
| 167 | #ifdef BUILD_COMMERCIAL |
| 168 | Widgets::Output::Base::installProtocolHelpers(engine); |
| 169 | #endif |
| 170 | DataModel::FrameBuilder::instance().injectTableApiJS(&engine); |
| 171 | |
| 172 | const auto wrapped = |
| 173 | QStringLiteral("(function() { %1; return transmit; })()").arg(m_transmitCode); |
| 174 | auto transmitFn = engine.evaluate(wrapped); |
| 175 | if (!transmitFn.isCallable()) { |
| 176 | const auto msg = |
| 177 | transmitFn.isError() ? transmitFn.toString() : tr("transmit function is not callable"); |
| 178 | displayOutput({}, msg); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | QJSValue jsValue; |
| 183 | if (m_hexCheckBox->isChecked()) { |
| 184 | const auto bytes = SerialStudio::hexToBytes(input); |
| 185 | jsValue = engine.toScriptValue(QString::fromLatin1(bytes)); |
| 186 | } else { |
| 187 | bool ok; |
| 188 | double num = SerialStudio::toDouble(input, &ok); |
| 189 | if (ok) |
| 190 | jsValue = engine.toScriptValue(num); |
| 191 | else |
| 192 | jsValue = engine.toScriptValue(input); |
| 193 | } |
| 194 | |
| 195 | auto result = transmitFn.call(QJSValueList{jsValue}); |
| 196 | if (result.isError()) { |
| 197 | displayOutput({}, result.toString()); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | QByteArray payload; |
| 202 | if (result.isString()) |
| 203 | payload = result.toString().toLatin1(); |
nothing calls this directly
no test coverage detected