首次提交:添加src文件夹代码
This commit is contained in:
262
Cowain.Bake.Main/ViewModels/MainWindowViewModel.cs
Normal file
262
Cowain.Bake.Main/ViewModels/MainWindowViewModel.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
using Cowain.Bake.Main.Models;
|
||||
using Cowain.Bake.Main.Views;
|
||||
using Cowain.Bake.Model;
|
||||
using Cowain.Bake.Model.Models;
|
||||
using Prism.Commands;
|
||||
using Prism.Events;
|
||||
using Prism.Mvvm;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using Unity;
|
||||
|
||||
namespace Cowain.Bake.Main.ViewModels
|
||||
{
|
||||
public class MainWindowViewModel : BindableBase
|
||||
{
|
||||
private string promptContent = "";
|
||||
public string PromptContent
|
||||
{
|
||||
get { return promptContent; }
|
||||
set { SetProperty<string>(ref promptContent, value); }
|
||||
}
|
||||
private string recentMsg; //最新的消息
|
||||
readonly IUnityContainer _unityContainer;
|
||||
private SubscriptionToken _subscriptionToken;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
//缩放因子,用于调整在不同分辨率屏幕的位置
|
||||
public MainWindowViewModel(IUnityContainer unityContainer, IEventAggregator eventAggregator)
|
||||
{
|
||||
_unityContainer = unityContainer;
|
||||
//Global.STOVE_LAYERS = _unityContainer.Resolve<MemoryDataProvider>().AllStation.Find(x => x.Type ==(int)EStationType.Stove).Layers;
|
||||
MainTitle = SettingProvider.Instance.ProductionLineName + Global.VS;
|
||||
SetlogDict();
|
||||
LogHelper.Instance.Warn($"-------------{Global.VS}------------------");
|
||||
LogHelper.Instance.Debug($"-------------{Global.VS}------------------");
|
||||
LogHelper.Instance.Error($"-------------{Global.VS}------------------");
|
||||
LogHelper.Instance.Fatal($"-------------{Global.VS}------------------");
|
||||
LogHelper.Instance.Info($"-------------{Global.VS}------------------");
|
||||
InitializeLogLevels();
|
||||
_eventAggregator = eventAggregator;
|
||||
_subscriptionToken = _eventAggregator.GetEvent<AlarmAddEvent>() //Unsubscribe: 取消订阅(如在组件销毁时)
|
||||
.Subscribe(OnAddAlarm, ThreadOption.UIThread); //事件触发时执行的方法,ThreadOption.UIThread,(默认):在发布事件的线程执行
|
||||
|
||||
_subscriptionToken = _eventAggregator.GetEvent<AlarmCancelEvent>()
|
||||
.Subscribe(OnCancelAlarm, ThreadOption.UIThread);
|
||||
|
||||
_subscriptionToken = _eventAggregator.GetEvent<AlarmStaionCancelEvent>()
|
||||
.Subscribe(OnCancelAlarm, ThreadOption.UIThread);
|
||||
|
||||
}
|
||||
|
||||
private void OnAddAlarm(TAlarm alarm)
|
||||
{
|
||||
var model = Alarms.Where(item => item.StationId == alarm.StationId && item.Desc == alarm.Desc).FirstOrDefault();
|
||||
|
||||
if (null == model)
|
||||
{
|
||||
Alarms.Add(alarm);
|
||||
}
|
||||
|
||||
//ShowPromptContent();
|
||||
}
|
||||
|
||||
private void OnCancelAlarm(TAlarm alarm)
|
||||
{
|
||||
var model = Alarms.Where(item => item.StationId == alarm.StationId && item.Desc == alarm.Desc).FirstOrDefault();
|
||||
|
||||
if (null != model)
|
||||
{
|
||||
Alarms.Remove(model);
|
||||
}
|
||||
|
||||
//ShowPromptContent();
|
||||
}
|
||||
|
||||
private void OnCancelAlarm(int stationId)
|
||||
{
|
||||
var models = Alarms.Where(item => item.StationId == stationId).ToList();
|
||||
|
||||
foreach (var model in models)
|
||||
{
|
||||
Alarms.Remove(model);
|
||||
}
|
||||
|
||||
//ShowPromptContent();
|
||||
}
|
||||
public void ShowPromptContent()
|
||||
{
|
||||
int index = 0;
|
||||
PromptContent = "";
|
||||
foreach (var item in Alarms.Reverse())
|
||||
{
|
||||
if (++index > 2)
|
||||
{
|
||||
return; //数据太多了,就会卡,所以只让显示二条数据
|
||||
}
|
||||
PromptContent += item.Desc + " "; //太多了,界面卡
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPromptContent(string msg)
|
||||
{
|
||||
//string addContent = promptContent;
|
||||
//if (string.IsNullOrEmpty(addContent))
|
||||
//{
|
||||
// addContent = msg;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// var parts = addContent.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
// if (!parts.Contains(msg))
|
||||
// {
|
||||
// addContent += "|" + msg;
|
||||
// }
|
||||
//}
|
||||
|
||||
//if (promptContent == addContent)
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
;
|
||||
PromptContent = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} : {msg}";
|
||||
}
|
||||
|
||||
public void DeletePromptContent(string msg ="")
|
||||
{
|
||||
string deleteContent = promptContent;
|
||||
if (string.IsNullOrEmpty(deleteContent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//if (!deleteContent.Contains(msg))
|
||||
//{
|
||||
// return;
|
||||
//}
|
||||
|
||||
//var parts = deleteContent.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
// .Select(p => p.Trim())
|
||||
// .Where(p => p != msg)
|
||||
// .ToList();
|
||||
//deleteContent = string.Join("|", parts);
|
||||
|
||||
PromptContent = "";
|
||||
}
|
||||
|
||||
|
||||
private void InitializeLogLevels()
|
||||
{
|
||||
var logTypes = EnumHelper.GetEnumList<E_LogType>();
|
||||
for (int i = 0; i < (logTypes.Count - 3); i++)
|
||||
{
|
||||
LogLevelModel1.Add(new MultiComboBox.MultiCbxBaseData
|
||||
{
|
||||
Id = i,
|
||||
IsCheck = true,
|
||||
ViewName = logTypes[i].EnumString.ToString()
|
||||
});
|
||||
}
|
||||
LogHelper.Instance.OnShowInvoke = LogShow;
|
||||
}
|
||||
|
||||
private Dictionary<E_LogType, (string FontColor, MessageBoxImage Image)> logDict;
|
||||
private void SetlogDict()
|
||||
{
|
||||
logDict = new Dictionary<E_LogType, (string, MessageBoxImage)>
|
||||
{
|
||||
{ E_LogType.Debug, ("#5352ed", MessageBoxImage.Exclamation) },
|
||||
{ E_LogType.Info, ("#1abc9c", MessageBoxImage.Information) },
|
||||
{ E_LogType.Warn, ("#eccc68", MessageBoxImage.Warning) },
|
||||
{ E_LogType.Error, ("red", MessageBoxImage.Error) },
|
||||
{ E_LogType.Fatal, ("red", MessageBoxImage.Error) }
|
||||
};
|
||||
}
|
||||
private void LogShow(string log, bool ispopup, E_LogType logType)
|
||||
{
|
||||
if (recentMsg == log && !ispopup)
|
||||
{
|
||||
return;
|
||||
}
|
||||
recentMsg = log;
|
||||
DateTime date = DateTime.Now;
|
||||
if (logDict.TryGetValue(logType, out var logInfo))
|
||||
{
|
||||
InfoLogModel = new LogModel
|
||||
{
|
||||
LogLevel = logType.ToString(),
|
||||
LogText = log,
|
||||
LogTime = date,
|
||||
FontColor = logInfo.FontColor
|
||||
};
|
||||
if (ispopup)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
HandyControl.Controls.MessageBox.Show(log, logType.GetDescription(), MessageBoxButton.OK, logInfo.Image);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DelegateCommand<object> AlarmQueryCommand => new DelegateCommand<object>((x) =>
|
||||
{
|
||||
HistoryAlarmsData.Clear();
|
||||
var service = _unityContainer.Resolve<AlarmService>();
|
||||
service.GetAlarmReport(AlarmStartTime, AlarmEndTime).ForEach(item => HistoryAlarmsData.Add(item));
|
||||
});
|
||||
#region 定义
|
||||
private string mainTitle;
|
||||
public string MainTitle
|
||||
{
|
||||
get => mainTitle;
|
||||
set => SetProperty(ref mainTitle, value);
|
||||
}
|
||||
|
||||
private LogModel logModel1;
|
||||
public LogModel InfoLogModel
|
||||
{
|
||||
get => logModel1;
|
||||
set => SetProperty(ref logModel1, value);
|
||||
}
|
||||
|
||||
private ObservableCollection<MultiComboBox.MultiCbxBaseData> logLevelModel1 = new ObservableCollection<MultiComboBox.MultiCbxBaseData>();
|
||||
public ObservableCollection<MultiComboBox.MultiCbxBaseData> LogLevelModel1
|
||||
{
|
||||
get => logLevelModel1;
|
||||
set => SetProperty(ref logLevelModel1, value);
|
||||
}
|
||||
|
||||
private string alarmStartTime = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
public string AlarmStartTime
|
||||
{
|
||||
get => alarmStartTime;
|
||||
set => SetProperty(ref alarmStartTime, value);
|
||||
}
|
||||
private string alarmEndTime = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
public string AlarmEndTime
|
||||
{
|
||||
get => alarmEndTime;
|
||||
set => SetProperty(ref alarmEndTime, value);
|
||||
}
|
||||
private ObservableCollection<TAlarm> alarms = new ObservableCollection<TAlarm>();
|
||||
public ObservableCollection<TAlarm> Alarms
|
||||
{
|
||||
get => alarms;
|
||||
set => SetProperty(ref alarms, value);
|
||||
}
|
||||
private ObservableCollection<TAlarm> historyAlarms = new ObservableCollection<TAlarm>();
|
||||
public ObservableCollection<TAlarm> HistoryAlarmsData
|
||||
{
|
||||
get => historyAlarms;
|
||||
set => SetProperty(ref historyAlarms, value);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user