chengsc
2024-09-12 4683c8a19e593e8f999c6f1a145e0bb73eb20dea
Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
@@ -13,16 +13,19 @@
    public static bool boOffline = false;
    //自刷新
    public static bool boRefresh = false;
    //演示模式
    public static bool boDemo = false;
    public static List<PlcPositionInfo> listPositionInfo = new List<PlcPositionInfo>();
    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>();
    private static List<WcsPosition> listPlcStation = new List<WcsPosition>();
    private static List<WcsPosition> listPlcPosition = new List<WcsPosition>();
    private static List<WcsAlarmInfo> listAlarmInfo = new List<WcsAlarmInfo>();
    private static List<PLCUtil> listPlcUtil = new List<PLCUtil>();
@@ -32,11 +35,24 @@
    {
        get { return listPlcUtil; }
    }
    // 交互点集合
    public static List<WcsDevice> plcDevices
    {
        get { return listPlcDevice; }
    }
    public static List<WcsPosition> plcPositions
    {
        get { return listPlcPosition; }
    }
    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;
        boDemo = _sysConfigService.GetConfigValue<bool>("sys_demo").Result;
    }
    /// <summary>
    /// 初始化PLC连接
@@ -46,7 +62,7 @@
        cts.Cancel();
        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();
        listPlcPosition = _db.Queryable<WcsPosition>().ToList();
        listAlarmInfo = _db.Queryable<WcsAlarmInfo>().ToList();
        //等待几秒钟,把已有线程取消掉再连接
        //Thread.Sleep(5000);
@@ -60,18 +76,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线程
@@ -136,10 +154,10 @@
                                dto.Value = value;
                                dto.Type = _modplc.Type;
                                dto.PLCUtil = modPlcUtil;
                                dto.listStation = listPlcStation.Where(s => s.DeviceId == modDevice.Id).ToList();
                                dto.listStation = listPlcPosition.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)
                            {