using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.Serialization; using System.Collections.Concurrent; using System.ServiceModel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Net; using NLog; using NLog.Config; using NLog.Targets; using GZ.DB.Map.OIDATABASE; using GZ.DB.App.OIDATABASE; using GZ.DB.Repository.OIDATABASE; using GZ.DB.IRepository.OIDATABASE; using GZ.DB.Entity.OIDATABASE; namespace GZ.Projects.AuxAllWCS { public partial class ViewModel : System.ComponentModel.INotifyPropertyChanged { #region 私有变量 //集成互联 private Conn Conn = new Conn(); //设备通信 private Device Device = new Device(); //变量数据库 private Tag tag = new Tag(); public Tag Tag { get { return tag; } set { tag = value; RaisePropertyChanged(nameof(Tag)); } } //简化PLC public EasyPLC EasyPLC = new EasyPLC(); #endregion #region 构造函数 private static ViewModel _ViewModel = null; private static List _Windows = new List(); public static ViewModel CreateInstance(object obj, Type type) { if (_ViewModel == null) { _ViewModel = new ViewModel(); } if (!_Windows.Contains(obj)) { _Windows.Add(obj); } return _ViewModel; } public ViewModel() { } #endregion #region 自定义指令 public void Method1(System.Int32 param1) { #region [自定义指令][20250323111354905][Method1] #endregion [自定义指令][20250323111354905][Method1] } public void SettingInit() { #region [自定义指令][20250323144605974][SettingInit] try { var jsonFile = System.AppDomain.CurrentDomain.BaseDirectory + "/Static/config.json"; using (System.IO.StreamReader file = System.IO.File.OpenText(jsonFile)) { using (JsonTextReader reader = new JsonTextReader(file)) { JObject o = (JObject)JToken.ReadFrom(reader); foreach (Newtonsoft.Json.Linq.JProperty keyValue in o.Properties()) { Console.WriteLine(keyValue.Name); if (keyValue.Name == "HostToAgvServerUrl") { Settings.HostToAgvServerUrl = keyValue.Value.ToString(); } if (keyValue.Name == "deviceInfo") { Settings.deviceInfos = JsonConvert.DeserializeObject>(keyValue.Value.ToString()); } if (keyValue.Name == "HKbaseUrl") { Settings.HKbaseUrl = keyValue.Value.ToString(); } if (keyValue.Name == "MESbaseUrl") { Settings.MESbaseUrl = keyValue.Value.ToString(); } if (keyValue.Name == "WMSbaseUrl") { Settings.WMSbaseUrl = keyValue.Value.ToString(); } } } } //if(!Settings.FirstG) //LogHelper.Info("加载配置文件信息 完成"); } catch (Exception ex) { LogHelper.Error("加载配置文件失败!" + ex.Message, ex); } #endregion [自定义指令][20250323144605974][SettingInit] } public void OperateHKTaskStatus(System.Object param1) { #region [自定义指令][20250324165239206][OperateHKTaskStatus] HaiKangOrderInfo model = (HaiKangOrderInfo)param1; if (string.IsNullOrEmpty(model.taskCode)) return; // 查询任务:根据任务号 //var wcsTask = WCSHelper.GetTask(model.taskCode); ITaskRepository MainMissionService = new TaskRepository(); var wcsTask = MainMissionService.FindEntity(x => model.taskCode == x.S_TASK_NO); if (wcsTask != null) { if (!string.IsNullOrWhiteSpace(model.method)) { ILocRepository locrep = new LocRepository(); var startloc = locrep.FindEntity(x => x.S_LOC_CODE == wcsTask.S_START_LOC); var endLoc = locrep.FindEntity(x => x.S_LOC_CODE == wcsTask.S_END_LOC); List cntrs = wcsTask.S_CNTRS.Split(',').ToList(); bool bobe = false; //model.method = model.method?.ToLower(); switch (model.method) { case "start": LogHelper.Info($"海康车任务状态变更为执行,任务号为:{model.taskCode}", "agv杭叉交互日志"); if (wcsTask.S_B_STATE.Trim() == "已推送") { wcsTask.S_B_STATE = "执行中"; wcsTask.T_START_TIME = DateTime.Now; MainMissionService.Update(wcsTask); bobe = true; } break; case "outBin": case "outbin": model.method = model.method?.ToLower(); LogHelper.Info($"海康车任务状态变更为取货完成,任务号为:{model.taskCode}", "agv杭叉交互日志"); wcsTask.S_B_STATE = "取货完成"; startloc.N_CURRENT_NUM = 0; startloc.S_LOCK_STATE = "无"; startloc.T_MODIFY = DateTime.Now; //ILocContainerRepository locContainerRepository = new LocContainerRepository(); ////var cnr = locContainerRepository.FindList(x => cntrs.Contains(x.S_CNTR_CODE.Trim()) && x.S_LOC_CODE == wcsTask.S_START_LOC); //locContainerRepository.Delete(x => cntrs.Contains(x.S_CNTR_CODE.Trim()) && x.S_LOC_CODE == wcsTask.S_START_LOC); MainMissionService.Update(wcsTask); locrep.Update(startloc); bobe = true; var device = Settings.deviceInfos.Find(x => x.location.Contains(wcsTask.S_START_LOC)); if (device != null) { if (device.deviceType == 1) { if (device.deviceName.Contains("维希尔抓臂")) { //卸货进行清空,触发扫码 var inde = device.location.ToList().FindIndex(x => x == wcsTask.S_START_LOC) == 0; string V = inde ? "R05" : "R06"; Conn.默认Redis.SetValue(device.deviceName + "." + V, "true", device.deviceName + "Queue", false); TcpServer.TrayIps.Remove(device.deviceNo[inde ? 0 : 1]); } } else if (device.deviceType == 2) { //Task.Run(() => //{ // var inde = device.location.ToList().FindIndex(x => x == wcsTask.S_START_LOC) == 0; // string V = inde ? "D1224" : "D1225"; // Conn.默认Redis.SetValue(device.deviceName + "." + V, "0", device.deviceName + "Queue"); //}); } } //LocationHelper.UnLockLoc(wcsTask.S_START_LOC); break; case "end": LogHelper.Info($"海康车任务状态变更为完成,任务号为:{model.taskCode}", "agv杭叉交互日志"); wcsTask.S_B_STATE = "完成"; wcsTask.T_END_TIME = DateTime.Now; bobe = true; //ILocContainerRepository locContainerRepository2 = new LocContainerRepository(); //List llls = new List(); //foreach (var c in cntrs) //{ // var action = new LocContainerEntity() // { // S_ID = Guid.NewGuid().ToString(), // S_STATE = "编辑", // T_CREATE = DateTime.Now, // T_MODIFY = DateTime.Now, // S_LOC_CODE = endLoc.S_LOC_CODE, // S_CNTR_CODE = c, // S_TYPE = "", // }; // llls.Add(action); //} endLoc.S_LOCK_STATE = "无"; endLoc.N_CURRENT_NUM = 1; endLoc.T_MODIFY = DateTime.Now; MainMissionService.Update(wcsTask); if (Settings.deviceInfos.Find(x => x.deviceType == 2 && (/*x.location.Contains(wcsTask.S_END_LOC)||*/x.deviceNo.Contains(wcsTask.S_END_LOC))) != null) locrep.Update(endLoc); //locContainerRepository2.Insert(llls); var device2 = Settings.deviceInfos.Find(x => x.location.Contains(wcsTask.S_END_LOC)); if (device2 != null) { if (device2.deviceType == 1) { if (device2.deviceName.Contains("维希尔抓臂")) { //string V = device2.location.ToList().FindIndex(x => x == wcsTask.S_START_LOC) == 0 ? "R10" : "R11"; //Conn.默认Redis.SetValue(device2.deviceName + "." + V, "true", device2.deviceName + "Queue"); //卸货再写托盘取走。 然后触发扫码进行校验。 var inde = device2.location.ToList().FindIndex(x => x == wcsTask.S_END_LOC) == 0; //string V = inde ? "R05" : "R06"; //Conn.默认Redis.SetValue(device2.deviceName + "." + V, "true", device2.deviceName + "Queue"); string V2 = inde ? "R10_LAST" : "R11_LAST"; //Conn.默认Redis.SetValue(device2.deviceName + "." + V2, "true", device2.deviceName + "Queue"); GZ.Modular.Redis.RedisHelper.Add(device2.deviceName + "." + V2, "true", out string msg1); TcpServer.TrayIps.Remove(device2.deviceNo[inde ? 0 : 1]); } } else if (device2.deviceType == 2) { if (device2.deviceName.Contains("Js捷顺")) { Task.Run(() => { var inde = device2.location.ToList().FindIndex(x => x == wcsTask.S_END_LOC) == 0; string V = inde ? "D1224" : "D1225"; Conn.默认Redis.SetValue(device2.deviceName + "." + V, "1", device2.deviceName + "Queue"); }); var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/202", JsonConvert.SerializeObject(new { sceneType = 3, cntrCode = cntrs.FirstOrDefault() })); //if (device2.deviceName.Contains("1")) //{ // if (inde) tag.Js1.D1224 = 1; // else tag.Js1.D1225 = 1; //}else //{ // if (inde) tag.Js2.D1224 = 1; // else tag.Js2.D1225 = 1; //} } var ssloc = device2.deviceNo[1]; var endloc = locrep.FindEntity(x => x.S_LOC_CODE == ssloc); LogHelper.Info($"捷顺卸货后, 查看缓存位 {ssloc}-{endloc?.N_CURRENT_NUM}>0? 则申请mes搬走。"); if (endloc != null && endloc.N_CURRENT_NUM == 1) { var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/200", JsonConvert.SerializeObject(new { requestType = 3, startBit = ssloc })); LogHelper.Info($"{ssloc}申请mes任务结果{str}"); } } } //LocationMethod.JudgeLocation(wcsTask); break; case "cancel": LogHelper.Info($"海康车任务状态变更为取消,任务号为:{model.taskCode}", "agv杭叉交互日志"); wcsTask.S_B_STATE = "取消"; //startloc.S_LOCK_STATE = "无"; //endLoc.S_LOCK_STATE = "无"; MainMissionService.Update(wcsTask); TaskActRepository taskActRepository = new TaskActRepository(); var tare = taskActRepository.FindList(x => x.S_TASK_NO == wcsTask.S_TASK_NO); if (tare.Any()) { if (tare.Find(x => x.S_ACTION_CODE == "outbin") == null) { startloc.S_LOCK_STATE = "无"; endLoc.S_LOCK_STATE = "无"; locrep.Update(startloc); locrep.Update(endLoc); } else if (tare.Find(x => x.S_ACTION_CODE == "end") == null) { endLoc.S_LOCK_STATE = "无"; locrep.Update(endLoc); } } //locrep.Update(startloc); //locrep.Update(endLoc); //TaskProcess.OperateStatus(wcsTask, 7); //WCSHelper.Fail(wcsTask); break; case "apply": // 判断该任务的接驳位是否有其他入库或出库任务,如果有,则在等位等待,如果没有,则入 TaskisOk(wcsTask); break; } if (!string.IsNullOrEmpty(wcsTask.S_SRC_NO)) { var mestask = MainMissionService.FindEntity(x => x.S_TASK_NO == wcsTask.S_SRC_NO); if (mestask != null && bobe) { if (model.method != "end") { if (mestask.S_START_LOC == wcsTask.S_START_LOC && mestask.S_B_STATE == "MES") Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/201", JsonConvert.SerializeObject(new { reqCode = wcsTask.S_SRC_NO, model.method })); } else { if (mestask.S_END_LOC != wcsTask.S_END_LOC) { LogHelper.Info($"{wcsTask.S_SRC_NO}-{wcsTask.S_TASK_NO} 一阶段完成,重置进行二阶段。"); mestask.N_PRIORITY = -88; MainMissionService.Update(mestask); } else if (mestask.S_B_STATE == "MES") { var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/201", JsonConvert.SerializeObject(new { reqCode = wcsTask.S_SRC_NO, model.method })); } } } else if (mestask != null && model.method == "cancel") { if (mestask.S_B_STATE == "MES") Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/201", JsonConvert.SerializeObject(new { reqCode = wcsTask.S_SRC_NO, model.method })); LogHelper.Info($"{wcsTask.S_SRC_NO}-{wcsTask.S_TASK_NO} 一阶段取消,重置点,线程处理。"); mestask.N_PRIORITY = -1; mestask.S_B_STATE = "MES_取消"; MainMissionService.Update(mestask); } } TaskProcess.AddActionRecord(model.taskCode, model.method, model.eqpCode, model.data, wcsTask.S_START_LOC, wcsTask.S_END_LOC); //调用第三方接口(如果有)TaskProcess.ReportStatus } else { // 暂时设定为0 wcsTask.S_B_STATE = "上报状态空"; //安全请求等 //TaskProcess.OperateReq(model.taskCode, wcsTask.N_B_STATE, model.eqpCode, model.data); } } else { LogHelper.Info($"当前任务号任务不存在{model.taskCode} agv杭叉交互日志"); } #endregion [自定义指令][20250324165239206][OperateHKTaskStatus] } public GZ.DB.Entity.OIDATABASE.TaskEntity Begin(GZ.DB.Entity.OIDATABASE.TaskEntity task) { #region [自定义指令][20250324170811305][Begin] if (task.S_B_STATE.Trim() == "已推送") { task.S_B_STATE = "执行中"; task.T_START_TIME = DateTime.Now; } return task; #endregion [自定义指令][20250324170811305][Begin] } public GZ.DB.Entity.OIDATABASE.TaskEntity End(GZ.DB.Entity.OIDATABASE.TaskEntity task) { #region [自定义指令][20250324170922920][End] if (task != null) { task.S_B_STATE = "完成"; task.T_END_TIME = DateTime.Now; } return task; #endregion [自定义指令][20250324170922920][End] } public GZ.DB.Entity.OIDATABASE.TaskEntity UpdateStatus(GZ.DB.Entity.OIDATABASE.TaskEntity task, System.String status) { #region [自定义指令][20250324171118766][UpdateStatus] task.S_B_STATE = status; return task; #endregion [自定义指令][20250324171118766][UpdateStatus] } public void RunafterMac(System.Object dev, System.Boolean blen) { #region [自定义指令][20250325085905222][RunafterMac] var device = (deviceInfo)dev; if (device.areaPriy.Any() && device.deviceNo.Any()) { LocRepository locRepository = new LocRepository(); LocContainerRepository locContainerRepository = new LocContainerRepository(); ContainerRepository containerRepository = new ContainerRepository(); LocEntity startLoc = null; string Sign = device.deviceType == 1 ? "F" : "E"; foreach (var sloc in device.deviceNo) { ContainerEntity c = null; var Startloc = locRepository.FindEntity(x => x.S_LOC_CODE == sloc); if (Startloc != null && Startloc.N_CURRENT_NUM == 1 && Startloc.S_LOCK_STATE == "无") { var cntr = locContainerRepository.FindEntity(x => x.S_LOC_CODE == sloc); if (cntr != null) { c = containerRepository.FindEntity(x => x.S_CODE == cntr.S_CNTR_CODE); if (c != null && c.S_TYPE == Sign) { startLoc = Startloc; } } } if (startLoc != null) { foreach (var dn in device.areaPriy) { LocEntity endLoc = null; var di = Settings.deviceInfos.Find(x => x.deviceName == dn); if (di != null) { foreach (var ssloc in di.deviceNo) { var endloc = locRepository.FindEntity(x => x.S_LOC_CODE == ssloc); if (endloc != null && endloc.N_CURRENT_NUM == 0 && endloc.S_LOCK_STATE == "无") { endLoc = endloc; break; } } if (endLoc != null) { TaskProcess.CreateTask("", startLoc.S_LOC_CODE, endLoc.S_LOC_CODE, $"托盘流转-{Sign}", 5, new List { c.S_CODE }); break; } } } } } } #endregion [自定义指令][20250325085905222][RunafterMac] } public System.Boolean RunTask(GZ.DB.Entity.OIDATABASE.TaskEntity task) { #region [自定义指令][20250325092323531][RunTask] ILocRepository locrep = new LocRepository(); var startloc = locrep.FindEntity(x => x.S_LOC_CODE == task.S_START_LOC); var endLoc = locrep.FindEntity(x => x.S_LOC_CODE == task.S_END_LOC); if (startloc == null || endLoc == null) { LogHelper.Info($"{task.S_TASK_NO}-任务起/终点 没找到数据。"); return false; } var start = startloc.S_AGV_SITE; var end = endLoc.S_AGV_SITE; // 创建两个路径 var positions = new List(){ new positionCodePath() { positionCode = start, Type = "00" }, new positionCodePath() { positionCode = end, Type = "00" } }; //调第三方接口 var model = new HaiKangOrderInfo { reqCode = task.S_TASK_NO.Trim(), reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), taskCode = task.S_TASK_NO.Trim(), ctnrCode = task.S_CNTRS, taskTyp = task.S_INDEX, positionCodePath = positions }; return CreateOrder(model); #endregion [自定义指令][20250325092323531][RunTask] } public System.Boolean CreateOrder(System.Object m) { #region [自定义指令][20250325095053549][CreateOrder] var model = (HaiKangOrderInfo)m; var msg = ""; var result = false; var request = JsonConvert.SerializeObject(model); var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/genAgvSchedulingTask", request); msg = $"[haikang-CreateOrder] request={request} response={response}"; LogHelper.Info(msg); if (response != "") { try { var dataResult = JsonConvert.DeserializeObject(response); if (dataResult.code == "0") { result = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); LogHelper.Info(msg, "海康"); } } else { msg = "[haikang-CreateOrder]创建订单失败"; Console.WriteLine(msg); } LogHelper.Info(msg, "海康"); return result; #endregion [自定义指令][20250325095053549][CreateOrder] } public void TaskisOk(GZ.DB.Entity.OIDATABASE.TaskEntity task) { #region [自定义指令][20250325100336909][TaskisOk] // 车辆取放申请。 if ("取货申请" != "") { var device1 = Settings.deviceInfos.Find(x => x.location.Contains(task.S_START_LOC)); if (device1 != null) { var left = device1.location.ToList().FindIndex(x => x == task.S_START_LOC) == 0; if (device1.deviceType == 1) { if (device1.deviceName.Contains("维希尔抓臂")) { if ("IN_BEFORE" == "IN_BEFORE" || "out_BEFORE" == "out_BEFORE") { string V = left ? "R01" : "R03"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("IN_after" == "IN_after" || "out_after" == "out_after") { string V = left ? "R02" : "R04"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } } else if (device1.deviceName.Contains("Js捷顺")) { if ("IN_BEFORE" == "IN_BEFORE") { string V = left ? "M0" : "M1"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("out_BEFORE" == "out_BEFORE") { //string V = left ? "R01" : "R03"; //Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("IN_after" == "IN_after") { string V = left ? "M2" : "M3"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("out_after" == "out_after") { string V = left ? "M6" : "M7"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } } } } } if ("放货申请" != "") { var device1 = Settings.deviceInfos.Find(x => x.location.Contains(task.S_END_LOC)); if (device1 != null) { var left = device1.location.ToList().FindIndex(x => x == task.S_END_LOC) == 0; if (device1.deviceType == 1) { if (device1.deviceName.Contains("维希尔抓臂")) { if ("IN_BEFORE" == "IN_BEFORE" || "out_BEFORE" == "out_BEFORE") { string V = left ? "R01" : "R03"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("IN_after" == "IN_after" || "out_after" == "out_after") { string V = left ? "R02" : "R04"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } } else if (device1.deviceName.Contains("Js捷顺")) { if ("IN_BEFORE" == "IN_BEFORE") { string V = left ? "M0" : "M1"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("out_BEFORE" == "out_BEFORE") { string V = left ? "M4" : "M5"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("IN_after" == "IN_after") { string V = left ? "M2" : "M3"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } if ("out_after" == "out_after") { string V = left ? "M6" : "M7"; Conn.默认Redis.SetValue(device1.deviceName + "." + V, "true", device1.deviceName + "Queue"); } } } } } #endregion [自定义指令][20250325100336909][TaskisOk] } public System.Boolean continueTask(System.Object m) { #region [自定义指令][20250325100607380][continueTask] var model = (HaiKangOrderInfo)m; bool result = false; string msg = ""; var request = JsonConvert.SerializeObject(model); var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/continueTask", request); msg = $"[haikang-CancelOrder] request={request};response={response}"; Console.WriteLine(msg); if (response != "") { var dataResult = JsonConvert.DeserializeObject(response); if (dataResult.code == "0") { result = true; } } else { msg = "[haikang-CancelOrder]获取任务状态失败"; Console.WriteLine(msg); } LogHelper.Info(msg, "海康"); return result; #endregion [自定义指令][20250325100607380][continueTask] } public System.Boolean CancelOrder(System.Object m) { #region [自定义指令][20250325100705351][CancelOrder] var model = (HaiKangOrderInfo)m; bool result = false; string msg = ""; var request = JsonConvert.SerializeObject(model); var response = Settings.apiHelper.Post(Settings.HKbaseUrl + "rcms/services/rest/hikRpcService/continueTask", request); msg = $"[haikang-CancelOrder] request={request};response={response}"; Console.WriteLine(msg); if (response != "") { var dataResult = JsonConvert.DeserializeObject(response); if (dataResult.code == "0") { result = true; } } else { msg = "[haikang-CancelOrder]取消订单失败"; Console.WriteLine(msg); } LogHelper.Info(msg, "海康"); return result; #endregion [自定义指令][20250325100705351][CancelOrder] } public System.String GetSerialNumber(System.String snType, System.String prefix) { #region [自定义指令][20250325131530407][GetSerialNumber] int result = 0; return ""; #endregion [自定义指令][20250325131530407][GetSerialNumber] } #endregion } }