import 'dart:async'; import '../../programs/models/step.dart'; import '../../programs/models/program.dart'; /// 模拟运行器回调 typedef RunProgressCallback = void Function( int currentStepIndex, int remainingSeconds, double progress, String currentWell, ); typedef RunCompleteCallback = void Function(); /// 模拟运行器 /// 用于在没有实际硬件连接时模拟程序执行过程 class MockRunner { Timer? _timer; Program? _currentProgram; List _steps = []; int _currentStepIndex = 0; int _remainingSeconds = 0; bool _isPaused = false; RunProgressCallback? _onProgress; RunCompleteCallback? _onComplete; /// 是否正在运行 bool get isRunning => _timer != null && !_isPaused; /// 是否已暂停 bool get isPaused => _isPaused; /// 当前程序 Program? get currentProgram => _currentProgram; /// 开始运行程序 void start( Program program, List steps, RunProgressCallback onProgress, RunCompleteCallback onComplete, ) { _currentProgram = program; _steps = steps; _onProgress = onProgress; _onComplete = onComplete; _currentStepIndex = 0; _isPaused = false; if (steps.isEmpty) { onComplete(); return; } // 开始执行第一个步骤 _startStep(steps[0]); } /// 暂停运行 void pause() { if (_timer != null && !_isPaused) { _isPaused = true; _timer!.cancel(); _timer = null; } } /// 继续运行 void resume() { if (_isPaused && _currentProgram != null) { _isPaused = false; _resumeStep(); } } /// 停止运行 void stop() { _timer?.cancel(); _timer = null; _currentProgram = null; _steps = []; _currentStepIndex = 0; _remainingSeconds = 0; _isPaused = false; } /// 开始执行步骤 void _startStep(Step step) { // 计算步骤总时间(混合时间 + 吸磁时间 + 吹气时间) _remainingSeconds = step.mixTime + step.magnetTime + step.blowTime; // 如果步骤时间为0,设置最小演示时间(5秒) if (_remainingSeconds == 0) { _remainingSeconds = 5; } // 启动定时器,每秒更新 _timer = Timer.periodic(const Duration(seconds: 1), (timer) { _remainingSeconds--; // 计算总进度 final totalSeconds = _calculateTotalSeconds(); final elapsedSeconds = _calculateElapsedSeconds(); final progress = totalSeconds > 0 ? elapsedSeconds / totalSeconds : 0.0; // 回调进度更新 _onProgress?.call( _currentStepIndex, _remainingSeconds, progress, step.position, ); // 步骤完成 if (_remainingSeconds <= 0) { timer.cancel(); _timer = null; _nextStep(); } }); } /// 继续执行步骤(从暂停恢复) void _resumeStep() { if (_currentStepIndex >= _steps.length) return; final step = _steps[_currentStepIndex]; _timer = Timer.periodic(const Duration(seconds: 1), (timer) { _remainingSeconds--; final totalSeconds = _calculateTotalSeconds(); final elapsedSeconds = _calculateElapsedSeconds(); final progress = totalSeconds > 0 ? elapsedSeconds / totalSeconds : 0.0; _onProgress?.call( _currentStepIndex, _remainingSeconds, progress, step.position, ); if (_remainingSeconds <= 0) { timer.cancel(); _timer = null; _nextStep(); } }); } /// 执行下一个步骤 void _nextStep() { _currentStepIndex++; if (_currentStepIndex >= _steps.length) { // 所有步骤完成 _onComplete?.call(); stop(); } else { // 执行下一个步骤 _startStep(_steps[_currentStepIndex]); } } /// 计算总执行时间 int _calculateTotalSeconds() { int total = 0; for (final step in _steps) { int stepTime = step.mixTime + step.magnetTime + step.blowTime; if (stepTime == 0) stepTime = 5; total += stepTime; } return total; } /// 计算已执行时间 int _calculateElapsedSeconds() { int elapsed = 0; for (int i = 0; i < _currentStepIndex; i++) { int stepTime = _steps[i].mixTime + _steps[i].magnetTime + _steps[i].blowTime; if (stepTime == 0) stepTime = 5; elapsed += stepTime; } // 加上当前步骤已执行的时间 final currentStep = _steps[_currentStepIndex]; int currentStepTime = currentStep.mixTime + currentStep.magnetTime + currentStep.blowTime; if (currentStepTime == 0) currentStepTime = 5; elapsed += currentStepTime - _remainingSeconds; return elapsed; } }