Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6dab0250

Přidáno uživatelem Jakub Šilhavý před asi 2 roky(ů)

re #9566 Moved magic values into the configuration file.

Zobrazit rozdíly:

ld_client/LDClient/Program.cs
10 10

  
11 11
internal static class Program {
12 12

  
13
    private const int MainLoopDelayMs = 30000; 
14

  
13 15
    public static ConfigLoader Config { get; } = new();
14 16
    public static ALogger DefaultLogger { get; } = ALogger.Current;
15 17
    private static IApiClient? DefaultApiClient { get; set; }
16 18
    
17 19
    private static readonly InfoFetcher InfoFetcher = new(
18
        5,
19
        1000,
20
        "output.txt"
20
        Config.FetchInfoMaxAttempts,
21
        Config.FetchInfoAttemptPeriod,
22
        Config.T32InfoLocation,
23
        Config.F32RemExecutable,
24
        Config.F32RemArguments
21 25
    );
22 26
    
23 27
    public static int Main() {
24
        var exists = GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1;
25
        if (exists) {
28
        if (GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1) {
26 29
            DefaultLogger.Error("Another instance of the application is already running");
27 30
            return 1;
28 31
        }
......
43 46
            DefaultApiClient
44 47
        );
45 48
        
46
        DefaultLogger.Debug("Main -> starting the ApiClient");
47 49
        var apiClientThread = new Thread(DefaultApiClient.Run) {
48 50
            IsBackground = true
49 51
        };
......
55 57
        processThread.Start();
56 58

  
57 59
        while (true) {
58
            Thread.Sleep(10 * 1000);
60
            Thread.Sleep(MainLoopDelayMs);
59 61
        }
62
        
60 63
        return 0;
61 64
    }
62 65
}
ld_client/LDClient/appsettings.json
26 26
    "CacheFileName": "cache"
27 27
  },
28 28
  "DebuggerDetection": {
29
    "F32RemExecutable": "/home/silhavyj/School/KIV-ASWI/aswi2022bug-thugs/ld_client/Mock/t32rem/build/t32rem_mock",
30
    "F32RemArguments": "localhost port=20000 VERSION.HARDWARE",
29 31
    "T32ProcessName": "ld_mock",
30
    "T32InfoLocation": "C:\\app\\tools\\T32\\results\\ldResult.txt",
31
    "DetectionPeriod": 5000
32
    "T32InfoLocation": "ldResult.txt",
33
    "DetectionPeriod": 5000,
34
    "FetchInfoMaxAttempts": 5,
35
    "FetchInfoAttemptPeriod": 1000
32 36
  }
33 37
}
ld_client/LDClient/detection/InfoFetcher.cs
4 4
namespace LDClient.detection {
5 5

  
6 6
    public class InfoFetcher {
7

  
8
        private const string F32RemExecutable = "/home/silhavyj/School/KIV-ASWI/aswi2022bug-thugs/ld_client/Mock/t32rem/build/t32rem_mock";
9
        private const string F32RemArguments = "localhost port=20000 VERSION.HARDWARE";
7
        
10 8
        private const string UndefinedSerialNumber = "number";
11 9

  
12
        private readonly int _maxAttempts;
13
        private readonly int _waitPeriodMs;
10
        private readonly string _f32RemExecutable;
11
        private readonly string _f32RemArguments;
12
        
13
        private readonly uint _maxAttempts;
14
        private readonly uint _waitPeriodMs;
14 15
        private readonly string _infoFilePath;
15 16

  
16 17
        public string HeadSerialNumber { get; private set; } = UndefinedSerialNumber;
17 18
        public string BodySerialNumber { get; private set; } = UndefinedSerialNumber;
18 19

  
19
        public InfoFetcher(int maxAttempts, int waitPeriodMs, string infoFilePath) {
20
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string f32RemArguments) {
20 21
            _maxAttempts = maxAttempts;
21 22
            _waitPeriodMs = waitPeriodMs;
22 23
            _infoFilePath = infoFilePath;
24
            _f32RemExecutable = f32RemExecutable;
25
            _f32RemArguments = f32RemArguments;
23 26
        }
24 27

  
25 28
        public async Task<bool> FetchDataAsync() {
26 29
            Program.DefaultLogger.Info("Fetching data from the debugger.");
27
            var success = await SendRetrieveInfoCommandAsync(F32RemExecutable, F32RemArguments);
30
            var success = await SendRetrieveInfoCommandAsync(_f32RemExecutable, _f32RemArguments);
28 31
            if (!success) {
29 32
                Program.DefaultLogger.Error("Failed to fetch data from the debugger.");
30 33
                return false;
......
35 38
                    Program.DefaultLogger.Info($"Info file has been parsed successfully.");
36 39
                    return true;
37 40
                }
38
                await Task.Delay(_waitPeriodMs);
41
                await Task.Delay((int)_waitPeriodMs);
39 42
            }
40 43
            Program.DefaultLogger.Error("Failed to parse the into file. It may have not been created.");
41 44
            return false;
ld_client/LDClient/detection/ProcessProcessDetection.cs
1 1
using System.Diagnostics;
2
using System.Text.Json;
2 3
using LDClient.network;
3 4
using LDClient.network.data;
4 5

  
......
7 8
	 public class ProcessProcessDetection : IProcessDetection {
8 9
        
9 10
        private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
10
        
11

  
11 12
        private readonly string _processName;
12 13
        private readonly uint _detectionPeriodMs;
13 14
        private bool _processIsActive;
14 15
        private bool _failedToRetrieveData;
15
        private Payload? _lastlyConnected;
16
        private Payload? _lastConnectedPayload;
16 17

  
17 18
        private readonly InfoFetcher _infoFetcher;
18 19
        private readonly IApiClient _apiClient;
......
28 29
        private async Task<bool> RetrieveDataFromDebugger() {
29 30
            var success = await _infoFetcher.FetchDataAsync();
30 31
            if (success) {
31
                _lastlyConnected = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat);
32
                _lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat);
32 33
            }
33 34
            return success;
34 35
        }
35

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

  
ld_client/LDClient/utils/ConfigLoader.cs
1
using LDClient.utils.loggers;
1
using System.Runtime.InteropServices;
2
using LDClient.utils.loggers;
2 3
using Microsoft.Extensions.Configuration;
3 4

  
4 5
namespace LDClient.utils {
......
15 16
        public int LogChunkSize { get; private set; }
16 17
        public int LogChunkMaxCount { get; private set; }
17 18
        public int LogArchiveMaxCount { get; private set; }
18

  
19 19
        public int LogCleanupPeriod { get; private set; }
20 20
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
21 21
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
......
43 43
        public string T32ProcessName { get; private set; }
44 44
        public uint DetectionPeriod { get; private set; }
45 45
        public string T32InfoLocation { get; private set; }
46
        public string F32RemExecutable { get; private set; }
47
        public string F32RemArguments { get; private set; }
48
        public uint FetchInfoMaxAttempts { get; private set;  }
49
        public uint FetchInfoAttemptPeriod { get; private set; }
50
        
46 51
        #endregion
47 52

  
48 53
        public ConfigLoader() {
......
68 73
                ApiBaseAddress = network["ApiBaseAddress"];
69 74
                ApiUsbEndPoint = network["ApiLDEndPoint"];
70 75
                ApiPort = uint.Parse(network["ApiPort"]);
71

  
72

  
76
                
73 77
                var cache = configuration.GetSection(CacheSection);
74 78
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
75 79
                MaxEntries = uint.Parse(cache["MaxEntries"]);
76 80
                MaxRetries = uint.Parse(cache["MaxRetries"]);
77 81
                CacheFileName = cache["CacheFileName"];
78

  
79

  
82
                
80 83
                var debugger = configuration.GetSection(DdSection);
81 84
                T32ProcessName = debugger["T32ProcessName"];
82 85
                T32InfoLocation = debugger["T32InfoLocation"];
83 86
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
87
                F32RemExecutable = debugger["F32RemExecutable"];
88
                F32RemArguments = debugger["F32RemArguments"];
89
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
90
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
84 91

  
85 92
                Console.WriteLine("Configuration successfully loaded!");
86 93
            } catch (FormatException e) {
ld_client/Mock/t32rem/t32rem_mock.cpp
7 7
#include <cstring>
8 8

  
9 9
static constexpr int EXPECTED_NUMBER_OF_ARGUMENTS = 4;
10
static constexpr const char *OUTPUT_FILE_NAME = "output.txt";
10
static constexpr const char *OUTPUT_FILE_NAME = "ldResult.txt";
11 11
static constexpr const char *DEBUGGER_INFO =
12 12
        "B::version.hardware\n"
13 13
        "PowerDebug USB 3.0 via USB 3.0\n"

Také k dispozici: Unified diff