modbus rtu 1.1 plugins
This commit is contained in:
83
src/api/API_INTERFACE_TABLE.md
Normal file
83
src/api/API_INTERFACE_TABLE.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# softbus_daemon API 接口表
|
||||
|
||||
本文档汇总 `src/api/ipc_dbus` 当前对外接口,分为:
|
||||
- D-Bus 直接方法
|
||||
- `executeCommand` JSON 命令接口
|
||||
|
||||
## 1) D-Bus 服务基础信息
|
||||
|
||||
| 项目 | 值 |
|
||||
|---|---|
|
||||
| Service | `com.softbus.Daemon` |
|
||||
| ObjectPath | `/com/softbus/Daemon` |
|
||||
| Interface | `com.softbus.Daemon` |
|
||||
| 适配类 | `IpcDBus` |
|
||||
|
||||
## 2) D-Bus 方法表(IpcDBus)
|
||||
|
||||
| 方法 | 入参 | 返回 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `isRunning()` | 无 | `bool` | 核心服务是否初始化完成 |
|
||||
| `ping()` | 无 | `QString` | 健康探针,固定返回 `pong` |
|
||||
| `getVersion()` | 无 | `QString` | 进程版本号 |
|
||||
| `getStatus()` | 无 | `QString(JSON)` | 返回服务状态(pid/role/running/timestamp 等) |
|
||||
| `getPid()` | 无 | `qint64` | 返回守护进程 PID |
|
||||
| `shutdown()` | 无 | `void` | 关闭 CoreService 并退出进程 |
|
||||
| `reloadConfig()` | 无 | `bool` | 预留,当前为占位实现 |
|
||||
| `executeCommand(requestJson)` | `QString(JSON)` | `QString(JSON)` | 统一 JSON 命令入口 |
|
||||
|
||||
## 3) executeCommand 协议
|
||||
|
||||
### 3.1 请求格式
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "req-001",
|
||||
"action": "add_filter",
|
||||
"params": {}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.2 响应格式
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "req-001",
|
||||
"code": 0,
|
||||
"message": "ok",
|
||||
"data": {}
|
||||
}
|
||||
```
|
||||
|
||||
## 4) action 路由表(CommandDispatcher)
|
||||
|
||||
| action | params 关键字段 | 成功 code | 失败 code | 说明 |
|
||||
|---|---|---:|---:|---|
|
||||
| `add_filter` | `id`, `endpoint?`, `deviceId?`, `stableKey?`, `condition?` | `0` | `4001` | 添加过滤规则 |
|
||||
| `remove_filter` | `id` | `0` | `4041` | 删除过滤规则 |
|
||||
| `add_transform_rule` | `id`, `endpoint?`, `deviceId?`, `stableKey?`, `scale?`, `offset?`, `unit?` | `0` | `4002` | 添加转换规则 |
|
||||
| `remove_transform_rule` | `id` | `0` | `4042` | 删除转换规则 |
|
||||
| `list_rules` | 无 | `0` | - | 返回规则计数状态 |
|
||||
| `get_status` | 无 | `0` | - | 返回规则计数状态 |
|
||||
| 其他未知 action | - | - | `4040` | 未知动作 |
|
||||
|
||||
## 5) 通用错误码约定(当前实现)
|
||||
|
||||
| code | message | 说明 |
|
||||
|---:|---|---|
|
||||
| `0` | `ok` | 成功 |
|
||||
| `4000` | `invalid_json` | 请求 JSON 无法解析或非对象 |
|
||||
| `4001` | `invalid_filter_rule` | 过滤规则参数非法 |
|
||||
| `4002` | `invalid_transform_rule` | 转换规则参数非法 |
|
||||
| `4003` | `invalid_action` | action 为空或分发器不可用 |
|
||||
| `4040` | `unknown_action` | action 未注册 |
|
||||
| `4041` | `filter_not_found` | 删除过滤规则时未找到 |
|
||||
| `4042` | `transform_rule_not_found` | 删除转换规则时未找到 |
|
||||
|
||||
## 6) 维护建议
|
||||
|
||||
- 新增 action 时,同时更新:
|
||||
- `CommandDispatcher::dispatch(...)`
|
||||
- 本文档第 4 节 action 表
|
||||
- 若扩展返回结构,保持 `code/message/data` 三元组不变,避免客户端兼容性问题。
|
||||
|
||||
66
src/api/ipc_dbus/CommandDispatcher.cpp
Normal file
66
src/api/ipc_dbus/CommandDispatcher.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "api/ipc_dbus/CommandDispatcher.h"
|
||||
|
||||
#include "message_bus/pipeline/DynamicRuleRegistry.h"
|
||||
|
||||
namespace softbus::api::ipc
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
softbus::message_bus::pipeline::RuleScope parseScope(const QJsonObject& params)
|
||||
{
|
||||
softbus::message_bus::pipeline::RuleScope scope;
|
||||
scope.endpoint = params.value(QStringLiteral("endpoint")).toString();
|
||||
scope.deviceId = params.value(QStringLiteral("deviceId")).toInt(-1);
|
||||
scope.stableKey = params.value(QStringLiteral("stableKey")).toString();
|
||||
return scope;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DispatchResult CommandDispatcher::dispatch(const QString& action, const QJsonObject& params) const
|
||||
{
|
||||
auto& reg = softbus::message_bus::pipeline::DynamicRuleRegistry::instance();
|
||||
if (action == QStringLiteral("add_filter")) {
|
||||
softbus::message_bus::pipeline::FilterRule rule;
|
||||
rule.id = params.value(QStringLiteral("id")).toString();
|
||||
rule.scope = parseScope(params);
|
||||
rule.condition = params.value(QStringLiteral("condition")).toObject();
|
||||
if (!reg.addFilterRule(rule)) {
|
||||
return {4001, QStringLiteral("invalid_filter_rule"), {}};
|
||||
}
|
||||
return {0, QStringLiteral("ok"), QJsonObject{{QStringLiteral("id"), rule.id}}};
|
||||
}
|
||||
if (action == QStringLiteral("remove_filter")) {
|
||||
const QString id = params.value(QStringLiteral("id")).toString();
|
||||
const bool ok = reg.removeFilterRule(id);
|
||||
return {ok ? 0 : 4041, ok ? QStringLiteral("ok") : QStringLiteral("filter_not_found"),
|
||||
QJsonObject{{QStringLiteral("id"), id}}};
|
||||
}
|
||||
if (action == QStringLiteral("add_transform_rule")) {
|
||||
softbus::message_bus::pipeline::TransformRule rule;
|
||||
rule.id = params.value(QStringLiteral("id")).toString();
|
||||
rule.scope = parseScope(params);
|
||||
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();
|
||||
if (!reg.addTransformRule(rule)) {
|
||||
return {4002, QStringLiteral("invalid_transform_rule"), {}};
|
||||
}
|
||||
return {0, QStringLiteral("ok"), QJsonObject{{QStringLiteral("id"), rule.id}}};
|
||||
}
|
||||
if (action == QStringLiteral("remove_transform_rule")) {
|
||||
const QString id = params.value(QStringLiteral("id")).toString();
|
||||
const bool ok = reg.removeTransformRule(id);
|
||||
return {ok ? 0 : 4042, ok ? QStringLiteral("ok") : QStringLiteral("transform_rule_not_found"),
|
||||
QJsonObject{{QStringLiteral("id"), id}}};
|
||||
}
|
||||
if (action == QStringLiteral("list_rules") || action == QStringLiteral("get_status")) {
|
||||
return {0, QStringLiteral("ok"), reg.status()};
|
||||
}
|
||||
return {4040, QStringLiteral("unknown_action"), {}};
|
||||
}
|
||||
|
||||
} // namespace softbus::api::ipc
|
||||
|
||||
23
src/api/ipc_dbus/CommandDispatcher.h
Normal file
23
src/api/ipc_dbus/CommandDispatcher.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace softbus::api::ipc
|
||||
{
|
||||
|
||||
struct DispatchResult
|
||||
{
|
||||
int code{0};
|
||||
QString message;
|
||||
QJsonObject data;
|
||||
};
|
||||
|
||||
class CommandDispatcher
|
||||
{
|
||||
public:
|
||||
DispatchResult dispatch(const QString& action, const QJsonObject& params) const;
|
||||
};
|
||||
|
||||
} // namespace softbus::api::ipc
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
#include <QDateTime>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
|
||||
#include "api/ipc_dbus/CommandDispatcher.h"
|
||||
#include "src/core/CoreService.h"
|
||||
|
||||
IpcDBus::IpcDBus(CoreService* coreService, const QString& role, QObject* parent)
|
||||
: QObject(parent),
|
||||
m_coreService(coreService),
|
||||
m_role(role)
|
||||
m_role(role),
|
||||
m_dispatcher(std::make_unique<softbus::api::ipc::CommandDispatcher>())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -68,6 +71,33 @@ bool IpcDBus::reloadConfig()
|
||||
return false;
|
||||
}
|
||||
|
||||
QString IpcDBus::executeCommand(const QString& requestJson)
|
||||
{
|
||||
auto makeResponse = [](const QString& id, int code, const QString& message, const QJsonObject& data) {
|
||||
QJsonObject o;
|
||||
o.insert(QStringLiteral("id"), id);
|
||||
o.insert(QStringLiteral("code"), code);
|
||||
o.insert(QStringLiteral("message"), message);
|
||||
o.insert(QStringLiteral("data"), data);
|
||||
return QString::fromUtf8(QJsonDocument(o).toJson(QJsonDocument::Compact));
|
||||
};
|
||||
|
||||
QJsonParseError err;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(requestJson.toUtf8(), &err);
|
||||
if (err.error != QJsonParseError::NoError || !doc.isObject()) {
|
||||
return makeResponse(QString(), 4000, QStringLiteral("invalid_json"), {});
|
||||
}
|
||||
const QJsonObject req = doc.object();
|
||||
const QString id = req.value(QStringLiteral("id")).toString();
|
||||
const QString action = req.value(QStringLiteral("action")).toString();
|
||||
const QJsonObject params = req.value(QStringLiteral("params")).toObject();
|
||||
if (action.isEmpty() || !m_dispatcher) {
|
||||
return makeResponse(id, 4003, QStringLiteral("invalid_action"), {});
|
||||
}
|
||||
const auto result = m_dispatcher->dispatch(action, params);
|
||||
return makeResponse(id, result.code, result.message, result.data);
|
||||
}
|
||||
|
||||
void IpcDBus::updateLoadInfo()
|
||||
{
|
||||
// 预留:后续可在此周期性采样 CPU/内存/队列长度等指标,并通过 getStatus() 输出
|
||||
|
||||
@@ -8,9 +8,11 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <memory>
|
||||
#include <QString>
|
||||
// 前向声明
|
||||
class CoreService;
|
||||
namespace softbus::api::ipc { class CommandDispatcher; }
|
||||
/**
|
||||
* @brief D-Bus 服务适配器 - 暴露核心服务接口
|
||||
*/
|
||||
@@ -54,6 +56,7 @@ public slots:
|
||||
|
||||
bool reloadConfig();
|
||||
|
||||
QString executeCommand(const QString& requestJson);
|
||||
|
||||
|
||||
|
||||
@@ -64,6 +67,7 @@ private slots:
|
||||
private:
|
||||
CoreService* m_coreService{nullptr}; // non-owning
|
||||
QString m_role;
|
||||
std::unique_ptr<softbus::api::ipc::CommandDispatcher> m_dispatcher;
|
||||
};
|
||||
|
||||
#endif // IPC_DBUS_H
|
||||
|
||||
Reference in New Issue
Block a user