71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
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
|
|
{
|
|
class Program
|
|
{
|
|
private static RegistersContainer registers;
|
|
private static List<BaseMonitoringUnit> units;
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
CultureInfo.CurrentCulture = new CultureInfo("en-US");
|
|
var rootCommand = new RootCommand();
|
|
var runCommand = new Command("run")
|
|
{
|
|
new Option<string>("--config", () => "", "Path to configuration file")
|
|
};
|
|
runCommand.Handler = CommandHandler.Create<string>(RunService);
|
|
rootCommand.AddCommand(runCommand);
|
|
rootCommand.InvokeAsync(args).Wait();
|
|
}
|
|
|
|
static void RunService(string config)
|
|
{
|
|
Logging.Setup();
|
|
logger.Info("Barsik monitoring service starting");
|
|
|
|
registers = new RegistersContainer();
|
|
var server = new ModbusServer(registers);
|
|
units = new List<BaseMonitoringUnit>();
|
|
|
|
if (string.IsNullOrEmpty(config))
|
|
{
|
|
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
config = Path.Combine(assemblyFolder, "barsik.json");
|
|
}
|
|
string configStr = File.ReadAllText(config);
|
|
int port = 0;
|
|
using (JsonDocument doc = JsonDocument.Parse(configStr))
|
|
{
|
|
var root = doc.RootElement;
|
|
port = root.GetProperty("Port").GetInt32();
|
|
foreach (var unitElement in root.GetProperty("Units").EnumerateArray())
|
|
{
|
|
var unit = BaseMonitoringUnit.Create(unitElement, registers);
|
|
if (unit != null)
|
|
{
|
|
units.Add(unit);
|
|
Thread unitThread = new Thread(new ThreadStart(unit.Run));
|
|
unitThread.Start();
|
|
}
|
|
}
|
|
}
|
|
logger.Info("Configuration loaded, we have " + units.Count + " units");
|
|
server.Port = port;
|
|
server.Run();
|
|
logger.Info("Exiting");
|
|
}
|
|
}
|
|
}
|