You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AUCMA_SCADA/Aucma.Core.BoxFoam/ViewModels/FoamMonitorPageViewModel.cs

517 lines
19 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using LiveCharts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
1 year ago
using System.Collections.ObjectModel;
1 year ago
using Admin.Core.IService;
using Admin.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Admin.Core.Common;
using Admin.Core.Tasks;
1 year ago
using Aucma.Core.BoxFoam.Business;
using System.Windows.Shapes;
using System.Timers;
using Aucma.Core.BoxFoam.Models;
using Admin.Core.Model;
using System.Threading;
1 year ago
using Aucma.Core.HwPLc;
namespace Aucma.Core.BoxFoam.ViewModels
{
public partial class FoamMonitorPageViewModel : ObservableObject
{
1 year ago
private readonly IBoxFoamDataServices _boxFoamDataServices;
1 year ago
private readonly ICurrentTeamTimeServices _currentTeamTimeServices;
private readonly IBoxFoamDataServices? _oldBoxFoamDataServices;
private readonly IBoxFoamPlanServices _boxFoamPlanServices;
private readonly ISysUserInfoServices _sysUserInfoServices;
private readonly IBoxFoamDataRecordServices _boxFoamDataRecordServices;
private readonly IBoxFoamDataRealTimeServices? _boxFoamDataRealTimeServices;
1 year ago
private readonly IBoxFoamDataRealTimeHistoryServices _boxFoamDataRealTimeHistoryServices;
1 year ago
System.Timers.Timer timer2 = new System.Timers.Timer(5000);
1 year ago
int totalAmount = 0;
public FoamMonitorPageViewModel()
{
1 year ago
_boxFoamPlanServices = App.ServiceProvider.GetService<IBoxFoamPlanServices>();
1 year ago
_boxFoamDataRealTimeServices = App.ServiceProvider.GetService<IBoxFoamDataRealTimeServices>();
_boxFoamDataRecordServices = App.ServiceProvider.GetService<IBoxFoamDataRecordServices>();
_currentTeamTimeServices = App.ServiceProvider.GetService<ICurrentTeamTimeServices>();
1 year ago
_boxFoamDataRealTimeHistoryServices = App.ServiceProvider.GetService<IBoxFoamDataRealTimeHistoryServices>();
1 year ago
_sysUserInfoServices = App.ServiceProvider.GetService<ISysUserInfoServices>();
1 year ago
CollectionFoamLine.RefreshBoxFoamDataDelegateEvent += InitData;
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
1 year ago
//Task.WaitAll(InitData());
1 year ago
}
1 year ago
#region 日产量
/// <summary>
/// 每日生产
/// </summary>
/// <returns></returns>
private Task InitEveryDayMethod()
{
#region 按时间统计
ChartValues<ObservablePoint> achievement = new ChartValues<ObservablePoint>
{
new ObservablePoint(0, 12),
new ObservablePoint(1, 14),
new ObservablePoint(2, 28),
1 year ago
new ObservablePoint(3, 2),
new ObservablePoint(4, 29),
new ObservablePoint(5, 29),
new ObservablePoint(6, 7),
new ObservablePoint(7, 31),
new ObservablePoint(8, 13),
new ObservablePoint(9, 11),
new ObservablePoint(10, 8),
1 year ago
new ObservablePoint(11, 5)
};
var column = new ColumnSeries();
column.DataLabels = true;
column.Title = "当日小时产量统计";
column.Values = achievement;
column.Foreground = Brushes.White;
//柱子宽度
//column.Width = 30;
//column.MaxColumnWidth = 30;
//column.LabelsPosition = BarLabelPosition.Perpendicular;
//column.Fill = new SolidColorBrush(Color.FromRgb(34, 139, 34)); //柱状图颜色填充
//column.LabelPoint = p => p.Y.ToString(); //柱状图数据显示位置
ProductionHourList = new List<string>()
{
1 year ago
"7:30", "8:30", "9:30", "10:30", "11:30", "12:30", "13:30", "14:30", "15:30", "16:30", "17:30", "18:30"
};
//Formatter = value => value.ToString("N");
Achievement.Add(column);
#endregion
#region 按类型统计
ChartValues<double> achievement2 = new ChartValues<double>();
Random random2 = new Random();
for (int i = 0; i < 2; i++)
{
1 year ago
achievement2.Add(random2.Next(0,50));
}
var column2 = new ColumnSeries();
column2.DataLabels = true;
column2.Title = "当日型号产量统计";
column2.Values = achievement2;
column2.Foreground = Brushes.White;
ModelStatistics.Add(column2);
MaterialNameList = new List<string>()
{
"玻璃门,SC-439", "玻璃门,SC-439,AC"
};
#endregion
1 year ago
//await InitExecMethod();
return Task.CompletedTask;
}
#endregion
#region 日产量柱状图X轴日期
/// <summary>
/// 日产量柱状图X轴日期
/// </summary>
private List<string> productionHourList;
public List<string> ProductionHourList
{
get { return productionHourList; }
set { productionHourList = value; }
}
#endregion
#region 型号统计
#region 型号统计柱状图x轴物料类型
/// <summary>
/// 型号统计柱状图x轴物料类型
/// </summary>
private List<string> materialNameList;
public List<string> MaterialNameList
{
get { return materialNameList; }
set { materialNameList = value; }
}
#endregion
#region 型号统计柱状图
/// <summary>
/// 型号统计柱状图
/// </summary>
private SeriesCollection modelStatistics = new SeriesCollection();
public SeriesCollection ModelStatistics
{
get { return modelStatistics; }
set { modelStatistics = value; }
}
#endregion
#endregion
#region 日产量柱状图
/// <summary>
/// 日产量柱状图
/// </summary>
private SeriesCollection achievement = new SeriesCollection();
public SeriesCollection Achievement
{
get { return achievement; }
set { achievement = value; }
1 year ago
}
#endregion
#region 颜色
/// <summary>
/// 颜色
/// </summary>
private ObservableCollection<string> _color = new ObservableCollection<string>();
public ObservableCollection<string> Color
{
get => _color;
set => SetProperty(ref _color, value);
}
#endregion
1 year ago
#region 总计
1 year ago
private int _totle;
1 year ago
public int Totle
{
get => _totle;
set => SetProperty(ref _totle, value);
}
#endregion
#region 夹具状态
private ObservableCollection<string> _statusColor = new ObservableCollection<string>();
public ObservableCollection<string> StatusColor
{
get => _statusColor;
set => SetProperty(ref _statusColor, value);
}
#endregion
#region 夹具名称
private ObservableCollection<string> _fixtureName = new ObservableCollection<string>();
public ObservableCollection<string> FixtureName
{
get => _fixtureName;
set => SetProperty(ref _fixtureName, value);
}
#endregion
#region 夹具产量
/// <summary>
///夹具产量
/// </summary>
private ObservableCollection<string> _production = new ObservableCollection<string>();
public ObservableCollection<string> Production
{
get => _production;
set => SetProperty(ref _production, value);
}
#endregion
#region 发泡量
/// <summary>
/// 发泡量
/// </summary>
private ObservableCollection<int> _foamVolume = new ObservableCollection<int>();
public ObservableCollection<int> FoamVolume
{
get => _foamVolume;
set => SetProperty(ref _foamVolume, value);
}
#endregion
#region 内侧温度
private ObservableCollection<string> _internalTemperature = new ObservableCollection<string>();
public ObservableCollection<string> InternalTemperature
{
get => _internalTemperature;
set => SetProperty(ref _internalTemperature, value);
}
#endregion
#region 侧板温度温度
private ObservableCollection<string> _outsideTemperature = new ObservableCollection<string>();
public ObservableCollection<string> OutsideTemperature
{
get => _outsideTemperature;
set => SetProperty(ref _outsideTemperature, value);
}
#endregion
#region 夹具节拍
1 year ago
private ObservableCollection<string> _beat = new ObservableCollection<string>();
public ObservableCollection<string> Beat
1 year ago
{
get => _beat;
set => SetProperty(ref _beat, value);
}
#endregion
1 year ago
#region 初始化发泡数据
1 year ago
/// <summary>
/// 初始化发泡数据
/// </summary>
/// <returns></returns>
1 year ago
public async Task InitData(List<FixtureStatus> list)
1 year ago
{
1 year ago
try
1 year ago
{
1 year ago
int totalAmount = 0;
1 year ago
if (list == null) return;
1 year ago
FixtureName.Clear();
Production.Clear();
Beat.Clear();
InternalTemperature.Clear();
OutsideTemperature.Clear();
1 year ago
1 year ago
foreach (var item in list)
{
1 year ago
if (item.Status == 1) StatusColor.Add("Green");
if (item.Status == 0) StatusColor.Add("Red");
1 year ago
if (!string.IsNullOrEmpty(item.FixtureBoxType)) FixtureName.Add(item.FixtureBoxType);
if (!string.IsNullOrEmpty(item.Yield.ToString())) Production.Add(item.Yield.ToString());
// if (!string.IsNullOrEmpty(item.InternalTemperature.ToString())) InternalTemperature.Add(item.InternalTemperature.ToString());
// if (!string.IsNullOrEmpty(item.OutsideTemperature.ToString())) OutsideTemperature.Add(item.OutsideTemperature.ToString());
1 year ago
if (!string.IsNullOrEmpty(item.ProductionCycle.ToString())) Beat.Add(item.ProductionCycle.ToString());
1 year ago
1 year ago
totalAmount = totalAmount + item.Yield;
1 year ago
}
1 year ago
Totle = totalAmount;
1 year ago
}
1 year ago
catch(Exception ex)
1 year ago
{
1 year ago
Console.WriteLine(ex.Message.ToString());
}
}
#endregion
1 year ago
1 year ago
Semaphore semaphore = new Semaphore(1, 1);
/// <summary>
/// 将采集到的数据保存
/// </summary>
/// <param name="mode"></param>
public async void RealTimeFoamingTask(object? sender, ElapsedEventArgs e)
{
semaphore.WaitOne();
1 year ago
1 year ago
try
{
1 year ago
string productLineCode = Appsettings.app("StationInfo", "ProductLineCode");
1 year ago
DateTime now = DateTime.Now;
1 year ago
CurrentTeamTime currentTeamTime = await _currentTeamTimeServices.FirstAsync(d => d.StartTime <= now && d.EndTime >= now);
1 year ago
1 year ago
if (currentTeamTime!=null)
1 year ago
{
1 year ago
//比较计划更新数据
1 year ago
ExecHourDataAsync(productLineCode);
}
1 year ago
else
1 year ago
{
1 year ago
// 白夜班切换
1 year ago
ShiftChangeAsync(productLineCode);
1 year ago
}
1 year ago
1 year ago
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
semaphore.Release();
}
}
public async void ExecHourDataAsync(string productLineCode)
{
List<FixtureStatus> bLis = CollectionFoamLine.listFixtureStatus;
if (bLis.Count == 0) return;
List<BoxFoamPlan> planList = await _boxFoamPlanServices.QueryAsync();
List<BoxFoamDataRealTime> bfds = await _boxFoamDataRealTimeServices.QueryAsync();
for (int j = 0; j < bLis.Count; j++)
{
try
1 year ago
{
1 year ago
if (string.IsNullOrEmpty(bLis[j].FixtureBoxType)) continue;
BoxFoamPlan plan = planList.FirstOrDefault(d => d.MaterialCode == bLis[j].FixtureBoxType && d.PlanAmount > d.CompleteAmount);
if (plan != null)
1 year ago
{
1 year ago
int complateAmount = 0;
1 year ago
BoxFoamDataRealTime bfd = bfds.SingleOrDefault(d => d.FixtureBoxtype == bLis[j].FixtureBoxType && d.SerialNumber == bLis[j].ObjId);
1 year ago
if (bfd != null)
1 year ago
{
1 year ago
//更新计划数量
complateAmount = bLis[j].Yield - bfd.FixtureProductioncapacity;
1 year ago
UpdateClampData(bLis[j], bfd);
}
else
{
FixtureStatus fixtureStatus = bLis[j];
1 year ago
SaveClampData(productLineCode, fixtureStatus);
}
plan.CompleteAmount = plan.CompleteAmount + complateAmount;
1 year ago
if (complateAmount>0)
{
1 year ago
//保存历史记录
1 year ago
BoxFoamDataRealTimeHistory history = new BoxFoamDataRealTimeHistory();
history.PlantCode = bfd.PlantCode;
history.ProductlineCode = bfd.ProductlineCode;
history.ProcessCode = bfd.ProcessCode;
history.StationCode= bfd.StationCode;
history.FixtureProductioncapacity = bLis[j].Yield;
history.FixtureCuringtimesettingvalue = bLis[j].PlanCuringTime.ToString();
history.FixtureActualtime = bLis[j].RealCuringTime.ToString();
history.ModeInternaltemperature = bLis[j].InternalTemperature;
history.ModeOutsidetemperature = bLis[j].InternalTemperature;
history.UpdatedTime = DateTime.Now;
1 year ago
//事务合并
1 year ago
await _boxFoamDataRealTimeHistoryServices.AddAsync(history);
await _boxFoamPlanServices.UpdateAsync(plan);
}
1 year ago
}
else
{
//更新小时数据
BoxFoamDataRealTime bfd = bfds.SingleOrDefault(d => d.FixtureBoxtype == bLis[j].FixtureBoxType && d.SerialNumber == bLis[j].ObjId);
if (bfd != null)
{
UpdateClampData(bLis[j], bfd);
}
else
{
SaveClampData(productLineCode, bLis[j]);
1 year ago
}
}
}
1 year ago
catch { }
1 year ago
}
1 year ago
}
1 year ago
private async void ShiftChangeAsync(string productLineCode)
1 year ago
{
1 year ago
try
{
//切换班组计划
var currentTeamTimeList = await _sysUserInfoServices.GetTeamData();
if (currentTeamTimeList.Count == 0) return;
1 year ago
// 夹具
1 year ago
var list = await _boxFoamDataRealTimeServices.QueryAsync(d => d.ProductlineCode == productLineCode);
//更新计划表操作
1 year ago
1 year ago
CurrentTeamTime currentTeamTime = await _currentTeamTimeServices.FirstAsync();
1 year ago
// 更新当班时间
1 year ago
currentTeamTime.TeamName = currentTeamTimeList.First().TeamName;
1 year ago
currentTeamTime.StartTime = currentTeamTimeList.Min(d => d.StartTime);
currentTeamTime.EndTime = currentTeamTimeList.Max(d => d.EndTime);
bool result = await _currentTeamTimeServices.UpdateAsync(currentTeamTime);
1 year ago
1 year ago
if (result)
{
//清空 当班计划
1 year ago
// var realTimeList = await _boxFoamDataRealTimeHistoryServices.QueryAsync(d => d.ProductlineCode == productLineCode);
1 year ago
var plan = await _boxFoamPlanServices.QueryAsync(d => d.ProductLineCode == productLineCode);
await _boxFoamPlanServices.DeletesAsync(plan);
}
}
catch { }
1 year ago
}
1 year ago
1 year ago
#region 更新夹具型号
/// <summary>
/// 更新夹具型号
/// </summary>
public async void UpdateClampData(FixtureStatus fixtureStatus, BoxFoamDataRealTime bfd)
{
1 year ago
try
{
bfd.FixtureBoxtype = fixtureStatus.FixtureBoxType;
bfd.FixtureStatus = fixtureStatus.Status.ToString();
bfd.FixtureProductioncapacity = fixtureStatus.Yield;
bfd.FixtureCuringtimesettingvalue = fixtureStatus.PlanCuringTime.ToString();
bfd.FixtureActualtime = fixtureStatus.RealCuringTime.ToString();
bfd.ModeInternaltemperature = fixtureStatus.InternalTemperature;
bfd.ModeOutsidetemperature = fixtureStatus.InternalTemperature;
bfd.Beat = fixtureStatus.Yield.ToString();
bfd.UpdatedTime = DateTime.Now;
await _boxFoamDataRealTimeServices.UpdateAsync(bfd);
}
catch { }
1 year ago
}
#endregion
1 year ago
1 year ago
#region 新增夹具型号
/// <summary>
/// 更新夹具型号
/// </summary>
1 year ago
public async void SaveClampData(string productLineCode,FixtureStatus fixtureStatus)
1 year ago
{
1 year ago
var obj=await _sysUserInfoServices.GetProductLineInfo(productLineCode);
if (obj!=null)
{
BoxFoamDataRealTime bfd = new BoxFoamDataRealTime();
bfd.PlantCode = obj.PlantCode;
bfd.ProductlineCode = productLineCode;
bfd.ProcessCode = obj.ProcessCode;
bfd.StationCode = obj.StationCode;
bfd.SerialNumber = fixtureStatus.ObjId;
bfd.FixtureBoxtype = fixtureStatus.FixtureBoxType;
bfd.FixtureStatus = fixtureStatus.Status.ToString();
bfd.FixtureProductioncapacity = fixtureStatus.Yield;
bfd.FixtureCuringtimesettingvalue = fixtureStatus.PlanCuringTime.ToString();
bfd.FixtureActualtime = fixtureStatus.RealCuringTime.ToString();
bfd.ModeInternaltemperature = fixtureStatus.InternalTemperature;
bfd.ModeOutsidetemperature = fixtureStatus.InternalTemperature;
bfd.CreatedTime = DateTime.Now;
bfd.UpdatedTime = DateTime.Now;
await _boxFoamDataRealTimeServices.AddAsync(bfd);
}
1 year ago
}
1 year ago
#endregion
1 year ago
}
}