|
| 1 | +#include "app/core/script_runner.h" |
| 2 | + |
| 3 | +#include "app/core/app_i18n.h" |
| 4 | + |
| 5 | +#include <QtCore/QElapsedTimer> |
| 6 | +#include <QtCore/QMutexLocker> |
| 7 | +#include <QtCore/QThread> |
| 8 | +#include <QtQml/QJSEngine> |
| 9 | +#include <QtQml/QJSValue> |
| 10 | + |
| 11 | +#include <utility> |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +QString normalizedLineEnding(QString value) |
| 16 | +{ |
| 17 | + value = value.trimmed().toLower(); |
| 18 | + if (value == QStringLiteral("cr") || value == QStringLiteral("lf") || value == QStringLiteral("crlf")) { |
| 19 | + return value; |
| 20 | + } |
| 21 | + return QStringLiteral("none"); |
| 22 | +} |
| 23 | + |
| 24 | +} // namespace |
| 25 | + |
| 26 | +ScriptBridge::ScriptBridge(QObject *parent) : QObject(parent) {} |
| 27 | + |
| 28 | +void ScriptBridge::setSendTextCallback(SendCallback callback) { m_sendTextCallback = std::move(callback); } |
| 29 | + |
| 30 | +void ScriptBridge::setSendHexCallback(SendCallback callback) { m_sendHexCallback = std::move(callback); } |
| 31 | + |
| 32 | +void ScriptBridge::setReceivedTextCallback(StringCallback callback) { m_receivedTextCallback = std::move(callback); } |
| 33 | + |
| 34 | +void ScriptBridge::setReceivedHexCallback(StringCallback callback) { m_receivedHexCallback = std::move(callback); } |
| 35 | + |
| 36 | +void ScriptBridge::setLastRxTextCallback(StringCallback callback) { m_lastRxTextCallback = std::move(callback); } |
| 37 | + |
| 38 | +void ScriptBridge::setLastRxHexCallback(StringCallback callback) { m_lastRxHexCallback = std::move(callback); } |
| 39 | + |
| 40 | +void ScriptBridge::setRecordsCallback(RecordsCallback callback) { m_recordsCallback = std::move(callback); } |
| 41 | + |
| 42 | +void ScriptBridge::setStopCallback(StopCallback callback) { m_stopCallback = std::move(callback); } |
| 43 | + |
| 44 | +bool ScriptBridge::sendText(const QString &payload, const QString &lineEnding) |
| 45 | +{ |
| 46 | + return sendWithCallback(m_sendTextCallback, payload, lineEnding); |
| 47 | +} |
| 48 | + |
| 49 | +bool ScriptBridge::sendHex(const QString &payload, const QString &lineEnding) |
| 50 | +{ |
| 51 | + return sendWithCallback(m_sendHexCallback, payload, lineEnding); |
| 52 | +} |
| 53 | + |
| 54 | +QString ScriptBridge::receivedText() const { return stringFromCallback(m_receivedTextCallback); } |
| 55 | + |
| 56 | +QString ScriptBridge::receivedHex() const { return stringFromCallback(m_receivedHexCallback); } |
| 57 | + |
| 58 | +QString ScriptBridge::lastRxText() const { return stringFromCallback(m_lastRxTextCallback); } |
| 59 | + |
| 60 | +QString ScriptBridge::lastRxHex() const { return stringFromCallback(m_lastRxHexCallback); } |
| 61 | + |
| 62 | +QVariantList ScriptBridge::records() const { return m_recordsCallback ? m_recordsCallback() : QVariantList(); } |
| 63 | + |
| 64 | +void ScriptBridge::log(const QString &message) |
| 65 | +{ |
| 66 | + m_lastError.clear(); |
| 67 | + emit logMessage(message); |
| 68 | +} |
| 69 | + |
| 70 | +void ScriptBridge::sleep(int milliseconds) |
| 71 | +{ |
| 72 | + const int duration = qBound(0, milliseconds, 600000); |
| 73 | + QElapsedTimer timer; |
| 74 | + timer.start(); |
| 75 | + while (!stopRequested() && timer.elapsed() < duration) { |
| 76 | + const int remaining = duration - static_cast<int>(timer.elapsed()); |
| 77 | + QThread::msleep(static_cast<unsigned long>(qMin(remaining, 50))); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +bool ScriptBridge::stopRequested() const { return m_stopCallback ? m_stopCallback() : false; } |
| 82 | + |
| 83 | +QString ScriptBridge::lastError() const { return m_lastError; } |
| 84 | + |
| 85 | +bool ScriptBridge::sendWithCallback(const SendCallback &callback, const QString &payload, const QString &lineEnding) |
| 86 | +{ |
| 87 | + if (stopRequested()) { |
| 88 | + m_lastError = AppI18n::text("脚本已停止"); |
| 89 | + emit logMessage(QStringLiteral("[ERROR] %1").arg(m_lastError)); |
| 90 | + return false; |
| 91 | + } |
| 92 | + if (!callback) { |
| 93 | + m_lastError = AppI18n::text("脚本发送接口不可用"); |
| 94 | + emit logMessage(QStringLiteral("[ERROR] %1").arg(m_lastError)); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + QString error; |
| 99 | + const bool ok = callback(payload, normalizedLineEnding(lineEnding), &error); |
| 100 | + if (!ok) { |
| 101 | + m_lastError = error.isEmpty() ? AppI18n::text("发送失败") : error; |
| 102 | + emit logMessage(QStringLiteral("[ERROR] %1").arg(m_lastError)); |
| 103 | + return false; |
| 104 | + } |
| 105 | + m_lastError.clear(); |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +QString ScriptBridge::stringFromCallback(const StringCallback &callback) const { return callback ? callback() : QString(); } |
| 110 | + |
| 111 | +ScriptRunner::ScriptRunner(QObject *parent) : QObject(parent) {} |
| 112 | + |
| 113 | +ScriptRunner::~ScriptRunner() { requestStop(); } |
| 114 | + |
| 115 | +void ScriptRunner::setSendTextCallback(ScriptBridge::SendCallback callback) { m_sendTextCallback = std::move(callback); } |
| 116 | + |
| 117 | +void ScriptRunner::setSendHexCallback(ScriptBridge::SendCallback callback) { m_sendHexCallback = std::move(callback); } |
| 118 | + |
| 119 | +void ScriptRunner::setReceivedTextCallback(ScriptBridge::StringCallback callback) |
| 120 | +{ |
| 121 | + m_receivedTextCallback = std::move(callback); |
| 122 | +} |
| 123 | + |
| 124 | +void ScriptRunner::setReceivedHexCallback(ScriptBridge::StringCallback callback) |
| 125 | +{ |
| 126 | + m_receivedHexCallback = std::move(callback); |
| 127 | +} |
| 128 | + |
| 129 | +void ScriptRunner::setLastRxTextCallback(ScriptBridge::StringCallback callback) |
| 130 | +{ |
| 131 | + m_lastRxTextCallback = std::move(callback); |
| 132 | +} |
| 133 | + |
| 134 | +void ScriptRunner::setLastRxHexCallback(ScriptBridge::StringCallback callback) |
| 135 | +{ |
| 136 | + m_lastRxHexCallback = std::move(callback); |
| 137 | +} |
| 138 | + |
| 139 | +void ScriptRunner::setRecordsCallback(ScriptBridge::RecordsCallback callback) { m_recordsCallback = std::move(callback); } |
| 140 | + |
| 141 | +void ScriptRunner::requestStop() |
| 142 | +{ |
| 143 | + m_stopRequested.store(true); |
| 144 | + QMutexLocker locker(&m_engineMutex); |
| 145 | + if (m_engine) { |
| 146 | + m_engine->setInterrupted(true); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void ScriptRunner::runScript(const QString &script, const QString &fileName) |
| 151 | +{ |
| 152 | + m_stopRequested.store(false); |
| 153 | + emit started(); |
| 154 | + |
| 155 | + if (script.trimmed().isEmpty()) { |
| 156 | + emit finished(false, AppI18n::text("脚本为空")); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + QJSEngine engine; |
| 161 | + { |
| 162 | + QMutexLocker locker(&m_engineMutex); |
| 163 | + m_engine = &engine; |
| 164 | + } |
| 165 | + |
| 166 | + ScriptBridge bridge; |
| 167 | + bridge.setSendTextCallback(m_sendTextCallback); |
| 168 | + bridge.setSendHexCallback(m_sendHexCallback); |
| 169 | + bridge.setReceivedTextCallback(m_receivedTextCallback); |
| 170 | + bridge.setReceivedHexCallback(m_receivedHexCallback); |
| 171 | + bridge.setLastRxTextCallback(m_lastRxTextCallback); |
| 172 | + bridge.setLastRxHexCallback(m_lastRxHexCallback); |
| 173 | + bridge.setRecordsCallback(m_recordsCallback); |
| 174 | + bridge.setStopCallback([this]() { return stopRequested(); }); |
| 175 | + connect(&bridge, &ScriptBridge::logMessage, this, &ScriptRunner::logMessage); |
| 176 | + |
| 177 | + engine.globalObject().setProperty(QStringLiteral("serial"), engine.newQObject(&bridge)); |
| 178 | + |
| 179 | + QStringList stackTrace; |
| 180 | + const QString effectiveName = fileName.trimmed().isEmpty() ? QStringLiteral("script.js") : fileName; |
| 181 | + const QJSValue result = engine.evaluate(script, effectiveName, 1, &stackTrace); |
| 182 | + { |
| 183 | + QMutexLocker locker(&m_engineMutex); |
| 184 | + m_engine = nullptr; |
| 185 | + } |
| 186 | + |
| 187 | + if (stopRequested()) { |
| 188 | + emit finished(false, AppI18n::text("脚本已停止")); |
| 189 | + return; |
| 190 | + } |
| 191 | + if (result.isError()) { |
| 192 | + emit finished(false, errorText(result, stackTrace)); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + emit finished(true, AppI18n::text("脚本执行完成")); |
| 197 | +} |
| 198 | + |
| 199 | +bool ScriptRunner::stopRequested() const { return m_stopRequested.load(); } |
| 200 | + |
| 201 | +QString ScriptRunner::errorText(const QJSValue &value, const QStringList &stackTrace) const |
| 202 | +{ |
| 203 | + const int line = value.property(QStringLiteral("lineNumber")).toInt(); |
| 204 | + QString message = line > 0 ? AppI18n::text("第 %1 行:%2").arg(line).arg(value.toString()) : value.toString(); |
| 205 | + if (!stackTrace.isEmpty()) { |
| 206 | + message.append(QStringLiteral("\n")); |
| 207 | + message.append(stackTrace.join(QLatin1Char('\n'))); |
| 208 | + } |
| 209 | + return message; |
| 210 | +} |
0 commit comments