# 类型定义修复总结

## 修复的类型定义

### 1. Command 接口层次结构 ✅

创建了更清晰的命令接口层次结构：

```typescript
// 基础命令接口
export interface BaseCommand {
  commandType?: CommandType;  // 命令类型（默认为 SIMPLE）
  commandCode: string;
  parameters?: Record<string, any>;
}

// 简单命令接口（SIMPLE 和 BATCH）
export interface SimpleCommand extends BaseCommand {
  commandType?: CommandType.SIMPLE | CommandType.BATCH;
  deviceId: number | number[] | string;  // 必需：支持批量
  deviceType: string;  // 必需
  operationType: OperationType;  // 必需
}

// 复杂命令接口（COMPLEX）
export interface ComplexCommand extends BaseCommand {
  commandType: CommandType.COMPLEX;
  deviceId?: number | number[] | string;  // 可选
  deviceType?: string;  // 可选
  operationType?: OperationType;  // 可选
}

// 通用命令接口（向后兼容）
export interface GenericCommand {
  commandType?: CommandType;
  commandCode: string;
  deviceId?: number | number[] | string;
  deviceType?: string;
  operationType?: OperationType;
  parameters?: Record<string, any>;
}

// 命令联合类型
export type Command = SpecificCommand | SimpleCommand | ComplexCommand | GenericCommand;
```

**关键改进**：
- 为不同命令类型创建了专门的接口
- SIMPLE/BATCH 命令必需 deviceId、deviceType 和 operationType
- COMPLEX 命令这些字段是可选的
- 支持批量操作：`deviceId: number | number[] | string`

### 2. ProgressUpdateMessage 接口 ✅

当前定义已包含所有必需字段：

```typescript
export interface ProgressUpdateMessage extends BaseMessage {
  type: MessageType.PROGRESS_UPDATE;
  requestRef: string;
  status: ProgressStatus;      // 状态
  phase: ProgressPhase | string;  // 当前阶段
  progress: number;            // 0-100 进度百分比
  sourceType: 'COMMAND' | 'SYSTEM';  // 必需字段
  context?: ProgramContext;    // 程序上下文
  command?: DeviceOperationRecord;  // 设备操作记录
  report?: ReportMessage;      // 包含 level、message、code、data
  timestamp: string;
  version: string;
}
```

**ReportMessage 结构**：
```typescript
export interface ReportMessage {
  level: ReportLevel;
  message: string;
  code?: string;
  data?: Record<string, any>;
}
```

### 3. 枚举定义 ✅

所有必需的枚举都已正确定义和导出：

```typescript
// 命令类型
export enum CommandType {
  SIMPLE = 'SIMPLE',   // 点对点命令
  BATCH = 'BATCH',     // 多设备命令
  COMPLEX = 'COMPLEX'  // 持续响应命令
}

// 操作类型
export enum OperationType {
  READ = 'READ',
  WRITE = 'WRITE'
}

// 进度状态
export enum ProgressStatus {
  PENDING = 'PENDING',
  IN_PROGRESS = 'IN_PROGRESS',
  PAUSED = 'PAUSED',
  COMPLETED = 'COMPLETED',
  FAILED = 'FAILED',
  CANCELLED = 'CANCELLED'
}

// 报告级别
export enum ReportLevel {
  DEBUG = 'DEBUG',
  INFO = 'INFO',
  WARNING = 'WARNING',
  ERROR = 'ERROR',
  CRITICAL = 'CRITICAL'
}
```

## 使用示例

### 创建简单命令
```typescript
const simpleCommand: SimpleCommand = {
  commandType: CommandType.SIMPLE,
  commandCode: 'LedSwitch',
  deviceId: 123,
  deviceType: 'pillar',
  operationType: OperationType.WRITE,
  parameters: { switch: 'ON' }
};
```

### 创建批量命令
```typescript
const batchCommand: SimpleCommand = {
  commandType: CommandType.BATCH,
  commandCode: 'LedSwitch',
  deviceId: [123, 124, 125],  // 多设备数组
  deviceType: 'pillar',
  operationType: OperationType.WRITE,
  parameters: { switch: 'OFF' }
};
```

### 创建复杂命令
```typescript
const complexCommand: ComplexCommand = {
  commandType: CommandType.COMPLEX,
  commandCode: 'SystemDiagnostics',
  // deviceId、deviceType、operationType 都是可选的
  parameters: { 
    diagnosticLevel: 'full',
    includeHistory: true 
  }
};
```

### 创建进度更新
```typescript
const progressUpdate: ProgressUpdateMessage = {
  type: MessageType.PROGRESS_UPDATE,
  requestRef: 'req-123',
  status: ProgressStatus.IN_PROGRESS,
  phase: ProgressPhase.DOWNLOAD,
  progress: 45,
  sourceType: 'COMMAND',
  command: {
    commandType: CommandType.SIMPLE,
    commandCode: 'LedSwitch',
    deviceType: 'pillar',
    deviceId: 123,
    operationType: OperationType.WRITE,
    result: { success: true }
  },
  report: {
    level: ReportLevel.INFO,
    message: 'Download in progress',
    code: 'DOWNLOAD_PROGRESS',
    data: { bytesDownloaded: 1024000 }
  },
  timestamp: new Date().toISOString(),
  version: '1.0'
};
```

## 验证器支持

MessageValidator 已更新以支持这些类型定义：

```typescript
// 验证命令消息
const result = MessageValidator.validateCommandMessage(commandMessage);
if (!result.valid) {
  console.error('Validation errors:', result.errors);
}

// 验证进度更新
const progressResult = MessageValidator.validateProgressUpdate(progressUpdate);
if (!progressResult.valid) {
  console.error('Validation errors:', progressResult.errors);
}
```

## 向后兼容性

- GenericCommand 接口保持向后兼容
- 现有代码可以继续使用，但建议迁移到新的类型定义
- 验证器会根据 commandType 自动应用正确的验证规则