128 lines
2.7 KiB
Markdown
128 lines
2.7 KiB
Markdown
# PowerManager系统架构优化设计方案
|
|
|
|
## 1. 总体架构
|
|
```mermaid
|
|
classDiagram
|
|
class PowerManagerCore {
|
|
+startup()
|
|
+shutdown()
|
|
+getSystemState()
|
|
}
|
|
|
|
class StateMachineService {
|
|
+triggerEvent()
|
|
+getCurrentState()
|
|
+addStateListener()
|
|
}
|
|
|
|
class CommunicationMediator {
|
|
+registerHandler()
|
|
+sendMessage()
|
|
+addMessageListener()
|
|
}
|
|
|
|
PowerManagerCore --> StateMachineService
|
|
PowerManagerCore --> CommunicationMediator
|
|
```
|
|
|
|
## 2. 状态机重构方案
|
|
|
|
### 2.1 状态设计模式
|
|
```cpp
|
|
class IPowerState {
|
|
public:
|
|
virtual void onEntry(PowerContext&) = 0;
|
|
virtual void handleEvent(PowerContext&, const Event&) = 0;
|
|
virtual void onExit(PowerContext&) = 0;
|
|
};
|
|
```
|
|
|
|
### 2.2 状态转换规则
|
|
```yaml
|
|
# states_config.yaml
|
|
transitions:
|
|
- from: STANDBY
|
|
event: START_CMD
|
|
to: OPERATING
|
|
- from: OPERATING
|
|
event: STOP_CMD
|
|
to: STANDBY
|
|
```
|
|
|
|
## 3. 通信中介者设计
|
|
|
|
### 3.1 核心架构
|
|
```mermaid
|
|
sequenceDiagram
|
|
participant Upper as 上位机
|
|
participant Mediator as 通信中介者
|
|
participant Lower as 下位机
|
|
|
|
Upper->>Mediator: 控制命令(JSON)
|
|
Mediator->>Lower: 设备指令(ProtocolBuffer)
|
|
Lower->>Mediator: 状态更新
|
|
Mediator->>Upper: 系统报告
|
|
```
|
|
|
|
### 3.2 关键实现
|
|
```cpp
|
|
class CommunicationMediator {
|
|
// 线程安全消息队列
|
|
moodycamel::ConcurrentQueue<Message> queue;
|
|
|
|
// 消息处理线程
|
|
void processMessages() {
|
|
while (running) {
|
|
Message msg;
|
|
if (queue.try_dequeue(msg)) {
|
|
handlers[msg.type]->handle(msg);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
```
|
|
|
|
## 4. 数据库服务设计
|
|
|
|
### 4.1 日志策略模式
|
|
```cpp
|
|
class ILogStrategy {
|
|
public:
|
|
virtual bool log(const SystemLog& log) = 0;
|
|
};
|
|
|
|
class SQLiteLogStrategy : public ILogStrategy {
|
|
// SQLite实现...
|
|
};
|
|
|
|
class FileLogStrategy : public ILogStrategy {
|
|
// 文件系统实现...
|
|
};
|
|
```
|
|
|
|
## 5. 实施计划
|
|
|
|
### 5.1 阶段划分
|
|
```mermaid
|
|
gantt
|
|
title 重构实施甘特图
|
|
section 核心框架
|
|
接口定义 :2025-04-08, 3d
|
|
基础实现 :2025-04-11, 5d
|
|
|
|
section 模块重构
|
|
状态机 :2025-04-16, 5d
|
|
通信模块 :2025-04-21, 5d
|
|
|
|
section 测试验证
|
|
单元测试 :2025-04-26, 3d
|
|
集成测试 :2025-04-29, 5d
|
|
```
|
|
|
|
## 6. 性能指标
|
|
|
|
| 模块 | 当前QPS | 目标QPS | 优化手段 |
|
|
|----------------|---------|---------|-----------------------|
|
|
| 状态机处理 | 100 | 500 | 无锁状态转换 |
|
|
| 消息处理 | 200 | 1000 | 批量处理+线程池 |
|
|
| 数据库写入 | 50 | 300 | 异步写入+批量提交 | |