From 393cbeb5a942636c0944d2baf80ce322dffb1722 Mon Sep 17 00:00:00 2001 From: taywon18 Date: Thu, 25 Apr 2024 22:02:17 +0200 Subject: [PATCH] added various stuff --- Salmon.Model/Monitor/Software.cs | 11 +- Salmon.Service/Configuration.cs | 47 ++++++++ Salmon.Service/Program.cs | 113 ++++++++++++++---- Salmon.Service/Properties/launchSettings.json | 1 + Salmon.Service/RunEnv/config.json | 3 + Salmon.Service/RunEnv/start.bat | 2 + Salmon.Web/Pages/ElementList.razor | 6 +- 7 files changed, 152 insertions(+), 31 deletions(-) create mode 100644 Salmon.Service/Configuration.cs create mode 100644 Salmon.Service/RunEnv/config.json create mode 100644 Salmon.Service/RunEnv/start.bat diff --git a/Salmon.Model/Monitor/Software.cs b/Salmon.Model/Monitor/Software.cs index 59a25f0..52a4b3b 100644 --- a/Salmon.Model/Monitor/Software.cs +++ b/Salmon.Model/Monitor/Software.cs @@ -42,11 +42,17 @@ public class Software } + public static IEnumerable FromPath(string path) + { + throw new NotImplementedException(); + } + public static Software FromLocal() { - var mypath = Environment.ProcessPath; + if (Environment.ProcessPath == null) + throw new Exception($"Cannot create process for local non-pathed process."); - return FromLocal(Helper.CreateIdFrom(Helper.GetMachineId(), mypath)); + return FromLocal(Helper.CreateIdFrom(Helper.GetMachineId(), Environment.ProcessPath)); } public static Software FromLocal(string id) @@ -54,7 +60,6 @@ public class Software var currentProcess = Process.GetCurrentProcess(); var processPath = Environment.ProcessPath; - var soft = new Software(id) { ShortName = System.AppDomain.CurrentDomain.FriendlyName, diff --git a/Salmon.Service/Configuration.cs b/Salmon.Service/Configuration.cs new file mode 100644 index 0000000..4e75059 --- /dev/null +++ b/Salmon.Service/Configuration.cs @@ -0,0 +1,47 @@ +using Salmon.Core; +using Salmon.Model.Monitor; +using System.Security.Cryptography.X509Certificates; + +namespace Salmon.Service; + +public class Configuration +{ + public Uri? Url { get; set; } + public int? Period { get; set; } + public bool? MonitorHardware { get; set; } = true; + public bool? MonitorLocalSoftware { get; set; } = true; + + public List Watch { get; set; } = new (); +} + +public abstract class WatcherConfiguration +{ + public abstract Task> ForgeElements(); +} + +public class ExecutableInstanceWatcherConfiguration + : WatcherConfiguration +{ + public string Path { get; set; } + + public override async Task> ForgeElements() + { + List ret = new(); + + foreach (var el in Software.FromPath(Path)) + ret.Add(el); + + return ret; + } +} +public class HttpPageWatcherConfiguration + : WatcherConfiguration +{ + public string Method { get; set; } = "GET"; + public string Uri { get; set; } + + public override async Task> ForgeElements() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Salmon.Service/Program.cs b/Salmon.Service/Program.cs index 7ea9171..154819d 100644 --- a/Salmon.Service/Program.cs +++ b/Salmon.Service/Program.cs @@ -1,35 +1,90 @@ using Salmon.Core; +using Salmon.Service; using System.Diagnostics; +using System.Text.Json; + +const string ENV_PERIOD = "SALMON_PERIOD"; +const string ENV_URI = "SALMON_URI"; Console.WriteLine($"Salmon.Service initializing..."); -string uri_args = args.LastOrDefault(); +int? period = null; +Uri? uri = null; +bool? monitor_hardware = null; +bool? monitor_localsoftware = null; - -// Arguments parsing -string period_str = Environment.GetEnvironmentVariable("SALMON_PERIOD") ?? "60000"; -int period; -if (!int.TryParse(period_str, out period)) +// 1: use environment variable +var period_env = Environment.GetEnvironmentVariable(ENV_PERIOD); +if (period_env != null) { - Console.WriteLine($"Cannot parse env variable SALMON_PERIOD, should be an int in milliseconds."); + if (int.TryParse(period_env, out int p)) + period = p; + else + Console.WriteLine($"Cannot use env variable {ENV_PERIOD}, should be an int in milliseconds."); +} + +var uri_env = Environment.GetEnvironmentVariable(ENV_URI); +if(uri_env != null) +{ + if (Uri.TryCreate(uri_env, UriKind.Absolute, out var u)) + uri = u; + else + Console.WriteLine($"Cannot use env variable {ENV_URI}, url is malformated."); +} + +// 2: override with configuration file +string configpath = "config.json"; +if (File.Exists(configpath)) +{ + Console.WriteLine("Found a configuration file, parsing..."); + string content = File.ReadAllText(configpath); + Configuration? conf = null; + try + { + conf = JsonSerializer.Deserialize(content); + } + catch(Exception e) + { + Console.WriteLine($"Cannot parse config file \"{configpath}\", {e}."); + return 1; + } + + if(conf == null) + { + Console.WriteLine($"Cannot parse config file \"{configpath}\"."); + return 1; + } + + if(conf.Url != null) + uri = conf.Url; + + if(conf.Period != null) + period = conf.Period; + + if(conf.MonitorHardware != null) + monitor_hardware = conf.MonitorHardware; + + if(conf.MonitorLocalSoftware != null) + monitor_localsoftware = conf.MonitorLocalSoftware; +} + +//todo: override with parameter + +// 3 : override with argument + +// 4 : override with default +period ??= 60000; +monitor_hardware ??= true; +monitor_localsoftware ??= true; + +// 5 : Check if valid +if(uri == null) +{ + Console.WriteLine($"Cannot start: not any URI defined."); return 1; } -string uri_str = uri_args ?? Environment.GetEnvironmentVariable("SALMON_URI"); -if (uri_str == null) -{ - Console.WriteLine($"Cannot find env variable SALMON_URI, should be a valid URL."); - Console.WriteLine($"You may also use cmd line, uri must be the last argument."); - return 2; -} -Uri uri; -if(!Uri.TryCreate(uri_str, UriKind.Absolute, out uri)) -{ - Console.WriteLine($"Cannot parse env variable SALMON_URI ({uri_str}), it should be a valid URL."); - return 3; -} - // Initialisation Transmitter transmitter = new(); @@ -39,22 +94,30 @@ Console.WriteLine($"Salmon.Service started at {DateTime.Now}."); while ( true ) { - var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware(); + var software = Salmon.Model.Monitor.Software.FromLocal(); List tosend = new(); tosend.Add(software); - tosend.AddRange(hardwares); + if(monitor_hardware == true) + { + var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware(); + tosend.AddRange(hardwares); + } + try { await transmitter.SendAsync(uri, tosend); } + catch(HttpRequestException e) + { + Console.WriteLine($"[{e.StatusCode}] Sending data failed: {e.Message}."); + } catch(Exception e) { Console.WriteLine($"Sending data failed: {e}"); } - - await Task.Delay(period); + await Task.Delay(period.Value); } diff --git a/Salmon.Service/Properties/launchSettings.json b/Salmon.Service/Properties/launchSettings.json index 86c3577..55285f5 100644 --- a/Salmon.Service/Properties/launchSettings.json +++ b/Salmon.Service/Properties/launchSettings.json @@ -2,6 +2,7 @@ "profiles": { "Salmon.Service": { "commandName": "Project", + "workingDirectory": "$(ProjectDir)/RunEnv", "environmentVariables": { "SALMON_URI": "http://localhost:5009/api/Push" } diff --git a/Salmon.Service/RunEnv/config.json b/Salmon.Service/RunEnv/config.json new file mode 100644 index 0000000..0251e3d --- /dev/null +++ b/Salmon.Service/RunEnv/config.json @@ -0,0 +1,3 @@ +{ + "Url":"http://localhost:5009/api/Push" +} \ No newline at end of file diff --git a/Salmon.Service/RunEnv/start.bat b/Salmon.Service/RunEnv/start.bat new file mode 100644 index 0000000..ec524c7 --- /dev/null +++ b/Salmon.Service/RunEnv/start.bat @@ -0,0 +1,2 @@ +cd .. +dotnet run \ No newline at end of file diff --git a/Salmon.Web/Pages/ElementList.razor b/Salmon.Web/Pages/ElementList.razor index 3d2ec72..148e998 100644 --- a/Salmon.Web/Pages/ElementList.razor +++ b/Salmon.Web/Pages/ElementList.razor @@ -4,10 +4,10 @@

Éléments

- +
@foreach(var e in Elements) { - + @e.LastType @@ -33,7 +33,7 @@ } } - +
@code { List Elements = new();