123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using PlcSiemens.Core.Extension;
- using ServiceCenter.Extensions;
- using ServiceCenter.Logs;
- using ServiceCenter.SqlSugars;
- using SqlSugar;
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using WCS.Core;
- using WCS.Entity;
- using WCS.Entity.Protocol.BCR;
- using WCS.Entity.Protocol.Station;
- using WCS.WorkEngineering.Extensions;
- using WCS.WorkEngineering.Model.WMS;
- using WCS.WorkEngineering.Worlds;
- using TaskStatus = WCS.Entity.TaskStatus;
- namespace WCS.WorkEngineering.Systems
- {
- /// <summary>
- /// 满轮主线预写入目标地址
- /// </summary>
- [BelongTo(typeof(SortingMainLineWorld))]
- [Description("满轮主线预写入目标地址")]
- public class 满轮主线预写入目标地址 : DeviceSystem<Device<IStation523, IBCR83, IStation525>>
- {
- protected override bool ParallelDo => true;
- private static Dictionary<string, string> CodeMap { get; set; } = new Dictionary<string, string>();
- private static ConcurrentDictionary<string, fjSysConfig> FlowConfig { get; set; } = new ConcurrentDictionary<string, fjSysConfig>();
- public 满轮主线预写入目标地址()
- {
- // 映射 Code 到 devCode 的映射关系
- CodeMap = new Dictionary<string, string>
- {
- ["18"] = "22",
- ["38"] = "41",
- ["58"] = "61",
- ["118"] = "122",
- ["138"] = "141",
- ["158"] = "161"
- };
- }
- public override void Do(Device<IStation523, IBCR83, IStation525> obj)
- {
- try
- {
- if (!CodeMap.TryGetValue(obj.Entity.Code, out var devCode))
- {
- World.Log($"设备号[{obj.Entity.Code}]未初始化对应关系", LogLevelEnum.High);
- return;
- }
- var dev = new Device<IStation520>(Device.All.First(x => x.Code == devCode), World);
- dev.Data.Mode = StationMode.Automatic;
- //从DB83中获取待处理条码组
- var pendingBcrList = obj.Data2.GetBcrCodeList().Where(x => !x.IsNullOrEmpty()).ToList();
- //从DB525获取已处理条码组
- var cacheBcrList = obj.Data3.GetBcrCodeList().ToList();
- //筛选出未处理条码组
- var codes = pendingBcrList.Where(x => !cacheBcrList.Contains(x)).ToList();
- fjSysConfig configFlowError = null;
- //循环处理所有缓存条码组中没有的条码
- foreach (var bcrCode in codes)
- {
- WCS_TaskInfo taskInfo = null;
- fjSysConfig config = null;
- SqlSugarHelper.Do(_db =>
- {
- var db = _db.Default;
- taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.BarCode == bcrCode && x.Status == TaskStatus.NewBuild);
- if (taskInfo == null)
- {
- LogAndRecordError(bcrCode, "找不到匹配的任务", obj.Entity.Code);
- return;
- }
- var key = taskInfo.WarehouseCode + "-Flow";
- if (!FlowConfig.TryGetValue(key, out config))
- {
- config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
- FlowConfig.AddOrUpdate(key, config, (k, v) => config);
- }
- if (!db.Queryable<fjSysConfig>().NoLock().Any(x => x.Code == key && x.SContent == config.SContent))
- {
- config = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == key) ?? new fjSysConfig { SContent = "4" };
- FlowConfig.AddOrUpdate(key, config, (k, v) => config);
- }
- //获取排异错误
- if (configFlowError == null)
- {
- configFlowError = db.Queryable<fjSysConfig>().NoLock().First(x => x.Code == "FlowErrorAllocation") ?? new fjSysConfig { SContent = "3" };
- }
- });
- if (taskInfo == null)
- {
- dev.Data.Mode = StationMode.Automatic;
- continue;
- }
- var srmCode = taskInfo.WarehouseCode.WarehouseToSrm();
- var flowError = false;
- //北侧主线
- if (obj.Entity.Code == "18" || obj.Entity.Code == "38" || obj.Entity.Code == "58")
- {
- if (taskInfo.WarehouseCode.Contains("S"))
- {
- flowError = true;
- }
- }
- //南侧主线
- else if (obj.Entity.Code == "118" || obj.Entity.Code == "138" || obj.Entity.Code == "158")
- {
- if (taskInfo.WarehouseCode.Contains("N"))
- {
- flowError = true;
- }
- }
- var path = DevicePath.GetPath(obj.Entity.Code, srmCode);
- if ((path == null || path is { Points.Count: < 2 }) && !flowError)
- {
- var msg = $"{bcrCode}:路径错误,当前位置{obj.Entity.Code},目标位置:{srmCode}";
- LogAndRecordError(bcrCode, msg, obj.Entity.Code);
- dev.Data.Mode = StationMode.Automatic;
- continue;
- }
- var next = path != null ? path.Points[1].Code : devCode;
- #region 计算应该去哪个分拣库
- //应该要去的分拣库
- if (taskInfo.WarehouseCode.Contains("N"))
- {
- next = config.SContent switch
- {
- "1" => "418",
- "2" => "818",
- "3" => "1218",
- _ => next
- };
- }
- else if (taskInfo.WarehouseCode.Contains("S"))
- {
- next = config.SContent switch
- {
- "1" => "618",
- "2" => "1018",
- "3" => "1418",
- _ => next
- };
- }
- if (flowError)
- {
- if (taskInfo.WarehouseCode.Contains("N"))
- {
- next = configFlowError.SContent switch
- {
- "1" => "618",
- "2" => "1018",
- "3" => "1418",
- _ => next
- };
- }
- else if (taskInfo.WarehouseCode.Contains("S"))
- {
- next = configFlowError.SContent switch
- {
- "1" => "418",
- "2" => "818",
- "3" => "1218",
- _ => next
- };
- }
- }
- #endregion 计算应该去哪个分拣库
- //开始赋值
- SetStation525Value(obj.Data3, "TaskNumber", taskInfo.ID);
- SetStation525Value(obj.Data3, "GoodsEnd", next.ToShort());
- SetStation525Value(obj.Data3, "BcrCode", bcrCode);
- obj.Data3.NextIndex = (short)(obj.Data3.NextIndex >= 49 ? 0 : obj.Data3.NextIndex + 1);
- dev.Data.Mode = StationMode.Automatic;
- }
- dev.Data.Mode = StationMode.Automatic;
- }
- catch (Exception e)
- {
- World.Log($"{e.Message}:---{e.StackTrace}", LogLevelEnum.High);
- }
- }
- public override bool Select(Device dev)
- {
- var codes = new List<string>();
- if (WorkStart.WareHouses.Contains("FJ1")) codes.AddRange(new List<string>() { "18", "118" });
- if (WorkStart.WareHouses.Contains("FJ2")) codes.AddRange(new List<string>() { "38", "58" });
- if (WorkStart.WareHouses.Contains("FJ3")) codes.AddRange(new List<string>() { "138", "158" });
- return codes.Contains(dev.Code);
- }
- private void LogAndRecordError(string barcode, string message, string memo)
- {
- World.Log($"{barcode}:{message}", LogLevelEnum.High);
- var errorInfo = new BaseErrorInfo()
- {
- BusName = "主线分流",
- BarCode = barcode,
- Message = message,
- Count = 1,
- AddTime = DateTime.Now,
- Memo = memo
- };
- errorInfo.AddBaseErrorInfo();
- }
- /// <summary>
- /// 设置站525的值
- /// </summary>
- /// <param name="data3"></param>
- /// <param name="propertyName"></param>
- /// <param name="value"></param>
- private void SetStation525Value(IStation525 data3, string propertyName, object value)
- {
- var property = data3.GetType().GetProperty($"{propertyName}{data3.NextIndex}");
- if (property != null && property.CanWrite)
- {
- property.SetValue(data3, value);
- property.SetValue(data3, value);
- }
- }
- }
- /// <summary>
- /// 主线分流
- /// </summary>
- public class MainlineDiversion
- {
- /// <summary>
- /// 任务号
- /// </summary>
- public int TaskId { get; set; }
- /// <summary>
- /// 仓库号
- /// </summary>
- public string WarehouseCode { get; set; }
- }
- }
|