aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/EventsViewVM.cs
blob: 57279f276b8f2c2df2e55cb41ba0a36cdd78c3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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<IEventsView>, INavigationObjectReceiver<NavigationObject>
    {
        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);
        }
    }
}