aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs
blob: 2c8020f7cfe656f3a923f0a5933db28bb41bb566 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Integration.Operation;
using Tango.Logging;
using Tango.Telemetry.Mappers;
using Tango.Telemetry.Telemetries;

namespace Tango.Telemetry.Sources
{
    public class TelemetryLogsStreamingSource : TelemetryConfigurableSource<TelemetryLogsStreamingSourceConfig>, ITelemetryStreamingSource
    {
        private KeyValuePair<DateTime, String> _lastFirmwareMessage;

        public IMachineOperator MachineOperator { get; }

        public bool IsStarted { get; private set; }

        public string Name { get; } = "Logs";

        public bool RequiresTelemetryDuplicationTracking { get; }

        public event EventHandler<TelemetryAvailableEventArgs> TelemetryAvailable;

        public TelemetryLogsStreamingSource()
        {
            _lastFirmwareMessage = new KeyValuePair<DateTime, string>(DateTime.MinValue, String.Empty);
        }

        public TelemetryLogsStreamingSource(IMachineOperator machineOperator) : this()
        {
            MachineOperator = machineOperator;
        }

        public virtual void Start()
        {
            if (!IsStarted)
            {
                IsStarted = true;
                LogManager.NewLog += LogManager_NewLog;
                if (MachineOperator != null) MachineOperator.DebugLogAvailable += MachineOperator_DebugLogAvailable;
            }
        }

        private void MachineOperator_DebugLogAvailable(object sender, PMR.Debugging.StartDebugLogResponse log)
        {
            if (IsStarted)
            {
                if (!Config.Categories.Contains((LogCategory)log.Category)) return;

                if (log.Message == _lastFirmwareMessage.Value && DateTime.UtcNow < _lastFirmwareMessage.Key.AddSeconds(1)) return;

                _lastFirmwareMessage = new KeyValuePair<DateTime, string>(DateTime.UtcNow, log.Message);

                TelemetryLog tLog = LogMapper.MapLog(log);
                TelemetryAvailable?.Invoke(this, new TelemetryAvailableEventArgs() { TelemetryObject = tLog });
            }
        }

        private void LogManager_NewLog(object sender, Logging.LogItemBase log)
        {
            if (IsStarted)
            {
                if (!Config.Categories.Contains(log.Category)) return;

                TelemetryLog tLog = LogMapper.MapLog(log);
                TelemetryAvailable?.Invoke(this, new TelemetryAvailableEventArgs() { TelemetryObject = tLog });
            }
        }

        public void Stop()
        {
            if (IsStarted)
            {
                IsStarted = false;
                LogManager.NewLog -= LogManager_NewLog;
                if (MachineOperator != null) MachineOperator.DebugLogAvailable -= MachineOperator_DebugLogAvailable;
            }
        }

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