You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
designPattern/DesignPattern/FSM/States/RunningState.cs

82 lines
2.2 KiB

using FSM.Machines;
using FSM.Recipes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace FSM.States
{
class RunningState : IEquipmentState
{
private Recipe _currentRecipe;
private Timer _timer;
public RunningState(Recipe recipe)
{
_currentRecipe = recipe;
_timer = new Timer(3000);
_timer.Elapsed += OnTimedEvent;
}
public void Enter()
{
Console.WriteLine(string.Format("Entering RunningState with recipe: {0}", _currentRecipe.Name));
ExecuteCurrentStep();
_timer.Start();
}
public void Exit()
{
Console.WriteLine("Exiting RunningState");
_timer.Stop();
}
public bool HandleEvent(EquipmentEvent equipmentEvent, Machines.EquipmentStateMachine machine)
{
switch (equipmentEvent)
{
case EquipmentEvent.Pause:
machine.SetState(new PausedState(_currentRecipe));
return true;
case EquipmentEvent.Stop:
machine.SetState(new IdleState());
return true;
case EquipmentEvent.EmergencyStop:
machine.SetState(new EmergencyStoppedState());
return true;
default:
return false;
}
}
private void ExecuteCurrentStep()
{
var step = _currentRecipe.GetCurrentStep();
if (string.IsNullOrEmpty(step))
{
Console.WriteLine("Recipe completed");
}
else
{
Console.WriteLine(string.Format("Execute step {0}: {1}", _currentRecipe.CurrentStepIndex + 1, step));
}
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
if (_currentRecipe.MoveToNextStep())
{
ExecuteCurrentStep();
}
else
{
Console.WriteLine("Recipe completed");
_timer.Stop();
}
}
}
}