218 lines
5.4 KiB
C++
218 lines
5.4 KiB
C++
// ConfigManager.cpp
|
|
#include "configManager.h"
|
|
#include <QCoreApplication>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QDebug>
|
|
#include <QMutex>
|
|
|
|
ConfigManager* ConfigManager::instance()
|
|
{
|
|
static ConfigManager* instance = nullptr;
|
|
static QMutex mutex;
|
|
|
|
if (!instance) {
|
|
QMutexLocker locker(&mutex);
|
|
if (!instance) {
|
|
instance = new ConfigManager;
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
ConfigManager::ConfigManager(QObject* parent)
|
|
: QObject(parent)
|
|
{
|
|
// 设置默认配置路径
|
|
QString appDir = QCoreApplication::applicationDirPath();
|
|
m_configFile = appDir + "/config.json";
|
|
|
|
qDebug() << "ConfigManager initialized, config file:" << m_configFile;
|
|
}
|
|
|
|
bool ConfigManager::loadConfig(const QString& configFile)
|
|
{
|
|
if (!configFile.isEmpty()) {
|
|
m_configFile = configFile;
|
|
}
|
|
|
|
QFile file(m_configFile);
|
|
|
|
// 如果文件不存在,创建默认配置
|
|
if (!file.exists()) {
|
|
qWarning() << "Config file not found, creating default config:" << m_configFile;
|
|
return createDefaultConfig();
|
|
}
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
qWarning() << "Failed to open config file:" << m_configFile;
|
|
return false;
|
|
}
|
|
|
|
QByteArray jsonData = file.readAll();
|
|
file.close();
|
|
|
|
QJsonParseError error;
|
|
QJsonDocument doc = QJsonDocument::fromJson(jsonData, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
qWarning() << "JSON parse error:" << error.errorString();
|
|
return false;
|
|
}
|
|
|
|
if (!doc.isObject()) {
|
|
qWarning() << "Config is not a JSON object";
|
|
return false;
|
|
}
|
|
|
|
QVariantMap root = doc.object().toVariantMap();
|
|
|
|
// 加载HTTP配置
|
|
if (root.contains("http")) {
|
|
m_httpConfig = ChannelConfig::fromMap(root["http"].toMap());
|
|
} else {
|
|
qWarning() << "Config missing 'http' section, creating default";
|
|
m_httpConfig = getDefaultHttpConfig();
|
|
}
|
|
|
|
// 加载WebSocket配置
|
|
if (root.contains("websocket")) {
|
|
m_websocketConfig = ChannelConfig::fromMap(root["websocket"].toMap());
|
|
} else {
|
|
qWarning() << "Config missing 'websocket' section, creating default";
|
|
m_websocketConfig = getDefaultWebSocketConfig();
|
|
}
|
|
|
|
qInfo() << "Config loaded from:" << m_configFile;
|
|
qInfo() << "HTTP endpoint:" << m_httpConfig.endpoint;
|
|
qInfo() << "WebSocket endpoint:" << m_websocketConfig.endpoint;
|
|
|
|
emit configLoaded();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool ConfigManager::saveConfig()
|
|
{
|
|
// 确保目录存在
|
|
QFileInfo fileInfo(m_configFile);
|
|
QDir dir = fileInfo.dir();
|
|
|
|
if (!dir.exists()) {
|
|
if (!dir.mkpath(".")) {
|
|
qCritical() << "Failed to create config directory:" << dir.path();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
QVariantMap root = {
|
|
{"http", m_httpConfig.toMap()},
|
|
{"websocket", m_websocketConfig.toMap()},
|
|
{"lastSaved", QDateTime::currentDateTime().toString(Qt::ISODate)}
|
|
};
|
|
|
|
QFile file(m_configFile);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
|
|
qCritical() << "Failed to open config file for writing:" << m_configFile;
|
|
return false;
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(root);
|
|
file.write(doc.toJson(QJsonDocument::Indented));
|
|
file.close();
|
|
|
|
qInfo() << "Config saved to:" << m_configFile;
|
|
emit configSaved();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool ConfigManager::createDefaultConfig()
|
|
{
|
|
// 设置默认配置
|
|
m_httpConfig = getDefaultHttpConfig();
|
|
m_websocketConfig = getDefaultWebSocketConfig();
|
|
|
|
// 保存默认配置
|
|
bool success = saveConfig();
|
|
|
|
if (success) {
|
|
qInfo() << "Default config created at:" << m_configFile;
|
|
} else {
|
|
qCritical() << "Failed to create default config";
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
ChannelConfig ConfigManager::getDefaultHttpConfig() const
|
|
{
|
|
ChannelConfig config;
|
|
|
|
config.id = "http_channel";
|
|
config.name = "SCADA数据接口";
|
|
config.endpoint = "http://192.168.1.100:8080/api";
|
|
config.timeout = 30000;
|
|
config.enabled = true;
|
|
config.autoConnect = true;
|
|
config.username = "";
|
|
config.password = "";
|
|
config.connected = false;
|
|
config.lastConnectTime = QDateTime();
|
|
config.errorCount = 0;
|
|
|
|
// 默认HTTP头
|
|
config.headers = QVariantMap{
|
|
{"Content-Type", "application/json"},
|
|
};
|
|
|
|
return config;
|
|
}
|
|
|
|
ChannelConfig ConfigManager::getDefaultWebSocketConfig() const
|
|
{
|
|
ChannelConfig config;
|
|
|
|
config.id = "websocket_channel";
|
|
config.name = "实时数据推送";
|
|
config.endpoint = "ws://192.168.1.101:8888/ws";
|
|
config.timeout = 30000;
|
|
config.enabled = true;
|
|
config.autoConnect = false;
|
|
config.connected = false;
|
|
config.lastConnectTime = QDateTime();
|
|
config.errorCount = 0;
|
|
config.heartbeatInterval = 30000;
|
|
|
|
return config;
|
|
}
|
|
|
|
ChannelConfig ConfigManager::getHttpConfig() const
|
|
{
|
|
return m_httpConfig;
|
|
}
|
|
|
|
ChannelConfig ConfigManager::getWebSocketConfig() const
|
|
{
|
|
return m_websocketConfig;
|
|
}
|
|
|
|
void ConfigManager::setHttpConfig(const ChannelConfig& config)
|
|
{
|
|
m_httpConfig = config;
|
|
emit httpConfigChanged(config);
|
|
}
|
|
|
|
void ConfigManager::setWebSocketConfig(const ChannelConfig& config)
|
|
{
|
|
m_websocketConfig = config;
|
|
emit websocketConfigChanged(config);
|
|
}
|
|
|
|
QString ConfigManager::configFilePath() const
|
|
{
|
|
return m_configFile;
|
|
}
|