V1版本
This commit is contained in:
380
CapMachine.Wpf/Models/State/MachineRunState.cs
Normal file
380
CapMachine.Wpf/Models/State/MachineRunState.cs
Normal file
@@ -0,0 +1,380 @@
|
||||
using CapMachine.Wpf.Services;
|
||||
using Prism.Events;
|
||||
using Prism.Mvvm;
|
||||
using Stateless;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace CapMachine.Wpf.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备的运行状态
|
||||
/// https://www.cnblogs.com/podolski/p/13215064.html
|
||||
/// </summary>
|
||||
public class MachineRunState : BindableBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 实例化
|
||||
/// </summary>
|
||||
public MachineRunState(string name, IEventAggregator eventAggregator, ConfigService configService)
|
||||
{
|
||||
Name = name;
|
||||
EventAggregator = eventAggregator;
|
||||
ConfigService = configService;
|
||||
InitConfig();
|
||||
|
||||
}
|
||||
|
||||
private string _RunStateMsg="等待";
|
||||
/// <summary>
|
||||
/// 运行状态
|
||||
/// </summary>
|
||||
public string RunStateMsg
|
||||
{
|
||||
get { return _RunStateMsg; }
|
||||
set { _RunStateMsg = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// PLC程序是否下载
|
||||
/// </summary>
|
||||
public bool IsProLoad { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态机
|
||||
/// </summary>
|
||||
public StateMachine<RunState, RunStateTrig> StateMachine { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化配置信息
|
||||
/// </summary>
|
||||
private void InitConfig()
|
||||
{
|
||||
//状态机初始化
|
||||
StateMachine = new StateMachine<RunState, RunStateTrig>(RunState.Wait);
|
||||
|
||||
StateMachine.Configure(RunState.Wait)
|
||||
.OnEntry(t => WaitEntryCall())
|
||||
.OnExit(t => WaitExitCall())
|
||||
.PermitIf(RunStateTrig.AlarmTrig, RunState.Alarm, () => AlarmPreValid())
|
||||
.PermitIf(RunStateTrig.StartTrig, RunState.Run, () => RunPreValid())
|
||||
//.Permit(RunStateTrig.StartTrig, RunState.Run)
|
||||
//.Permit(RunStateTrig.ResetTrig, RunState.Wait)
|
||||
.PermitIf(RunStateTrig.EndTrig, RunState.Stop, () => StopPreValid())
|
||||
.PermitIf(RunStateTrig.PauseTrig, RunState.Pause, () => PausePreValid())
|
||||
.Ignore(RunStateTrig.ResetTrig)
|
||||
.Ignore(RunStateTrig.WaitTrig);
|
||||
|
||||
StateMachine.Configure(RunState.Alarm)
|
||||
.OnEntry(t => AlarmEntryCall())
|
||||
.OnExit(t => AlarmExitCall())
|
||||
.PermitIf(RunStateTrig.WaitTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.ResetTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.StartTrig, RunState.Run, () => RunPreValid())
|
||||
.PermitIf(RunStateTrig.EndTrig, RunState.Stop, () => StopPreValid())
|
||||
.PermitIf(RunStateTrig.PauseTrig, RunState.Pause, () => PausePreValid())
|
||||
.Ignore(RunStateTrig.AlarmTrig);
|
||||
|
||||
StateMachine.Configure(RunState.Run)
|
||||
.OnEntry(t => RunEntryCall())
|
||||
.OnExit(t => RunExitCall())
|
||||
.PermitIf(RunStateTrig.WaitTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.ResetTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.AlarmTrig, RunState.Alarm, () => AlarmPreValid())
|
||||
.PermitIf(RunStateTrig.EndTrig, RunState.Stop, () => StopPreValid())
|
||||
.PermitIf(RunStateTrig.PauseTrig, RunState.Pause, () => PausePreValid())
|
||||
.Ignore(RunStateTrig.StartTrig);
|
||||
|
||||
StateMachine.Configure(RunState.Stop)
|
||||
.OnEntry(t => StopEntryCall())
|
||||
.OnExit(t => StopExitCall())
|
||||
.PermitIf(RunStateTrig.WaitTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.ResetTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.AlarmTrig, RunState.Alarm, () => AlarmPreValid())
|
||||
.PermitIf(RunStateTrig.StartTrig, RunState.Run, () => RunPreValid())
|
||||
.PermitIf(RunStateTrig.PauseTrig, RunState.Pause, () => PausePreValid())
|
||||
.Ignore(RunStateTrig.EndTrig);
|
||||
|
||||
StateMachine.Configure(RunState.Pause)
|
||||
.OnEntry(t => PauseEntryCall())
|
||||
.OnExit(t => PauseExitCall())
|
||||
.PermitIf(RunStateTrig.WaitTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.ResetTrig, RunState.Wait, () => WaitPreValid())
|
||||
.PermitIf(RunStateTrig.AlarmTrig, RunState.Alarm, () => AlarmPreValid())
|
||||
.PermitIf(RunStateTrig.StartTrig, RunState.Run, () => RunPreValid())
|
||||
.PermitIf(RunStateTrig.EndTrig, RunState.Stop, () => StopPreValid())
|
||||
.Ignore(RunStateTrig.PauseTrig);
|
||||
|
||||
}
|
||||
|
||||
#region 验证状态
|
||||
|
||||
/// <summary>
|
||||
/// 进入 Wait 状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FireWait()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.WaitTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.WaitTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"WaitTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 Alarm状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FireAlarm()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.AlarmTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.AlarmTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"AlarmTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 Reset状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FireReset()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.ResetTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.ResetTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"ResetTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 Start 状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FireStart()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.StartTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.StartTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"StartTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 End 状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FireEnd()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.EndTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.EndTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"EndTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 Pause 状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool FirePause()
|
||||
{
|
||||
var Result = StateMachine.CanFire(RunStateTrig.PauseTrig);
|
||||
if (Result)
|
||||
{
|
||||
StateMachine.Fire(RunStateTrig.PauseTrig);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"PauseTrig-没有满足条件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 运行 状态的条件判断验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private bool RunPreValid()
|
||||
{
|
||||
if (ConfigService.IsExpInfoOk && IsProLoad)
|
||||
{
|
||||
//程序下载并且,试验信息已经设置好后,就可以开始了
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///进入 报警 状态的条件判断验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private bool AlarmPreValid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///进入 等待 状态的条件判断验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private bool WaitPreValid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///进入 停止 状态的条件判断验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private bool StopPreValid()
|
||||
{
|
||||
//运行切换到停止
|
||||
if (StateMachine.State == RunState.Run)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///进入 暂停 状态的条件判断验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private bool PausePreValid()
|
||||
{
|
||||
//运行切换到停止
|
||||
if (StateMachine.State == RunState.Run)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 状态离开和进入方法
|
||||
|
||||
private void PauseExitCall()
|
||||
{
|
||||
Console.WriteLine($"{Name}-PauseExitCall");
|
||||
}
|
||||
|
||||
private void PauseEntryCall()
|
||||
{
|
||||
RunStateMsg = "暂停";
|
||||
Console.WriteLine($"{Name}-PauseEntryCall");
|
||||
}
|
||||
|
||||
private void StopExitCall()
|
||||
{
|
||||
Console.WriteLine($"{Name}-StopExitCall");
|
||||
}
|
||||
|
||||
private void StopEntryCall()
|
||||
{
|
||||
RunStateMsg = "停止";
|
||||
Console.WriteLine($"{Name}-StopEntryCall");
|
||||
}
|
||||
|
||||
private void RunExitCall()
|
||||
{
|
||||
Console.WriteLine($"{Name}-RunExitCall");
|
||||
}
|
||||
|
||||
private void RunEntryCall()
|
||||
{
|
||||
RunStateMsg = "运行";
|
||||
Console.WriteLine($"{Name}-RunEntryCall");
|
||||
|
||||
}
|
||||
|
||||
private void AlarmExitCall()
|
||||
{
|
||||
Console.WriteLine($"{Name}-AlarmExitCall");
|
||||
}
|
||||
|
||||
private void AlarmEntryCall()
|
||||
{
|
||||
RunStateMsg = "报警";
|
||||
Console.WriteLine($"{Name}-AlarmEntryCall");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 离开 等待
|
||||
/// </summary>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private void WaitExitCall()
|
||||
{
|
||||
Console.WriteLine($"{Name}-WaitExitCall");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入 等待
|
||||
/// </summary>
|
||||
private void WaitEntryCall()
|
||||
{
|
||||
RunStateMsg = "等待";
|
||||
Console.WriteLine($"{Name}-WaitExitCall");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
public IEventAggregator EventAggregator { get; }
|
||||
public ConfigService ConfigService { get; }
|
||||
}
|
||||
}
|
||||
78
CapMachine.Wpf/Models/State/RunState.cs
Normal file
78
CapMachine.Wpf/Models/State/RunState.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CapMachine.Wpf.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// 状态触发枚举
|
||||
/// </summary>
|
||||
public enum RunStateTrig
|
||||
{
|
||||
/// <summary>
|
||||
/// 等待 触发
|
||||
/// </summary>
|
||||
WaitTrig = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 报警 触发
|
||||
/// </summary>
|
||||
AlarmTrig = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 开始 触发
|
||||
/// </summary>
|
||||
StartTrig = 3,
|
||||
|
||||
/// <summary>
|
||||
/// 结束 触发
|
||||
/// </summary>
|
||||
EndTrig = 4,
|
||||
|
||||
/// <summary>
|
||||
/// 暂停 触发
|
||||
/// </summary>
|
||||
PauseTrig = 5,
|
||||
|
||||
/// <summary>
|
||||
/// 复位 触发
|
||||
/// </summary>
|
||||
ResetTrig = 6,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 状态枚举
|
||||
/// </summary>
|
||||
public enum RunState
|
||||
{
|
||||
/// <summary>
|
||||
/// 等待中
|
||||
/// </summary>
|
||||
Wait = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 报警中
|
||||
/// </summary>
|
||||
Alarm = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 运行中
|
||||
/// </summary>
|
||||
Run = 3,
|
||||
|
||||
/// <summary>
|
||||
/// 停止中
|
||||
/// </summary>
|
||||
Stop = 4,
|
||||
|
||||
/// <summary>
|
||||
/// 暂停中
|
||||
/// </summary>
|
||||
Pause = 5,
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user