Projekt

Obecné

Profil

Stáhnout (3.59 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System.Diagnostics;
2
using LDClient.network;
3
using LDClient.network.data;
4

    
5
namespace LDClient.detection {
6
   
7
	 public sealed class ProcessDetection : IProcessDetection {
8
        
9
        private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
10

    
11
        private readonly string _processName;
12
        private readonly uint _detectionPeriodMs;
13
        private readonly IInfoFetcher _infoFetcher;
14
        private readonly IApiClient _apiClient;
15
        private readonly IProcessUtils _processUtils;
16

    
17
        private bool _processIsActive;
18
        private bool _failedToRetrieveData;
19
        private Payload? _lastConnectedPayload;
20

    
21
        public bool DetectionRunning = false;
22

    
23
        public ProcessDetection(string processName, uint detectionPeriodMs, IInfoFetcher infoFetcher, IApiClient apiClient, IProcessUtils processUtils) {
24
            _processName = processName;
25
            _detectionPeriodMs = detectionPeriodMs;
26
            _infoFetcher = infoFetcher;
27
            _apiClient = apiClient;
28
            _failedToRetrieveData = false;
29
            _processUtils = processUtils;
30
        }
31

    
32
        private async Task<bool> RetrieveDataFromDebugger() {
33
            var success = await _infoFetcher.FetchDataAsync();
34
            if (success) {
35
                _lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat);
36
            }
37
            return success;
38
        }
39

    
40
        private async Task DebuggerDisconnected() {
41
            if (_lastConnectedPayload is not null) {
42
                _lastConnectedPayload.Status = ConnectionStatus.Disconnected;
43
                _lastConnectedPayload.TimeStamp = DateTime.Now.ToString(DatetimeFormat);
44
                await _apiClient.SendPayloadAsync(_lastConnectedPayload);
45
                _lastConnectedPayload = null;
46
            }
47
        }
48

    
49
        private async Task DetectProcessAsync() {
50
            var processExists = _processUtils.IsProcessRunning(_processName);
51

    
52
            if (processExists && !_processIsActive) {
53
                Program.DefaultLogger.Info($"Process started: {_processName}");
54
                if (!_failedToRetrieveData) {
55
                    _failedToRetrieveData = !await RetrieveDataFromDebugger();
56
                }
57
            } else if (!processExists && _processIsActive) {
58
                Program.DefaultLogger.Info($"Process stopped: {_processName}");
59
                _failedToRetrieveData = false;
60
                await DebuggerDisconnected();
61
            }
62
            _processIsActive = processExists;
63
        }
64
        
65
        private async Task<Payload> SendDataToServerAsync(string headSerialNumber, string bodySerialNumber, string datetimeFormat) {
66
            Payload payload = new() {
67
                UserName = Environment.UserName,
68
                HostName = Environment.MachineName,
69
                TimeStamp = DateTime.Now.ToString(datetimeFormat),
70
                HeadDevice = new DebuggerInfo {
71
                    SerialNumber = headSerialNumber
72
                },
73
                BodyDevice = new DebuggerInfo {
74
                    SerialNumber = bodySerialNumber
75
                },
76
                Status = ConnectionStatus.Connected
77
            };
78
            await _apiClient.SendPayloadAsync(payload);
79
            return payload;
80
        }
81
        
82
        public async void RunPeriodicDetection() {
83
            Program.DefaultLogger.Info("Process periodic detector has started");
84
            DetectionRunning = true;
85
            while (DetectionRunning) {
86
                await DetectProcessAsync();
87
                Thread.Sleep((int)_detectionPeriodMs);
88
            }
89
        }
90
    }
91
}
(6-6/7)