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 Admin.Core.Tasks; using Aucma.Core.HwPLc; using Aucma.Core.OldBoxFoam.Business; using Aucma.Core.OldBoxFoam.Models; using Aucma.Core.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows.Documents; /** * 老发泡线监控 * */ namespace Aucma.Core.OldBoxFoam.ViewModels { public partial class OldBoxFoamPageViewModel : ObservableObject { IBoxFoamDataServices? _oldBoxFoamDataServices; IOldBoxFoamTypeServices _oldBoxFoamTypeServices; public static System.Threading.Timer GetStoreTimer; private readonly IBoxFoamPlanServices _boxFoamPlanServices; private readonly ISysUserInfoServices _sysUserInfoServices; private readonly ICurrentTeamTimeServices _currentTeamTimeServices; private readonly IBoxFoamDataRecordServices _boxFoamDataRecordServices; private static readonly log4net.ILog log = LogManager.GetLogger(typeof(OldBoxFoamPageViewModel)); System.Timers.Timer timer1 = new System.Timers.Timer(2000); System.Timers.Timer timer2 = new System.Timers.Timer(10000); public OldBoxFoamPageViewModel() { _oldBoxFoamDataServices = App.ServiceProvider.GetService(); _oldBoxFoamTypeServices = App.ServiceProvider.GetService(); _boxFoamPlanServices = App.ServiceProvider.GetService(); _sysUserInfoServices = App.ServiceProvider.GetService(); _currentTeamTimeServices = App.ServiceProvider.GetService(); _currentTeamTimeServices = App.ServiceProvider.GetService(); _boxFoamDataRecordServices = App.ServiceProvider.GetService(); //AucamTaskService.RefreshOldBoxFoamDataDelegateEvent += OldBoxFoamOnLoad3; timer1.Elapsed += new System.Timers.ElapsedEventHandler(OldBoxFoamOnLoad); //到达时间的时候执行事件; timer1.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer1.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer1.Start();//timer.Start()的内部原理还是设置timer.Enabled = true; 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; } #region 初始化加载数据 private void OldBoxFoamOnLoad(object? sender, ElapsedEventArgs e) { try { _fixtureName.Clear(); _production.Clear(); _beat.Clear(); _statusColor.Clear(); if (Collection.listFixtureStatus == null) return; int i = 0; var prderList= Collection.listFixtureStatus; int num = prderList.Sum(d => d.Yield); Console.WriteLine($"总产量{prderList.Sum(d => d.Yield)}"); _totle = $"总产量:{num}"; foreach (var item in prderList) { //OldBoxFoamDataModel model = new OldBoxFoamDataModel(); //model.ObjId = item.ObjId; //model.Fixtureboxtype = item.FixtureBoxType; //model.Fixturestatus = item.Status; //model.Production = item.Yield; //model.CuringTimeSettingValue = item.PlanCuringTime.ToString(); if (item.Status == 1)//生产 { _statusColor.Add("green"); } else { _statusColor.Add("red"); } _fixtureName.Add(item.FixtureBoxType); _production.Add(item.Yield); //_beat.Add(item.BoxBeat); i = +item.Yield; } _totle = $"总产量:{i}"; } catch (Exception ex) { log.Error("获取库存接口数据异常." + ex.Message); } finally { if (GetStoreTimer != null) { GetStoreTimer.Change(20000, Timeout.Infinite); } } } public async Task OldBoxFoamOnLoad3(List list) { try { if (list == null) return; System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { _fixtureName.Clear(); _production.Clear(); _beat.Clear(); _statusColor.Clear(); OldBoxFoamDataGrid.Clear(); int i = 0; var prderList = list.OrderBy(d => d.MainId); foreach (var item in prderList) { OldBoxFoamDataModel model = new OldBoxFoamDataModel(); model.ObjId = item.ObjId; model.Fixtureboxtype = item.Fixtureboxtype; model.Fixturestatus = item.Fixturestatus; model.Production = item.Production; model.CuringTimeSettingValue = item.CuringTimeSettingValue; if (item.Fixturestatus == 1)//停止 { _statusColor.Add("green"); } else { _statusColor.Add("red"); } _fixtureName.Add(item.Fixtureboxtype); _production.Add(item.Production); _beat.Add(item.BoxBeat); i = +item.Production; } })); } catch (Exception ex) { log.Error("获取库存接口数据异常." + ex.Message); } finally { if (GetStoreTimer != null) { GetStoreTimer.Change(10000, Timeout.Infinite); } } } 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 /// /// 将采集到的数据保存 /// /// public async void RealTimeFoamingTask(object? sender, ElapsedEventArgs e) { string productLineCode = Appsettings.app("StationInfo", "ProductLineCode"); if (Collection.listFixtureStatus == null) return; DateTime now = DateTime.Now; CurrentTeamTime time = await _currentTeamTimeServices.FirstAsync(d => d.StartTime <= now && d.EndTime >= now); if (time != null) { ExecHourData(productLineCode); } else { ShiftChange(productLineCode); } } 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); await _currentTeamTimeServices.UpdateAsync(currentTeamTime); //可以使用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; }); int result = await _boxFoamDataRecordServices.AddAsync(recordList); if (result > 0) { var obj2 = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc1")); if (obj2 == null) { return; } //obj2.plc.WriteInt16("M216", "1");//换班 数据清空 //清空 当班计划 await _oldBoxFoamDataServices.DeletesAsync(list); } } private async void ExecHourData(string productLineCode) { var boxList = Collection.listFixtureStatus; foreach (var item in boxList) { OldBoxFoamType space = await _oldBoxFoamTypeServices.FirstAsync(d => d.Boxtype == item.FixtureBoxType); //根据夹具找货道,根据货道找物料 if (space != null) { BoxFoamPlan plan = _boxFoamPlanServices.FirstAsync(d => d.MaterialCode == space.MaterialCode).Result; if (plan != null) { plan.CompleteAmount = plan.CompleteAmount + 1; await _boxFoamPlanServices.UpdateAsync(plan); //更新小时数据 BoxFoamData bfd = await _oldBoxFoamDataServices.FirstAsync(d => d.Fixtureboxtype == item.FixtureBoxType && d.MainId == item.ObjId); if (bfd != null) { UpdateBoxData(item, bfd); } else { SaveBoxData(productLineCode, item); } } else { //更新小时数据 BoxFoamData bfd = await _oldBoxFoamDataServices.FirstAsync(d => d.Fixtureboxtype == item.FixtureBoxType && d.MainId == item.ObjId); if (bfd != null) { UpdateBoxData(item, bfd); } else { SaveBoxData(productLineCode, item); } } } } } private async void UpdateBoxData(FixtureStatus item, BoxFoamData bfd) { bfd.Production = item.Yield; switch (GetTeamHour().Seq) { 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; } 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(); switch (GetTeamHour().Seq) { 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; } } }