diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-04-15 19:51:07 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-04-15 19:51:07 +0300 |
| commit | ca293b80c52a54c73251fbf3cd50741fb5653ae9 (patch) | |
| tree | f1168fa167a26bf8455e601291b8a19945a70187 /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs | |
| parent | 9ff8293b603f72c5faa8d238b3005524c31cc5a8 (diff) | |
| download | Tango-ca293b80c52a54c73251fbf3cd50741fb5653ae9.tar.gz Tango-ca293b80c52a54c73251fbf3cd50741fb5653ae9.zip | |
Lots Of Work !
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs new file mode 100644 index 000000000..361adef01 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs @@ -0,0 +1,195 @@ +using GalaSoft.MvvmLight.Messaging; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.Core.Commands; +using Tango.MachineStudio.Common.EventLogging; +using Tango.MachineStudio.Common.Messages; +using Tango.MachineStudio.Common.Notifications; +using Tango.MachineStudio.Common.StudioApplication; +using Tango.MachineStudio.Logging.Navigation; +using Tango.MachineStudio.Logging.Views; +using Tango.SharedUI; + +namespace Tango.MachineStudio.Logging.ViewModels +{ + public class EventsViewVM : ViewModel + { + private INotificationProvider _notification; + private IStudioApplicationManager _application; + private IEventLogger _eventLogger; + private ObservableCollection<MachinesEvent> _realTimeEvents; + private Machine _connectedMachine; + private LoggingNavigationManager _navigation; + + private Machine _selectedMachine; + public Machine SelectedMachine + { + get { return _selectedMachine; } + set { _selectedMachine = value; RaisePropertyChangedAuto(); OnSelectedMachineChanged(); } + } + + private ObservableCollection<MachinesEvent> _events; + public ObservableCollection<MachinesEvent> Events + { + get { return _events; } + set { _events = value; RaisePropertyChangedAuto(); } + } + + private MachinesEvent _selectedEvent; + public MachinesEvent SelectedEvent + { + get { return _selectedEvent; } + set { _selectedEvent = value; RaisePropertyChangedAuto(); OnSelectedEventChanged(); } + } + + private ObservableCollection<DateTime> _dates; + public ObservableCollection<DateTime> Dates + { + get { return _dates; } + set { _dates = value; RaisePropertyChangedAuto(); } + } + + private DateTime _selectedDate; + public DateTime SelectedDate + { + get { return _selectedDate; } + set { _selectedDate = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } + } + + private DateTime _minDate; + public DateTime MinDate + { + get { return _minDate; } + set { _minDate = value; RaisePropertyChangedAuto(); } + } + + private DateTime _maxDate; + public DateTime MaxDate + { + get { return _maxDate; } + set { _maxDate = value; RaisePropertyChangedAuto(); } + } + + + private bool _isRealTime; + public bool IsRealTime + { + get { return _isRealTime; } + set { _isRealTime = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } + } + + private TimelineViewVM _timelineViewVM; + + public TimelineViewVM TimelineViewVM + { + get { return _timelineViewVM; } + set { _timelineViewVM = value; RaisePropertyChangedAuto(); } + } + + public RelayCommand<MachinesEvent> DisplayTimelineCommand { get; set; } + + public RelayCommand NavigateToEventsCommand { get; set; } + + public RelayCommand NavigateToHomeCommand { get; set; } + + public EventsViewVM(INotificationProvider notification, IEventLogger eventLogger, IStudioApplicationManager application, LoggingNavigationManager navigation) + { + TimelineViewVM = new TimelineViewVM(notification); + + _navigation = navigation; + _application = application; + _notification = notification; + _eventLogger = eventLogger; + _realTimeEvents = new ObservableCollection<MachinesEvent>(); + _eventLogger.NewLog += _eventLogger_NewLog; + + RegisterMessage<MachineConnectionChangedMessage>(OnMachineConnectionChanged); + DisplayTimelineCommand = new RelayCommand<MachinesEvent>(DisplayTimeline); + NavigateToEventsCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.EventsView)); + NavigateToHomeCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.HomeView)); + } + + private void OnMachineConnectionChanged(MachineConnectionChangedMessage msg) + { + if (msg.Machine != null) + { + _connectedMachine = ObservablesEntitiesAdapter.Instance.Machines.SingleOrDefault(x => x.SerialNumber == msg.Machine.SerialNumber); + SelectedMachine = _connectedMachine; + } + else + { + _connectedMachine = null; + } + } + + private void _eventLogger_NewLog(object sender, MachinesEvent machineEvent) + { + InvokeUI(() => + { + _realTimeEvents.Insert(0, machineEvent); + }); + } + + private void OnSelectedMachineChanged() + { + if (SelectedMachine != null) + { + Dates = new ObservableCollection<DateTime>(); + + if (SelectedMachine == _connectedMachine) + { + IsRealTime = true; + } + + foreach (var day in SelectedMachine.MachinesEvents.GroupBy(x => x.DateTime.DayOfYear).Select(x => x.First().DateTime).OrderByDescending(x => x)) + { + Dates.Add(day); + } + + MinDate = Dates.Min(); + MaxDate = Dates.Max(); + + SelectedDate = Dates.FirstOrDefault(); + } + } + + private void OnSelectedDateChanged() + { + if (SelectedDate != null && SelectedMachine != null) + { + if (IsRealTime) + { + Events = _realTimeEvents; + } + else + { + Events = SelectedMachine.MachinesEvents.Where(x => x.DateTime.DayOfYear == SelectedDate.Date.DayOfYear).OrderByDescending(x => x.DateTime).ToObservableCollection(); + } + } + } + + private void OnSelectedEventChanged() + { + if (SelectedEvent != null && SelectedEvent.Type != BL.Enumerations.EventTypes.ApplicationStarted) + { + _notification.ShowModalDialog<EventDetailsViewVM, EventDetailsView>(new EventDetailsViewVM(SelectedEvent), (x) => { }, () => { }); + } + } + + private void DisplayTimeline(MachinesEvent ev) + { + var events = Events.OrderBy(x => x.DateTime).SkipWhile(x => x != ev).Skip(1).TakeWhile(x => x.DateTime > ev.DateTime && x.Type != BL.Enumerations.EventTypes.ApplicationStarted).ToObservableCollection(); + events.Insert(0, ev); + + TimelineViewVM.Initialize(events.ToList()); + + _navigation.NavigateTo(LoggingNavigationView.TimelineView); + } + } +} |
