hwh
2024-08-30 47ed1ba420ed01ea50e7f9a09247abd0deb531e1
Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
@@ -1,10 +1,20 @@
using Admin.NET.Core.Service;
using Furion.Logging;
using Microsoft.AspNetCore.SignalR;
namespace WCS.Application;
public static class PLCTaskAction
{
    //服务运行状态
    public static bool boRunningState = false;
    //脱机模式
    public static bool boOffline = false;
    //自刷新
    public static bool boRefresh = false;
    private static readonly ISqlSugarClient _db = SqlSugarSetup.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
    private static readonly SysCacheService sysCacheService = App.GetRequiredService<SysCacheService>();
    private static readonly IHubContext<PlcHub, IPlcHub> _plcHubContext = App.GetService<IHubContext<PlcHub, IPlcHub>>();
    private static List<WcsPlc> listPlc;
    private static List<WcsDevice> listPlcDevice;
@@ -31,7 +41,7 @@
        if (listPlcUtil.Count != 0)
        {
            cts.Cancel();
            listPlc = _db.Queryable<WcsPlc>().ToList();
            listPlc = _db.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.StackingMachine || s.Type == PLCTypeEnum.ConveyorLine || s.Type == PLCTypeEnum.BoxConveyorLine).ToList();
            listPlcDevice = _db.Queryable<WcsDevice>().ToList();
            listPlcStation = _db.Queryable<WcsPosition>().ToList();
            //等待几秒钟,把已有线程取消掉再连接
@@ -48,6 +58,7 @@
            listPlcUtil.Add(plc);
        }
        cts = new CancellationTokenSource();
        boRunningState = true;
        StartRead();
    }
    /// <summary>
@@ -121,11 +132,65 @@
            }, cts.Token);
        }
    }
    /// <summary>
    /// 连接状态线程
    /// </summary>
    public static void ConnectionStatus()
    {
        Task.Run(() =>
        {
            try
            {
                //取消线程 关闭PLC连接
                if (cts.Token.IsCancellationRequested)
                {
                    foreach (var modPlcUtil in listPlcUtil)
                    {
                        modPlcUtil.Close();
                    }
                    throw new OperationCanceledException();
                }
                //获取每个PLC连接状态
                foreach (var modPlc in listPlc)
                {
                    var modPlcUtil = listPlcUtil.FirstOrDefault(s => s.PlcId == modPlc.Id);
                    if (modPlcUtil == null)
                        modPlc.IsConn = false;
                    else
                        modPlc.IsConn = modPlcUtil.Connected;
                    if (sysCacheService.ExistKey("PLCCONN" + modPlc.Id))
                    {
                        var cachePlc = sysCacheService.Get<WcsPlc>("PLCCONN" + modPlc.Id);
                        if (cachePlc.IsConn != modPlc.IsConn)
                        {
                            //连接状态变更 通知前端
                            _plcHubContext.Clients.All.PublicPlcConn(modPlc);
                        }
                    }
                    sysCacheService.Set("PLCCONN" + modPlc.Id, modPlc);
                }
                Thread.Sleep(3000);
            }
            catch (OperationCanceledException)
            {
                sysCacheService.RemoveByPrefixKey("PLCCONN");
                Console.WriteLine("中止线程");
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }
        });
    }
    /// <summary>
    /// 停止服务
    /// </summary>
    public static void Stop()
    {
        cts.Cancel();
        boRunningState = false;
    }
}