1 |
f281acac
|
silhavyj
|
using System.Diagnostics;
|
2 |
|
|
using LDClient.network;
|
3 |
|
|
using LDClient.network.data;
|
4 |
|
|
|
5 |
|
|
namespace LDClient.detection {
|
6 |
|
|
|
7 |
0932a9e2
|
Pultak
|
public sealed class ProcessDetection : IProcessDetection {
|
8 |
f281acac
|
silhavyj
|
|
9 |
|
|
private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
|
10 |
6dab0250
|
silhavyj
|
|
11 |
f281acac
|
silhavyj
|
private readonly string _processName;
|
12 |
|
|
private readonly uint _detectionPeriodMs;
|
13 |
0932a9e2
|
Pultak
|
private readonly IInfoFetcher _infoFetcher;
|
14 |
33c231a4
|
silhavyj
|
private readonly IApiClient _apiClient;
|
15 |
0932a9e2
|
Pultak
|
private readonly IProcessUtils _processUtils;
|
16 |
|
|
|
17 |
f281acac
|
silhavyj
|
private bool _processIsActive;
|
18 |
|
|
private bool _failedToRetrieveData;
|
19 |
6dab0250
|
silhavyj
|
private Payload? _lastConnectedPayload;
|
20 |
f281acac
|
silhavyj
|
|
21 |
0932a9e2
|
Pultak
|
public bool DetectionRunning = false;
|
22 |
|
|
|
23 |
|
|
public ProcessDetection(string processName, uint detectionPeriodMs, IInfoFetcher infoFetcher, IApiClient apiClient, IProcessUtils processUtils) {
|
24 |
f281acac
|
silhavyj
|
_processName = processName;
|
25 |
|
|
_detectionPeriodMs = detectionPeriodMs;
|
26 |
|
|
_infoFetcher = infoFetcher;
|
27 |
|
|
_apiClient = apiClient;
|
28 |
|
|
_failedToRetrieveData = false;
|
29 |
0932a9e2
|
Pultak
|
_processUtils = processUtils;
|
30 |
f281acac
|
silhavyj
|
}
|
31 |
|
|
|
32 |
|
|
private async Task<bool> RetrieveDataFromDebugger() {
|
33 |
|
|
var success = await _infoFetcher.FetchDataAsync();
|
34 |
|
|
if (success) {
|
35 |
6dab0250
|
silhavyj
|
_lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat);
|
36 |
f281acac
|
silhavyj
|
}
|
37 |
|
|
return success;
|
38 |
|
|
}
|
39 |
08616eff
|
silhavyj
|
|
40 |
f281acac
|
silhavyj
|
private async Task DebuggerDisconnected() {
|
41 |
6dab0250
|
silhavyj
|
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 |
f281acac
|
silhavyj
|
}
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
private async Task DetectProcessAsync() {
|
50 |
0932a9e2
|
Pultak
|
var processExists = _processUtils.IsProcessRunning(_processName);
|
51 |
f281acac
|
silhavyj
|
|
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 |
0932a9e2
|
Pultak
|
DetectionRunning = true;
|
85 |
|
|
while (DetectionRunning) {
|
86 |
f281acac
|
silhavyj
|
await DetectProcessAsync();
|
87 |
|
|
Thread.Sleep((int)_detectionPeriodMs);
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
}
|