/** * 命令类型定义 - 桥接 C# 生成的类型 * * 这个文件作为适配层,将 C# 模型生成的 TypeScript 类型 * 与 WebSocket 协议层连接起来,处理字段名称差异 */ import { OperationType, CommandType } from './index'; // TODO: 实际使用时,从 C# 生成的类型导入 // import { // LedSwitchCommand as CSharpLedSwitchCommand, // LedSwitchParameters as CSharpLedSwitchParameters // } from '@jrsoft/csharp-model/typescript-schemas'; // 示例命令参数接口 export interface ExampleCommandParameters { exampleField: string; exampleValue: number; } // 示例命令接口(仅用于展示命令结构,不可实际使用) export interface ExampleCommand { commandType: CommandType; // 协议需要的字段 commandCode: string; deviceType: string; deviceId: number | number[] | string; // 支持单个设备或批量操作 operationType: string; parameters?: ExampleCommandParameters; } // TODO: 实际使用时,从 C# 生成的类型导入更多命令 // import { ... } from '@jrsoft/csharp-model/typescript-schemas'; // 联合类型:所有支持的具体命令 export type SpecificCommand = | ExampleCommand // TODO: 实际使用时,这里应该列出所有从 C# 生成的命令类型 ; // 命令代码到类型的映射 export interface CommandTypeMap { 'ExampleCommand': ExampleCommand; // TODO: 实际使用时,这里应该映射所有从 C# 生成的命令 // 例如: 'LedSwitch': LedSwitchCommand, // 'BlockPlay': BlockPlayCommand, } // 类型安全的命令创建器 export function createTypedCommand( commandCode: T, baseProps: Omit ): CommandTypeMap[T] { return { commandCode, ...baseProps } as CommandTypeMap[T]; } // 示例类型守卫 export function isExampleCommand(cmd: any): cmd is ExampleCommand { return cmd?.commandCode === 'ExampleCommand'; } // 通用类型守卫 export function isSpecificCommand(cmd: any): cmd is SpecificCommand { return cmd && typeof cmd.commandCode === 'string' && cmd.commandCode in ({ 'ExampleCommand': true, // TODO: 实际使用时,这里应该包含所有真实的命令 } as Record); }