From 4efebef143f1ed702bc5bc1c29349e2a7f2af414 Mon Sep 17 00:00:00 2001
From: hwh <332078369@qq.com>
Date: 星期一, 02 九月 2024 08:17:26 +0800
Subject: [PATCH] Merge branch 'master' into hwh
---
Admin.NET/WCS.Application/PLC/PLCTaskAction.cs | 91 ++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 81 insertions(+), 10 deletions(-)
diff --git a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
index 3be04f1..47c28e2 100644
--- a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
+++ b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs
@@ -1,13 +1,24 @@
锘縰sing 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;
private static List<WcsPlc> listPlc;
private static List<WcsDevice> listPlcDevice;
- private static List<WcsStation> listPlcStation;
+ private static List<WcsPosition> listPlcStation;
private static List<PLCUtil> listPlcUtil = new List<PLCUtil>();
private static CancellationTokenSource cts;//鍙栨秷绾跨▼鏍囪瘑
@@ -20,7 +31,7 @@
listPlc = _db.Queryable<WcsPlc>().ToList();
listPlcDevice = _db.Queryable<WcsDevice>().ToList();
- listPlcStation = _db.Queryable<WcsStation>().ToList();
+ listPlcStation = _db.Queryable<WcsPosition>().ToList();
}
/// <summary>
/// 鍒濆鍖朠LC杩炴帴
@@ -30,9 +41,9 @@
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<WcsStation>().ToList();
+ listPlcStation = _db.Queryable<WcsPosition>().ToList();
//绛夊緟鍑犵閽燂紝鎶婂凡鏈夌嚎绋嬪彇娑堟帀鍐嶈繛鎺�
Thread.Sleep(5000);
foreach (var modPlcUtil in listPlcUtil)
@@ -47,6 +58,7 @@
listPlcUtil.Add(plc);
}
cts = new CancellationTokenSource();
+ boRunningState = true;
StartRead();
}
/// <summary>
@@ -80,21 +92,22 @@
}
var listDevice = listPlcDevice.Where(s => s.PlcId == _modplc.Id).ToList();
//寰幆璇昏澶�
- foreach (var modDevice in listDevice)
+ foreach (var modDevice in listDevice.Where(s => s.Level == DeviceLevelEnum.DB))
{
- var (result, value) = modPlcUtil.GetPlcDBValue(modDevice.PosType.Value, modDevice.DbNumber, modDevice.PlcPos);
+ var (result, value) = modPlcUtil.GetPlcDBValue(modDevice.PosType, modDevice.DbNumber, modDevice.PlcPos);
if (result.IsSucceed)
{
- //if (value != 0)
- //{
+ //鏃犳祦绋嬭烦鍑�
+ if (value == 0)
+ continue;
var dto = modDevice.Adapt<WcsDeviceDto>();
dto.Value = value;
dto.Type = _modplc.Type;
dto.PLCUtil = modPlcUtil;
dto.listStation = listPlcStation.Where(s => s.DeviceId == modDevice.Id).ToList();
+ dto.listDevice = listDevice.Where(s => s.StationNum == modDevice.StationNum).ToList();
//杩欓噷瑙﹀彂鍊煎彉鏇翠簨浠�
DeviceValueChangeEvent?.Invoke(dto, EventArgs.Empty);
- //}
}
else
{
@@ -107,13 +120,70 @@
Thread.Sleep(3000);
}
+ catch (OperationCanceledException)
+ {
+ Console.WriteLine("涓绾跨▼");
+ }
catch (Exception ex)
{
-
+ Log.Error(ex.Message, ex);
}
}
}, 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>
/// 鍋滄鏈嶅姟
@@ -121,5 +191,6 @@
public static void Stop()
{
cts.Cancel();
+ boRunningState = false;
}
}
\ No newline at end of file
--
Gitblit v1.8.0