| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 | using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection.Emit;using System.Text;using SqlSugar;using WCS.Data;using WCS.Data.Models;using WCS.Data.Utils;using WCS.PLC.Model.Equipment;using WCS.WMSWorkflow;namespace WCS.PLC{    public enum TaskTypeEnum    {        TaskType_InStorage = 1,        TaskType_OutStorage = 2,        TaskType_StockTransfer = 3,        TaskType_Carry = 4,        TaskType_Move = 5,        TaskType_TrayExceptionBack = 6    }    public enum SrmModeEnum    {        远程 = 1,        半自动 = 2,        手动 = 3,        维修 = 4    }    public enum SrmStateFork1Enum    {        空闲 = 1,        定位 = 2,        取货 = 3,        放货 = 4,        维护 = 99    }    public enum ComTypeEnum    {        入库 = 1,        出库 = 2,        移库 = 3,        搬运 = 4,        移动 = 5,        托盘异常退回 = 6    }    public enum WkStatus    {        未下发 = 0,        未执行 = 1,        输送机执行 = 2,        RGV上料执行 = 3,        RGV下料执行 = 4,        堆垛机执行 = 5,        堆垛机完成 = 6,        RGV执行中 = 7,        RGV移动 = 8,        RGV完成 = 9,        任务已下发AGV = 10,        AGV取货完成输送中 = 11,        AGV任务完成 = 12,        熟化房熟化中 = 13,        熟化房熟化完成=14,        完成任务 = 99,        取消任务 = 106,        手动过账 = 109    }    public class Current: BaseCurrent    {        public static string LED_DEFAULTMESSAGE = "欢迎光临博众精工科技股份有限公司";        /// <summary>        /// 当前任务列表        /// </summary>        public static List<WCS_TASK> TaskSet = new List<WCS_TASK>();        /// <summary>        /// AGV任务列表        /// </summary>        public static List<Middle_AGVTask> AgvTaskSet = new List<Middle_AGVTask>();        /// <summary>        /// 熟化架缓存位信息        /// </summary>        public static List<Middle_CachePostionInfo> AgvCachePostionSet = new List<Middle_CachePostionInfo>();        /// <summary>        /// 需要手动生成任务的工位清单        /// </summary>        public static List<WCS_TASKTIMEPOINT> WCS_TASKTIMEPOINTSet = new List<WCS_TASKTIMEPOINT>();        /// <summary>        /// plc列表        /// </summary>        public static List<WCS_PLC> PlcSet = new List<WCS_PLC>();        /// <summary>        /// 设备路由列表        /// </summary>        public static List<WCS_EQUIPMENTROUTE> EquRouteSet = new List<WCS_EQUIPMENTROUTE>();        /// <summary>        ///  堆垛机出入口输送线列表        /// </summary>        public static List<WCS_SrmOutInInfo> WCS_SrmOutInInfoSet = new List<WCS_SrmOutInInfo>();        /// <summary>        /// 缓存区信息        /// </summary>        public static List<WCS_CacheInfo> WCS_CacheInfoSet = new List<WCS_CacheInfo>();        /// <summary>        /// 系统设置信息        /// </summary>        public static List<WCS_SYSSET> SysSets = new List<WCS_SYSSET>();        /// <summary>        /// RGV对接口列表        /// </summary>        public static List<WCS_RGVOutInInfo> WCS_RGVOutInInfoSet = new List<WCS_RGVOutInInfo>();        /// <summary>        /// WMS接口        /// </summary>        public static IWMS WmsInterface { get; set; }        /// <summary>        /// WCS扩展接口        /// </summary>        public static IWCS WcsInterface { get; set; }        /// <summary>        /// 原料仓库编号        /// </summary>        public static readonly string WareHouseId = "pvchouseyuanmo";        /// <summary>        /// PVC普通立体库        /// </summary>        public static readonly string Pvchouseputong = "pvchouseputong";        public static readonly string PvchouseputongCh = "PVC普通立库";        /// <summary>        /// 仓库        /// </summary>        public static List<string> WareNameList { get; set; }        public static void InitData(string wcssystem)        {            try            {                DeleteLog(30);                WareNameList = new List<string>() { "PVC原膜立库", "PVC普通立库" };                PlcSet = SugarBase.DB.Queryable<WCS_PLC>().Where(v => v.PLC_WCSSYSTEM == wcssystem).ToList();                EquRouteSet = SugarBase.DB.Queryable<WCS_EQUIPMENTROUTE>().ToList();                WCS_SrmOutInInfoSet = SugarBase.DB.Queryable<WCS_SrmOutInInfo>().ToList().Where(v => PlcSet.Any(t => t.PLC_NAME == v.SRMOUTIN_CONVPLCNAME)).ToList();                WCS_RGVOutInInfoSet = SugarBase.DB.Queryable<WCS_RGVOutInInfo>().ToList().Where(v => PlcSet.Any(t => t.PLC_NAME == v.PLCNAME)).ToList();                SysSets = SugarBase.DB.Queryable<WCS_SYSSET>().ToList();                var sysset_wms = SysSets.FirstOrDefault(v => v.SET_TYPE == "WmsInterface" && v.SET_VALUE == "1");                WmsInterface = (IWMS)Activator.CreateInstance(Type.GetType(sysset_wms.SET_NAME));                var sysset_wcs = SysSets.FirstOrDefault(v => v.SET_TYPE == "WcsInterface" && v.SET_VALUE == "1");                WcsInterface = (IWCS)Activator.CreateInstance(Type.GetType(sysset_wcs.SET_NAME));                foreach (var plc in PlcSet)                {                    for (int i = 0; i < plc.PLC_INSTANCECOUNT; i++)                    {                        plc.PlcInstanceSet.Add(null);                    }                    plc.WCS_DBSet = SugarBase.DB.Queryable<WCS_DBDEFINITION>().Where(v => v.DB_PLCNAME == plc.PLC_NAME).ToList();                    plc.WCS_ScannSet = SugarBase.DB.Queryable<WCS_SCANN>().Where(v => v.SCANN_PLCNAME == plc.PLC_NAME).OrderBy(v => v.SCANN_SEQUENCE).ToList();                    plc.WCS_EquipmentInfoSet = SugarBase.DB.Queryable<WCS_EQUIPMENTINFO>().Where(v => v.Equ_PlcName == plc.PLC_NAME).OrderBy(v => v.Equ_Sequence).ToList();                    foreach (var item in plc.WCS_EquipmentInfoSet)                    {                        var equDBInfoSet = GetConvDBInfo(plc, item);                        if (item.Equ_Type.ToLower().Trim() == EquipmentType.conveyor.ToString())                        {                            item.EquSignal = new ConvSignal(item.Equ_PlcName, equDBInfoSet);                        }                        else if (item.Equ_Type.ToLower().Trim() == EquipmentType.srm.ToString())                        {                            item.EquSignal = new Model.Equipment.SrmSignal(item.Equ_PlcName, equDBInfoSet);                        }                        else if (item.Equ_Type.ToLower().Trim() == EquipmentType.rgv.ToString())                        {                            item.EquSignal = new Model.Equipment.RGVSignal(item.Equ_PlcName, equDBInfoSet);                        }                    }                    plc.Init();                }            }            catch (Exception ex)            {                Log4netHelper.Logger_Error.ErrorFormat(ex.ToString());            }        }        private static List<ConvDbInfo> GetConvDBInfo(WCS_PLC plc, WCS_EQUIPMENTINFO item)        {            List<ConvDbInfo> equDBInfoSet = new List<ConvDbInfo>();            var dbSet = plc.WCS_DBSet.Where(v => v.DB_PLCNAME == item.Equ_PlcName).ToList();            foreach (var DB in dbSet)            {                if (DB.DB_TYPE == (int)DB_TypeEnum.WCS可读可写DB)                {                    if (string.IsNullOrWhiteSpace(item.Equ_ReadWriteDB_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS可读可写DB;                    convdb.DBReadLength = item.Equ_ReadWriteDB_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_ReadWriteDB_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = temp_sequence * item.Equ_ReadWriteDB_Length;// plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_ReadWriteDB_Sequence) == false && Convert.ToInt32(v.Equ_ReadWriteDB_Sequence) < temp_sequence).Sum(v => v.Equ_ReadWriteDB_Length);                        convdb.DBReadIndox = Totallength;                    }                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS只读DB)                {                    if (string.IsNullOrWhiteSpace(item.Equ_ReadDB_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS只读DB;                    convdb.DBReadLength = item.Equ_ReadDB_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_ReadDB_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = temp_sequence * item.Equ_ReadDB_Length;//plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_ReadDB_Sequence) == false && Convert.ToInt32(v.Equ_ReadDB_Sequence) < temp_sequence).Sum(v => v.Equ_ReadDB_Length);                        convdb.DBReadIndox = Totallength;                    }                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS状态和故障DB)                {                    if (string.IsNullOrWhiteSpace(item.Equ_StatusDB_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS状态和故障DB;                    convdb.DBReadLength = item.Equ_StatusDB_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_StatusDB_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = temp_sequence * item.Equ_StatusDB_Length;//plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_StatusDB_Sequence) == false && Convert.ToInt32(v.Equ_StatusDB_Sequence) < temp_sequence).Sum(v => v.Equ_StatusDB_Length);                        convdb.DBReadIndox = Totallength;                    }                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS读取扫码DB)                {                    if (string.IsNullOrWhiteSpace(item.Equ_BarCodeDB_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS读取扫码DB;                    convdb.DBReadLength = item.Equ_BarCodeDB_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_BarCodeDB_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_BarCodeDB_Sequence) == false && Convert.ToInt32(v.Equ_BarCodeDB_Sequence) < temp_sequence).Sum(v => v.Equ_BarCodeDB_Length);                        convdb.DBReadIndox = Totallength;                    }                    convdb.ScannSet = plc.WCS_ScannSet.Where(v => v.SCANN_STATIONNO == item.Equ_No).OrderBy(v => v.SCANN_SEQUENCE).ToList();                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS读写扩展DB1)                {                    if (string.IsNullOrWhiteSpace(item.Equ_ExtendOne_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS读写扩展DB1;                    convdb.DBReadLength = item.Equ_ExtendOne_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_ExtendOne_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_ExtendOne_Sequence) == false && Convert.ToInt32(v.Equ_ExtendOne_Sequence) < temp_sequence).Sum(v => v.Equ_ExtendOne_Length);                        convdb.DBReadIndox = Totallength;                    }                    convdb.ScannSet = plc.WCS_ScannSet.Where(v => v.SCANN_STATIONNO == item.Equ_No).OrderBy(v => v.SCANN_SEQUENCE).ToList();                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS读写扩展DB2)                {                    if (string.IsNullOrWhiteSpace(item.Equ_ExtendTwo_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS读写扩展DB2;                    convdb.DBReadLength = item.Equ_ExtendTwo_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_ExtendTwo_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_ExtendTwo_Sequence) == false && Convert.ToInt32(v.Equ_ExtendTwo_Sequence) < temp_sequence).Sum(v => v.Equ_ExtendTwo_Length);                        convdb.DBReadIndox = Totallength;                    }                    convdb.ScannSet = plc.WCS_ScannSet.Where(v => v.SCANN_STATIONNO == item.Equ_No).OrderBy(v => v.SCANN_SEQUENCE).ToList();                    equDBInfoSet.Add(convdb);                }                else if (DB.DB_TYPE == (int)DB_TypeEnum.WCS读写扩展DB3)                {                    if (string.IsNullOrWhiteSpace(item.Equ_ExtendThree_Sequence)) continue;                    var convdb = new ConvDbInfo();                    convdb.DBName = DB.DB_NAME;                    convdb.DBType = DB_TypeEnum.WCS读写扩展DB3;                    convdb.DBReadLength = item.Equ_ExtendThree_Length;                    //convdb.DBReadIndox = item.Equ_ReadWriteDB_Sequence * item.Equ_ReadWriteDB_Length;                    int temp_sequence = Convert.ToInt32(item.Equ_ExtendThree_Sequence);                    if (temp_sequence != 0)                    {                        int Totallength = plc.WCS_EquipmentInfoSet.Where(v => string.IsNullOrWhiteSpace(v.Equ_ExtendThree_Sequence) == false && Convert.ToInt32(v.Equ_ExtendThree_Sequence) < temp_sequence).Sum(v => v.Equ_ExtendThree_Length);                        convdb.DBReadIndox = Totallength;                    }                    convdb.ScannSet = plc.WCS_ScannSet.Where(v => v.SCANN_STATIONNO == item.Equ_No).OrderBy(v => v.SCANN_SEQUENCE).ToList();                    equDBInfoSet.Add(convdb);                }            }            return equDBInfoSet;        }        /// <summary>        /// 删除过期日志        /// </summary>        public static void DeleteLog(int day)        {            string path = string.Format("{0}bin\\Debug\\netcoreapp3.1\\App_Log\\Info\\", AppDomain.CurrentDomain.BaseDirectory);            DeleteLog(day, path);            path = string.Format("{0}bin\\Debug\\netcoreapp3.1\\App_Log\\Error\\", AppDomain.CurrentDomain.BaseDirectory);            DeleteLog(day, path);            path = string.Format("{0}bin\\Debug\\netcoreapp3.1\\App_Log\\Product\\", AppDomain.CurrentDomain.BaseDirectory);            DeleteLog(day, path);            //DeleteAGVLog(day);        }        private static void DeleteLog(int day, string path)        {            try            {                int daycount = -day;                DirectoryInfo folder = new DirectoryInfo(path);                FileInfo[] files = folder.GetFiles("*.log.*");                DateTime dt = DateTime.Now.AddDays(daycount);                DateTime date = new DateTime();                foreach (FileInfo info in files)                {                    if (DateTime.TryParse(info.Name.Substring(0, 10), out date))                    {                        if (date < dt)                        {                            info.Delete();                        }                    }                }            }            catch (Exception ex)            {                Log4netHelper.Logger_Error.ErrorFormat(string.Format("路径[{0}]下删除超过[{1}]天的日志失败", path, day));                Log4netHelper.Logger_Error.ErrorFormat(ex.ToString());            }        }        ///// <summary>        ///// 根据箱子尺寸是可用范围获取箱子剁型编号        ///// </summary>        //public static WCS_PalletizingCode IsAvailableRange_BoxSize(List<WCS_PalletizingCode> palletizingCodeSet,string boxSize, int task_no)        //{        //    WCS_PalletizingCode palletizingCode = null;        //    try        //    {        //        if (string.IsNullOrWhiteSpace(boxSize))        //            throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));        //        string[] boxsizeSet = boxSize.Split('*');        //        if (boxsizeSet.Count() < 3)        //            throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));        //        decimal length = Convert.ToDecimal(boxsizeSet[0]);        //        decimal width = Convert.ToDecimal(boxsizeSet[1]);        //        decimal height = Convert.ToDecimal(boxsizeSet[2]);        //        palletizingCode = palletizingCodeSet.FirstOrDefault(v => length == v.BoxSizeLength && width == v.BoxSizeWidth && height == v.BoxSizeHeight);        //        //palletizingCode = palletizingCodeSet.FirstOrDefault(v => length >= v.BoxSizeLength_Min && length <= v.BoxSizeLength_Max &&        //        //                                                         width >= v.BoxSizeWidth_Min && width <= v.BoxSizeWidth_Max &&        //        //                                                         height >= v.BoxSizeHeight_Min && height <= v.BoxSizeHeight_Max);        //    }        //    catch (Exception ex)        //    {        //        LogMessageHelper.RecordLogMessage(ex);        //    }        //    return palletizingCode;        //}        /// <summary>        /// 根据箱子尺寸是可用范围获取箱子剁型编号        /// </summary>        public static WCS_PalletizingCode IsAvailableRange_BoxSize(List<WCS_PalletizingCode> palletizingCodeSet, string boxSize, int task_no)        {            WCS_PalletizingCode palletizingCode = null;            try            {                if (string.IsNullOrWhiteSpace(boxSize))                    throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));                string[] boxsizeSet = boxSize.Split('*');                if (boxsizeSet.Count() < 3)                    throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));                decimal length = Convert.ToDecimal(boxsizeSet[0]);                decimal width = Convert.ToDecimal(boxsizeSet[1]);                decimal height = Convert.ToDecimal(boxsizeSet[2]);                palletizingCode = palletizingCodeSet.FirstOrDefault(v => length == v.BoxSizeLength && width == v.BoxSizeWidth && height == v.BoxSizeHeight);                //palletizingCode = palletizingCodeSet.FirstOrDefault(v => length >= v.BoxSizeLength_Min && length <= v.BoxSizeLength_Max &&                //                                                         width >= v.BoxSizeWidth_Min && width <= v.BoxSizeWidth_Max &&                //                                                         height >= v.BoxSizeHeight_Min && height <= v.BoxSizeHeight_Max);            }            catch (Exception ex)            {                LogMessageHelper.RecordLogMessage(ex);            }            return palletizingCode;        }        /// <summary>        ///根据箱子尺寸托盘尺寸是可用范围获取机械手剁型编号        /// </summary>        public static WCS_PalletizingCode IsAvailableRange_SonTraySize(List<WCS_PalletizingCode> palletizingCodeSet, string sonTraySize, string boxSize, int task_no)        {            WCS_PalletizingCode palletizingCode = null;            try            {                if (string.IsNullOrWhiteSpace(boxSize))                    throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));                string[] sonTraySizeSet = sonTraySize.Split('*');                if (sonTraySizeSet.Count() < 2)                    throw new Exception(string.Format("任务[{0}]子托盘尺寸[{1}]不正确", task_no, sonTraySize));                if (string.IsNullOrWhiteSpace(boxSize))                    throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));                string[] boxsizeSet = boxSize.Split('*');                if (boxsizeSet.Count() < 3)                    throw new Exception(string.Format("任务[{0}]箱子尺寸[{1}]不正确", task_no, boxSize));                decimal boxlength = Convert.ToDecimal(boxsizeSet[0]);                decimal boxwidth = Convert.ToDecimal(boxsizeSet[1]);                decimal boxheight = Convert.ToDecimal(boxsizeSet[2]);                decimal length = Convert.ToDecimal(sonTraySizeSet[0]);                decimal width = Convert.ToDecimal(sonTraySizeSet[1]);                palletizingCode = palletizingCodeSet.FirstOrDefault(v => boxlength == v.BoxSizeLength &&                                                                         boxwidth == v.BoxSizeWidth &&                                                                         boxheight == v.BoxSizeHeight &&                                                                         length == v.SonTraySizeLength &&                                                                         width == v.SonTraySizeWidth);                //palletizingCode = palletizingCodeSet.FirstOrDefault(v => boxlength >= v.BoxSizeLength_Min && boxlength <= v.BoxSizeLength_Max &&                //                                                         boxwidth >= v.BoxSizeWidth_Min && boxwidth <= v.BoxSizeWidth_Max &&                //                                                         boxheight >= v.BoxSizeHeight_Min && boxheight <= v.BoxSizeHeight_Max &&                //                                                         length >= v.SonTraySizeLength_Min && length <= v.SonTraySizeLength_Max &&                //                                                         width >= v.SonTraySizeWidth_Min && width <= v.SonTraySizeWidth_Max);            }            catch (Exception ex)            {                LogMessageHelper.RecordLogMessage(ex);            }            return palletizingCode;        }    }}
 |