dommessage
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "api/ipc_dbus/CommandDispatcher.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
@@ -29,6 +30,35 @@ QString rulesConfigPath()
|
||||
return QDir::homePath() + QStringLiteral("/softbus/config/runtime_rules.json");
|
||||
}
|
||||
|
||||
QString runtimeMappingsConfigPath()
|
||||
{
|
||||
const QFileInfo fi(rulesConfigPath());
|
||||
return fi.dir().filePath(QStringLiteral("runtime_mappings.json"));
|
||||
}
|
||||
|
||||
bool persistMappingsToDedicatedConfig(const softbus::message_bus::pipeline::MappingRegistry& reg)
|
||||
{
|
||||
const QString path = runtimeMappingsConfigPath();
|
||||
const QFileInfo fi(path);
|
||||
if (!QDir().mkpath(fi.dir().absolutePath())) {
|
||||
LOG_ERROR() << "CommandDispatcher: failed to create mappings directory:" << fi.dir().absolutePath();
|
||||
return false;
|
||||
}
|
||||
return reg.savePersistedFile(path);
|
||||
}
|
||||
|
||||
void appendMappingAuditLine(const QString& line)
|
||||
{
|
||||
const QFileInfo fi(runtimeMappingsConfigPath());
|
||||
const QString auditPath = fi.dir().filePath(QStringLiteral("mapping_audit.log"));
|
||||
QFile f(auditPath);
|
||||
if (!f.open(QIODevice::Append | QIODevice::Text)) {
|
||||
return;
|
||||
}
|
||||
f.write(line.toUtf8());
|
||||
f.write("\n");
|
||||
}
|
||||
|
||||
softbus::message_bus::pipeline::RuleScope parseScope(const QJsonObject& params)
|
||||
{
|
||||
softbus::message_bus::pipeline::RuleScope scope;
|
||||
@@ -188,14 +218,38 @@ DispatchResult CommandDispatcher::dispatch(const QString& action, const QJsonObj
|
||||
rule.scale = params.value(QStringLiteral("scale")).toDouble(1.0);
|
||||
rule.offset = params.value(QStringLiteral("offset")).toDouble(0.0);
|
||||
rule.unit = params.value(QStringLiteral("unit")).toString();
|
||||
rule.metadataId = params.value(QStringLiteral("metadataId")).toString();
|
||||
rule.pointId = params.value(QStringLiteral("pointId")).toString();
|
||||
rule.catalogVersion = params.value(QStringLiteral("catalogVersion")).toString();
|
||||
rule.updatedBy = params.value(QStringLiteral("updatedBy")).toString();
|
||||
rule.updatedAtMs = QDateTime::currentMSecsSinceEpoch();
|
||||
if (!mappingReg.upsert(rule)) {
|
||||
return {4004, QStringLiteral("invalid_mapping_rule"), {}};
|
||||
}
|
||||
return {0, QStringLiteral("ok"), QJsonObject{{QStringLiteral("compositeKey"), keyText}}};
|
||||
if (!persistMappingsToDedicatedConfig(mappingReg)) {
|
||||
return {5002, QStringLiteral("persist_mappings_failed"), {}};
|
||||
}
|
||||
appendMappingAuditLine(QStringLiteral("upsert compositeKey=%1 domPath=%2 at %3")
|
||||
.arg(keyText)
|
||||
.arg(rule.domPath)
|
||||
.arg(rule.updatedAtMs));
|
||||
return {0,
|
||||
QStringLiteral("ok"),
|
||||
QJsonObject{{QStringLiteral("compositeKey"), keyText},
|
||||
{QStringLiteral("mappingCatalogVersion"),
|
||||
QString::number(mappingReg.mappingCatalogVersion())}}};
|
||||
}
|
||||
if (action == QStringLiteral("remove_mapping_rule")) {
|
||||
const QString keyText = params.value(QStringLiteral("compositeKey")).toString();
|
||||
const bool ok = mappingReg.remove(keyText.toULongLong());
|
||||
if (ok) {
|
||||
if (!persistMappingsToDedicatedConfig(mappingReg)) {
|
||||
return {5002, QStringLiteral("persist_mappings_failed"), {}};
|
||||
}
|
||||
appendMappingAuditLine(QStringLiteral("remove compositeKey=%1 at %2")
|
||||
.arg(keyText)
|
||||
.arg(QDateTime::currentMSecsSinceEpoch()));
|
||||
}
|
||||
return {ok ? 0 : 4043, ok ? QStringLiteral("ok") : QStringLiteral("mapping_rule_not_found"),
|
||||
QJsonObject{{QStringLiteral("compositeKey"), keyText}}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user