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 = "欢迎光临博众精工科技股份有限公司";
///
/// 当前任务列表
///
public static List TaskSet = new List();
///
/// AGV任务列表
///
public static List AgvTaskSet = new List();
///
/// 熟化架缓存位信息
///
public static List AgvCachePostionSet = new List();
///
/// 需要手动生成任务的工位清单
///
public static List WCS_TASKTIMEPOINTSet = new List();
///
/// plc列表
///
public static List PlcSet = new List();
///
/// 设备路由列表
///
public static List EquRouteSet = new List();
///
/// 堆垛机出入口输送线列表
///
public static List WCS_SrmOutInInfoSet = new List();
///
/// 缓存区信息
///
public static List WCS_CacheInfoSet = new List();
///
/// 系统设置信息
///
public static List SysSets = new List();
///
/// RGV对接口列表
///
public static List WCS_RGVOutInInfoSet = new List();
///
/// WMS接口
///
public static IWMS WmsInterface { get; set; }
///
/// WCS扩展接口
///
public static IWCS WcsInterface { get; set; }
///
/// 原料仓库编号
///
public static readonly string WareHouseId = "pvchouseyuanmo";
///
/// PVC普通立体库
///
public static readonly string Pvchouseputong = "pvchouseputong";
public static readonly string PvchouseputongCh = "PVC普通立库";
///
/// 仓库
///
public static List WareNameList { get; set; }
public static void InitData(string wcssystem)
{
try
{
DeleteLog(30);
WareNameList = new List() { "PVC原膜立库", "PVC普通立库" };
PlcSet = SugarBase.DB.Queryable().Where(v => v.PLC_WCSSYSTEM == wcssystem).ToList();
EquRouteSet = SugarBase.DB.Queryable().ToList();
WCS_SrmOutInInfoSet = SugarBase.DB.Queryable().ToList().Where(v => PlcSet.Any(t => t.PLC_NAME == v.SRMOUTIN_CONVPLCNAME)).ToList();
WCS_RGVOutInInfoSet = SugarBase.DB.Queryable().ToList().Where(v => PlcSet.Any(t => t.PLC_NAME == v.PLCNAME)).ToList();
SysSets = SugarBase.DB.Queryable().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().Where(v => v.DB_PLCNAME == plc.PLC_NAME).ToList();
plc.WCS_ScannSet = SugarBase.DB.Queryable().Where(v => v.SCANN_PLCNAME == plc.PLC_NAME).OrderBy(v => v.SCANN_SEQUENCE).ToList();
plc.WCS_EquipmentInfoSet = SugarBase.DB.Queryable().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 GetConvDBInfo(WCS_PLC plc, WCS_EQUIPMENTINFO item)
{
List equDBInfoSet = new List();
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;
}
///
/// 删除过期日志
///
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());
}
}
/////
///// 根据箱子尺寸是可用范围获取箱子剁型编号
/////
//public static WCS_PalletizingCode IsAvailableRange_BoxSize(List 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;
//}
///
/// 根据箱子尺寸是可用范围获取箱子剁型编号
///
public static WCS_PalletizingCode IsAvailableRange_BoxSize(List 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;
}
///
///根据箱子尺寸托盘尺寸是可用范围获取机械手剁型编号
///
public static WCS_PalletizingCode IsAvailableRange_SonTraySize(List 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;
}
}
}