| | |
| | | using Furion.Logging; |
| | | using Admin.NET.Core.Service; |
| | | using Furion.Logging; |
| | | using Microsoft.AspNetCore.SignalR; |
| | | |
| | | namespace WCS.Application; |
| | |
| | | { |
| | | //服务运行状态 |
| | | 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; |
| | | |
| | | private static List<WcsPlc> listPlc; |
| | | private static List<WcsDevice> listPlcDevice; |
| | |
| | | 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(); |
| | | //等待几秒钟,把已有线程取消掉再连接 |
| | |
| | | }, 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> |