using PLCSharp.Core.Common;
using PLCSharp.Models;
using Prism.Commands;
using Prism.Mvvm;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace PLCSharp.VVMs.Workflows
{
///
/// 工作流
///
public class Workflow : BindableBase
{
///
/// 唯一标识
///
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
///
/// 配方标识
///
public Guid RecipeID { get; set; }
private string _Name;
///
/// 配置项
///
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Name, value);
}
}
private string _Prompt;
///
/// 提示
///
[NotMapped]
///
/// 提示
///
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private double _CycleDelayTime = 0.01;
///
/// 循环结束延迟
///
public double CycleDelayTime
{
get { return _CycleDelayTime; }
set { SetProperty(ref _CycleDelayTime, value); }
}
private string _Code;
private double _CycleTime;
///
/// CycleTime
///
[NotMapped]
public double CycleTime
{
get { return _CycleTime; }
set { SetProperty(ref _CycleTime, value); }
}
///
/// 配置项
///
public string Code
{
get { return _Code; }
set
{
if (_Code != value)
{
IsCompiled = false;
Prompt = "已修改,请编译";
}
SetProperty(ref _Code, value);
}
}
private string _Comment;
///
/// 备注
///
public string Comment
{
get { return _Comment; }
set
{
if (_Comment != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Comment, value);
}
}
private bool _AutomaticExecution;
///
/// 脚本自动运行
///
public bool AutomaticExecution
{
get { return _AutomaticExecution; }
set { SetProperty(ref _AutomaticExecution, value); }
}
private bool _IsCompiled;
///
/// IsCompiled
///
[NotMapped]
///
/// 已编译
///
public bool IsCompiled
{
get { return _IsCompiled; }
set { SetProperty(ref _IsCompiled, value); }
}
private bool _IsRuning;
///
/// IsRuning
///
[NotMapped]
public bool IsRuning
{
get { return _IsRuning; }
set { SetProperty(ref _IsRuning, value); }
}
///
/// Assembly
///
[NotMapped]
public Assembly Assembly { get; set; }
private string _DebugLog;
///
/// Debug日志
///
[NotMapped]
///
/// 调试日志
///
public string DebugLog
{
get { return _DebugLog; }
set { SetProperty(ref _DebugLog, value); }
}
private bool _Exception;
///
/// 异常
///
[NotMapped]
///
/// 异常
///
public bool Exception
{
get { return _Exception; }
set { SetProperty(ref _Exception, value); }
}
AssemblyCSharpBuilder AssemblyCSharpBuilder;
private DelegateCommand _Compile;
///
/// 编译
///
public DelegateCommand Compile =>
_Compile ??= new DelegateCommand(ExecuteCompile);
void ExecuteCompile()
{
lock (this)
{
try
{
Flow.Reset();
AssemblyCSharpBuilder = new();
DebugLog = "编译中,请稍等。\n";
AssemblyCSharpBuilder.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode