Configuration now uses JSON. Fixed command line parsing
This commit is contained in:
+1
-2
@@ -2,13 +2,12 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<ApplicationIcon />
|
<ApplicationIcon />
|
||||||
<StartupObject>Barsik.Program</StartupObject>
|
<StartupObject>Barsik.Program</StartupObject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ini-parser" Version="2.5.2" />
|
|
||||||
<PackageReference Include="NLog" Version="4.7.4" />
|
<PackageReference Include="NLog" Version="4.7.4" />
|
||||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
|
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
using IniParser.Model;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Barsik
|
namespace Barsik
|
||||||
@@ -21,7 +21,7 @@ namespace Barsik
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
class MDStatMonitoringUnit : BaseMonitoringUnit
|
class MDStatMonitoringUnit : BaseMonitoringUnit
|
||||||
{
|
{
|
||||||
public MDStatMonitoringUnit(SectionData config, RegistersContainer registers) : base(config, registers)
|
public MDStatMonitoringUnit(JsonElement config, RegistersContainer registers) : base(config, registers)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-8
@@ -1,29 +1,29 @@
|
|||||||
using IniParser.Model;
|
using System;
|
||||||
using System;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Barsik
|
namespace Barsik
|
||||||
{
|
{
|
||||||
class BaseMonitoringUnit
|
class BaseMonitoringUnit
|
||||||
{
|
{
|
||||||
protected SectionData config;
|
protected JsonElement config;
|
||||||
protected RegistersContainer registers;
|
protected RegistersContainer registers;
|
||||||
|
|
||||||
protected int baseAddress;
|
protected int baseAddress;
|
||||||
|
|
||||||
public BaseMonitoringUnit(SectionData config, RegistersContainer registers)
|
public BaseMonitoringUnit(JsonElement config, RegistersContainer registers)
|
||||||
{
|
{
|
||||||
this.config = config;
|
this.config = config.Clone();
|
||||||
this.registers = registers;
|
this.registers = registers;
|
||||||
baseAddress = int.Parse(config.Keys["BaseAddress"]);
|
baseAddress = config.GetProperty("BaseAddress").GetInt32();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Run()
|
public virtual void Run()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BaseMonitoringUnit Create(SectionData config, RegistersContainer registers)
|
public static BaseMonitoringUnit Create(JsonElement config, RegistersContainer registers)
|
||||||
{
|
{
|
||||||
var type = config.Keys["Type"];
|
var type = config.GetProperty("Type").GetString();
|
||||||
if (type == "Sensors")
|
if (type == "Sensors")
|
||||||
{
|
{
|
||||||
return new SensorsMonitoringUnit(config, registers);
|
return new SensorsMonitoringUnit(config, registers);
|
||||||
|
|||||||
+13
-11
@@ -1,11 +1,11 @@
|
|||||||
using IniParser;
|
using NLog;
|
||||||
using NLog;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.CommandLine;
|
using System.CommandLine;
|
||||||
using System.CommandLine.Invocation;
|
using System.CommandLine.Invocation;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Barsik
|
namespace Barsik
|
||||||
@@ -30,7 +30,7 @@ namespace Barsik
|
|||||||
rootCommand.InvokeAsync(args).Wait();
|
rootCommand.InvokeAsync(args).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RunService(string configPath)
|
static void RunService(string config)
|
||||||
{
|
{
|
||||||
Logging.Setup();
|
Logging.Setup();
|
||||||
logger.Info("Barsik monitoring service starting");
|
logger.Info("Barsik monitoring service starting");
|
||||||
@@ -39,18 +39,20 @@ namespace Barsik
|
|||||||
var server = new ModbusServer(registers);
|
var server = new ModbusServer(registers);
|
||||||
units = new List<BaseMonitoringUnit>();
|
units = new List<BaseMonitoringUnit>();
|
||||||
|
|
||||||
var configParser = new FileIniDataParser();
|
if (string.IsNullOrEmpty(config))
|
||||||
if (string.IsNullOrEmpty(configPath))
|
|
||||||
{
|
{
|
||||||
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||||
configPath = Path.Combine(assemblyFolder, "barsik.ini");
|
config = Path.Combine(assemblyFolder, "barsik.json");
|
||||||
}
|
}
|
||||||
var data = configParser.ReadFile(configPath);
|
string configStr = File.ReadAllText(config);
|
||||||
foreach (var section in data.Sections)
|
int port = 0;
|
||||||
|
using (JsonDocument doc = JsonDocument.Parse(configStr))
|
||||||
{
|
{
|
||||||
if (section.SectionName.StartsWith("Unit"))
|
var root = doc.RootElement;
|
||||||
|
port = root.GetProperty("Port").GetInt32();
|
||||||
|
foreach (var unitElement in root.GetProperty("Units").EnumerateArray())
|
||||||
{
|
{
|
||||||
var unit = BaseMonitoringUnit.Create(section, registers);
|
var unit = BaseMonitoringUnit.Create(unitElement, registers);
|
||||||
if (unit != null)
|
if (unit != null)
|
||||||
{
|
{
|
||||||
units.Add(unit);
|
units.Add(unit);
|
||||||
@@ -60,7 +62,7 @@ namespace Barsik
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.Info("Configuration loaded, we have " + units.Count + " units");
|
logger.Info("Configuration loaded, we have " + units.Count + " units");
|
||||||
server.Port = int.Parse(data["Server"]["Port"]);
|
server.Port = port;
|
||||||
server.Run();
|
server.Run();
|
||||||
logger.Info("Exiting");
|
logger.Info("Exiting");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using IniParser.Model;
|
using System;
|
||||||
using System;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Barsik
|
namespace Barsik
|
||||||
@@ -12,14 +12,14 @@ namespace Barsik
|
|||||||
{
|
{
|
||||||
const string CORE_STR = "Core{0} Temp:";
|
const string CORE_STR = "Core{0} Temp:";
|
||||||
|
|
||||||
public SensorsMonitoringUnit(SectionData config, RegistersContainer registers) : base(config, registers)
|
public SensorsMonitoringUnit(JsonElement config, RegistersContainer registers) : base(config, registers)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Run()
|
public override void Run()
|
||||||
{
|
{
|
||||||
int cores = int.Parse(config.Keys["Cores"]);
|
int cores = config.GetProperty("Cores").GetInt32();
|
||||||
var multiplier = float.Parse(config.Keys["Multiplier"]);
|
var multiplier = config.GetProperty("Multiplier").GetDouble();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("sensors");
|
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("sensors");
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
[Server]
|
|
||||||
Port = 4500
|
|
||||||
|
|
||||||
[Unit_CPU]
|
|
||||||
Type = Sensors
|
|
||||||
BaseAddress = 1
|
|
||||||
Multiplier = 100
|
|
||||||
Cores = 2
|
|
||||||
|
|
||||||
[Unit_RAID]
|
|
||||||
Type = MDStat
|
|
||||||
BaseAddress = 3
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"Port": 4500,
|
||||||
|
"Units": [
|
||||||
|
{
|
||||||
|
"Name": "CPU",
|
||||||
|
"Type": "Sensors",
|
||||||
|
"BaseAddress": 1,
|
||||||
|
"Multiplier": 100,
|
||||||
|
"Cores": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "RAID",
|
||||||
|
"Type": "MDStat",
|
||||||
|
"BaseAddress": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user