using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using Tango.BL.Enumerations; using Tango.Core.Commands; using Tango.FSE.Common; using Tango.FSE.Common.Navigation; using Tango.FSE.UI.Contracts; using Tango.FSE.UI.Dialogs; using static Tango.FSE.UI.ViewModels.EventsViewVM; namespace Tango.FSE.UI.ViewModels { public class EventsViewVM : FSEViewModel, INavigationObjectReceiver { public class NavigationObject { public MachinesEvent SelectedEvent { get; set; } } private double _InfoEvents; public double InfoEvents { get { return _InfoEvents; } set { _InfoEvents = value; RaisePropertyChangedAuto(); } } private double _warningEvents; public double WarningEvents { get { return _warningEvents; } set { _warningEvents = value; RaisePropertyChangedAuto(); } } private double _errorEvents; public double ErrorEvents { get { return _errorEvents; } set { _errorEvents = value; RaisePropertyChangedAuto(); } } private double _criticalEvents; public double CriticalEvents { get { return _criticalEvents; } set { _criticalEvents = value; RaisePropertyChangedAuto(); } } private bool _canExecuteJob; public bool CanExecuteJob { get { return _canExecuteJob; } set { _canExecuteJob = value; RaisePropertyChangedAuto(); } } private MachinesEvent _selectedEvent; public MachinesEvent SelectedEvent { get { return _selectedEvent; } set { _selectedEvent = value; RaisePropertyChangedAuto(); } } public RelayCommand EmulateMachineEventCommand { get; set; } public EventsViewVM() { CanExecuteJob = true; EmulateMachineEventCommand = new RelayCommand(EmulateMachineEvent); } private async void EmulateMachineEvent() { var events = await Services.MachineEventsService.GetAllEventTypes(); events = events.Where(x => x.Group != EventTypeGroups.Application && x.Group != EventTypeGroups.Jobs && x.Group != EventTypeGroups.Transport).OrderBy(x => x.Code).ToList(); var vm = await NotificationProvider.ShowDialog(new EmulateMachineEventDialogViewVM() { EventTypes = events }); if (vm.DialogResult) { try { using (NotificationProvider.PushTaskItem("Pushing emulated event...")) { await Task.Delay(1000); await EventsProvider.PushEmulatedEvent(new PMR.Diagnostics.Event() { Type = (PMR.Diagnostics.EventType)vm.SelectedEventType.Type, Message = vm.Message.ToStringOrEmpty() }, TimeSpan.FromSeconds(vm.DurationSeconds)); } } catch (Exception ex) { LogManager.Log(ex, "Error pushing emulated event."); await NotificationProvider.ShowError($"Error pushing emulated event.\n{ex.FlattenMessage()}"); } } } public override void OnApplicationStarted() { EventsProvider.ActiveEvents.CollectionChanged += ActiveEvents_CollectionChanged; } private void ActiveEvents_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { InfoEvents = EventsProvider.ActiveEvents.ToList().Where(x => x.Category == EventTypeCategories.Info).Count(); WarningEvents = EventsProvider.ActiveEvents.ToList().Where(x => x.Category == EventTypeCategories.Warning).Count(); ErrorEvents = EventsProvider.ActiveEvents.ToList().Where(x => x.Category == EventTypeCategories.Error).Count(); CriticalEvents = EventsProvider.ActiveEvents.ToList().Where(x => x.Category == EventTypeCategories.Critical).Count(); CanExecuteJob = !EventsProvider.ActiveEvents.Any(x => x.EventType.Actions.Contains(EventTypeActions.PreventJob)); } public void OnNavigatedToWithObject(NavigationObject obj) { SelectedEvent = obj.SelectedEvent; View?.ScrollToEvent(SelectedEvent); } } }