added various stuff

This commit is contained in:
taywon18 2024-04-25 22:02:17 +02:00
parent 1c4821f2be
commit 393cbeb5a9
7 changed files with 152 additions and 31 deletions

View File

@ -42,11 +42,17 @@ public class Software
} }
public static IEnumerable<Software> FromPath(string path)
{
throw new NotImplementedException();
}
public static Software FromLocal() 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) public static Software FromLocal(string id)
@ -54,7 +60,6 @@ public class Software
var currentProcess = Process.GetCurrentProcess(); var currentProcess = Process.GetCurrentProcess();
var processPath = Environment.ProcessPath; var processPath = Environment.ProcessPath;
var soft = new Software(id) var soft = new Software(id)
{ {
ShortName = System.AppDomain.CurrentDomain.FriendlyName, ShortName = System.AppDomain.CurrentDomain.FriendlyName,

View File

@ -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<WatcherConfiguration> Watch { get; set; } = new ();
}
public abstract class WatcherConfiguration
{
public abstract Task<IEnumerable<Element>> ForgeElements();
}
public class ExecutableInstanceWatcherConfiguration
: WatcherConfiguration
{
public string Path { get; set; }
public override async Task<IEnumerable<Element>> ForgeElements()
{
List<Element> 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<IEnumerable<Element>> ForgeElements()
{
throw new NotImplementedException();
}
}

View File

@ -1,34 +1,89 @@
using Salmon.Core; using Salmon.Core;
using Salmon.Service;
using System.Diagnostics; using System.Diagnostics;
using System.Text.Json;
const string ENV_PERIOD = "SALMON_PERIOD";
const string ENV_URI = "SALMON_URI";
Console.WriteLine($"Salmon.Service initializing..."); Console.WriteLine($"Salmon.Service initializing...");
string uri_args = args.LastOrDefault(); int? period = null;
Uri? uri = null;
bool? monitor_hardware = null;
bool? monitor_localsoftware = null;
// 1: use environment variable
// Arguments parsing var period_env = Environment.GetEnvironmentVariable(ENV_PERIOD);
string period_str = Environment.GetEnvironmentVariable("SALMON_PERIOD") ?? "60000"; if (period_env != null)
int period;
if (!int.TryParse(period_str, out period))
{ {
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<Configuration>(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; 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 // Initialisation
@ -39,22 +94,30 @@ Console.WriteLine($"Salmon.Service started at {DateTime.Now}.");
while ( true ) while ( true )
{ {
var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware();
var software = Salmon.Model.Monitor.Software.FromLocal(); var software = Salmon.Model.Monitor.Software.FromLocal();
List<Element> tosend = new(); List<Element> tosend = new();
tosend.Add(software); tosend.Add(software);
tosend.AddRange(hardwares);
if(monitor_hardware == true)
{
var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware();
tosend.AddRange(hardwares);
}
try try
{ {
await transmitter.SendAsync(uri, tosend); await transmitter.SendAsync(uri, tosend);
} }
catch(HttpRequestException e)
{
Console.WriteLine($"[{e.StatusCode}] Sending data failed: {e.Message}.");
}
catch(Exception e) catch(Exception e)
{ {
Console.WriteLine($"Sending data failed: {e}"); Console.WriteLine($"Sending data failed: {e}");
} }
await Task.Delay(period.Value);
await Task.Delay(period);
} }

View File

@ -2,6 +2,7 @@
"profiles": { "profiles": {
"Salmon.Service": { "Salmon.Service": {
"commandName": "Project", "commandName": "Project",
"workingDirectory": "$(ProjectDir)/RunEnv",
"environmentVariables": { "environmentVariables": {
"SALMON_URI": "http://localhost:5009/api/Push" "SALMON_URI": "http://localhost:5009/api/Push"
} }

View File

@ -0,0 +1,3 @@
{
"Url":"http://localhost:5009/api/Push"
}

View File

@ -0,0 +1,2 @@
cd ..
dotnet run

View File

@ -4,10 +4,10 @@
<h3>Éléments</h3> <h3>Éléments</h3>
<CardGroup> <div Style="display:flex;flex-wrap:wrap; height:100%;">
@foreach(var e in Elements) @foreach(var e in Elements)
{ {
<Card Class="col-4" Style="width:18rem;"> <Card Class="col-6" Style="width:18rem;margin:10px;">
<CardHeader> <CardHeader>
@e.LastType @e.LastType
</CardHeader> </CardHeader>
@ -33,7 +33,7 @@
} }
</Card> </Card>
} }
</CardGroup> </div>
@code { @code {
List<Element> Elements = new(); List<Element> Elements = new();