using Hanhe.iWCS.Common; using Hanhe.iWCS.MData; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hanhe.iWCS.JingmenGEMNorthProtocol { public class MESHelper { private static string mesurl = Settings.MESUrl; private static string timeurl = Settings.TimeCuo; private static string timeur2 = Settings.TimeCuo1; private static WebApiHelper helper = new WebApiHelper(); /// /// 时间戳数据计数 /// public static int total; /// /// 包装机下线任务调用的MES接口——GlmTonBagFillInfo /// /// 设备号 /// internal static MachineInfoCache GetPackingMachineInfo(string location) { //mes 地址读配置文件 MachineInfoCache result = new MachineInfoCache(); //需要进一步确认MES接口传输的数据 var param = new { machineNo = location }; CMMLog.Info($"向MES发送数据{JsonConvert.SerializeObject(param)},接口地址为:{mesurl}"); var res = helper.WebPost(mesurl, JsonConvert.SerializeObject(param)); if (!string.IsNullOrEmpty(res)) { CMMLog.Info($"MES获取到数据:{JsonConvert.DeserializeObject(res)}"); var req = JsonConvert.DeserializeObject(res); if (req != null) { if(location == req.machineNo) { CMMLog.Info($"转换后MES数据{req}"); var machine = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", req.machineNo), "MachineInfo"); if (machine == null) { MongoDBSingleton.Instance.Insert(new MachineInfo { machineNo = req.machineNo, lotNo = req.lotNo, bagNo = req.bagNo, productType = req.productType, palletLayers = req.palletLayers, materialCode = req.materialCode, overlappingLayers = req.overlappingLayers }); } else { CMMLog.Info($"当前中间表有当前包装机点位的信息存在,请检查MST主表是否有任务正在执行!"); Console.WriteLine($"当前中间表有当前包装机点位的信息存在,请检查MST主表是否有任务正在执行!"); } CMMLog.Info($"MES接口传输数据信息:machineNo:{req.machineNo},lotNo:{req.lotNo},bagNo:{req.bagNo},productType:{req.productType},materialCode:{req.materialCode},overlappingLayers:{req.overlappingLayers}"); Console.WriteLine($"MES接口传输数据信息:machineNo:{req.machineNo},lotNo:{req.lotNo},bagNo:{req.bagNo},productType:{req.productType},materialCode:{req.materialCode},overlappingLayers:{req.overlappingLayers}"); result = req; } else CMMLog.Info($"MES传输的包装机号与当前包装机号不符合:当前包装机号:{location},MES传输的包装机号:{req.machineNo}"); } else CMMLog.Info($"MES接口:Json返回数据为空值!接受的数据为:{req}"); } else CMMLog.Info($"MES接口:返回数据为空值!接受的数据为:{res}"); return result; } /// /// 3楼打包下线口到1楼成品货架口——调用打包线下线口接受传输的照相机接口数据 /// /// 时间戳 /// internal static TimeChuoInfo TimeCuo(string timeStamp,string deviceType,ref string deviceNo) { //时间戳 地址读配置文件 TimeChuoInfo result = new TimeChuoInfo(); var param = new { machineNo = timeStamp }; string timeUrl = deviceType == "29" ? timeur2 : timeurl; CMMLog.Info($"TimeCuo-01:SendMsg:{JsonConvert.SerializeObject(param)},url:{timeUrl}"); //var res = helper.WebGet(timeurl, JsonConvert.SerializeObject(param)); var res = helper.WebGet(timeUrl + "?timeStamp="+timeStamp); CMMLog.Info($"TimeCuo-02:ReqMsg:{JsonConvert.SerializeObject(res)},url:{timeUrl}"); try { if (!string.IsNullOrEmpty(res)) { CMMLog.Info("time 02"); var req = JsonConvert.DeserializeObject(res); if (req != null) { CMMLog.Info("time 03"); //req.info = TransUTF8(req.info);//MES传输的UTF-8格式参数,C#默认UniCode格式,出现解析错误,但是此字段无意义,看情况更新 //var time = req.data[req.data.Count]; var timeReq = req.data.Where(a => a.timeStamp == int.Parse(timeStamp)).FirstOrDefault(); CMMLog.Info("需要处理的参数:" + JsonConvert.SerializeObject(timeReq)); if (timeReq != null && req.count == 1) { CMMLog.Info("time 04"); total = timeReq.totalCount; if (timeReq.batchNumber.Length % 2 == 0 && timeReq.batchNumber.Length > 1 && timeReq.batchNumber.Substring(0, 1) == "0") timeReq.batchNumber = timeReq.batchNumber.Substring(1, timeReq.batchNumber.Length - 1); if (timeReq.productModel.Length % 2 == 0 && timeReq.productModel.Length > 1 && timeReq.productModel.Substring(0, 1) == "0") timeReq.productModel = timeReq.batchNumber.Substring(1, timeReq.productModel.Length - 1); CMMLog.Info("time 05"); var timeCuo = MongoDBSingleton.Instance.FindOne(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), "TimeCuoInfoCom"); if (timeCuo == null) { MongoDBSingleton.Instance.Insert(new TimeCuoInfoCom { isFold = timeReq.isFold, packingMachineNumber = timeReq.packingMachineNumber_1, //bagNumber = timeReq.bagNumber_1+","+timeReq.bagNumber_2, //needWeight = timeReq.needWeight_1, realWeight = timeReq.realWeight_1, totalWeight = timeReq.totalWeight, batchNumber = timeReq.batchNumber, productModel = timeReq.materialCode, isNeedTray = timeReq.isNeedTray, //totalCount = timeReq.totalCount, totalCount = timeReq.totalCount, timeStamp = timeReq.timeStamp, photoURL = timeReq.photoURL, // ERP变更-照相机新增字段 materialCode = timeReq.materialCode, materialName = timeReq.materialName, measurementUnit = timeReq.measurementUnit, createOrganization = timeReq.createOrganization, count = timeReq.count, WarehouseCode = timeReq.WarehouseCode, workshopCode = timeReq.workshopCode, BusinessDate = timeReq.BusinessDate, barcode = timeReq.barcode, codeRules = timeReq.codeRules, employeeId = timeReq.employeeID1, employeeID2 = timeReq.employeeID2, employeeID3 = timeReq.employeeID3 }); } else { var query = Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1); UpdateBuilder update = Update.Set("isFold", timeReq.isFold).Set("totalWeight", timeReq.totalWeight) .Set("batchNumber", timeReq.batchNumber).Set("productModel", timeReq.materialCode).Set("isNeedTray", timeReq.isNeedTray) .Set("totalCount", timeReq.totalCount).Set("timeStamp", timeReq.timeStamp).Set("photoURL", timeReq.photoURL) .Set("materialCode", timeReq.materialCode).Set("materialName", timeReq.materialName).Set("measurementUnit", timeReq.measurementUnit) .Set("createOrganization", timeReq.createOrganization).Set("count", timeReq.count).Set("WarehouseCode", timeReq.WarehouseCode) .Set("workshopCode", timeReq.workshopCode).Set("BusinessDate", timeReq.BusinessDate).Set("barcode", timeReq.barcode) .Set("codeRules", timeReq.codeRules).Set("employeeId", timeReq.employeeID1).Set("employeeID2", timeReq.employeeID2).Set("employeeID3", timeReq.employeeID3); MongoDBSingleton.Instance.Update(query, update, UpdateFlags.None); } CMMLog.Info($"获取到的时间戳接口数据:包装机号packingMachineNumber:{timeReq.packingMachineNumber_1},叠托后的实际重量totalWeight:{timeReq.totalWeight},产品批次号batchNumber:{timeReq.batchNumber}" + $",产品型号productModel:{timeReq.productModel},totalCount:{timeReq.totalCount},timeStamp:{timeReq.timeStamp},photoURL:{timeReq.photoURL}"); #region 判断叠包层数 if (timeReq.isFold == 1 || timeReq.isFold == 2 || timeReq.isFold == 3) { var query = Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1); UpdateBuilder update = null; if (timeReq.isFold == 1) update = Update.Set("bagNumber", timeReq.bagNumber_1).Set("realWeight", (timeReq.realWeight_1 / 100).ToString().Trim()); else if (timeReq.isFold == 2) update = Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2); else if (timeReq.isFold == 3) update = Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2 + "," + timeReq.bagNumber_3); if (update != null) MongoDBSingleton.Instance.Update(query, update, "TimeCuoInfoCom", UpdateFlags.None); } else CMMLog.Error($"isFold:{timeReq.isFold}"); #region // if (timeReq.isFold == 1) // MongoDBSingleton.Instance.Update(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), Update.Set("bagNumber", timeReq.bagNumber_1).Set("realWeight", (timeReq.realWeight_1 / 100).ToString().Trim()), UpdateFlags.None); // else if (timeReq.isFold == 2) // //MongoDBSingleton.Instance.Update(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2).Set("realWeight", (timeReq.realWeight_1 / 100).ToString().Trim()+","+ (timeReq.realWeight_2 / 100).ToString().Trim()), UpdateFlags.None); // MongoDBSingleton.Instance.Update(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2), UpdateFlags.None); // else if (timeReq.isFold == 3) // //MongoDBSingleton.Instance.Update(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2 + "," + timeReq.bagNumber_3).Set("realWeight", (timeReq.realWeight_1 / 100).ToString().Trim() + "," + (timeReq.realWeight_2 / 100).ToString().Trim()+","+ (timeReq.realWeight_3 / 100).ToString().Trim()), UpdateFlags.None); // MongoDBSingleton.Instance.Update(Query.EQ("packingMachineNumber", timeReq.packingMachineNumber_1), Update.Set("bagNumber", timeReq.bagNumber_1 + "," + timeReq.bagNumber_2 + "," + timeReq.bagNumber_3), UpdateFlags.None); // else CMMLog.Error($"isFold:{timeReq.isFold}"); #endregion #endregion //判断是否叠底托 deviceNo = timeReq.isNeedTray == 2 ? "7" : "10"; result = req; } else result = null; } else result = null; CMMLog.Info($"code:{req.code},count:{req.count},info:{req.info},info:{req.msg},info:{req.status}"); } CMMLog.Info("time over!"); } catch(Exception ex) { CMMLog.Error($"TimeCuo Error:{ex.Message}"); } return result; } public static string TransUTF8(string text) { //声明字符集 System.Text.Encoding utf8, gb2312; //utf8 utf8 = System.Text.Encoding.GetEncoding("utf-8"); //gb2312 gb2312 = System.Text.Encoding.GetEncoding("gb2312"); byte[] utf; utf = gb2312.GetBytes(text); utf = System.Text.Encoding.Convert(gb2312, utf8, utf); //返回转换后的字符 return utf8.GetString(utf); } /// /// 存放从MES接受的数据和设备通道里面读取的数据(包括转换后的数据) /// public class MachineInfo { public ObjectId _id { get; set; } /// /// mes返回的包装机号(T1,T2,......,T9) /// public string machineNo { get; set; } /// /// mes返回的成品批次号 /// public string lotNo { get; set; } = "0"; /// /// mes返回的袋号 /// public string bagNo { get; set; } = "0"; /// /// mes返回的产品型号 /// public string productType { get; set; } = "0"; /// /// MES返回的是否叠底托(1-不叠底托 2-叠底托) /// public string palletLayers { get; set; } = "0"; /// /// mes返回的物料编码 /// public string materialCode { get; set; } = "0"; /// /// mes返回的物料名称 /// public string materialName { get; set; } = "0"; /// /// 计量单位 /// public string measurementUnit { get; set; } = "0"; /// /// mes返回的叠包层数 (例1,2,3:1代表不叠包,2代表叠2层,3代表叠3层) /// public string overlappingLayers { get; set; } = "0"; /// /// 设备通道数据3——托盘码 /// public string trayCode { get; set; } = "0"; /// /// 设备通道数据1——包装机号 /// public string location { get; set; } = "0"; /// /// 设备通道数据2——产品需求重量:32位整数 /// public string productWeight { get; set; } = "0"; /// /// 设备通道数据2——托盘重量:32位整数 ERP变更:根据最新信号协议 trayCodeWeight 变更为 【员工编号】通道 /// //public string trayCodeWeight { get; set; } = "0"; /// /// 设备通道数据2——单托毛种:32位整数 /// public string oneTrayWeight1 { get; set; } = "0"; /// /// 设备通道数据2——单托实际重量:32位整数 /// public string oneTrayWeight { get; set; } = "0"; /// /// 设备通道数据1——托盘类型:32位整数 /// public string trayType { get; set; } = "0"; /// /// 设备通道数据2——叠包后实际重量:32位整数 /// public string addWeight { get; set; } = "0"; /// /// 设备通道数据1——包装机出口称重结果:1:OK,2:NG /// public int packNg { get; set; } = 1; /// /// 设备通道数据1——复称口称重结果:1:OK,2:NG /// public int secondNg { get; set; } = 1; /// /// 设备通道数据1——是否需要叠包:1:叠包,2:不叠包 /// public int addState { get; set; } = 0; /// /// 设备通道数据1——袋号(001,002,003…999) /// public string packageCode { get; set; } = "0"; /// /// 设备通道数据——全16位short /// public int[] machinedown = new int[20]; /// /// 设备通道数据——数据库JSON /// public string jsonData { get; set; } = "0"; public string occupy { get; set; } = "1"; public DateTime modify { get; set; } public string isOne { get; set; } } public class MachineInfoTwo { public ObjectId _id { get; set; } /// /// mes返回的包装机号(T1,T2,......,T9) /// public string machineNo { get; set; } /// /// mes返回的成品批次号 /// public string lotNo { get; set; } = "0"; /// /// mes返回的袋号 /// public string bagNo { get; set; } = "0"; /// /// mes返回的产品型号 /// public string productType { get; set; } = "0"; /// /// MES返回的是否叠底托(1-不叠底托 2-叠底托) /// public string palletLayers { get; set; } = "0"; /// /// mes返回的物料编码 /// public string materialCode { get; set; } = "0"; /// /// mes返回的物料名称 /// public string materialName { get; set; } = "0"; /// /// 计量单位 /// public string measurementUnit { get; set; } = "0"; /// /// mes返回的叠包层数 (例1,2,3:1代表不叠包,2代表叠2层,3代表叠3层) /// public string overlappingLayers { get; set; } = "0"; /// /// 设备通道数据3——托盘码 /// public string trayCode { get; set; } = "0"; /// /// 设备通道数据1——包装机号 /// public string location { get; set; } = "0"; /// /// 设备通道数据2——产品需求重量:32位整数 /// public string productWeight { get; set; } = "0"; /// /// 设备通道数据2——托盘重量:32位整数 ERP变更:根据最新信号协议 trayCodeWeight 变更为 【员工编号】通道 /// //public string trayCodeWeight { get; set; } = "0"; /// /// 设备通道数据2——单托毛重:32位整数 /// public string oneTrayWeight1 { get; set; } = "0"; /// /// 复称重量:32位整数 /// public string oneTrayWeightFC { get; set; } = "0,0"; /// /// 设备通道数据2——单托实际重量:32位整数 /// public string oneTrayWeight { get; set; } = "0"; /// /// 设备通道数据1——托盘类型:32位整数 /// public string trayType { get; set; } = "0"; /// /// 设备通道数据2——叠包后实际重量:32位整数 /// public string addWeight { get; set; } = "0"; /// /// 设备通道数据1——包装机出口称重结果:1:OK,2:NG /// public int packNg { get; set; } = 1; /// /// 设备通道数据1——复称口称重结果:1:OK,2:NG /// public int secondNg { get; set; } = 1; /// /// 设备通道数据1——是否需要叠包:1:叠包,2:不叠包 /// public int addState { get; set; } = 0; /// /// 设备通道数据1——袋号(001,002,003…999) /// public string packageCode { get; set; } = "0"; /// /// 设备通道数据——全16位short /// public int[] machinedown = new int[20]; /// /// 设备通道数据——数据库JSON /// public string jsonData { get; set; } = "0"; public string occupy { get; set; } = "1"; public DateTime modify { get; set; } public string isOne { get; set; } } public class MachineInfoCache { /// /// mes返回的包装机号(T1,T2,......,T9) /// public string machineNo { get; set; } /// /// mes返回的成品批次号 /// public string lotNo { get; set; } /// /// mes返回的袋号 /// public string bagNo { get; set; } /// /// mes返回的产品型号 /// public string productType { get; set; } /// /// mes返回的是否叠底托信号 /// public string palletLayers { get; set; } /// /// mes返回的物料编码 /// public string materialCode { get; set; } /// /// mes返回的叠包层数 (例1,2,3:1代表不叠包,2代表叠2层,3代表叠3层) /// public string overlappingLayers { get; set; } } /// /// 时间戳——调用照相机设备获取的返回信息 /// public class TimeChuoInfo { public int code { get; set; } public int count { get; set; } public string info { get; set; } public string msg { get; set; } public int status { get; set; } public List data { get; set; } public class TimeCuoInfo { public int isFold { get; set; } /// /// 员工编号1 /// public string employeeID1 { get; set; } /// /// 员工编号2 /// public string employeeID2 { get; set; } /// /// 员工编号3 /// public string employeeID3 { get; set; } public string packingMachineNumber_1 { get; set; } public string bagNumber_1 { get; set; } public int needWeight_1 { get; set; } public int realWeight_1 { get; set; } public string packingMachineNumber_2 { get; set; } public string bagNumber_2 { get; set; } public int needWeight_2 { get; set; } public int realWeight_2 { get; set; } public string packingMachineNumber_3 { get; set; } public string bagNumber_3 { get; set; } public int needWeight_3 { get; set; } public int realWeight_3 { get; set; } public int totalWeight { get; set; } public string batchNumber { get; set; } public string productModel { get; set; } public int totalCount { get; set; } public int timeStamp { get; set; } public string photoURL { get; set; } /// /// 是否叠底托(ERP变更功能之后不再使用此字段) /// public int isNeedTray { get; set; } /// /// 物料编码 /// public string materialCode { get; set; } = "1"; /// /// 物料名称 /// public string materialName { get; set; } /// /// 计量单位 /// public string measurementUnit { get; set; } /// /// 创建组织 /// public string createOrganization { get; set; } /// /// 数量 /// public int count { get; set; } /// /// 仓库编码 /// public string WarehouseCode { get; set; } /// /// 车间编码 /// public string workshopCode { get; set; } /// /// 业务日期 /// public string BusinessDate { get; set; } /// /// 条形码 /// public string barcode { get; set; } /// /// 条码规则 /// public string codeRules { get; set; } } } public class TimeCuoInfoCom { public ObjectId _id { get; set; } /// /// 是否叠包(1,2,3) /// public int isFold { get; set; } /// /// 包装机号 /// public string packingMachineNumber{ get; set; } /// /// 袋号(001,002,003…999)1 /// public string bagNumber { get; set; } /// /// 产品需求重量1:32位整数 /// public int needWeight { get; set; } /// /// 单托实际重量1:32位整数 /// public int realWeight { get; set; } /// /// 叠托后的实际重量:32位整数 /// public int totalWeight { get; set; } /// /// 产品批次号 /// public string batchNumber { get; set; } /// /// 产品型号 /// public string productModel { get; set; } /// /// 是否叠底托(1-不叠底托 2-叠底托) /// public int isNeedTray { get; set; } /// /// 计数(只在数据库) /// public int totalCount { get; set; } /// /// 时间戳 /// public int timeStamp { get; set; } /// /// 照片url /// public string photoURL { get; set; } /// /// 物料编码 /// public string materialCode { get; set; } /// /// 物料名称 /// public string materialName { get; set; } /// /// 计量单位 /// public string measurementUnit { get; set; } /// /// 创建组织 /// public string createOrganization { get; set; } /// /// 数量 /// public int count { get; set; } /// /// 仓库编码 /// public string WarehouseCode { get; set; } /// /// 车间编码 /// public string workshopCode { get; set; } /// /// 业务日期 /// public string BusinessDate { get; set; } /// /// 条形码 /// public string barcode { get; set; } /// /// 条码规则 /// public string codeRules { get; set; } /// /// 员工编号 /// public string employeeId { get; set; } /// /// 员工编号2 /// public string employeeID2 { get; set; } /// /// 员工编号3 /// public string employeeID3 { get; set; } } /// /// 物料表 /// public class T_JY_MATERIALSync { /// /// 流水号 /// public int FID { get; set; } /// /// 名称 /// public string FName { get; set; } /// /// 编码 /// public string FNumber { get; set; } /// /// 规格型号 /// public string FSpecification { get; set; } /// /// 使用组织编码 /// public string FUseOrgNumber { get; set; } /// /// 车间名称 /// public string FWorkShopNumber { get; set; } /// /// 保质期 /// public int FExpPeriod { get; set; } /// /// 保质期单位 /// public string FExpUnit { get; set; } /// /// 库存单位 /// public string FStoreUnit { get; set; } /// /// 物料分组名称 /// public string FMaterialGroupName { get; set; } /// /// 物料分组编码 /// public string FMaterialGroupNumber { get; set; } } /// /// 员工表 /// public class T_JY_EMPINFOSync { /// /// 流水号 /// public int FID { get; set; } /// /// 编码 /// public string FNumber { get; set; } /// /// 名称 /// public string FName { get; set; } /// /// 员工编号 /// public string FStaffNumber { get; set; } } } }