119 lines
4.1 KiB
C#
119 lines
4.1 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.CommandLine;
|
|
using System.CommandLine.Invocation;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
|
|
namespace Barsik
|
|
{
|
|
class Program
|
|
{
|
|
private static RegistersContainer registers;
|
|
private static List<BaseMonitoringUnit> units;
|
|
private static JsonDocument configJson;
|
|
|
|
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);
|
|
var openhabConfigCommand = new Command("openhab-config")
|
|
{
|
|
new Option<string>("--config", () => "", "Path to configuration file")
|
|
};
|
|
openhabConfigCommand.Handler = CommandHandler.Create<string>(GenerateOpenhabConfig);
|
|
rootCommand.AddCommand(openhabConfigCommand);
|
|
// Init common things
|
|
registers = new RegistersContainer();
|
|
//
|
|
rootCommand.InvokeAsync(args).Wait();
|
|
}
|
|
|
|
|
|
private static JsonDocument LoadConfig(string config)
|
|
{
|
|
if (string.IsNullOrEmpty(config))
|
|
{
|
|
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
config = Path.Combine(assemblyFolder, "barsik.json");
|
|
}
|
|
string configStr = File.ReadAllText(config);
|
|
return JsonDocument.Parse(configStr);
|
|
}
|
|
|
|
private static List<BaseMonitoringUnit> LoadUnits(JsonDocument config)
|
|
{
|
|
var units = new List<BaseMonitoringUnit>();
|
|
var root = config.RootElement;
|
|
foreach (var unitElement in root.GetProperty("Units").EnumerateArray())
|
|
{
|
|
var unit = BaseMonitoringUnit.Create(unitElement, registers);
|
|
if (unit != null)
|
|
{
|
|
units.Add(unit);
|
|
}
|
|
}
|
|
return units;
|
|
}
|
|
|
|
static void GenerateOpenhabConfig(string config)
|
|
{
|
|
configJson = LoadConfig(config);
|
|
units = LoadUnits(configJson);
|
|
var root = configJson.RootElement;
|
|
string name = root.GetProperty("Name").GetString();
|
|
|
|
StringBuilder thingsConfig = new StringBuilder();
|
|
StringBuilder itemsConfig = new StringBuilder();
|
|
thingsConfig.AppendFormat("Bridge modbus:tcp:{0} [ host=\"{1}\", port={2}, id=1 ] {{\n",
|
|
name,
|
|
root.GetProperty("IP").GetString(),
|
|
root.GetProperty("Port").GetInt32());
|
|
// generate units configs
|
|
foreach (var unit in units)
|
|
{
|
|
unit.GenerateThingsConfig(thingsConfig);
|
|
unit.GenerateItemsConfig(name, itemsConfig);
|
|
}
|
|
//
|
|
thingsConfig.Append("}\n");
|
|
|
|
File.WriteAllText(name + ".things", thingsConfig.ToString());
|
|
File.WriteAllText(name + ".items", itemsConfig.ToString());
|
|
}
|
|
|
|
static void RunService(string config)
|
|
{
|
|
Logging.Setup();
|
|
logger.Info("Barsik monitoring service starting");
|
|
|
|
configJson = LoadConfig(config);
|
|
units = LoadUnits(configJson);
|
|
foreach (var unit in units)
|
|
{
|
|
Thread unitThread = new Thread(new ThreadStart(unit.Run));
|
|
unitThread.Start();
|
|
}
|
|
|
|
logger.Info("Configuration loaded, we have " + units.Count + " units");
|
|
var server = new ModbusServer(registers);
|
|
server.Port = configJson.RootElement.GetProperty("Port").GetInt32();
|
|
server.Run();
|
|
logger.Info("Exiting");
|
|
}
|
|
}
|
|
}
|