lss
7 小时以前 1319cb7072623dff12369a889af766db2d166def
HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/process/DeviceProcess.cs
@@ -10,6 +10,8 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Services.Description;
using static HH.WCS.JiaTong.LISTA.process.HttpModel;
using static System.Collections.Specialized.BitVector32;
@@ -48,7 +50,7 @@
        /// <summary>
        /// 叠盘机空托返回
        /// </summary>    
        internal static void Traystacker(string loc, int qty, int type,string cntrcode="")
        internal static void Traystacker(string loc, int qty, int type, string cntrcode = "")
        {
            try
            {
@@ -106,8 +108,8 @@
                        var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                        if (cntr.Count > 0)
                        {
                             cntrcode = cntr.OrderBy(a=>a.T_CREATE).First().S_CNTR_CODE;
                           // cntrcode = string.Join(",", cntrcodes);
                            cntrcode = cntr.OrderBy(a => a.T_CREATE).First().S_CNTR_CODE;
                            // cntrcode = string.Join(",", cntrcodes);
                        }
                        //创建过未完成的任务不发起请求
@@ -169,7 +171,8 @@
        }
        public static Dictionary<string, DpjErrorInfo> DpjInfoDirectory = new Dictionary<string, DpjErrorInfo>();
        //public static Dictionary<string, DpjErrorInfo> DpjInfoDirectory = new Dictionary<string, DpjErrorInfo>();
        public static Dictionary<string, DpjInfo> DpjInfoDirectory = new Dictionary<string, DpjInfo>();
        /// <summary>
        /// 井松叠盘机叠盘回库
        /// </summary>
@@ -184,80 +187,275 @@
                {
                    foreach (var device in devices)
                    {
                        var IsDp = S7Helper.ReadDpj(device.address, 2001, S7.Net.VarType.Byte);
                        var value = S7Helper.ReadDpj(device.address, 2014, S7.Net.VarType.Int);
                        var ErrorCode = S7Helper.ReadDpj(device.address, 2016, S7.Net.VarType.Int);
                        //记录叠盘机报错信息
                        if (DpjInfoDirectory.ContainsKey(device.address))
                        {
                            DpjInfoDirectory[device.address].Address = device.address;
                            DpjInfoDirectory[device.address].DeviceName = device.deviceName;
                            var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                            if (ErrorInfo != null)
                            {
                                DpjInfoDirectory[device.address].ErrorInfo = ErrorInfo.ErrorInfo;
                            }
                        }
                        else
                        {
                            string errorinfo = "";
                            var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                            if (ErrorInfo != null)
                            {
                                errorinfo = ErrorInfo.ErrorInfo;
                            }
                            DpjInfoDirectory.Add(device.address, new DpjErrorInfo() { Address = device.address, DeviceName = device.deviceName, ErrorInfo = errorinfo });
                        }
                        if (value >= 1 && IsDp == 1)
                        {
                            //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                            //if (cntr.Count > 0)
                            //{
                            //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                            //    cntrcode = string.Join(",", cntrcodes);
                            //}
                            //创建过未完成的任务不发起请求
                            var mst = WCSHelper.GetTaskByStartloc(device.TN_Location);
                            if (mst == null)
                            {
                                Traystacker(device.TN_Location, value, 2);
                            }
                            else
                            {
                                LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                            }
                            //var loc = LocationHelper.GetLocByLoc(device.TN_Location);
                            //if (loc != null)
                            //{
                            //    if (loc.N_LOCK_STATE == 0) {
                            //        Traystacker(device.TN_Location, value, 2);
                            //    }
                            //    else
                            //    {
                            //        LogHelper.Info($"设备起点:{device.TN_Location}有锁,不生成任务");
                            //    }
                            //}
                            //else
                            //{
                            //    LogHelper.Info($"根据设备:{device.deviceName},起点:{device.TN_Location},未找到货位");
                            //}
                        }
                        else
                        try
                        {
                            LogHelper.Info($"叠盘机数量:{value},叠盘信号:{IsDp},不触发叠盘任务");
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTraffic1(device);
                            });
                        }
                        Thread.Sleep(8000);
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.deviceName}({device.address})处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
        }
        /// <summary>
        /// 叠盘机线程读取
        /// </summary>
        internal static void DpjTrafficRead()
        {
            var devices = Settings.deviceInfos.Where(a => a.deviceType == 1).ToList();
            //var db = new SqlHelper<object>().GetInstance();
            //var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0  && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            try
            {
                if (devices.Count > 0)
                {
                    foreach (var device in devices)
                    {
                        try
                        {
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTrafficRead(device);
                            });
                        }
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.deviceName}({device.address})处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
        }
        internal static void DpjCreateTask()
        {
            var devices = DpjInfoDirectory;
            //var db = new SqlHelper<object>().GetInstance();
            //var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0  && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            try
            {
                if (devices.Count > 0)
                {
                    foreach (var device in devices.Values)
                    {
                        try
                        {
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTraffic2(device);
                            });
                        }
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.Address}处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
        }
        internal static void DpjTrafficRead(Settings.deviceInfo device)
        {
            while (true)
            {
                try
                {
                    var IsDp = S7Helper.ReadDpj(device.address, 2001, S7.Net.VarType.Byte);
                    var value = S7Helper.ReadDpj(device.address, 2014, S7.Net.VarType.Int);
                    int value2004 = S7Helper.ReadDpj(device.address, 2004, S7.Net.VarType.Byte);
                    LogHelper.Info($"读取叠盘机{device.address},叠盘信号{IsDp},数量{value},安全交互信号{value2004}");
                    if (DpjInfoDirectory.ContainsKey(device.address))
                    {
                        DpjInfoDirectory[device.address].Address = device.address;
                        DpjInfoDirectory[device.address].Loc = device.TN_Location;
                        DpjInfoDirectory[device.address].PickUp = IsDp;
                        DpjInfoDirectory[device.address].qty = value;
                        DpjInfoDirectory[device.address].value2004 = value2004;
                    }
                    else
                    {
                        DpjInfoDirectory.Add(device.address, new DpjInfo() { Address = device.address, Loc = device.TN_Location, PickUp = IsDp, qty = value, value2004 = value2004 });
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
                }
            }
        }
        internal static void DpjTraffic1(Settings.deviceInfo device)
        {
            try
            {
                var IsDp = S7Helper.ReadDpj(device.address, 2001, S7.Net.VarType.Byte);
                var value = S7Helper.ReadDpj(device.address, 2014, S7.Net.VarType.Int);
               // int value2004 = S7Helper.ReadDpj(device.address, 2004, S7.Net.VarType.Byte);
                //var ErrorCode = S7Helper.ReadDpj(device.address, 2016, S7.Net.VarType.Int);
                ////记录叠盘机报错信息
                //if (DpjInfoDirectory.ContainsKey(device.address))
                //{
                //    DpjInfoDirectory[device.address].Address = device.address;
                //    DpjInfoDirectory[device.address].DeviceName = device.deviceName;
                //    var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                //    if (ErrorInfo != null)
                //    {
                //        DpjInfoDirectory[device.address].ErrorInfo = ErrorInfo.ErrorInfo;
                //    }
                //}
                //else
                //{
                //    string errorinfo = "";
                //    var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                //    if (ErrorInfo != null)
                //    {
                //        errorinfo = ErrorInfo.ErrorInfo;
                //    }
                //    DpjInfoDirectory.Add(device.address, new DpjErrorInfo() { Address = device.address, DeviceName = device.deviceName, ErrorInfo = errorinfo });
                //}
                if (value >= 1 && IsDp == 1)
                {
                    //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                    //if (cntr.Count > 0)
                    //{
                    //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                    //    cntrcode = string.Join(",", cntrcodes);
                    //}
                    //创建过未完成的任务不发起请求
                    var mst = WCSHelper.GetTaskByStartloc(device.TN_Location);
                    if (mst == null)
                    {
                        Traystacker(device.TN_Location, value, 2);
                    }
                    else
                    {
                        LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                    }
                    //var loc = LocationHelper.GetLocByLoc(device.TN_Location);
                    //if (loc != null)
                    //{
                    //    if (loc.N_LOCK_STATE == 0) {
                    //        Traystacker(device.TN_Location, value, 2);
                    //    }
                    //    else
                    //    {
                    //        LogHelper.Info($"设备起点:{device.TN_Location}有锁,不生成任务");
                    //    }
                    //}
                    //else
                    //{
                    //    LogHelper.Info($"根据设备:{device.deviceName},起点:{device.TN_Location},未找到货位");
                    //}
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
        }
        internal static void DpjTraffic2(DpjInfo dpj)
        {
            try
            {
                LogHelper.Info($"叠盘机{dpj.Address},叠盘信号{dpj.PickUp},数量{dpj.qty}");
                if (dpj.qty >= 1 && dpj.PickUp == 1)
                {
                    //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                    //if (cntr.Count > 0)
                    //{
                    //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                    //    cntrcode = string.Join(",", cntrcodes);
                    //}
                    //创建过未完成的任务不发起请求
                    var mst = WCSHelper.GetTaskByStartloc(dpj.Loc);
                    if (mst == null)
                    {
                        Traystacker(dpj.Loc, dpj.qty, 2);
                    }
                    else
                    {
                        LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                    }
                    //var loc = LocationHelper.GetLocByLoc(device.TN_Location);
                    //if (loc != null)
                    //{
                    //    if (loc.N_LOCK_STATE == 0) {
                    //        Traystacker(device.TN_Location, value, 2);
                    //    }
                    //    else
                    //    {
                    //        LogHelper.Info($"设备起点:{device.TN_Location}有锁,不生成任务");
                    //    }
                    //}
                    //else
                    //{
                    //    LogHelper.Info($"根据设备:{device.deviceName},起点:{device.TN_Location},未找到货位");
                    //}
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
        }
        /// <summary>
@@ -275,7 +473,7 @@
                result1.data = traystacker;
                HttpHelper httpHelper = new HttpHelper();
                //调用接口
                string date = JsonConvert.SerializeObject(result1);
                string url = Settings.tableUrls.Find(a => a.id == 11).url;
                LogHelper.Info($"空托返回:地址:{url},内容:{date}");
@@ -306,5 +504,15 @@
            public string DeviceName { get; set; }
            public string ErrorInfo { get; set; }
        }
        public class DpjInfo
        {
            public string Address { get; set; }
            public string Loc { get; set; }
            public int PickUp { get; set; }
            public int qty { get; set; }
            public int value2004 { get; set; }
        }
    }
}