使用emit代替表达式树构造委托。

内置了websocket server与相应的导航功能,可在实例工程中找到相应的实现。
This commit is contained in:
fengjiayi
2024-10-10 10:45:53 +08:00
parent 0bab770f0a
commit d1b9a3f28f
43 changed files with 1953 additions and 392 deletions

View File

@@ -1,8 +1,6 @@
using IoTClient.Clients.PLC;
using IoTClient.Common.Enums;
using IoTClient.Common.Enums;
using Net462DllTest.Enums;
using Net462DllTest.Model;
using Net462DllTest.Signal;
using Net462DllTest.Trigger;
using Net462DllTest.Web;
using Serein.Library.Api;
@@ -10,25 +8,14 @@ using Serein.Library.Attributes;
using Serein.Library.Enums;
using Serein.Library.Ex;
using Serein.Library.Framework.NodeFlow;
using Serein.Library.Network.WebSocketCommunication;
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
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class AutoSocketAttribute : Attribute
{
public string BusinessField;
}
[AutoRegister]
[DynamicFlow("[SiemensPlc]")]
@@ -48,33 +35,44 @@ namespace Net462DllTest.LogicControl
[NodeAction(NodeType.Init)]
public void Init(IDynamicContext context)
{
context.Env.IOC.Register<IRouter, Router>();
context.Env.IOC.Register<WebServer>();
context.Env.IOC.Register<WebSocketServer>();
context.Env.IOC.Register<WebSocketClient>();
//context.Env.IOC.Register<SocketServer>();
//context.Env.IOC.Register<SocketClient>();
context.Env.IOC.Register<IRouter, Router>();
context.Env.IOC.Register<WebApiServer>();
}
[NodeAction(NodeType.Loading)] // Loading 初始化完成已注入依赖项,可以开始逻辑上的操作
public void Loading(IDynamicContext context)
{
// 注册控制器
context.Env.IOC.Run<IRouter, WebServer>((router, web) => {
router.RegisterController(typeof(CommandController));
web.Start("http://*:8089/"); // 开启 Web 服务
context.Env.IOC.Run<IRouter, WebApiServer>((router, apiServer) => {
router.RegisterController(typeof(FlowController));
apiServer.Start("http://*:8089/"); // 开启 Web Api 服务
});
//context.Env.IOC.Run<SocketServer>(server => {
// server.Start(5000); // 开启 Socket 监听
//});
context.Env.IOC.Run<WebSocketServer>(async (socketServer) => {
// socketServer.RegisterModuleInstance(userService);
await socketServer.StartAsync("http://localhost:5005/"); // 开启 Web Socket 监听
});
context.Env.IOC.Run<WebSocketClient>(async client => {
await client.ConnectAsync("ws://localhost:5005/"); // 连接到服务器
});
}
[NodeAction(NodeType.Exit)] // 流程结束时自动执行
public void Exit(IDynamicContext context)
{
context.Env.IOC.Run<WebServer>((web) =>
context.Env.IOC.Run<WebApiServer>((apiServer) =>
{
web?.Stop(); // 关闭 Web 服务
apiServer?.Stop(); // 关闭 Web 服务
});
context.Env.IOC.Run<WebSocketServer>((socketServer) =>
{
socketServer?.Stop(); // 关闭 Web 服务
});
MyPlc.Close();
MyPlc.CancelAllTasks();

View File

@@ -1,5 +1,6 @@
using Net462DllTest.Signal;
using Net462DllTest.Trigger;
using Net462DllTest.ViewModel;
using Serein.Library.Api;
using Serein.Library.Attributes;
@@ -15,41 +16,12 @@ using System.Threading.Tasks;
using System.Windows.Forms;
namespace Net462DllTest.LogicControl
{
{
/// <summary>
/// 视图管理
/// </summary>
[AutoRegister]
public class ViewManagement:ChannelFlowTrigger<CommandSignal>
{
private readonly 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("[View]")]
public class ViewLogicControl
{
@@ -67,10 +39,10 @@ namespace Net462DllTest.LogicControl
{
try
{
TriggerData triggerData = await ViewManagement.CreateChannelWithTimeoutAsync(command, TimeSpan.FromMinutes(120), 0);
TriggerData triggerData = await ViewManagement.CreateChannelWithTimeoutAsync(command, TimeSpan.FromHours(10), 0);
if (triggerData.Type == TriggerType.Overtime)
{
throw new FlipflopException("超时取消");
return new FlipflopContext(FlipflopStateType.Cancel, triggerData.Value);
}
return new FlipflopContext(FlipflopStateType.Succeed, triggerData.Value);
}

View File

@@ -42,6 +42,9 @@
<Reference Include="IoTClient, Version=1.0.40.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IoTClient.1.0.40\lib\netstandard2.0\IoTClient.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
@@ -72,6 +75,7 @@
<Compile Include="Enums\FromValue.cs" />
<Compile Include="Signal\CommandSignal.cs" />
<Compile Include="Signal\PLCVarSignal.cs" />
<Compile Include="Trigger\ViewManagement.cs" />
<Compile Include="Utils\GSModel.cs" />
<Compile Include="Utils\RelayCommand.cs" />
<Compile Include="ViewModel\FromWorkBenchViewModel.cs" />
@@ -87,7 +91,8 @@
<Compile Include="View\TestFormView.Designer.cs">
<DependentUpon>TestFormView.cs</DependentUpon>
</Compile>
<Compile Include="Web\CommandController.cs" />
<Compile Include="Web\FlowController.cs" />
<Compile Include="Web\UserService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\DynamicControl\SereinFlow\Library.Framework\Serein.Library.Framework.csproj">

View File

@@ -0,0 +1,49 @@
using Net462DllTest.Signal;
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.NodeFlow.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Net462DllTest.Trigger
{
/// <summary>
/// 视图管理
/// </summary>
[AutoRegister]
public class ViewManagement : ChannelFlowTrigger<CommandSignal>
{
public ViewManagement(IFlowEnvironment environment)
{
}
public int Id = new Random().Next(1, 10000);
private readonly 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);
}
}
}
}

View File

@@ -43,6 +43,7 @@
this.button1.TabIndex = 0;
this.button1.Text = "查看状态";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBoxPlcInfo
//
@@ -60,6 +61,7 @@
this.button2.TabIndex = 2;
this.button2.Text = "触发";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// listBoxCommand
//
@@ -90,6 +92,7 @@
this.Controls.Add(this.button1);
this.Name = "FromWorkBenchView";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FromWorkBenchView_FormClosing);
this.Load += new System.EventHandler(this.FromWorkBenchView_Load);
this.ResumeLayout(false);
this.PerformLayout();

View File

@@ -23,8 +23,8 @@ namespace Net462DllTest
public FromWorkBenchView(IFlowEnvironment env)
{
InitializeComponent();
ViewModel = env.IOC.Get<FromWorkBenchViewModel>(); // 获取对象
if(ViewModel is null)
ViewModel = env.IOC.Get<FromWorkBenchViewModel>();
if (ViewModel is null)
{
Console.WriteLine("创建对象并注入依赖项");
ViewModel = env.IOC.Instantiate<FromWorkBenchViewModel>();
@@ -41,14 +41,25 @@ namespace Net462DllTest
listBoxCommand.DataBindings.Add(nameof(listBoxCommand.SelectedItem), ViewModel, nameof(ViewModel.SelectedSignal), false, DataSourceUpdateMode.OnPropertyChanged);
listBoxCommand.SelectedIndexChanged += (s, e) => listBoxCommand.DataBindings[nameof(listBoxCommand.SelectedItem)].WriteValue();
button1.Click += (s, e) => ViewModel.CommandViewPlcInfo.Execute();
button2.Click += (s, e) => ViewModel.CommandGetParkingSpace.Execute();
}
private void FromWorkBenchView_Load(object sender, EventArgs e)
{
}
private void FromWorkBenchView_FormClosing(object sender, FormClosingEventArgs e)
{
ViewModel.CommandCloseForm.Execute();
}
private void button2_Click(object sender, EventArgs e)
{
ViewModel.CommandGetParkingSpace.Execute();
}
private void button1_Click(object sender, EventArgs e)
{
ViewModel.CommandViewPlcInfo.Execute();
}
}
}

View File

@@ -13,26 +13,123 @@ using System.Threading.Tasks;
using System.Windows.Input;
using Net462DllTest.LogicControl;
using Net462DllTest.Model;
using Serein.Library.Network.WebSocketCommunication;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
namespace Net462DllTest.ViewModel
{
public class FromWorkBenchViewModel : INotifyPropertyChanged
public class LibSpace
{
public string SpaceNum { get; set; }
public string PlateNumber { get; set; }
public override string ToString()
{
return $"{nameof(SpaceNum)}{SpaceNum}{nameof(PlateNumber)}{PlateNumber}";
}
}
[AutoSocketModule(JsonThemeField = "theme", JsonDataField = "data")]
public class FromWorkBenchViewModel : INotifyPropertyChanged, ISocketControlBase
{
public Guid HandleGuid { get; } = new Guid();
private readonly SiemensPlcDevice Device;
private readonly ViewManagement viewManagement;
private readonly PlcVarModelDataProxy plcVarModelDataProxy;
public FromWorkBenchViewModel(SiemensPlcDevice Device,
ViewManagement viewManagement,
PlcVarModelDataProxy plcVarModelDataProxy)
PlcVarModelDataProxy plcVarModelDataProxy,
WebSocketServer webSocketServer)
{
this.Device = Device;
this.viewManagement = viewManagement;
this.plcVarModelDataProxy = plcVarModelDataProxy;
InitCommand(); // 初始化指令
webSocketServer.MsgHandleHelper.AddModule(this); // 用于关闭窗体时移除监听
CommandCloseForm = new RelayCommand((p) =>
{
webSocketServer.MsgHandleHelper.RemoteModule(this); // 用于关闭窗体时移除监听
});
InitCommand();
//Console.WriteLine($"Emit {sw.ElapsedTicks * 1000000F / Stopwatch.Frequency:n3}μs");
//var msgHandl = new WsMsgHandl<LibSpace>()
//{
// DataJsonKey = "data", // 数据键
// ThemeJsonKey = "theme", // 主题键
//};
//msgHandl.AddHandle<LibSpace>(nameof(GetSpace), GetSpace); // theme:AddUser
//msgHandl.AddHandle<LibSpace>(nameof(UpData), UpData); // theme:DeleteUser
//webSocketServer.AddModule(msgHandl);
//CommandCloseForm = new RelayCommand((p) =>
//{
// webSocketServer.RemoteModule(msgHandl); // 用于关闭窗体时移除监听
//});
}
Action<string> Recover;
[AutoSocketHandle(ThemeValue = "SavaRecover")]
public async Task SavaRecover(int waitTime , Action<string> Recover)
{
if(waitTime <=0 || waitTime>= 10000)
{
return;
}
await Task.Delay(waitTime);
Recover("haha~" );
this.Recover = Recover;
return;
}
[AutoSocketHandle(ThemeValue = "Invoke")]
public void Invoke(string a)
{
if (Recover is null)
return;
Recover("haha~"+a);
}
[AutoSocketHandle(ThemeValue = "PlcModel")]
public async Task<PlcVarModelDataProxy> GetModelData(Action<object> Recover)
{
Recover("等待5秒");
await Task.Delay(5000);
//Recover.Invoke(plcVarModelDataProxy);
return plcVarModelDataProxy;
}
[AutoSocketHandle(ThemeValue = "Up")]
public void UpData(LibSpace libSpace, Action<dynamic> Recover)
{
Recover("收到");
return;
}
[AutoSocketHandle(ThemeValue = "Bind")]
public LibSpace SpaceBind(string spaceNum, string plateNumber, Action<string> Recover)
{
return new LibSpace
{
PlateNumber = plateNumber,
SpaceNum = spaceNum,
};
}
#region
@@ -98,7 +195,11 @@ namespace Net462DllTest.ViewModel
/// 调取车位
/// </summary>
public RelayCommand CommandGetParkingSpace { get; private set; }
/// <summary>
/// 关闭窗体
/// </summary>
public RelayCommand CommandCloseForm { get; private set; }
public void InitCommand()
{
CommandViewPlcInfo = new RelayCommand((p) =>
@@ -109,6 +210,7 @@ namespace Net462DllTest.ViewModel
{
viewManagement.TriggerSignal(SelectedSignal, SpcaeNumber);
});
}

View File

@@ -1,56 +0,0 @@
using Net462DllTest.Enums;
using Net462DllTest.Signal;
using Net462DllTest.Trigger;
using Serein.Library.Attributes;
using Serein.Library.Utils;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Net462DllTest.Web
{
[AutoHosting]
public class CommandController : ControllerBase
{
private readonly SiemensPlcDevice plcDevice;
public CommandController(SiemensPlcDevice plcDevice)
{
this.plcDevice = plcDevice;
}
/*
* 类型 POST
* url : http://127.0.0.1:8089/command/trigger?command=
* body [JSON]
*
* {
* "value":0,
* }
*/
[WebApi(API.POST)]
public dynamic Trigger([Url] string var, int value)
{
if (EnumHelper.TryConvertEnum<PlcVarName>(var,out var signal))
{
Console.WriteLine($"外部触发 {signal} 信号,信号内容 {value} ");
plcDevice.TriggerSignal(signal, value);// 通过 Web Api 模拟外部输入信号
return new { state = "succeed" };
}
else
{
return new { state = "fail" };
}
}
}
}

View File

@@ -0,0 +1,85 @@
using Net462DllTest.Enums;
using Net462DllTest.Signal;
using Net462DllTest.Trigger;
using Serein.Library.Attributes;
using Serein.Library.Utils;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Net462DllTest.Web
{
[AutoHosting]
public class FlowController : ControllerBase
{
private readonly SiemensPlcDevice plcDevice;
private readonly ViewManagement viewManagement;
public FlowController(SiemensPlcDevice plcDevice, ViewManagement viewManagement)
{
this.plcDevice = plcDevice;
this.viewManagement = viewManagement;
}
/*
* 类型 POST
* url : http://127.0.0.1:8089/flow/plcop?var=
* url : http://127.0.0.1:8089/flow/plcop?var=SpaceNum
* body [JSON]
*
* {
* "value":0,
* }
*/
[WebApi(API.POST)]
public dynamic PlcOp([Url] string var, int value)
{
if (EnumHelper.TryConvertEnum<PlcVarName>(var,out var signal))
{
Console.WriteLine($"外部触发 {signal} 信号,信号内容 {value} ");
plcDevice.TriggerSignal(signal, value);// 通过 Web Api 模拟外部输入信号
return new { state = "succeed" };
}
else
{
return new { state = "fail" };
}
}
/*
* 类型 POST
* url : http://127.0.0.1:8089/flow/trigger?command=
* url : http://127.0.0.1:8089/flow/trigger?command=Command_1
* body [JSON]
*
* {
* "value":0,
* }
*/
[WebApi(API.POST)]
public dynamic Trigger([Url] string command, int value)
{
if (EnumHelper.TryConvertEnum<CommandSignal>(command, out var signal))
{
Console.WriteLine($"外部触发 {signal} 信号,信号内容 {value} ");
viewManagement.TriggerSignal(signal, value);// 通过 Web Api 模拟外部输入信号
return new { state = "succeed" };
}
else
{
return new { state = "fail" };
}
}
}
}

View File

@@ -0,0 +1,15 @@
using Net462DllTest.ViewModel;
using Serein.Library.Attributes;
using Serein.Library.Network.WebSocketCommunication;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Net462DllTest.Web
{
}

View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.IO.Ports" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0"/>
<assemblyIdentity name="System.IO.Ports" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" /></startup></configuration>

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="IoTClient" version="1.0.40" targetFramework="net461" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net462" />
<package id="System.IO.Ports" version="4.6.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
</packages>