From d1f9aaafc965e752c6ecf5b8f677640542aabae7 Mon Sep 17 00:00:00 2001 From: "liulb@mesnac.com" Date: Wed, 24 Jan 2024 21:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A7=E5=89=8D=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6=E6=8F=90?= =?UTF-8?q?=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Alarm/AlarmHelper.cs | 19 +- .../DBHelper/PlanDbHelper.cs | 321 +++++++ .../DBHelper/PlcConnect.cs | 43 + .../DBHelper/Xl_DownLoadPlan.cs | 76 ++ .../FinishBatch/FinishBatchService.cs | 84 +- .../Mesnac.Action.ChemicalWeighing.csproj | 7 + .../Product/XlPlan/PlanHelper.cs | 1 - .../Services/PlanHelperServer.cs | 866 ++++++++++++++++++ .../Show/XlInitAction.cs | 49 +- Main/MCEdit/Data/MCProject/nodeDataSource.xml | 2 +- Main/MCEdit/Data/MCProject/nodeDevice.xml | 2 +- Main/MCRun/MCRun.csproj | 5 +- 12 files changed, 1411 insertions(+), 64 deletions(-) create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlanDbHelper.cs create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlcConnect.cs create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/Xl_DownLoadPlan.cs create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Services/PlanHelperServer.cs diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Alarm/AlarmHelper.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Alarm/AlarmHelper.cs index da16d27..001118c 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Alarm/AlarmHelper.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Alarm/AlarmHelper.cs @@ -625,14 +625,12 @@ namespace Mesnac.Action.ChemicalWeighing.Alarm { IFreeSql fsql = FreeHelper.Instance; - string sql = @"select * from Pmt_Alarm "; + //string sql = @"select * from Pmt_Alarm "; - List list = fsql.Select().WithSql(sql).ToList(); + List list = fsql.Select().ToList(); if (list!=null&&list.Count() > 1) { - // var affrows = Convert.ToInt32(fsql.Insert(lrPlan).ExecuteIdentity()); - foreach ( var item in list) { ReadAlarm(item); @@ -665,7 +663,7 @@ namespace Mesnac.Action.ChemicalWeighing.Alarm } else { - //alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); + //alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //alarm.Alarm_Status = 1; //_= _lrAlarmlogServices.UpdateAsync(alarm).Result; } @@ -678,7 +676,16 @@ namespace Mesnac.Action.ChemicalWeighing.Alarm { alarm.Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); alarm.Alarm_Status = 0; - var affrows = Convert.ToInt32(fsql.Update(alarm).ExecuteAffrows()); + // var affrows = Convert.ToInt32(fsql.Update(alarm).ExecuteAffrows()); + + int affrows = fsql.Update().Set(a => new LR_Alarmlog + { + Alarm_ClearTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + Alarm_Status = 0 + }).Where(d => d.Alarm_ID == item.Alarm_ID && d.Alarm_Status == 1).ExecuteAffrows(); + + + Console.WriteLine($"更新预警信息:{alarm.Alarm_ID}"); } } diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlanDbHelper.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlanDbHelper.cs new file mode 100644 index 0000000..fa02056 --- /dev/null +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlanDbHelper.cs @@ -0,0 +1,321 @@ +using HslCommunication.Profinet.Siemens; +using Mesnac.Action.ChemicalWeighing.Entity; +using Mesnac.Action.ChemicalWeighing.Entity.PptPlan; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Mesnac.Action.ChemicalWeighing.DBHelper +{ + public class PlanDbHelper + { + #region 根据计划号获取当前正在执行的计划任务 + /// + /// 根据计划号获取当前正在执行的计划任务 + /// + /// + /// + /// + /// + public static List GetExecPlan(int serial) + { + try + { + SiemensS7Net plc = PlcConnect.Instance; + string strSql = string.Empty; + //处理任务取消,但是当前任务会在执行完当前物料称量结束后才能 + if (GetPlanInfo().Count() > 0) + { + strSql = $@"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, + r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,ISNULL(p.FlagNum,0) as FlagNum + from xl_plan p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join + (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial + where p.Plan_State=3 "; + IFreeSql fsql = FreeHelper.Instance; + Console.WriteLine($"正在执行的计划:{strSql}"); + var list = fsql.Select().WithSql(strSql).ToList(); + Console.WriteLine($"正在执行的计划:{list.Count}"); + return list; + } + else + { + if (plc.ReadInt16("DB104.DBX4.1").Content == 1) + { + strSql = @"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, + r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,ISNULL(p.FlagNum,0) as FlagNum + from (select * from xl_plan where Plan_State=7 and Start_Date=(select Max(Start_Date) from xl_plan where Plan_State=7) ) p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join + (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial"; + IFreeSql fsql = FreeHelper.Instance; + Console.WriteLine($"正在执行的计划:{strSql}"); + var list = fsql.Select().WithSql(strSql).ToList(); + Console.WriteLine($"正在执行的计划:{list.Count}"); + return list; + } + } + return null; + } + catch (Exception ex) + { + Console.WriteLine($"查询执行计划:{ex.Message}"); + return null; + } + } + + public static List GetPlanInfo() + { + try + { + string sql = @"select 0 Id,p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, + r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,0 FlagNum + from xl_plan p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join + (select b.Bin_Serial,b.Bin_Name,b.Bin_Code,b.Material_ID,m.Material_name from Pmt_Bin b left join xl_material m on m.ID=b.Material_ID) m on w.Bin_Serial=m.Bin_Serial + where p.Plan_State=3 "; + + IFreeSql fsql = FreeHelper.Instance; + Console.WriteLine($"验证正在执行的计划1"); + var list = fsql.Select().WithSql(sql).ToList(); + Console.WriteLine($"验证正在执行的计划:{list.Count}"); + var planList = list.OrderBy(d => d.Plan_Serial); + + return planList.ToList(); + } + catch (Exception ex) + { + Console.WriteLine($"验证正在执行的计划:{ex.Message}"); + return null; + } + } + + #endregion + + #region 获取LR_plan + + /// + /// 获取LR_plan + /// + /// 计划号 + /// 返回符合条件List + public static LR_plan GetLrPlan(string planID, int realNum, int flagNum) + { + try + { + string strSql = "SELECT * FROM LR_plan where Plan_Id = @Plan_Id and Real_Num=@Real_Num and FlagNum=@flagNum"; + IFreeSql fsql = FreeHelper.Instance; + LR_plan list = fsql.Select().WithSql(strSql, new { Plan_Id = planID, Real_Num = realNum, flagNum = flagNum }).ToOne(); + + return list; + } + catch (Exception ex) + { + Console.WriteLine($"查询LR_plan表数据失败:{ex.Message}"); + return null; + } + } + + #endregion + + #region 产前检测——更新计划完成数量 + /// + /// 更新计划完成数量 + /// + /// 计划号 + /// 完成数量 + /// + public static bool UpdateCheckPlanCompletedQuantity(Xl_DownLoadPlan dp, int tcheckBatch, int state, DateTime endTime, double totalWeight) + { + try + { + IFreeSql fsql = FreeHelper.Instance; + int result = fsql.Update().Set(a => new xl_plan + { + Real_Num = tcheckBatch, + Plan_TotalWeight = totalWeight, + Plan_State = state, + Plan_StateText = StateToShow(state), + End_Date = endTime.ToString("yyyy-MM-dd HH:mm:ss") + }).Where(a => a.Plan_Id == dp.Plan_Id).ExecuteAffrows(); + + if (result > 0) return true; + else return false; + } + catch (Exception ex) + { + Console.WriteLine($"产前检测更新xl_plan数据失败:{ex.Message}"); + return false; + } + } + #endregion + + #region 将计划运行状态转换为文字进行显示 + + /// + /// 将计划运行状态转换为文字进行显示 + /// + /// 计划状态代码 + /// 显示的文字信息 + public static string StateToShow(int stateNum) + { + string reStr = null; + + if (stateNum == 3) + { + reStr = "正在运行"; + } + else if (stateNum == 4) + { + reStr = "已下传"; + } + else if (stateNum == 5) + { + reStr = "未启动"; + } + else if (stateNum == 7) + { + reStr = "已终止"; + } + else if (stateNum == 8) + { + reStr = "已完成"; + } + + return reStr; + } + + + #endregion + + #region 根据ID获取料仓 + /// + /// 获取所有物料对象集合 + /// + /// + public static Pmt_Bin GetFristBinInfo(int Id) + { + IFreeSql fsql = FreeHelper.Instance; + Pmt_Bin bin = fsql.Select().Where(d => d.Bin_Serial == Id).ToOne(); + + return bin; + } + #endregion + + #region 更新计划详情 + + public static bool UpdatePlanDetail(Xl_DownLoadPlan item, LR_plan lrPlan, LR_weigh lrWeigh, Pmt_Bin bin, Hw_BinAlarm alarm, int key) + { + try + { + IFreeSql fsql = FreeHelper.Instance; + if (item.IsPrenatalTest == 1) + { + if (key == 1) + { + fsql.Transaction(() => + { + var affrows = fsql.Insert(lrPlan).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 + + affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 + }); + } + else + { + fsql.Transaction(() => + { + var affrows = fsql.Insert(lrPlan).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 + lrWeigh.MainId = affrows; + affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 + }); + + } + } + else + { + if (key == 1) + { + fsql.Transaction(() => + { + var affrows = Convert.ToInt32(fsql.Insert(lrPlan).ExecuteIdentity()); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘计划详细数据失败!"); //抛出异常,回滚事务,事务退出 + + lrWeigh.MainId = affrows; + affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 + affrows = fsql.Update(bin.Bin_Serial).Set(a => a.BinWeight, bin.BinWeight).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("保存库存实时数据失败!");//抛出异常,回滚事务,事务退出 + affrows = fsql.Insert(alarm).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("物料出库存盘数据失败!");//抛出异常,回滚事务,事务退出 + }); + } + else + { + fsql.Transaction(() => + { + var affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("存盘详细数据失败!");//抛出异常,回滚事务,事务退出 + bin.Equip_Code = "00"; + affrows = fsql.Update(bin.Bin_Serial).Set(a => a.BinWeight, bin.BinWeight).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("保存库存实时数据失败!");//抛出异常,回滚事务,事务退出 + affrows = fsql.Insert(alarm).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("物料出库存盘数据失败!");//抛出异常,回滚事务,事务退出 + }); + + } + + } + + return true; + } + catch (Exception ex) + { + return false; + } + } + + #endregion + + #region 保存执行计划 + public static bool UpdateCheckLrPlanCompletedQuantity(Xl_DownLoadPlan dp, int tcheckBatch, int state, DateTime endTime, double totalWeight, int flagNum) + { + try + { + IFreeSql fsql = FreeHelper.Instance; + int result = fsql.Update().Set(a => new LR_plan + { + Real_Num = tcheckBatch, + Total_Weight = decimal.Parse(totalWeight.ToString()), + Plan_State = state, + End_Date = endTime.ToString("yyyy-MM-dd HH:mm:ss") + }).Where(a => a.Plan_Id == dp.Plan_Id && a.FlagNum == flagNum).ExecuteAffrows(); + + if (result > 0) return true; + else return false; + } + catch (Exception) + { + return false; + } + } + #endregion + + } +} diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlcConnect.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlcConnect.cs new file mode 100644 index 0000000..a432ca8 --- /dev/null +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/PlcConnect.cs @@ -0,0 +1,43 @@ +using HslCommunication.Profinet.Siemens; +using System; + +namespace Mesnac.Action.ChemicalWeighing +{ + public class PlcConnect + { + private static readonly Lazy lazy = new Lazy(() => new PlcConnect().SiemensS7NetConnection()); + public static SiemensS7Net Instance => lazy.Value; + + private PlcConnect() + { + + } + + public SiemensS7Net SiemensS7NetConnection() + { + SiemensPLCS siemensPLCS = SiemensPLCS.S1500; + SiemensS7Net s7 = new SiemensS7Net(siemensPLCS); + + //配料IP + s7.IpAddress = "192.168.10.100"; + s7.Port = 102; + var su = s7.ConnectServer(); + + if (!su.IsSuccess) + { + s7.IpAddress = "192.168.10.100"; + s7.Port = 102; + var su1 = s7.ConnectServer(); + if (!su1.IsSuccess) + { + return new SiemensS7Net(siemensPLCS); + } + } + + return s7; + } + } + + + +} diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/Xl_DownLoadPlan.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/Xl_DownLoadPlan.cs new file mode 100644 index 0000000..707ba0f --- /dev/null +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/DBHelper/Xl_DownLoadPlan.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Mesnac.Action.ChemicalWeighing.DBHelper +{ + public class Xl_DownLoadPlan + { + public int Id { get; set; } + public string Dosing_Id { get; set; } + public string Plan_Id { get; set; } + public int Plan_Serial { get; set; } + public string Recipe_ID { get; set; } + public string Recipe_Name { get; set; } + public int Version { get; set; } + public int Plan_Num { get; set; } + public int Real_Num { get; set; } + public int Shift_Id { get; set; } + public string Shift_Class { get; set; } + public int Plan_State { get; set; } + public string Plan_StateText { get; set; } + /// + /// 计划实际执行重量总计 + /// + public double Plan_TotalWeight { get; set; } + /// + /// 计划实际执行误差总计 + /// + public double Plan_TotalError { get; set; } + /// + /// 配方总重量 + /// + public double Total_Weight { get; set; } + /// + /// 配方总误差 + /// + public double Total_Error { get; set; } + + public string MaterialID { get; set; } + public string Material_name { get; set; } + /// + /// 工位编码 + /// + public int Station { get; set; } + /// + /// 工位名称 + /// + public string StationName { get; set; } + /// + /// 物料设置重量 + /// + public decimal Set_Error { get; set; } + /// + /// 物料设置误差 + /// + public decimal Set_Weight { get; set; } + /// + /// 是否重传 + /// + public int IsRetransmission { get; set; } + /// + /// 重传次数 + /// + public int Retransmission_Num { get; set; } + /// + /// 是否产前检查 + /// + public int IsPrenatalTest { get; set; } + /// + /// 是否产前检查执行计数 + /// + public int FlagNum { get; set; } + } +} diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FinishBatch/FinishBatchService.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FinishBatch/FinishBatchService.cs index 8102dd6..1ae3ea0 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FinishBatch/FinishBatchService.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FinishBatch/FinishBatchService.cs @@ -1,17 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; -using System.Data; -using ICSharpCode.Core; -using Mesnac.Equips; -using Mesnac.Codd.Session; -using Mesnac.Controls.Base; -using Mesnac.Action.ChemicalWeighing.DBHelper; -using Mesnac.Action.ChemicalWeighing.Entity; -using Mesnac.Action.ChemicalWeighing.Product.PptPlan; -using Mesnac.Action.ChemicalWeighing.Alarm; +using System.Timers; +using HslCommunication.Profinet.Siemens; +using Mesnac.Action.ChemicalWeighing.Services; namespace Mesnac.Action.ChemicalWeighing.FinishBatch { @@ -26,7 +16,7 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch /// 运行状态,true:已运行,false:未运行 /// private bool runState = false; - + private bool flag = true; #endregion #region 单例实现 @@ -35,7 +25,11 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch private FinishBatchService() { - + System.Timers.Timer timer = new System.Timers.Timer(1000); + timer.Elapsed += new ElapsedEventHandler(ReadData); + timer.AutoReset = true; + timer.Enabled = true; + timer.Start(); } public static FinishBatchService Instance @@ -68,12 +62,12 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch lock (String.Empty) { //BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.read_data, out int[] p); - Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(ReadData); - Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(UpdateAlarmData); + //Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(ReadData); + //Mesnac.Equips.Factory.Instance.SetMinHZReadDataEvent(UpdateAlarmData); this.runState = true; } } - catch(Exception ex) + catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("自动更新计划状态服务异常:" + ex.Message, ex); } @@ -114,63 +108,51 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch { try { - lock(String.Empty) + lock (String.Empty) { #region 实时刷新自动保存报警 - SaveHelper.AlarmSaveHelper.Save(); + // SaveHelper.AlarmSaveHelper.Save(); #endregion #region 更新小料计划状态 - //XlPlcHelper.PlcPlanHelper.ExecPlanState(); + XlPlcHelper.PlcPlanHelper.ExecPlanState(); #endregion } } - catch(Exception ex) - { - ICSharpCode.Core.LoggingService.Error(ex.Message, ex); - } - } - - private void UpdateAlarmData2(object sender, Mesnac.Equips.ReadEventArgs e) - { - try - { - lock (String.Empty) - { - #region 更新计划状态 - - XlPlcHelper.PlcPlanHelper.UpdateXlPlanStateFromPlc(); - XlPlcHelper.PlcPlanHelper.UpdateBinAlarm(); - #endregion - } - } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message, ex); } } + #endregion - private void UpdateAlarmData(object sender, Mesnac.Equips.ReadEventArgs e) + private void ReadData(object sender, ElapsedEventArgs e) { - try + if (flag) { - lock (String.Empty) + flag = false; + try { - #region 更新计划状态 - AlarmHelper.QueryUpdateAlarm(); - - #endregion + PlanHelperServer.SavePlcData(); + } + catch (Exception ex) + { + Console.WriteLine($"FinishBatchService异常:{ex.Message}"); + ICSharpCode.Core.LoggingService.Error(ex.Message, ex); + } + finally + { + GC.Collect(); + flag = true; } } - catch (Exception ex) - { - ICSharpCode.Core.LoggingService.Error(ex.Message, ex); - } + } + } } diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj index 3216001..a9f7d47 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj @@ -130,6 +130,9 @@ False ..\..\..\dll\FreeSql.Repository.dll + + E:\普力通处理\dll\HslCommunication.dll + False ..\..\..\PlugInPlatform\ICSharpCode.Core.dll @@ -269,6 +272,9 @@ + + + @@ -412,6 +418,7 @@ + diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Product/XlPlan/PlanHelper.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Product/XlPlan/PlanHelper.cs index af4bf6d..16aad52 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Product/XlPlan/PlanHelper.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Product/XlPlan/PlanHelper.cs @@ -2927,7 +2927,6 @@ namespace Mesnac.Action.ChemicalWeighing.Product.XlPlan { if (BasePlcHelper.Instance.plt_plc_SystemRunning.NowValue.ToInt() == 1) { - strSql = @"select p.Dosing_Id,p.Plan_Id,p.Plan_Serial,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Shift_Id,p.Shift_Class,p.Plan_State,p.Plan_StateText,p.Plan_TotalWeight,p.Plan_TotalError, r.Total_Weight,r.Total_Error,m.Material_ID as MaterialID,m.Material_name,m.Bin_Serial as Station,m.Bin_Name as StationName,w.Set_Weight,w.Set_Error,p.IsRetransmission,p.Retransmission_Num,p.IsPrenatalTest,ISNULL(p.FlagNum,0) as FlagNum from (select * from xl_plan where Plan_State=7 and Start_Date=(select Max(Start_Date) from xl_plan where Plan_State=7) ) p left join xl_recipe r on p.Recipe_ID=r.ID left join xl_weigh w on r.ID=w.Recipe_ID left join diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Services/PlanHelperServer.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Services/PlanHelperServer.cs new file mode 100644 index 0000000..90401cb --- /dev/null +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Services/PlanHelperServer.cs @@ -0,0 +1,866 @@ +using HslCommunication.Profinet.Siemens; +using Mesnac.Action.ChemicalWeighing.DBHelper; +using Mesnac.Action.ChemicalWeighing.Entity; +using Mesnac.Action.ChemicalWeighing.Entity.PptPlan; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Mesnac.Action.ChemicalWeighing.Services +{ + public class PlanHelperServer + { + #region 更新任务状态 + /// + /// 更新任务状态 + /// + /// + public static void ExecPlanState() + { + bool result = true; + + #region 获取当前计划号,判断当前计划状态 + //读取配方编码获取配方物料所在的工位,根据工位查询状态 + //当PLC状态未"1" PLC准备好 上位机读取数据 + //存盘完成后上位机需要将PLC点位置为2 + //点位DBW0 设定PLC需要执行次数,点位DBW212 为计量秤称量批次,可以此判断计划是否完成 + if (result) + { + result = false; + try + { + + } + catch (Exception ex) + { + Console.WriteLine($"执行称量任务异常:{ex.Message}"); + } + finally { result = true; } + } + #endregion + } + + #endregion + + #region 保存数据 + /// + /// 保存数据 PLC点位信息 + /// + /// 计划列表 + /// 是否设置成功 + public static void SavePlcData() + { + try + { + SiemensS7Net plc = PlcConnect.Instance; + int plt_Set_Plan_Serial = plc.ReadInt16("DB114.DBW126").Content;//执行序号 Plan_Serial + List materialList = PlanDbHelper.GetExecPlan(plt_Set_Plan_Serial);//正在执行的任务 + if (materialList == null || materialList.Count == 0) { return; } + materialList.OrderBy(d => d.Station); + foreach (var item in materialList) + { + switch (item.Station) + { + case 1: + if (plc.ReadInt16("DB110.DBW0").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW152").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW150").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW0", short.Parse("2")); + Console.WriteLine("1工位存盘成功上位机完成置为2"); + } + } + break; + case 2: + if (plc.ReadInt16("DB110.DBW2").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW152").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW150").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW2", short.Parse("2")); + Console.WriteLine("2工位存盘成功上位机完成置为2"); + } + } + break; + case 3: + if (plc.ReadInt16("DB110.DBW6").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW156").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW154").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW6", short.Parse("2")); + Console.WriteLine("3工位存盘成功上位机完成置为2"); + } + } + break; + case 4: + if (plc.ReadInt16("DB110.DBW8").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW156").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW156").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW8", short.Parse("2")); + Console.WriteLine("4工位存盘成功上位机完成置为2"); + } + } + break; + case 5: + if (plc.ReadInt16("DB110.DBW12").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW160").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW158").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW12", short.Parse("2")); + Console.WriteLine("5工位存盘成功上位机完成置为2"); + } + } + break; + case 6: + if (plc.ReadInt16("DB110.DBW14").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW160").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW158").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW14", short.Parse("2")); + Console.WriteLine("6工位存盘成功上位机完成置为2"); + } + } + break; + case 7: + if (plc.ReadInt16("DB110.DBW18").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW164").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW162").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW18", short.Parse("2")); + Console.WriteLine("7工位存盘成功上位机完成置为2"); + } + } + break; + case 8: + if (plc.ReadInt16("DB110.DBW20").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW164").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW162").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW20", short.Parse("2")); + Console.WriteLine("8工位存盘成功上位机完成置为2"); + } + } + break; + case 9: + if (plc.ReadInt16("DB110.DBW24").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW168").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW166").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW24", short.Parse("2")); + Console.WriteLine("9工位存盘成功上位机完成置为2"); + } + } + break; + case 10: + if (plc.ReadInt16("DB110.DBW26").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW168").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW166").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW26", short.Parse("2")); + Console.WriteLine("10工位存盘成功上位机完成置为2"); + } + } + break; + case 11: + if (plc.ReadInt16("DB110.DBW30").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW172").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW170").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW30", short.Parse("2")); + Console.WriteLine("11工位存盘成功上位机完成置为2"); + } + } + break; + case 12: + if (plc.ReadInt16("DB110.DBW32").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW172").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW170").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW32", short.Parse("2")); + Console.WriteLine("12工位存盘成功上位机完成置为2"); + } + } + break; + case 13: + if (plc.ReadInt16("DB110.DBW36").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW176").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW174").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW36", short.Parse("2")); + Console.WriteLine("13工位存盘成功上位机完成置为2"); + } + } + break; + case 14: + if (plc.ReadInt16("DB110.DBW38").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW176").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW174").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW38", short.Parse("2")); + Console.WriteLine("14工位存盘成功上位机完成置为2"); + } + } + break; + case 15: + if (plc.ReadInt16("DB110.DBW42").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW180").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW178").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW42", short.Parse("2")); + Console.WriteLine("15工位存盘成功上位机完成置为2"); + } + } + break; + case 16: + if (plc.ReadInt16("DB110.DBW44").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW180").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW178").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW44", short.Parse("2")); + Console.WriteLine("16工位存盘成功上位机完成置为2"); + } + } + break; + case 17: + if (plc.ReadInt16("DB110.DBW48").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW184").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW182").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW48", short.Parse("2")); + Console.WriteLine("17工位存盘成功上位机完成置为2"); + } + } + break; + case 18: + if (plc.ReadInt16("DB110.DBW50").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW184").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW182").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW50", short.Parse("2")); + Console.WriteLine("18工位存盘成功上位机完成置为2"); + } + } + break; + case 19: + if (plc.ReadInt16("DB110.DBW54").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW188").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW186").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW54", short.Parse("2")); + Console.WriteLine("19工位存盘成功上位机完成置为2"); + } + } + break; + case 20: + if (plc.ReadInt16("DB110.DBW56").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW188").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW186").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW56", short.Parse("2")); + Console.WriteLine("20工位存盘成功上位机完成置为2"); + } + } + break; + case 21: + if (plc.ReadInt16("DB110.DBW60").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW192").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW190").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW60", short.Parse("2")); + Console.WriteLine("21工位存盘成功上位机完成置为2"); + } + } + break; + case 22: + if (plc.ReadInt16("DB110.DBW62").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW192").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW190").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW62", short.Parse("2")); + Console.WriteLine("22工位存盘成功上位机完成置为2"); + } + } + break; + case 23: + if (plc.ReadInt16("DB110.DBW66").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW196").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW194").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW66", short.Parse("2")); + Console.WriteLine("23工位存盘成功上位机完成置为2"); + } + } + break; + case 24: + if (plc.ReadInt16("DB110.DBW68").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW196").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW194").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW68", short.Parse("2")); + Console.WriteLine("24工位存盘成功上位机完成置为2"); + } + } + break; + case 25: + if (plc.ReadInt16("DB110.DBW72").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW200").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW198").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW72", short.Parse("2")); + Console.WriteLine("25工位存盘成功上位机完成置为2"); + } + } + break; + case 26: + if (plc.ReadInt16("DB110.DBW74").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW200").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW198").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW74", short.Parse("2")); + Console.WriteLine("26工位存盘成功上位机完成置为2"); + } + } + break; + case 27: + if (plc.ReadInt16("DB110.DBW78").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW204").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW202").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW78", short.Parse("2")); + Console.WriteLine("27工位存盘成功上位机完成置为2"); + } + } + break; + case 28: + if (plc.ReadInt16("DB110.DBW80").Content == 1)//1工位A + { + int batch = plc.ReadInt16("DB110.DBW204").Content;//工位批次 + Console.WriteLine($"工位批次:{batch}"); + decimal real_Weight = Convert.ToDecimal(plc.ReadInt16("DB110.DBW202").Content) / 1000;//实际重量 + Console.WriteLine($"实际重量:{real_Weight}"); + bool result = SaveToData(item, batch, real_Weight); + Console.WriteLine($"保存结果{result}"); + if (result) + { + //存盘成功上位机完成置为2 + plc.Write("DB110.DBW80", short.Parse("2")); + Console.WriteLine("28工位存盘成功上位机完成置为2"); + } + } + break; + + default: + break; + } + } + + //检量秤检查结束信息 + Xl_DownLoadPlan down = materialList.FirstOrDefault(); + if (down == null) return; + + if (down.IsPrenatalTest == 1) + { + Console.WriteLine($"产前自检检量称:{plc.ReadInt16("DB110.DBW90").Content}"); + if (plc.ReadInt16("DB110.DBW90").Content == 1) + { + double tcheckWeight = plc.ReadInt16("DB110.DBW210").Content / 1000;//检量秤重量 + int tcheckBatch = plc.ReadInt16("DB110.DBW212").Content;//当前执行的批次 + if (tcheckBatch == down.Plan_Num) + { + PlanDbHelper.UpdateCheckPlanCompletedQuantity(down, tcheckBatch, 8, DateTime.Now, tcheckWeight); + PlanDbHelper.UpdateCheckLrPlanCompletedQuantity(down, tcheckBatch, 8, DateTime.Now, tcheckWeight, down.FlagNum); + + plc.Write("DB110.DBW90",short.Parse("2")); + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"执行保存工位称量数据异常:{ex.Message}"); + } + } + + #endregion + + #region 存盘数据 + public static bool SaveToData(Xl_DownLoadPlan item, int batch, decimal real_Weight) + { + try + { + decimal error = real_Weight - item.Set_Weight; + + if (item.IsPrenatalTest == 1) + { + bool result = SaveCHeckData(item, batch, real_Weight, error); + return result; + } + return SavePlanInfo(item, batch, real_Weight, error); + } + catch (Exception) + { + return false; + } + } + #endregion + + #region 保存产前检查数据 + /// + /// 保存产前检查数据 + /// + /// + /// + /// + /// + /// + public static bool SaveCHeckData(Xl_DownLoadPlan item, int batch, decimal real_Weight, decimal error) + { + LR_plan lrPlan = PlanDbHelper.GetLrPlan(item.Plan_Id, batch, item.FlagNum); + if (lrPlan != null) + { + //保存物料 + LR_weigh weigh = new LR_weigh(); + weigh.Dosing_ID = batch.ToString(); + weigh.Plan_ID = item.Plan_Id; + weigh.Recipe_ID = item.Recipe_ID; + weigh.Bin_Serial = item.Station; + weigh.Material_ID = item.MaterialID; + weigh.Material_Name = item.Material_name; + weigh.Set_Weight = item.Set_Weight; + weigh.Set_Error = item.Set_Error; + weigh.Real_Weight = real_Weight; + weigh.Real_Error = error; + weigh.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + weigh.MainId = lrPlan.ID; + bool saveResult = SaveDataToBDb(item, weigh, lrPlan, null, null, 0); + if (saveResult) return true; + else return false; + + } + else + { + //保存计划 + LR_plan plan = new LR_plan(); + plan.Plan_Id = item.Plan_Id; + plan.Plan_Serial = item.Plan_Serial; + plan.Recipe_ID = item.Recipe_ID; + plan.Recipe_Name = item.Recipe_Name; + plan.Version = item.Version; + plan.Plan_Num = item.Plan_Num; + plan.Real_Num = batch; + plan.Shift_Id = item.Shift_Id; + plan.Shift_Class = item.Shift_Class; + plan.Total_Weight = real_Weight; + plan.Total_Error = error; + plan.IsRetransmission = item.IsRetransmission; + plan.Retransmission_Num = item.Retransmission_Num; + plan.FlagNum = item.FlagNum; + //保存物料 + LR_weigh weigh = new LR_weigh(); + weigh.Dosing_ID = batch.ToString(); + weigh.Plan_ID = item.Plan_Id; + weigh.Recipe_ID = item.Recipe_ID; + weigh.Bin_Serial = item.Station; + weigh.Material_ID = item.MaterialID; + weigh.Material_Name = item.Material_name; + weigh.Set_Weight = item.Set_Weight; + weigh.Set_Error = item.Set_Error; + weigh.Real_Weight = real_Weight; + weigh.Real_Error = error; + weigh.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + bool result = SaveDataToBDb(item, weigh, plan, null, null, 1); + + if (result) + { + return true; + } + else + { + return false; + } + } + } + + #endregion + + #region 根据料仓号实时更新料仓物料重量 + public static bool SaveDataToBDb(Xl_DownLoadPlan item, LR_weigh lrWeigh, LR_plan lrPlan, Pmt_Bin bin, Hw_BinAlarm alarm, int key) + { + try + { + IFreeSql fsql = FreeHelper.Instance; + { + if (item.IsPrenatalTest == 1) + { + if (key == 1) + { + fsql.Transaction(() => + { + var affrows = Convert.ToInt32(fsql.Insert(lrPlan).ExecuteIdentity()); + if (affrows < 1) + { + throw new Exception("存盘计划详细数据失败!");//抛出异常,回滚事务,事务退出 + } + lrWeigh.MainId = affrows;//获取主键 + affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + { + throw new Exception("存盘物料详细数据失败!"); //抛出异常,回滚事务,事务退出 + } + }); + } + else + { + fsql.Transaction(() => + { + var affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + if (affrows < 1) + throw new Exception("存盘物料详细数据失败!");//抛出异常,回滚事务,事务退出 + }); + } + } + else + { + if (key == 1) + { + fsql.Transaction(() => + { + var affrows = Convert.ToInt32(fsql.Insert(lrPlan).ExecuteIdentity()); + if (affrows < 1) + throw new Exception("存盘计划详细数据失败!");//抛出异常,回滚事务,事务退出 + affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘物料详细数据失败!"); //抛出异常,回滚事务,事务退出 + }); + } + else + { + fsql.Transaction(() => + { + var affrows = fsql.Insert(lrWeigh).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘物料详细数据失败!"); //抛出异常,回滚事务,事务退出 + affrows = fsql.Update(bin.Bin_Serial).Set(a => a.BinWeight, bin.BinWeight).ExecuteAffrows(); + //判断是否插入成功 + if (affrows < 1) + throw new Exception("存盘物料详细数据失败!"); //抛出异常,回滚事务,事务退出 + + }); + } + } + return true; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + return false; + } + } + #endregion + + #region 保存非产前检查数据 + /// + /// 保存非产前检查数据 + /// + /// + /// + /// + /// + /// + private static bool SavePlanInfo(Xl_DownLoadPlan item, int batch, decimal real_Weight, decimal error) + { + try + { + LR_plan lrPlan = PlanDbHelper.GetLrPlan(item.Plan_Id, batch, item.FlagNum); + if (lrPlan != null) + { + decimal totalWeight = lrPlan.Total_Weight + real_Weight; + decimal totalError = lrPlan.Total_Error + error; + + //保存物料 + LR_weigh weigh = new LR_weigh(); + weigh.Dosing_ID = batch.ToString(); + weigh.Plan_ID = item.Plan_Id; + weigh.Recipe_ID = item.Recipe_ID; + weigh.Bin_Serial = item.Station; + weigh.Material_ID = item.MaterialID; + weigh.Material_Name = item.Material_name; + weigh.Set_Weight = item.Set_Weight; + weigh.Set_Error = item.Set_Error; + weigh.Real_Weight = real_Weight; + weigh.Real_Error = error; + weigh.MainId = lrPlan.ID; + weigh.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + lrPlan.Plan_Id = item.Plan_Id; + lrPlan.Real_Num = batch; + lrPlan.Total_Weight = totalWeight; + lrPlan.Total_Error = totalError; + + var binInfo = PlanDbHelper.GetFristBinInfo(item.Station); + binInfo.BinWeight = binInfo.BinWeight - real_Weight; + + Hw_BinAlarm alarm = new Hw_BinAlarm(); + alarm.BinId = item.Station; + alarm.BinName = item.StationName; + alarm.MaterialCode = item.MaterialID; + alarm.MaterialName = item.Material_name; + alarm.Weights = real_Weight; + alarm.TypeName = "出库"; + + return PlanDbHelper.UpdatePlanDetail(item, lrPlan, weigh, binInfo, alarm, 0); + } + else + { + //保存计划 + LR_plan plan = new LR_plan(); + plan.Plan_Id = item.Plan_Id; + plan.Plan_Serial = item.Plan_Serial; + plan.Recipe_ID = item.Recipe_ID; + plan.Recipe_Name = item.Recipe_Name; + plan.Version = item.Version; + plan.Plan_Num = item.Plan_Num; + plan.Real_Num = batch; + plan.Shift_Id = item.Shift_Id; + plan.Shift_Class = item.Shift_Class; + plan.Total_Weight = real_Weight; + plan.Total_Error = error; + plan.IsRetransmission = item.IsRetransmission; + plan.Retransmission_Num = item.Retransmission_Num; + + //保存物料 + LR_weigh weigh = new LR_weigh(); + weigh.Dosing_ID = batch.ToString(); + weigh.Plan_ID = item.Plan_Id; + weigh.Recipe_ID = item.Recipe_ID; + weigh.Bin_Serial = item.Station; + weigh.Material_ID = item.MaterialID; + weigh.Material_Name = item.Material_name; + weigh.Set_Weight = item.Set_Weight; + weigh.Set_Error = item.Set_Error; + weigh.Real_Weight = real_Weight; + weigh.Real_Error = error; + weigh.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + var binInfo = PlanDbHelper.GetFristBinInfo(item.Station); + binInfo.Equip_Code = "00"; + binInfo.BinWeight = binInfo.BinWeight - real_Weight; + + Hw_BinAlarm alarm = new Hw_BinAlarm(); + alarm.BinId = binInfo.Bin_Serial; + alarm.BinName = binInfo.Bin_Name; + alarm.MaterialCode = binInfo.Material_ID; + alarm.MaterialName = item.Material_name; + alarm.Weights = real_Weight; + alarm.TypeName = "出库"; + + return PlanDbHelper.UpdatePlanDetail(item, plan, weigh, binInfo, alarm, 1); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } + + #endregion + + } +} diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Show/XlInitAction.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Show/XlInitAction.cs index 57242a3..c74b387 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Show/XlInitAction.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Show/XlInitAction.cs @@ -1,9 +1,11 @@ using DevExpress.Data.Filtering.Helpers; using DevExpress.Office.Utils; using Mesnac.Action.Base; +using Mesnac.Action.ChemicalWeighing.Alarm; using Mesnac.Action.ChemicalWeighing.BinManage; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.PptPlan; +using Mesnac.Action.ChemicalWeighing.FinishBatch; using Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper; using Mesnac.Action.ChemicalWeighing.Product.XlPlan; using Mesnac.Communication; @@ -12,6 +14,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Security.Claims; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -25,6 +28,7 @@ namespace Mesnac.Action.ChemicalWeighing.Show { #region 字段定义 bool flag = true; + bool alarm = true; public static bool IsFirstRun = true; //是否首次运行 private RuntimeParameter _runtime; //private DbMCControl _planDateControl = null; //计划日期 @@ -113,6 +117,12 @@ namespace Mesnac.Action.ChemicalWeighing.Show timer2.AutoReset = true; timer2.Enabled = true; timer2.Start(); + + System.Timers.Timer timer3 = new System.Timers.Timer(5000); + timer3.Elapsed += new ElapsedEventHandler(UpdateAlarmData); + timer3.AutoReset = true; + timer3.Enabled = true; + timer3.Start(); } #region 方法定义 @@ -166,9 +176,10 @@ namespace Mesnac.Action.ChemicalWeighing.Show var labeTaskState = base.GetControlById("MCLabeTaskState") as System.Windows.Forms.Label;//任务状态 List materialList = PlanHelper.GetPlanList();//正在执行的任务 - if (materialList == null&& materialList.Count()==0) { return; } + if (materialList == null|| materialList.Count()==0) { return; } var item = materialList.FirstOrDefault(); + if (!string.IsNullOrEmpty(planFlag)) { if (item != null) @@ -185,7 +196,8 @@ namespace Mesnac.Action.ChemicalWeighing.Show } else { - planFlag = item.Plan_Id; + if (item!=null) planFlag = item.Plan_Id; + } if (item != null) @@ -220,8 +232,8 @@ namespace Mesnac.Action.ChemicalWeighing.Show var data = PlanHelper.GetExecPlanDataTable(); if (data != null && data.Rows.Count > 0) { - clientGridControl.BaseControl.BindDataSource = null; + ((DataGridView)clientGridControl.BaseControl).AutoGenerateColumns = false; clientGridControl.BaseControl.BindDataSource = data; SetWeight(materialList); } @@ -795,6 +807,37 @@ namespace Mesnac.Action.ChemicalWeighing.Show } + + private void UpdateAlarmData(object sender, ElapsedEventArgs e) + { + try + { + if (alarm) + { + try + { + alarm = false; + #region 更新计划状态 + AlarmHelper.QueryUpdateAlarm(); + } + catch (Exception ex) + { + Console.WriteLine($"异常:{ex.Message}"); + } + finally + { + alarm = true; + } + + #endregion + } + } + catch (Exception ex) + { + ICSharpCode.Core.LoggingService.Error(ex.Message, ex); + } + } + #region 设置(网格控件)计划状态背景色 /// /// 设置计划状态背景色 diff --git a/Main/MCEdit/Data/MCProject/nodeDataSource.xml b/Main/MCEdit/Data/MCProject/nodeDataSource.xml index 91d4223..479381b 100644 --- a/Main/MCEdit/Data/MCProject/nodeDataSource.xml +++ b/Main/MCEdit/Data/MCProject/nodeDataSource.xml @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/Main/MCEdit/Data/MCProject/nodeDevice.xml b/Main/MCEdit/Data/MCProject/nodeDevice.xml index ee6f693..d7ad5ee 100644 --- a/Main/MCEdit/Data/MCProject/nodeDevice.xml +++ b/Main/MCEdit/Data/MCProject/nodeDevice.xml @@ -7,7 +7,7 @@ - + diff --git a/Main/MCRun/MCRun.csproj b/Main/MCRun/MCRun.csproj index 2c363b2..9ad4260 100644 --- a/Main/MCRun/MCRun.csproj +++ b/Main/MCRun/MCRun.csproj @@ -6,7 +6,7 @@ 8.0.30703 2.0 {91C803A6-90D1-4361-8B92-9AFD9E766411} - WinExe + Exe Properties MCRun MCRun @@ -96,6 +96,9 @@ False ..\..\Libraries\Host.dll + + E:\普力通处理\dll\HslCommunication.dll + False ..\..\PlugInPlatform\ICSharpCode.Core.dll