# Feature 设计思路

## 1. ImageInspection 免登录自动启动功能 (未提交)

### 需求背景
ImageInspection 应用需要实现"免登录"体验，同时保证相机自动启动功能正常工作。

### 问题排查
1. `StartAllTasks()` 调用了 `EnsureLoggedIn()` 检查
2. 免登录模式下 `_authenticationService.IsLoggedIn` 为 `false`
3. 导致相机启动逻辑提前返回

### 设计方案

#### 1.1 添加 SetCurrentSession 方法
在 `PersonnelAuthenticationService` 中添加公开方法，用于设置当前会话：

```csharp
// PersonnelAuthenticationService.cs
public void SetCurrentSession(PersonnelSession session)
{
    CurrentSession = session;
    CurrentSessionChanged?.Invoke(this, EventArgs.Empty);
}
```

#### 1.2 自动登录管理员
App 启动时直接设置管理员会话，无需密码验证：

```csharp
// App.xaml.cs
authenticationService.SetCurrentSession(new PersonnelSession("admin", "管理员", "", true));
```

#### 1.3 自动启动相机
在 `MainWindow.Loaded` 事件中调用 `StartAllTasksCommand`：

```csharp
// MainWindow.xaml.cs
Loaded += OnLoaded;

private async void OnLoaded(object sender, RoutedEventArgs e)
{
    if (_viewModel.StartAllTasksCommand.CanExecute(null))
    {
        await _viewModel.StartAllTasksCommand.ExecuteAsync(null);
    }
}
```

---

## 2. ImageInspection 屏幕启动支持 (486750c)

### 需求背景
ImageInspection 应用需要与 VideoInference 保持一致的功能体验：
- 程序启动时自动启动相机
- 支持多屏幕环境，窗口可自动定位到指定屏幕的指定区域
- 开机自动启动程序

### 设计方案

#### 2.1 参数设置中的屏幕配置
在 `InspectionParameterSettings` 中添加屏幕相关配置：

| 参数 | 类型 | 说明 |
|------|------|------|
| DefaultStartupScreen | 枚举 | 不使用 / 左半屏 / 右半屏 |
| StartupScreenIndex | int | 屏幕编号（0=主屏，1+=副屏） |
| LinkedApplicationPath | string | 联动启动的程序路径 |
| AutoStartOnBoot | bool | 开机自动启动程序 |

**UI 位置**：参数设置窗口 → 屏幕设置区域

#### 2.2 联动启动程序
```csharp
private void LaunchLinkedApplication(InspectionParameterSettings settings)
{
    var path = settings.LinkedApplicationPath;
    if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) return;

    Process.Start(new ProcessStartInfo
    {
        FileName = path,
        UseShellExecute = true
    });
}
```

---

## 3. 协作登录图像检测 (596e23e)

### 需求背景
ImageInspection 应用需要支持用户登录功能，实现多用户协作和权限管理。

### 设计方案
- 复用 VideoInference 的登录认证体系（PersonnelAuthenticationService）
- 使用 LoginWindow 弹窗进行用户名密码验证
- 登录成功后传递认证服务给 MainWindow

**后续演进**：由于用户反馈"免登录"体验更佳，已改为自动登录管理员模式。

---

## 4. 自动启动相机 / 默认启动屏幕 / 服务器和客户端兼容模式 (c46a05b)

### 需求背景
- 程序启动时需要自动启动已配置的相机，提高用户体验
- 支持多屏幕环境，窗口可自动定位到指定屏幕的指定区域
- ModbusTCP 服务端启动失败时，系统仍需正常运行

### 设计方案

#### 4.1 自动启动相机
- 在 MainViewModel 构造函数末尾调用 `AutoStartEnabledCameras()`

#### 4.2 默认启动屏幕
**窗口定位逻辑**：
```csharp
private void ApplyScreenSettings(InspectionParameterSettings settings)
{
    if (settings.DefaultStartupScreen == DefaultStartupScreen.None) return;

    var screens = System.Windows.Forms.Screen.AllScreens;
    var screenIndex = Math.Clamp(settings.StartupScreenIndex, 0, screens.Length - 1);
    var workArea = screens[screenIndex].WorkingArea;

    if (settings.DefaultStartupScreen == DefaultStartupScreen.LeftHalf)
    {
        MainWindow.Left = workArea.Left;
        MainWindow.Width = workArea.Width / 2.0;
    }
    else if (settings.DefaultStartupScreen == DefaultStartupScreen.RightHalf)
    {
        MainWindow.Left = workArea.Left + workArea.Width / 2.0;
        MainWindow.Width = workArea.Width / 2.0;
    }
    MainWindow.Top = workArea.Top;
    MainWindow.Height = workArea.Height;
}
```

**UI 位置**：系统设置窗口 → 启动屏幕区域

#### 4.3 Modbus 服务器兼容模式
```csharp
public IModbusAccessor GetAccessor()
{
    return _server?.Accessor ?? _localAccessor;
}
```

---

## 5. SOP1 和 SOP2 完成

### 需求背景
视觉检测系统需要支持两种标准操作流程（SOP），每种流程包含多个检测步骤。

### 设计方案

#### SOP1 - 自动扩口机流程
| 步骤 | 名称 | 检测目标 | 条件 |
|------|------|---------|------|
| 1 | 检测位置 | InspectionConnector | 累计 3 帧 |
| 2 | 视觉检测 | Modbus | 地址 0→1→2 |
| 3 | 紧固尼龙管 | Product/Product1 + Button | 累计 3 帧产品 + 1 帧按钮 |
| 4 | 自动扩口 | Press 移动 | 向上 30px + 向下 30px |
| 5 | 产品消失 | 无 Product/Product1 | 连续 5 帧 |

#### SOP2 - 半自动扩口机流程
| 步骤 | 名称 | 检测目标 | 条件 |
|------|------|---------|------|
| 1 | 检测位置 | InspectionConnector | 累计 3 帧 |
| 2 | 视觉检测 | Modbus | 地址 0→1→2 |
| 3 | 紧固尼龙管 | Handle | 连续 2 帧 |
| 4 | 扩口 | Expansion | 累计 2 帧 |
| 5 | 主接头 | Connector | 连续 2 帧，无检测接头 |
| 6 | 产品消失 | 无 Product/Handle | 连续 2 帧 |

### 模型类别映射
```
0 = Press, 1 = Product, 2 = InspectionConnector, 3 = Button, 4 = Product1

SOP2:
0 = Press, 1 = Expansion, 2 = Product, 3 = Handle,
4 = InspectionConnector, 5 = ReverseExpansion, 6 = Connector
```

---

## 6. 智能录制功能 (2026-09-08)

### 需求背景
- SOP 活跃时才开始录制，节省存储空间
- 支持外部 PLC 通过 Modbus 控制录制启停
- NG 片段自动标记，便于后续追溯

### 录制模式

#### 连续录制
- 相机启动后立即开始录制
- 按时间/文件大小自动分段

#### 智能录制
- SOP 步骤转换时激活录制
- 5分钟无操作自动停止
- 支持外部 Modbus 控制

### 录制状态机

```
触发激活：
① SOP 步骤转换 → 激活录制（重置5分钟超时）
② 外部 Modbus 写入寄存器50=1 → 激活录制

触发停止：
③ 5分钟无操作 → 自动停止
④ 外部 Modbus 写入寄存器50=0 → 停止录制
```

### NG 标记机制

| 事件 | 行为 |
|------|------|
| 检测到 NG | 文件名添加 `_ng` 后缀，当前分段绘制 NG 标识 |
| NG 清除 | NG 标识不再绘制，但文件名后缀保留 |
| 录制结束 | 最终文件名决定是否带 `_ng` |

**注意**：NG 事件不再强制分段，保持正常录制。

### 文件命名规则
```
{yyyyMMdd_HHmmss}[_ng].mp4
示例：
- 无 NG：20260908_143025.mp4
- 有 NG：20260908_143025_ng.mp4
```

### 架构组件

| 组件 | 职责 |
|------|------|
| SmartRecordingStateMachine | 管理录制激活状态，与 PLC 交互 |
| PipelineSmartRecordingCoordinator | 协调连续录制和智能录制模式 |
| SegmentedVideoRecorder | 按分段录制，标记 NG 状态 |
| SegmentWriter | 写入视频分段，应用 NG 标识 |

### 寄存器定义

| 地址 | 功能 | 说明 |
|------|------|------|
| 50 | 录制状态 | 0=停止，1=录制中 |
