* @brief Runs the JavaScript transmit(value) function and returns the result as a QByteArray. */
| 156 | * @brief Runs the JavaScript transmit(value) function and returns the result as a QByteArray. |
| 157 | */ |
| 158 | QByteArray Widgets::Output::Base::evaluateTransmitFunction(const QVariant& value) |
| 159 | { |
| 160 | if (!m_hasFn) |
| 161 | return {}; |
| 162 | |
| 163 | auto jsValue = m_jsEngine.toScriptValue(value); |
| 164 | QJSValueList args = QJSValueList{jsValue}; |
| 165 | auto result = m_watchdog.call(m_transmitFn, args); |
| 166 | |
| 167 | if (m_watchdog.lastCallTimedOut()) [[unlikely]] { |
| 168 | Q_EMIT transmitError(tr("Transmit script timed out after %1 ms").arg(kTransmitWatchdogMs)); |
| 169 | return {}; |
| 170 | } |
| 171 | |
| 172 | if (result.isError()) [[unlikely]] { |
| 173 | Q_EMIT transmitError(result.toString()); |
| 174 | return {}; |
| 175 | } |
| 176 | |
| 177 | QByteArray data; |
| 178 | if (result.isString()) |
| 179 | data = SerialStudio::encodeText(result.toString(), m_txEncoding); |
| 180 | else |
| 181 | data = result.toVariant().toByteArray(); |
| 182 | |
| 183 | if (data.size() > kMaxPayloadBytes) [[unlikely]] { |
| 184 | Q_EMIT transmitError(tr("Payload exceeds maximum size")); |
| 185 | return {}; |
| 186 | } |
| 187 | |
| 188 | return data; |
| 189 | } |
| 190 | |
| 191 | //-------------------------------------------------------------------------------------------------- |
| 192 | // Protocol helper injection |
nothing calls this directly
no test coverage detected