dommessage
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
#include "message_bus/egress/EgressRouter.h"
|
||||
#include "message_bus/ingress/DriverIngressAdapter.h"
|
||||
#include "message_bus/pipeline/PipelineEngine.h"
|
||||
#include "core/metadata/ProfileRegistry.h"
|
||||
#include "message_bus/pipeline/mapping/MappingRegistry.h"
|
||||
#include "message_bus/pipeline/rules/DynamicRuleRegistry.h"
|
||||
#include "core/plugin_system/PluginManager.h"
|
||||
#include "device_bus/manager/SerialDeviceManager.h"
|
||||
@@ -44,39 +46,118 @@ 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 ensureConfigFileInitialized(const QString& targetPath,
|
||||
const QString& fallbackPath,
|
||||
const QByteArray& defaultContent,
|
||||
const char* fileLabel)
|
||||
{
|
||||
if (QFile::exists(targetPath)) {
|
||||
return true;
|
||||
}
|
||||
const QFileInfo fi(targetPath);
|
||||
if (!QDir().mkpath(fi.dir().absolutePath())) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create config directory:" << fi.dir().absolutePath();
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray initBytes = defaultContent;
|
||||
QFile fallback(fallbackPath);
|
||||
if (!fallbackPath.isEmpty() && fallback.exists() && fallback.open(QIODevice::ReadOnly)) {
|
||||
initBytes = fallback.readAll();
|
||||
}
|
||||
|
||||
QSaveFile initFile(targetPath);
|
||||
if (!initFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create" << fileLabel << "file:" << targetPath;
|
||||
return false;
|
||||
}
|
||||
if (initFile.write(initBytes) != initBytes.size()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to initialize" << fileLabel << "file:" << targetPath;
|
||||
initFile.cancelWriting();
|
||||
return false;
|
||||
}
|
||||
if (!initFile.commit()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to commit" << fileLabel << "file:" << targetPath;
|
||||
return false;
|
||||
}
|
||||
LOG_INFO() << "DeviceBusManager: initialized" << fileLabel << "file:" << targetPath;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ensureCoreConfigFilesInitialized()
|
||||
{
|
||||
const QFileInfo rulesFi(rulesConfigPath());
|
||||
const QString baseDir = rulesFi.dir().absolutePath();
|
||||
|
||||
const QByteArray defaultRules = QJsonDocument(QJsonObject{
|
||||
{QStringLiteral("filter"), QJsonArray{}},
|
||||
{QStringLiteral("transform"), QJsonArray{}},
|
||||
}).toJson(QJsonDocument::Indented);
|
||||
(void)ensureConfigFileInitialized(rulesConfigPath(),
|
||||
QStringLiteral(),
|
||||
defaultRules,
|
||||
"runtime_rules");
|
||||
|
||||
const QByteArray defaultMappings = QStringLiteral(
|
||||
"{\n \"catalogVersion\": 1,\n \"items\": []\n}\n")
|
||||
.toUtf8();
|
||||
(void)ensureConfigFileInitialized(runtimeMappingsConfigPath(),
|
||||
QStringLiteral(),
|
||||
defaultMappings,
|
||||
"runtime_mappings");
|
||||
|
||||
const QString profilesPath = QDir(baseDir).filePath(QStringLiteral("device_profiles.json"));
|
||||
const QByteArray defaultProfiles = QStringLiteral(
|
||||
"{\n \"catalogVersion\": \"\",\n \"profiles\": []\n}\n")
|
||||
.toUtf8();
|
||||
(void)ensureConfigFileInitialized(profilesPath,
|
||||
QStringLiteral("config/device_profiles.json"),
|
||||
defaultProfiles,
|
||||
"device_profiles");
|
||||
|
||||
const QString metadataPath = QDir(baseDir).filePath(QStringLiteral("metadata_dictionary.json"));
|
||||
const QByteArray defaultMetadata = QStringLiteral(
|
||||
"{\n \"catalogVersion\": \"\",\n \"metadatas\": []\n}\n")
|
||||
.toUtf8();
|
||||
(void)ensureConfigFileInitialized(metadataPath,
|
||||
QStringLiteral("config/metadata_dictionary.json"),
|
||||
defaultMetadata,
|
||||
"metadata_dictionary");
|
||||
}
|
||||
|
||||
void loadRuntimeMappingsFromDedicatedConfig()
|
||||
{
|
||||
const QString path = runtimeMappingsConfigPath();
|
||||
if (!softbus::message_bus::pipeline::MappingRegistry::instance().loadPersistedFile(path)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to load runtime_mappings from" << path;
|
||||
} else {
|
||||
LOG_INFO() << "DeviceBusManager: loaded runtime_mappings from" << path;
|
||||
}
|
||||
}
|
||||
|
||||
void loadProfileCatalogSingleton()
|
||||
{
|
||||
const QString dir = QFileInfo(rulesConfigPath()).dir().absolutePath();
|
||||
const QString profilePath = QDir(dir).filePath(QStringLiteral("device_profiles.json"));
|
||||
if (!softbus::core::metadata::ProfileRegistry::instance().loadFromFile(profilePath)) {
|
||||
LOG_WARNING() << "DeviceBusManager: profile catalog load failed path=" << profilePath
|
||||
<< "err=" << softbus::core::metadata::ProfileRegistry::instance().lastError();
|
||||
} else {
|
||||
LOG_INFO() << "DeviceBusManager: loaded device_profiles from" << profilePath;
|
||||
}
|
||||
}
|
||||
|
||||
void loadDynamicRulesFromDedicatedConfig()
|
||||
{
|
||||
auto& reg = softbus::message_bus::pipeline::DynamicRuleRegistry::instance();
|
||||
const QString path = rulesConfigPath();
|
||||
QFile file(path);
|
||||
if (!file.exists()) {
|
||||
const QFileInfo fi(path);
|
||||
if (!QDir().mkpath(fi.dir().absolutePath())) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create rules config directory:" << fi.dir().absolutePath();
|
||||
return;
|
||||
}
|
||||
QSaveFile initFile(path);
|
||||
if (!initFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
const QJsonObject emptyRules{
|
||||
{QStringLiteral("filter"), QJsonArray{}},
|
||||
{QStringLiteral("transform"), QJsonArray{}},
|
||||
};
|
||||
const QByteArray initBytes = QJsonDocument(emptyRules).toJson(QJsonDocument::Indented);
|
||||
if (initFile.write(initBytes) != initBytes.size()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to initialize rules config file:" << path;
|
||||
initFile.cancelWriting();
|
||||
return;
|
||||
}
|
||||
if (!initFile.commit()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to commit initialized rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
LOG_INFO() << "DeviceBusManager: created empty rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to open rules config:" << path;
|
||||
return;
|
||||
@@ -207,7 +288,10 @@ bool DeviceBusManager::initialize(const QJsonObject& rootConfig)
|
||||
m_pipelineEngine->setMemoryPool(m_memoryPool);
|
||||
m_pipelineEngine->loadRuntimeConfigFromJson(rootConfig.value(QStringLiteral("pipeline")).toObject());
|
||||
m_pipelineEngine->setPluginManager(m_pluginManager);
|
||||
ensureCoreConfigFilesInitialized();
|
||||
loadDynamicRulesFromDedicatedConfig();
|
||||
loadRuntimeMappingsFromDedicatedConfig();
|
||||
loadProfileCatalogSingleton();
|
||||
m_pipelineEngine->start();
|
||||
}
|
||||
if (!m_ingressAdapter) {
|
||||
|
||||
Reference in New Issue
Block a user