using Admin.Core.Common; using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Model.Model_New; using Admin.Core.Model.ViewModels; using Admin.Core.Service; using Aucma.Core.HwPLc; using Aucma.Core.OldBoxFoam.Business; using Aucma.Core.OldBoxFoam.Models; using CommunityToolkit.Mvvm.ComponentModel; using log4net; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Log4Net.AspNetCore.Extensions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Timers; using static NPOI.HSSF.Util.HSSFColor; /** * 老发泡线监控 * */ namespace Aucma.Core.OldBoxFoam.ViewModels { public partial class OldBoxFoamPageViewModel : ObservableObject { IBoxFoamDataServices? _oldBoxFoamDataServices; IOldBoxFoamTypeServices _oldBoxFoamTypeServices; private readonly IBoxFoamPlanServices _boxFoamPlanServices; private readonly ISysUserInfoServices _sysUserInfoServices; private readonly ICurrentTeamTimeServices _currentTeamTimeServices; private readonly IBoxFoamDataRecordServices _boxFoamDataRecordServices; private readonly IBoxFoamDataRealTimeServices? _boxFoamDataRealTimeServices; private readonly IBoxFoamDataRealTimeHistoryServices _boxFoamDataRealTimeHistoryServices; private static readonly log4net.ILog log = LogManager.GetLogger(typeof(OldBoxFoamPageViewModel)); System.Timers.Timer timer2 = new System.Timers.Timer(10000); int totalAmount = 0; bool flag = true; private Collection collection = new Collection(); public OldBoxFoamPageViewModel() { _oldBoxFoamDataServices = App.ServiceProvider.GetService(); _oldBoxFoamTypeServices = App.ServiceProvider.GetService(); _boxFoamPlanServices = App.ServiceProvider.GetService(); _sysUserInfoServices = App.ServiceProvider.GetService(); _currentTeamTimeServices = App.ServiceProvider.GetService(); _boxFoamDataRealTimeServices = App.ServiceProvider.GetService(); _boxFoamDataRecordServices = App.ServiceProvider.GetService(); _boxFoamDataRealTimeHistoryServices = App.ServiceProvider.GetService(); //AucamTaskService.RefreshOldBoxFoamDataDelegateEvent += OldBoxFoamOnLoad3; collection.RefreshFoamMachineEvent += info => { OldBoxFoamOnLoad(info); }; timer2.Elapsed += new System.Timers.ElapsedEventHandler(RealTimeFoamingTask); //到达时间的时候执行事件; timer2.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer2.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer2.Start();//timer.Start()的内部原理还是设置timer.Enabled = true; //Task.Run(() => //{ // Thread.Sleep(1000 * 5); // collection.startCollect(); //}); DayShiftInfoCut(); NightShiftInfoCut(); } #region 初始化加载数据 private void OldBoxFoamOnLoad(List prderList) { try { int num = prderList.Sum(d => d.Yield); if (prderList.Count==0) return; totalAmount = 0; _fixtureName.Clear(); _production.Clear(); _beat.Clear(); _statusColor.Clear(); foreach (var item in prderList) { switch (item.Status) { case 1: _statusColor.Add("Yellow"); Console.WriteLine("Yellow"); break; case 2: _statusColor.Add("Green"); Console.WriteLine("Green"); break; case 3: _statusColor.Add("Red"); Console.WriteLine("Red"); break; default: _statusColor.Add("Gray"); Console.WriteLine("Gray"); break; } _fixtureName.Add(item.FixtureBoxType); _production.Add(item.Yield); totalAmount = totalAmount + item.Yield; } Totle = totalAmount.ToString(); } catch (Exception ex) { log.Error("获取库存接口数据异常." + ex.Message); } } public async Task OldBoxFoamTypeOnLoad() { var list =await _oldBoxFoamTypeServices.QueryAsync(); if (list == null) return; foreach (var item in list) { OldBoxFoamTypeModel model = new OldBoxFoamTypeModel(); model.ObjId = item.ObjId; model.Local = item.Local; model.Boxtype = item.Boxtype; model.Storeamount = item.Storeamount; _oldBoxFoamTypeDataGrid.Add(model); } } public Task OldBoxFoamTypeOnLoad3(List list) { if (list == null) return Task.CompletedTask; System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { OldBoxFoamTypeDataGrid.Clear(); foreach (var item in list) { OldBoxFoamTypeModel model = new OldBoxFoamTypeModel(); model.ObjId = item.ObjId; model.Local = item.Local; model.Boxtype = item.Boxtype; model.Storeamount = item.Storeamount; OldBoxFoamTypeDataGrid.Add(model); } })); return Task.CompletedTask; } #endregion #region 初始化发泡线 datagrid private ObservableCollection _oldBoxFoamDataGrid = new ObservableCollection(); public ObservableCollection OldBoxFoamDataGrid { get => _oldBoxFoamDataGrid; set => SetProperty(ref _oldBoxFoamDataGrid, value); } #endregion #region 初始化泡前库 datagrid private ObservableCollection _oldBoxFoamTypeDataGrid = new ObservableCollection(); public ObservableCollection OldBoxFoamTypeDataGrid { get => _oldBoxFoamTypeDataGrid; set => SetProperty(ref _oldBoxFoamTypeDataGrid, value); } #endregion #region 夹具状态 private ObservableCollection _statusColor = new ObservableCollection(); public ObservableCollection StatusColor { get => _statusColor; set => SetProperty(ref _statusColor, value); } #endregion #region 夹具名称 private ObservableCollection _fixtureName = new ObservableCollection(); public ObservableCollection FixtureName { get => _fixtureName; set => SetProperty(ref _fixtureName, value); } #endregion #region 夹具产量 /// ///夹具产量 /// private ObservableCollection _production = new ObservableCollection(); public ObservableCollection Production { get => _production; set => SetProperty(ref _production, value); } #endregion #region 节拍 /// ///夹具产量 /// private ObservableCollection _beat = new ObservableCollection(); public ObservableCollection Beat { get => _beat; set => SetProperty(ref _beat, value); } #endregion #region 总计 private string _totle; public string Totle { get => _totle; set => SetProperty(ref _totle, value); } #endregion Semaphore semaphore = new Semaphore(1, 1); #region 原数据采集方法 /// /// 将采集到的数据保存 /// /// public async void RealTimeFoamingTask(object? sender, ElapsedEventArgs e) { semaphore.WaitOne(); try { string productLineCode = Appsettings.app("StationInfo", "ProductLineCode"); //if (Collection.listFixtureStatus == null || Collection.listFixtureStatus.Count == 0) return; DateTime now = DateTime.Now; CurrentTeamTime time = await _currentTeamTimeServices.FirstAsync(d => d.StartTime <= now && d.EndTime >= now); collection.startCollect(); //if (time != null) //{ // ExecHourData(productLineCode); //} //else //{ // ShiftChange(productLineCode); //} } catch (Exception ex) { log.Error("拆分异常:" + ex.Message); Console.WriteLine(ex.Message.ToString()); } finally { semaphore.Release(); } } private async void ShiftChange(string productLineCode) { //切换班组计划 var list = await _oldBoxFoamDataServices.QueryAsync(d => d.ProductLineCode == productLineCode); List recordList = new List(); var currentTeamTimeList = await _sysUserInfoServices.GetTeamData(); if (currentTeamTimeList.Count == 0) return; //更新计划表操作 CurrentTeamTime currentTeamTime = await _currentTeamTimeServices.FirstAsync(); currentTeamTime.TeamName = currentTeamTimeList.FirstOrDefault().TeamName; currentTeamTime.StartTime = currentTeamTimeList.Min(d => d.StartTime); currentTeamTime.EndTime = currentTeamTimeList.Max(d => d.EndTime); //可以使用Mapping 映射 list.ForEach(d => { BoxFoamDataRecord record = new BoxFoamDataRecord(); record.OrderCode = d.OrderCode; record.ProductLineCode = d.ProductLineCode; record.Fixturestatus = d.Production; record.CuringTimeSettingValue = d.CuringTimeSettingValue; record.ActualValue = d.ActualValue; record.AnHour = d.AnHour; record.TwoHour = d.TwoHour; record.ThreeHour = d.ThreeHour; record.FourHour = d.FourHour; record.FiveHour = d.FiveHour; record.SixHour = d.SixHour; record.SevenHour = d.SevenHour; record.EightHour = d.EightHour; record.NineHour = d.NineHour; record.TenHour = d.TenHour; record.ElevenHour = d.ElevenHour; record.TwelveHour = d.TwelveHour; record.BoxFixturesideplate = d.BoxFixturesideplate; record.BoxClosebetatemperature = d.BoxClosebetatemperature; record.BoxBeat = d.BoxBeat; record.CreateTime = DateTime.Now; }); await _currentTeamTimeServices.UpdateAsync(currentTeamTime); if (recordList == null) return; int result = await _boxFoamDataRecordServices.AddAsync(recordList); if (result > 0) { var obj1 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc1")); if (obj1 != null) { if (obj1.plc.IsConnected) { obj1.plc.WriteInt16("M216", "1");//换班 数据清空 } } var obj2 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc2")); if (obj2 != null) { if (obj2.plc.IsConnected) { obj2.plc.WriteInt16("M216", "1");//换班 数据清空 } } var obj3 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc3")); if (obj3 != null) { if (obj3.plc.IsConnected) { obj3.plc.WriteInt16("M216", "1");//换班 数据清空 } } var obj4 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc4")); if (obj4 != null) { if (obj4.plc.IsConnected) { obj4.plc.WriteInt16("M216", "1");//换班 数据清空 } } //清空 当班计划 await _oldBoxFoamDataServices.UpdateAsync(list); var planList=_boxFoamPlanServices.QueryAsync().Result; planList.ForEach(d => { d.ShiftType = 0; }); await _boxFoamPlanServices.UpdateAsync(planList); } } #endregion #region 原计划完工逻辑 Delete By wenjy 2024-01-07 10:11:00 //private async void ExecHourData(string productLineCode) //{ // try // { // List bList = new List(); // var collectionList = Collection.listFixtureStatus; // if (collectionList.Count == 0) return; // OldBoxFoamOnLoad(collectionList); // bList.AddRange(collectionList); // List bfds = await _oldBoxFoamDataServices.QueryAsync(); // List spaceList = await _oldBoxFoamTypeServices.QueryAsync(); // List planList = await _boxFoamPlanServices.QueryAsync(); // for (int j = 0; j < bList.Count; j++) // { // try // { // OldBoxFoamType space = spaceList.FirstOrDefault(d => d.Boxtype == bList[j].FixtureBoxType); // //根据夹具找货道,根据货道找物料 // if (space != null) // { // BoxFoamPlan plan = planList.FirstOrDefault(d => d.MaterialCode == space.MaterialCode && d.PlanAmount > d.CompleteAmount); // if (plan != null) // { // int complateAmount = 0; // //更新小时数据 // BoxFoamData bfd = bfds.FirstOrDefault(d => d.Fixtureboxtype == bList[j].FixtureBoxType && d.MainId == bList[j].ObjId); // if (bfd != null) // { // //更新计划数量 // complateAmount = bList[j].Yield - bfd.Production; // UpdateBoxData(bList[j], bfd); // } // else // { // SaveBoxData(productLineCode, bList[j]); // } // plan.CompleteAmount = plan.CompleteAmount + complateAmount; // await _boxFoamPlanServices.UpdateAsync(plan); // } // else // { // //更新小时数据 // BoxFoamData bfd = bfds.FirstOrDefault(d => d.Fixtureboxtype == bList[j].FixtureBoxType && d.MainId == bList[j].ObjId); // if (bfd != null) // { // UpdateBoxData(bList[j], bfd); // } // else // { // SaveBoxData(productLineCode, bList[j]); // } // } // } // else // { // //更新小时数据 // BoxFoamData bfd = bfds.FirstOrDefault(d => d.Fixtureboxtype == bList[j].FixtureBoxType && d.MainId == bList[j].ObjId); // if (bfd != null) // { // UpdateBoxData(bList[j], bfd); // } // else // { // SaveBoxData(productLineCode, bList[j]); // } // } // } // catch { } // } // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } //} //private async void UpdateBoxData(FixtureStatus item, BoxFoamData bfd) //{ // int hour = GetTeamHour(DateTime.Now).Seq; // switch (hour) // { // case 1: // bfd.AnHour = item.Yield - bfd.Production; // break; // case 2: // bfd.TwoHour = item.Yield - bfd.Production; // break; // case 3: // bfd.ThreeHour = item.Yield - bfd.Production; // break; // case 4: // bfd.FourHour = item.Yield - bfd.Production; // break; // case 5: // bfd.FiveHour = item.Yield - bfd.Production; // break; // case 6: // bfd.SixHour = item.Yield - bfd.Production; // break; // case 7: // bfd.SevenHour = item.Yield - bfd.Production; // break; // case 8: // bfd.EightHour = item.Yield - bfd.Production; // break; // case 9: // bfd.NineHour = item.Yield - bfd.Production; // break; // case 10: // bfd.TenHour = item.Yield - bfd.Production; // break; // case 11: // bfd.ElevenHour = item.Yield - bfd.Production; // break; // case 12: // bfd.TwelveHour = item.Yield - bfd.Production; // break; // default: // break; // } // bfd.Production = item.Yield; // await _oldBoxFoamDataServices.UpdateAsync(bfd); //} //private async void SaveBoxData(string productLineCode, FixtureStatus item) //{ // BoxFoamData bfdata = new BoxFoamData(); // bfdata.MainId = item.ObjId; // bfdata.ProductLineCode = productLineCode; // bfdata.Fixtureboxtype = item.FixtureBoxType; // bfdata.Fixturestatus = item.Status; // bfdata.Production = item.Yield; // bfdata.CuringTimeSettingValue = item.PlanCuringTime.ToString(); // bfdata.ActualValue = item.RealCuringTime.ToString(); // int hour = GetTeamHour(DateTime.Now).Seq; // switch (hour) // { // case 1: // bfdata.AnHour = item.Yield = bfdata.Production; // break; // case 2: // bfdata.TwoHour = item.Yield = bfdata.Production; // break; // case 3: // bfdata.ThreeHour = item.Yield = bfdata.Production; // break; // case 4: // bfdata.FourHour = item.Yield = bfdata.Production; // break; // case 5: // bfdata.FiveHour = item.Yield = bfdata.Production; // break; // case 6: // bfdata.SixHour = item.Yield = bfdata.Production; // break; // case 7: // bfdata.SevenHour = item.Yield = bfdata.Production; // break; // case 8: // bfdata.EightHour = item.Yield = bfdata.Production; // break; // case 9: // bfdata.NineHour = item.Yield = bfdata.Production; // break; // case 10: // bfdata.TenHour = item.Yield = bfdata.Production; // break; // case 11: // bfdata.ElevenHour = item.Yield = bfdata.Production; // break; // case 12: // bfdata.TwelveHour = item.Yield = bfdata.Production; // break; // default: // break; // } // bfdata.MainId = item.ObjId; // await _oldBoxFoamDataServices.AddAsync(bfdata); //} //public CurrentTeamTimeView GetTeamHour() //{ // DateTime now = DateTime.Now; // var list = _sysUserInfoServices.GetTeamData().Result; // if (list != null) // { // //var obj = list.FirstOrDefault().EndTime.ToString("HH:mm"); // var view = list.FirstOrDefault(); // return view; // } // return null; //} //public CurrentTeamTimeView GetTeamHour(DateTime now) //{ // var obj = _sysUserInfoServices.GetTeamData(now).Result; // if (obj != null) // { // return obj; // } // return null; //} #endregion /// /// 白班信息切换 /// private void DayShiftInfoCut() { DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 08, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += DayShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); } private void DayShiftTimerCallback(object sender, ElapsedEventArgs e) { Console.WriteLine("切换为白班!"); UpdateShiftInfo(1); #region 重新定义Timer,进行第二天执行 DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 08, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Elapsed += DayShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); #endregion } /// /// 夜班班组切换 /// private void NightShiftInfoCut() { DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 20, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += NightShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); } private void NightShiftTimerCallback(object sender, ElapsedEventArgs e) { Console.WriteLine("切换为夜班!"); UpdateShiftInfo(2); #region 重新定义Timer,进行第二天执行 DateTime now = DateTime.Now; DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 20, 00, 00); if (now > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } TimeSpan timeToGo = scheduledTime - now; int dueTime = (int)timeToGo.TotalMilliseconds; System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Elapsed += NightShiftTimerCallback; timer.AutoReset = true; timer.Interval = dueTime; timer.Start(); #endregion } /// /// 班组切换 /// /// 班组类型:1-白班;2-夜班 private void UpdateShiftInfo(int shiftType) { log.Warn($"{(shiftType == 1 ? "白班" :"夜班")}班组切换,开始处理切换逻辑"); try { //下发PLC清空产量信号 #region 清空PLC产量 var obj1 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc1")); if (obj1 != null) { if (obj1.plc.IsConnected) { var flag = obj1.plc.WriteInt16("M216", "1");//换班 数据清空 log.Warn($"班组切换清空A区产量数据:{(flag == true ? "成功" : "失败")}"); } } var obj2 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc2")); if (obj2 != null) { if (obj2.plc.IsConnected) { bool flag = obj2.plc.WriteInt16("M216", "1");//换班 数据清空 log.Warn($"班组切换清空B区产量数据:{(flag == true ? "成功" : "失败")}"); } } var obj3 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc3")); if (obj3 != null) { if (obj3.plc.IsConnected) { var flag = obj3.plc.WriteInt16("M216", "1");//换班 数据清空 log.Warn($"班组切换清空C区产量数据:{(flag == true ? "成功" : "失败")}"); } } var obj4 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc4")); if (obj4 != null) { if (obj4.plc.IsConnected) { var flag = obj4.plc.WriteInt16("M216", "1");//换班 数据清空 log.Warn($"班组切换清空D区产量数据:{(flag == true ? "成功" : "失败")}"); } } #endregion //小时统计报表数据存至记录表,清空小时统计报表 var hourDataList = _oldBoxFoamDataServices.Query(x => x.ProductLineCode == "CX_01" && x.StationCode == "1005"); List hourRecordList = new List(); if (hourDataList != null) { foreach (var item in hourDataList) { BoxFoamDataRecord hourRecord = new BoxFoamDataRecord(); hourRecord.OrderCode = item.OrderCode; hourRecord.ProductLineCode = item.ProductLineCode; hourRecord.Fixturestatus = item.Production; hourRecord.CuringTimeSettingValue = item.CuringTimeSettingValue; hourRecord.ActualValue = item.ActualValue; hourRecord.AnHour = item.AnHour; hourRecord.TwoHour = item.TwoHour; hourRecord.ThreeHour = item.ThreeHour; hourRecord.FourHour = item.FourHour; hourRecord.FiveHour = item.FiveHour; hourRecord.SixHour = item.SixHour; hourRecord.SevenHour = item.SevenHour; hourRecord.EightHour = item.EightHour; hourRecord.NineHour = item.NineHour; hourRecord.TenHour = item.TenHour; hourRecord.ElevenHour = item.ElevenHour; hourRecord.TwelveHour = item.TwelveHour; hourRecord.BoxFixturesideplate = item.BoxFixturesideplate; hourRecord.BoxClosebetatemperature = item.BoxClosebetatemperature; hourRecord.BoxBeat = item.BoxBeat; hourRecord.CreateTime = DateTime.Now; hourRecordList.Add(hourRecord); bool deleteFlag = _oldBoxFoamDataServices.DeleteAsync(item).Result; if (deleteFlag) { log.Warn($"{item.Fixtureboxtype}:小时数据清除成功"); } else { log.Warn($"{item.Fixtureboxtype}:小时数据清除失败"); } } } int addFlag = _boxFoamDataRecordServices.AddAsync(hourRecordList).Result; if (addFlag > 0) { log.Warn("班组切换小时数据记录保存成功"); } else { log.Warn("班组切换小时数据记录保存失败"); } //当班计划,班组计划设为0 var planInfoList = _boxFoamPlanServices.QueryAsync(x => x.ProductLineCode == "CX_01" && x.StationCode == "1005").Result; if (planInfoList != null) { foreach (var plan in planInfoList) { plan.ShiftType = 0; bool updateFlag = _boxFoamPlanServices.UpdateAsync(plan).Result; if (updateFlag) { log.Warn($"计划:{plan.OrderCode},班组切换标识更新成功"); } else { log.Warn($"计划:{plan.OrderCode},班组切换标识更新失败"); } } } }catch(Exception ex) { log.Warn($"{(shiftType == 1 ? "白班" : "夜班")}班组切换,切换逻辑处理异常:{ex.Message}"); } } } }