Configuration now uses JSON. Fixed command line parsing
This commit is contained in:
+13
-11
@@ -1,11 +1,11 @@
|
||||
using IniParser;
|
||||
using NLog;
|
||||
using NLog;
|
||||
using System.Collections.Generic;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Invocation;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace Barsik
|
||||
@@ -30,7 +30,7 @@ namespace Barsik
|
||||
rootCommand.InvokeAsync(args).Wait();
|
||||
}
|
||||
|
||||
static void RunService(string configPath)
|
||||
static void RunService(string config)
|
||||
{
|
||||
Logging.Setup();
|
||||
logger.Info("Barsik monitoring service starting");
|
||||
@@ -39,18 +39,20 @@ namespace Barsik
|
||||
var server = new ModbusServer(registers);
|
||||
units = new List<BaseMonitoringUnit>();
|
||||
|
||||
var configParser = new FileIniDataParser();
|
||||
if (string.IsNullOrEmpty(configPath))
|
||||
if (string.IsNullOrEmpty(config))
|
||||
{
|
||||
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
configPath = Path.Combine(assemblyFolder, "barsik.ini");
|
||||
config = Path.Combine(assemblyFolder, "barsik.json");
|
||||
}
|
||||
var data = configParser.ReadFile(configPath);
|
||||
foreach (var section in data.Sections)
|
||||
string configStr = File.ReadAllText(config);
|
||||
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)
|
||||
{
|
||||
units.Add(unit);
|
||||
@@ -60,7 +62,7 @@ namespace Barsik
|
||||
}
|
||||
}
|
||||
logger.Info("Configuration loaded, we have " + units.Count + " units");
|
||||
server.Port = int.Parse(data["Server"]["Port"]);
|
||||
server.Port = port;
|
||||
server.Run();
|
||||
logger.Info("Exiting");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user