示例工程版本提升至net462,项目添加了部分空引用检测逻辑。累了,消不完的空引用警告(T.T)

This commit is contained in:
fengjiayi
2024-09-30 22:20:02 +08:00
parent 8ecbdfa7a6
commit e4aa3b6185
45 changed files with 562 additions and 581 deletions

View File

@@ -0,0 +1,86 @@
using Net462DllTest.Device;
using Net462DllTest.Signal;
using Net462DllTest.ViewModel;
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Enums;
using Serein.Library.Ex;
using Serein.Library.Framework.NodeFlow;
using Serein.Library.NodeFlow.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Net462DllTest.LogicControl
{
public enum ParkingCommand
{
GetPparkingSpace,
}
[AutoRegister]
[DynamicFlow]
public class ParkingLogicControl
{
private readonly PrakingDevice PrakingDevice;
public ParkingLogicControl(PrakingDevice PrakingDevice)
{
this.PrakingDevice = PrakingDevice;
}
[NodeAction(NodeType.Flipflop, "等待车位调取命令",ReturnType=typeof(string))]
public async Task<IFlipflopContext> GetPparkingSpace(ParkingCommand parkingCommand = ParkingCommand.GetPparkingSpace)
{
try
{
TriggerData triggerData = await PrakingDevice.CreateChannelWithTimeoutAsync(parkingCommand, TimeSpan.FromMinutes(5), 0);
if (triggerData.Type == TriggerType.Overtime)
{
throw new FlipflopException("超时取消");
}
if(triggerData.Value is string spaceNum)
{
await Console.Out.WriteLineAsync("收到命令:调取车位,车位号"+ spaceNum);
return new FlipflopContext(FlipflopStateType.Succeed, spaceNum);
}
else
{
throw new FlipflopException("并非车位号");
}
}
catch (FlipflopException)
{
throw;
}
catch (Exception)
{
return new FlipflopContext(FlipflopStateType.Error);
}
}
[NodeAction(NodeType.Action, "手动触发模拟调取车位")]
public void Storage(string spaceNum = "101")
{
if (PrakingDevice.TriggerSignal(ParkingCommand.GetPparkingSpace, spaceNum))
{
Console.WriteLine("发送命令成功:调取车位" + spaceNum);
}
else
{
Console.WriteLine("发送命令失败:调取车位" + spaceNum);
}
}
}
}

View File

@@ -0,0 +1,205 @@
using IoTClient.Clients.PLC;
using IoTClient.Common.Enums;
using Net462DllTest.Device;
using Net462DllTest.Enums;
using Net462DllTest.Signal;
using Net462DllTest.Web;
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Enums;
using Serein.Library.Ex;
using Serein.Library.Framework.NodeFlow;
using Serein.Library.NodeFlow.Tool;
using Serein.Library.Utils;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Threading.Tasks;
namespace Net462DllTest.LogicControl
{
[AutoRegister]
[DynamicFlow]
public class PlcLogicControl
{
private readonly SiemensPlcDevice MyPlc;
public PlcLogicControl(SiemensPlcDevice MyPlc)
{
this.MyPlc = MyPlc;
}
#region 退
[NodeAction(NodeType.Init)] // Init 初始化事件,流程启动时执行
public void Init(IDynamicContext context)
{
context.Env.IOC.Register<IRouter, Router>();
context.Env.IOC.Register<WebServer>();
}
[NodeAction(NodeType.Loading)] // Loading 初始化完成已注入依赖项,可以开始逻辑上的操作
public void Loading(IDynamicContext context)
{
// 注册控制器
context.Env.IOC.Run<IRouter, WebServer>((router, web) => {
try
{
router.RegisterController(typeof(CommandController));
web.Start("http://*:8089/"); // 开启 Web 服务
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
}
[NodeAction(NodeType.Exit)] // 流程结束时自动执行
public void Exit(IDynamicContext context)
{
context.Env.IOC.Run<WebServer>((web) =>
{
web?.Stop(); // 关闭 Web 服务
});
MyPlc.ResetDevice();
MyPlc.CancelAllTasks();
}
#endregion
#region
[NodeAction(NodeType.Flipflop, "等待信号触发", ReturnType = typeof(int))]
public async Task<IFlipflopContext> WaitTask(CommandSignal order = CommandSignal.Command_1)
{
try
{
TriggerData triggerData = await MyPlc.CreateChannelWithTimeoutAsync(order, TimeSpan.FromMinutes(5), 0);
if (triggerData.Type == TriggerType.Overtime)
{
throw new FlipflopException("超时取消");
}
//int.TryParse(triggerData.Value.ToString(),out int result);
return new FlipflopContext(FlipflopStateType.Succeed, triggerData.Value);
}
catch (FlipflopException)
{
throw;
}
catch (Exception)
{
return new FlipflopContext(FlipflopStateType.Error);
}
}
#endregion
#region
[NodeAction(NodeType.Action, "初始化")]
public SiemensPlcDevice PlcInit(SiemensVersion version = SiemensVersion.None,
string ip = "192.168.10.100",
int port = 102)
{
if (MyPlc.Client is null)
{
try
{
MyPlc.Init(version, ip, port);
Console.WriteLine($"西门子PLC初始化成功[{version},{ip}:{port}]");
}
catch (Exception ex)
{
Console.WriteLine($"西门子PLC[{version},{ip}:{port}]初始化异常:{ex.Message}");
}
}
else
{
Console.WriteLine( $"西门子PLC已经初始化[{version},{ip}:{port}]");
}
return MyPlc;
}
[NodeAction(NodeType.Action, "设置PLC状态")]
public SiemensPlcDevice SetState(PlcState state = PlcState.PowerOff)
{
if(MyPlc.Client != null)
{
var oldState = MyPlc.State;
MyPlc.State = state;
Console.WriteLine($"PLC状态从[{oldState}]转为[{state}]");
return MyPlc;
}
else
{
Console.WriteLine($"PLC尚未初始化");
return MyPlc;
}
}
[NodeAction(NodeType.Action, "PLC获取变量")]
public object ReadVar(PlcVarEnum plcVarEnum)
{
var varInfo = ToVarInfo(plcVarEnum);
var result = MyPlc.Read(varInfo);
Console.WriteLine($"获取变量成功:({varInfo})\t result = {result}");
return result;
}
[NodeAction(NodeType.Action, "PLC写入变量")]
public SiemensPlcDevice WriteVar2(object value, PlcVarEnum plcVarEnum)
{
var varInfo = ToVarInfo(plcVarEnum);
if (MyPlc.State == PlcState.Runing)
{
if (varInfo.IsProtected)
{
Console.WriteLine($"PLC变量{varInfo}当前禁止写入");
}
else
{
MyPlc.Write(varInfo, value);
Console.WriteLine($"PLC变量{varInfo}写入数据:{value}");
}
}
else
{
Console.WriteLine($"PLC处于非预期状态{MyPlc.State}");
}
return MyPlc;
}
/// <summary>
/// 缓存变量信息
/// </summary>
private readonly Dictionary<PlcVarEnum, PlcVarInfo> VarInfoDict = new Dictionary<PlcVarEnum, PlcVarInfo>();
private PlcVarInfo ToVarInfo(PlcVarEnum plcVarEnum)
{
if (VarInfoDict.ContainsKey(plcVarEnum))
{
return VarInfoDict[plcVarEnum];
}
if (plcVarEnum == PlcVarEnum.None)
{
throw new Exception("非预期枚举值");
}
var plcValue = EnumHelper.GetBoundValue<PlcVarEnum, PlcValueAttribute, PlcVarInfo>(plcVarEnum, attr => attr.PlcInfo)
?? throw new Exception($"获取变量异常:{plcVarEnum}没有标记PlcValueAttribute");
if (string.IsNullOrEmpty(plcValue.VarAddress))
{
throw new Exception($"获取变量异常:{plcVarEnum},变量地址为空");
}
VarInfoDict.Add(plcVarEnum, plcValue);
return plcValue;
}
#endregion
}
}

View File

@@ -0,0 +1,98 @@
using Net462DllTest.Device;
using Net462DllTest.Signal;
using Net462DllTest.ViewModel;
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Enums;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Net462DllTest.LogicControl
{
/// <summary>
/// 视图管理
/// </summary>
[AutoRegister]
public class ViewManagement
{
private List<Form> forms = new List<Form>();
/// <summary>
/// 打开窗口
/// </summary>
/// <param name="form">要打开的窗口类型</param>
/// <param name="isTop">是否置顶</param>
public void OpenView(Form form, bool isTop)
{
form.TopMost = isTop;
form.Show();
forms.Add(form);
}
public void CloseView(Type formType)
{
var remoteForms = forms.Where(f => f.GetType() == formType).ToArray();
foreach (Form f in remoteForms)
{
f.Close();
f.Dispose();
this.forms.Remove(f);
}
}
}
[DynamicFlow]
public class ViewLogicControl
{
private readonly ViewManagement ViewManagement;
public ViewLogicControl(ViewManagement ViewManagement)
{
this.ViewManagement = ViewManagement;
}
[NodeAction(NodeType.Init)]
public void Init(IDynamicContext context)
{
context.Env.IOC.Register<ViewManagement>();
context.Env.IOC.Register<FromWorkBenchViewModel>();
}
[NodeAction(NodeType.Action, "打开窗体(指定枚举值)")]
public void OpenForm(IDynamicContext context, FromValue fromId = FromValue.None, bool isTop = true)
{
var fromType = EnumHelper.GetBoundValue<FromValue, Type>(fromId, attr => attr.Value);
if (fromType is null) return;
if (context.Env.IOC.Instantiate(fromType) is Form form)
{
ViewManagement.OpenView(form, isTop);
}
}
[NodeAction(NodeType.Action, "打开窗体(使用转换器)")]
public void OpenForm2([EnumTypeConvertor(typeof(FromValue))] Form form, bool isTop = true)
{
ViewManagement.OpenView(form, isTop);
}
[NodeAction(NodeType.Action, "关闭窗体")]
public void CloseForm(IDynamicContext context, FromValue fromId = FromValue.None)
{
var fromType = EnumHelper.GetBoundValue<FromValue, Type>(fromId, attr => attr.Value);
if (fromType is null) return;
ViewManagement.CloseView(fromType);
}
}
}