aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operation/DefaultMachineEventsStateProvider.cs
blob: d8ac112df2e7313d9add8ad15065f9e312cf9e0f (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Diagnostics;

namespace Tango.Integration.Operation
{
    /// <summary>
    /// Represents the default machine events state provider.
    /// </summary>
    /// <seealso cref="Tango.Integration.Operation.IMachineEventsStateProvider" />
    public class DefaultMachineEventsStateProvider : IMachineEventsStateProvider
    {
        private ReadOnlyCollection<Event> _events;
        /// <summary>
        /// Gets the current machine events.
        /// </summary>
        public ReadOnlyCollection<Event> Events
        {
            get
            {
                return _events;
            }
        }

        /// <summary>
        /// Occurs when new events are available.
        /// </summary>
        public event EventHandler<IEnumerable<Event>> NewEvents;

        /// <summary>
        /// Occurs when a new events states has been received.
        /// </summary>
        public event EventHandler<IEnumerable<Event>> EventsReceived;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultMachineEventsStateProvider"/> class.
        /// </summary>
        public DefaultMachineEventsStateProvider()
        {
            _events = new ReadOnlyCollection<Event>(new Collection<Event>());
        }

        /// <summary>
        /// Applies the collection of events received from the machine operator.
        /// </summary>
        /// <param name="events">The events.</param>
        public void ApplyEvents(IEnumerable<Event> events)
        {
            List<Event> currentEvents = Events.ToList();
            List<Event> newEvents = events.Where(x => !currentEvents.Exists(y => y.Type == x.Type)).ToList();
            _events = new ReadOnlyCollection<Event>(new Collection<Event>(events.ToList()));

            OnNewEvents(newEvents);
            OnEventsReceived(events);
        }

        /// <summary>
        /// Called when the new has been events
        /// </summary>
        /// <param name="events">The events.</param>
        protected virtual void OnNewEvents(IEnumerable<Event> events)
        {
            NewEvents?.Invoke(this, events);
        }

        /// <summary>
        /// Called when the events has been received
        /// </summary>
        /// <param name="events">The events.</param>
        protected virtual void OnEventsReceived(IEnumerable<Event> events)
        {
            EventsReceived?.Invoke(this, events);
        }
    }
}