267 lines
10 KiB
C#
267 lines
10 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;
|
|
}
|
|
}
|
|
|
|
//消除报警缓存信息
|
|
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.出库自检丢失)
|
|
{
|
|
//清掉需要出库的缓存
|
|
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);
|
|
//TODO 如何保证丢失的继续进行
|
|
}
|
|
}
|
|
}
|
|
#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);
|
|
break;
|
|
case Mode.待机模式:
|
|
module.Reset(smartShelf.TcpCleint);
|
|
smartShelf.WarningLight.CloseLight(smartShelf.TcpCleint);
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logs.Write("重置模组状态异常" + e.Message);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|