Files
serein-flow/Net462DllTest/LogicControl/PlcLogicControl.cs
2024-10-07 15:15:18 +08:00

201 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using IoTClient.Clients.PLC;
using IoTClient.Common.Enums;
using Net462DllTest.Enums;
using Net462DllTest.Signal;
using Net462DllTest.Trigger;
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
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class AutoSocketAttribute : Attribute
{
public string BusinessField;
}
[AutoRegister]
[DynamicFlow("[SiemensPlc]")]
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>();
//context.Env.IOC.Register<SocketServer>();
//context.Env.IOC.Register<SocketClient>();
}
[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<SocketServer>(server => {
// server.Start(5000); // 开启 Socket 监听
//});
}
[NodeAction(NodeType.Exit)] // 流程结束时自动执行
public void Exit(IDynamicContext context)
{
context.Env.IOC.Run<WebServer>((web) =>
{
web?.Stop(); // 关闭 Web 服务
});
MyPlc.Close();
MyPlc.CancelAllTasks();
}
#endregion
#region
[NodeAction(NodeType.Flipflop, "等待变量更新", ReturnType = typeof(int))]
public async Task<IFlipflopContext> WaitTask(PlcVarName varName = PlcVarName.ErrorCode)
{
try
{
TriggerData triggerData = await MyPlc.CreateChannelWithTimeoutAsync(varName, TimeSpan.FromMinutes(120), 0);
if (triggerData.Type == TriggerType.Overtime)
{
throw new FlipflopException("超时取消");
}
await Console.Out.WriteLineAsync($"PLC变量触发器[{varName}]传递数据:{triggerData.Value}");
return new FlipflopContext(FlipflopStateType.Succeed, triggerData.Value);
}
catch (FlipflopException)
{
throw;
}
catch (Exception)
{
return new FlipflopContext(FlipflopStateType.Error);
}
}
#endregion
#region
[NodeAction(NodeType.Action, "PLC初始化")]
public SiemensPlcDevice PlcInit(SiemensVersion version = SiemensVersion.None,
string ip = "192.168.10.100",
int port = 102)
{
//MyPlc.Model.Set(PlcVarName.DoorVar,1);
//MyPlc.Model.Value.SpaceNum = 1;
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)
{
var oldState = MyPlc.State;
MyPlc.State = state;
Console.WriteLine($"PLC状态从[{oldState}]转为[{state}]");
return MyPlc;
}
[NodeAction(NodeType.Action, "PLC获取变量")]
public object ReadVar(PlcVarName plcVarEnum)
{
var varInfo = plcVarEnum.ToVarInfo();
var result = MyPlc.Read(varInfo);
Console.WriteLine($"获取变量成功:({varInfo})\t result = {result}");
return result;
}
[NodeAction(NodeType.Action, "PLC写入变量")]
public SiemensPlcDevice WriteVar2(object value, PlcVarName varName)
{
var varInfo = varName.ToVarInfo();
if (MyPlc.State == PlcState.Runing)
{
if (varInfo.IsReadOnly)
{
Console.WriteLine($"PLC变量{varInfo}当前禁止写入");
}
else
{
MyPlc.Write(varInfo, value);
Console.WriteLine($"PLC变量{varInfo}写入数据:{value}");
}
}
else
{
Console.WriteLine($"PLC处于非预期状态{MyPlc.State}");
}
return MyPlc;
}
[NodeAction(NodeType.Action, "批量读取")]
public void BatchReadVar()
{
MyPlc.BatchRefresh();
}
[NodeAction(NodeType.Action, "开启定时刷新")]
public void OpenTimedRefresh()
{
Task.Run(async () => await MyPlc.OpenTimedRefreshAsync());
}
[NodeAction(NodeType.Action, "关闭定时刷新")]
public void CloseTimedRefresh()
{
MyPlc.CloseTimedRefresh();
}
#endregion
}
}