aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryMachineUpdatesStreamingSource.cs
blob: 85d5309a51d459ebacbc2bc561a20faa2ed912ed (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
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Telemetry.Mappers;
using Tango.Telemetry.Telemetries;

namespace Tango.Telemetry.Sources
{
    public class TelemetryMachineUpdatesStreamingSource : ITelemetryStreamingSource
    {
        public string Name { get; } = "Machine Updates Streaming";

        public bool RequiresTelemetryDuplicationTracking { get; } = true;

        public bool IsStarted { get; private set; }

        public event EventHandler<TelemetryAvailableEventArgs> TelemetryAvailable;

        public void Start()
        {
            if (!IsStarted)
            {
                IsStarted = true;
                TangoUpdate.TangoUpdateSaved += TangoUpdate_UpdateSaved;
            }
        }

        public void Stop()
        {
            if (IsStarted)
            {
                IsStarted = false;
                TangoUpdate.TangoUpdateSaved -= TangoUpdate_UpdateSaved;
            }
        }

        private void TangoUpdate_UpdateSaved(object sender, TangoUpdate update)
        {
            if (IsStarted)
            {
                if (!update.IsSynchronization)
                {
                    using (ObservablesContext db = ObservablesContext.CreateDefault())
                    {
                        if (db.TangoUpdates.Any(x => x.Guid == update.Guid)) return;

                        TelemetryMachineUpdate tUpdate = MachineUpdateMapper.MapMachineUpdate(update);

                        TelemetryAvailable?.Invoke(this, new TelemetryAvailableEventArgs() { TelemetryObject = tUpdate });
                    }
                }
            }
        }

        public void Dispose()
        {
            Stop();
        }
    }
}