bklLiudl
2024-09-11 5af0a043ffbbe444f7e43b6bf52a9e94c74283f2
Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
@@ -19,6 +19,7 @@
    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 readonly SysConfigService _sysConfigService = App.GetService<SysConfigService>();
    private static List<WcsPlc> listPlc = new List<WcsPlc>();
    private static List<WcsDevice> listPlcDevice = new List<WcsDevice>();
@@ -32,11 +33,20 @@
    {
        get { return listPlcUtil; }
    }
    // 交互点集合
    public static List<WcsDevice> plcDevices
    {
        get { return listPlcDevice; }
    }
    public static event EventHandler DeviceValueChangeEvent;
    static PLCTaskAction()
    {
        //订阅事件
        DeviceValueChangeEvent += PLCService.OnChangeEvent;
        boRunningState = _sysConfigService.GetConfigValue<bool>("sys_RunningState").Result;
        boOffline = _sysConfigService.GetConfigValue<bool>("sys_Offline").Result;
        boRefresh = _sysConfigService.GetConfigValue<bool>("sys_Refresh").Result;
    }
    /// <summary>
    /// 初始化PLC连接
@@ -60,18 +70,20 @@
            var plc = new PLCUtil(modPlc);
            listPlcUtil.Add(plc);
        }
        cts = new CancellationTokenSource();
        boRunningState = true;
        _plcHubContext.Clients.All.UpdateService(new PLCServiceModel()
        {
            BoRunningState = boRunningState,
            BoOffline = boOffline,
            BoRefresh = boRefresh
        });
        StartRead();
        ConnectionStatus();
        StartWatchAlarm();
        StartWatchPosition();
        if (boRunningState)
        {
            cts = new CancellationTokenSource();
            StartRead();
            ConnectionStatus();
            StartWatchAlarm();
            StartWatchPosition();
        }
    }
    /// <summary>
    /// 开启读取plc线程
@@ -139,7 +151,7 @@
                                dto.listStation = listPlcStation.Where(s => s.DeviceId == modDevice.Id).ToList();
                                dto.listDevice = listDevice.Where(s => s.StationNum == modDevice.StationNum && s.Level == DeviceLevelEnum.Station).ToList();
                                //这里触发值变更事件
                                //DeviceValueChangeEvent?.Invoke(dto, EventArgs.Empty);
                                DeviceValueChangeEvent?.Invoke(dto, EventArgs.Empty);
                            }
                            else if (!modPlcUtil.Connected)
                            {