110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
using OrpaonVision.Core.Results;
|
||
using OrpaonVision.SiteApp.Runtime.Contracts;
|
||
using OrpaonVision.SiteApp.Runtime.Options;
|
||
|
||
namespace OrpaonVision.SiteApp.Runtime.Services
|
||
{
|
||
/// <summary>
|
||
/// 运行状态机简化实现(MVP 阶段)。
|
||
/// </summary>
|
||
public sealed class SimpleRuntimeStateMachineService : IRuntimeStateMachineService
|
||
{
|
||
private readonly RuntimeOptions _options;
|
||
|
||
private int _currentLayer;
|
||
|
||
/// <summary>
|
||
/// 构造函数。
|
||
/// </summary>
|
||
public SimpleRuntimeStateMachineService(RuntimeOptions options)
|
||
{
|
||
_options = options;
|
||
_currentLayer = 1;
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public RuntimeStateSnapshotDto GetSnapshot()
|
||
{
|
||
return new RuntimeStateSnapshotDto
|
||
{
|
||
CurrentLayer = _currentLayer,
|
||
TotalLayers = _options.TotalLayers,
|
||
StateText = _currentLayer > _options.TotalLayers ? "工位完成" : "运行中"
|
||
};
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public RuntimeState GetCurrentState()
|
||
{
|
||
return _currentLayer > _options.TotalLayers ? RuntimeState.Completed : RuntimeState.Running;
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public int GetCurrentLayer()
|
||
{
|
||
return _currentLayer;
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public bool CanExecuteOperation(StateTrigger trigger)
|
||
{
|
||
_ = trigger;
|
||
return true;
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public Result<StateTransitionEvent> TriggerTransition(StateTrigger trigger, string? reason = null, object? parameters = null)
|
||
{
|
||
_ = reason;
|
||
_ = parameters;
|
||
|
||
// 简化实现:对某些触发器映射到已有操作
|
||
if (trigger == StateTrigger.MoveToNextLayer)
|
||
{
|
||
var move = MoveToNextLayer();
|
||
if (!move.Succeeded)
|
||
{
|
||
return Result<StateTransitionEvent>.Fail(move.Code, move.Message);
|
||
}
|
||
}
|
||
else if (trigger == StateTrigger.Reset)
|
||
{
|
||
Reset();
|
||
}
|
||
|
||
var ev = new StateTransitionEvent
|
||
{
|
||
EventType = StateTransitionEventType.Error,
|
||
PreviousState = RuntimeState.Running,
|
||
NewState = GetCurrentState(),
|
||
PreviousLayer = _currentLayer,
|
||
NewLayer = _currentLayer,
|
||
Timestamp = DateTime.UtcNow,
|
||
Reason = reason ?? string.Empty,
|
||
Trigger = trigger,
|
||
GuardResult = "Simple"
|
||
};
|
||
|
||
return Result<StateTransitionEvent>.Success(ev);
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public Result MoveToNextLayer()
|
||
{
|
||
if (_currentLayer >= _options.TotalLayers)
|
||
{
|
||
return Result.Fail("STATE_MACHINE_FINISHED", "当前已到最后一层,无法继续切层。");
|
||
}
|
||
|
||
_currentLayer += 1;
|
||
return Result.Success("STATE_MACHINE_MOVED", $"已切换到第 {_currentLayer} 层。");
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public void Reset()
|
||
{
|
||
_currentLayer = 1;
|
||
}
|
||
}
|
||
}
|