using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core;
using Tango.PMR.Diagnostics;
namespace Tango.Integration.Operation
{
///
/// Represents the default machine events state provider.
///
///
public class DefaultMachineEventsStateProvider : ExtendedObject, IMachineEventsStateProvider
{
private ReadOnlyObservableCollection _events;
///
/// Gets the current machine events.
///
public ReadOnlyObservableCollection Events
{
get
{
return _events;
}
}
///
/// Gets or sets a value indicating whether this instance has events.
///
public bool HasEvents
{
get
{
return Events.Count > 0;
}
}
///
/// Occurs when new events are available.
///
public event EventHandler> NewEvents;
///
/// Occurs when a new events states has been received.
///
public event EventHandler> EventsReceived;
///
/// Occurs when old events has been resolved.
///
public event EventHandler> EventsResolved;
///
/// Occurs when the collection of current events has changed.
///
public event EventHandler> EventsChanged;
///
/// Initializes a new instance of the class.
///
public DefaultMachineEventsStateProvider()
{
_events = new ReadOnlyObservableCollection(new ObservableCollection());
}
///
/// Applies the collection of events received from the machine operator.
///
/// The events.
public void ApplyEvents(IEnumerable events)
{
List receivedEvents = events.Select(x => new MachinesEvent(x)).ToList();
List newEvents = receivedEvents.Where(x => !_events.ToList().Exists(y => y.Type == x.Type)).ToList();
List oldEvents = _events.Where(x => !receivedEvents.Exists(y => y.Type == x.Type)).ToList();
_events = new ReadOnlyObservableCollection(new ObservableCollection(receivedEvents));
if (newEvents.Count > 0)
{
OnNewEvents(newEvents);
}
if (oldEvents.Count > 0)
{
OnEventsResolved(oldEvents);
}
if (newEvents.Count > 0 || oldEvents.Count > 0)
{
RaisePropertyChanged(nameof(Events));
OnEventsChanged(_events);
}
OnEventsReceived(_events);
RaisePropertyChanged(nameof(HasEvents));
}
///
/// Called when the new has been events
///
/// The events.
protected virtual void OnNewEvents(IEnumerable events)
{
NewEvents?.Invoke(this, events);
}
///
/// Called when the events has been received
///
/// The events.
protected virtual void OnEventsReceived(IEnumerable events)
{
EventsReceived?.Invoke(this, events);
}
///
/// Called when the events has been resolved
///
/// The events.
protected virtual void OnEventsResolved(IEnumerable events)
{
EventsResolved?.Invoke(this, events);
}
///
/// Called when the events has been changed
///
/// The events.
protected virtual void OnEventsChanged(IEnumerable events)
{
EventsChanged?.Invoke(this, events);
}
///
/// Resets the current events tracking.
///
public void Reset()
{
_events = new ReadOnlyObservableCollection(new ObservableCollection(new List()));
RaisePropertyChanged(nameof(Events));
OnEventsChanged(new List());
}
}
}