Files
wcs/WCS.BLL/Manager/WarningManager.cs
2024-06-11 22:25:02 +08:00

290 lines
12 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.OscarClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.BLL.DbModels;
using WCS.BLL.HardWare;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model.ApiModel.InOutRecord;
using WCS.Model.WebSocketModel;
namespace WCS.BLL.Manager
{
/// <summary>
/// 报警信息
/// </summary>
public static class WarningManager
{
public static object flag = new object();
public static void StartWarningMessageThread()
{
Task.Run(() =>
{
while (true)
{
try
{
var noReceivedWarningMessage = Warnings.Where(t => t.ClientIsReceived == false && t.LastSendTime < DateTime.Now.AddSeconds(-5))
.ToList();
foreach (var warning in noReceivedWarningMessage)
{
WebSoceketManager.TrySendMessage(warning.ClientIp, JsonConvert.SerializeObject(warning));
warning.LastSendTime = DateTime.Now;
}
}
catch (Exception ex)
{
}
Thread.Sleep(5000);
}
});
}
public static List<WebSocketMessageModel> Warnings { get; set; } = new List<WebSocketMessageModel>();
public static void SendWarning(WebSocketMessageModel warning)
{
lock (flag)
{
Warnings.Add(warning);
WebSoceketManager.TrySendMessage(warning.ClientIp, JsonConvert.SerializeObject(warning));
warning.LastSendTime = DateTime.Now;
}
}
public static void ClearWarning(WebSocketMessageModel warning, SolveTypeEnum solveType)
{
try
{
var shelfIsWarning = true;
var warningInManager = Warnings.Where(t => t.Guid == warning.Guid).FirstOrDefault();
if (warningInManager != null)
{
if (solveType == SolveTypeEnum.)
{
//TODO 对应报警处理对应的数据
switch (warningInManager.WarningType)
{
case WarningTypeEnum.:
SolveLoss(warningInManager);
break;
case WarningTypeEnum.:
SolveLoss(warningInManager);
break;
case WarningTypeEnum.:
SolveLoss(warningInManager);
break;
}
}
else if (solveType == SolveTypeEnum.)
{
//不发指令了
}
//消除报警缓存信息
lock (flag)
{
Warnings.Remove(warningInManager);
}
}
//货架是否还存在报警信息
shelfIsWarning = Warnings.Where(t => t.ShelfId == warning.ShelfId)
.Any();
//对应货架如果不存在报警信息了 指示灯回到对应的状态
if (!shelfIsWarning)
{
var shelf = ShelfManager.Shelves.Where(t => t.ShelfId == warning.ShelfId)
.FirstOrDefault();
if (shelf != null)
{
try
{
var smartShelf = shelf as SmartShelf;
smartShelf?.ClearWarning();
smartShelf.IsWarning = false;
}
catch (Exception ex)
{
}
}
}
#region
GoInRightMode(warning);
#endregion
}
catch (Exception e)
{
Logs.Write($"消除报警信息失败:发生异常" + e.Message);
}
}
public static void RemoveMessage(WebSocketMessageModel warning)
{
var warningInManager = Warnings.Where(t => t.Guid == warning.Guid).FirstOrDefault();
lock (flag)
{
Warnings.Remove(warningInManager);
}
}
#region
public static void SolveNoScan(WebSocketMessageModel warning)
{
}
public static void SolveLoss(WebSocketMessageModel warning)
{
#region
try
{
//获取库位
var storeInfo = DbHelp.db.Queryable<StoreInfo>()
.Where(t => t.ShelfId == warning.ShelfId)
.Where(t => t.Id == warning.StoreId)
.First();
if (storeInfo != null)
{
DbHelp.db.BeginTran();
//库位表字段清空
storeInfo.CurrentMatSn = string.Empty;
DbHelp.db.Updateable(storeInfo).ExecuteCommand();
//库存表记录删除、插入出入记录
var inventoryDetail = DbHelp.db.Queryable<InventoryDetail>().Where(t => t.StoreId == storeInfo.Id).First();
if (inventoryDetail != null)
{
var inOutRecord = new InOutRecord()
{
StoreCode = storeInfo.StoreCode,
StoreId = storeInfo.Id,
StoreInfo = storeInfo,
R = storeInfo.R,
C = storeInfo.C,
Wei = storeInfo.Wei,
BigShelfCode = storeInfo.BigShelfCode,
GroupName = storeInfo.GroupName,
MatSN = inventoryDetail.MatSN,
MatCode = inventoryDetail.MatCode,
MatName = inventoryDetail.MatName,
MatBatch = inventoryDetail.MatBatch,
MatQty = inventoryDetail.MatQty,
MatSpec = inventoryDetail.MatSpec,
MatCustomer = inventoryDetail.MatCustomer,
MatSupplier = inventoryDetail.MatSupplier,
Direction = DirectionEnum.,
OperateUser = warning.SolvedUser,
};
DbHelp.db.Insertable(inOutRecord).ExecuteCommand();
DbHelp.db.Deleteable(inventoryDetail).ExecuteCommand();
#region
if (warning.WarningType == WarningTypeEnum.)
{
Logs.Write($"【出库自检】发现物料{inventoryDetail.MatSN}丢失,用户点击【处理】,删除数据",LogsType.Outstore);
//清掉需要出库的缓存
var shelf = ShelfManager.Shelves
.Where(t => t.ShelfId == warning.ShelfId)
.FirstOrDefault();
if (shelf != null)
{
var smartShelf = shelf as SmartShelf;
if (smartShelf != null)
{
//删除货架上缓存的那一条数据
smartShelf.CurrentOutStoreMatSNs.RemoveAll(t => t == inventoryDetail.MatSN);
Logs.Write($"【出库自检】发现物料{inventoryDetail.MatSN}丢失,已删除货架缓存,剩余物料为{string.Join(",", smartShelf.CurrentOutStoreMatSNs)}", LogsType.Outstore);
//删除模组上缓存的那一条数据
var module = smartShelf.Modules.Where(t => t.ModuleId == warning.ModuleId).FirstOrDefault();
if (module != null) {
module.CurrentOutSns.RemoveAll(t => t == inventoryDetail.MatSN);
Logs.Write($"【出库自检】发现物料{inventoryDetail.MatSN}丢失,已删除模组缓存,剩余物料为{string.Join(",", module.CurrentOutSns)}", LogsType.Outstore);
}
//删除已丢失的出库明细数据
var outOrderMatDetail = DbHelp.db.Queryable<OutOrderMatDetail>()
.Where(t => t.OrderId == smartShelf.CurrentOutOrder.Id)
.Where(t => t.MatSN == inventoryDetail.MatSN && t.IsSended == false)
.ToList();
Logs.Write($"【出库自检】发现物料{inventoryDetail.MatSN}丢失,删除出库物料明细数据{outOrderMatDetail.Count}条!");
DbHelp.db.Deleteable(outOrderMatDetail).ExecuteCommand();
}
}
}
#endregion
}
DbHelp.db.CommitTran();
}
}
catch (Exception e)
{
DbHelp.db.RollbackTran();
}
#endregion
}
public static void GoInRightMode(WebSocketMessageModel warning)
{
try
{
//模组还存在其他异常 暂时不进入对应模式
var moduleOtherError = WarningManager.Warnings.Where(t => t.ShelfId == warning.ShelfId && t.ModuleId == warning.ModuleId).Any();
if (moduleOtherError)
{
return;
}
var shelf = ShelfManager.Shelves.Where(t => t.ShelfId == warning.ShelfId)
.FirstOrDefault();
if (shelf != null)
{
var smartShelf = shelf as SmartShelf;
if (smartShelf != null)
{
var module = smartShelf.Modules.Where(t => t.ModuleId == warning.ModuleId).FirstOrDefault();
if (module != null)
{
switch (smartShelf.CurrentMode)
{
case Mode.:
module.GoInInstoreMode(smartShelf.TcpCleint);
smartShelf.WarningLight.BlueLight(smartShelf.TcpCleint);
break;
case Mode.:
//module.GoInOutStoreMode(smartShelf.TcpCleint, module.CurrentOutSns);
//smartShelf.WarningLight.GreenLight(smartShelf.TcpCleint);
smartShelf.GoInOutstoreByWebSocket(module.ModuleId);
break;
case Mode.:
module.Reset(smartShelf.TcpCleint);
smartShelf.WarningLight.CloseLight(smartShelf.TcpCleint);
break;
default: break;
}
}
}
}
}
catch (Exception e)
{
Logs.Write("重置模组状态异常" + e.Message);
}
}
#endregion
}
}