diff --git a/scr/FSI.BT.IR.Plc.TimeSync/FSI.BT.IR.Plc.TimeSync.csproj b/scr/FSI.BT.IR.Plc.TimeSync/FSI.BT.IR.Plc.TimeSync.csproj index 4cde1ce..787de7b 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/FSI.BT.IR.Plc.TimeSync.csproj +++ b/scr/FSI.BT.IR.Plc.TimeSync/FSI.BT.IR.Plc.TimeSync.csproj @@ -8,6 +8,7 @@ + diff --git a/scr/FSI.BT.IR.Plc.TimeSync/Program.cs b/scr/FSI.BT.IR.Plc.TimeSync/Program.cs index 80a2443..e6b19f6 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/Program.cs +++ b/scr/FSI.BT.IR.Plc.TimeSync/Program.cs @@ -1,5 +1,4 @@ using FSI.BT.IR.Plc.TimeSync; -using FSI.BT.IR.Plc.TimeSync.Settings; using static FSI.BT.IR.Plc.TimeSync.Settings.Context; var builder = Host.CreateApplicationBuilder(args); diff --git a/scr/FSI.BT.IR.Plc.TimeSync/Settings/AppContext.cs b/scr/FSI.BT.IR.Plc.TimeSync/Settings/AppContext.cs index a6a7155..35dcf83 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/Settings/AppContext.cs +++ b/scr/FSI.BT.IR.Plc.TimeSync/Settings/AppContext.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using System.Runtime.CompilerServices; +using CommandLine; using Microsoft.Extensions.Primitives; using static FSI.BT.IR.Plc.TimeSync.Settings.Context; @@ -7,27 +8,23 @@ namespace FSI.BT.IR.Plc.TimeSync.Settings { public class AppContext : ISettings { - private Cfg _cfg; - + private string[] _args; public AppContext() { + _args = new string[0]; LoadSettings(); LoadCfg(); - } private void LoadSettings() { var values = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) .Build(); Settings = values.Get(); - - var tmo = Settings.Logging.LogLevel.MicrosoftHostingLifetime; - var verstion = Settings.Version.ToString(); } private void LoadCfg() diff --git a/scr/FSI.BT.IR.Plc.TimeSync/Settings/ConsoleArgs.cs b/scr/FSI.BT.IR.Plc.TimeSync/Settings/ConsoleArgs.cs new file mode 100644 index 0000000..90f5616 --- /dev/null +++ b/scr/FSI.BT.IR.Plc.TimeSync/Settings/ConsoleArgs.cs @@ -0,0 +1,12 @@ +using CommandLine; + +namespace FSI.BT.IR.Plc.TimeSync.Settings +{ + public class ConsoleArgs + { + [Option('v', "version", Required = false, HelpText = "Version")] + public bool Version { get; set; } + + + } +} diff --git a/scr/FSI.BT.IR.Plc.TimeSync/Settings/Context.cs b/scr/FSI.BT.IR.Plc.TimeSync/Settings/Context.cs index 0d8ccab..9d8b96a 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/Settings/Context.cs +++ b/scr/FSI.BT.IR.Plc.TimeSync/Settings/Context.cs @@ -1,41 +1,106 @@ using System.ComponentModel; -using System.Configuration; -using System.Text.Json; -using System.Text.Json.Serialization; namespace FSI.BT.IR.Plc.TimeSync.Settings { + /// + /// Datenbasis von config.json und appsettings.json + /// Einstellungen und Konfigurationsdaten + /// public class Context { public interface ISettings : INotifyPropertyChanged { + /// + /// Daten von appsettings.json + /// public Settings Settings { get; set; } + /// + /// Daten von config.json + /// public Cfg Cfg { get; set; } } + /// + /// Daten von appsettings.json + /// public record Settings { + /// + /// Versions-Informationen + /// public Version Version { get; set; } + + /// + /// Build-Informationen + /// + public Build Build { get; set; } + + /// + /// Logging-Einstellungen + /// public Logging Logging { get; set; } } + /// + /// Versions-Informationen + /// public record Version { + /// + /// Haupt-Versionsnummer + /// public uint Major { get; set; } = 0; + + /// + /// Unter-Versionsnummer + /// public uint Minor { get; set; } = 0; + /// + /// Patch/Hotfix + /// public uint Patch { get; set; } = 0; + /// + /// optinoale Versionsinformationen + /// public string? Optional { get; set; } + /// + /// gibt die Versions-Nummer zurück + /// + /// Versionsnummer public override string ToString() - { + { return Major.ToString() + "." + Minor.ToString() + "." + Patch.ToString() + ((Optional == string.Empty || Optional == null) ? "" : "-" + Optional); } + } - + /// + /// Build-Informationen + /// + public record Build + { + /// + /// Ersteller + /// + public string Creator { get; set; } + /// + /// Organisation + /// + public string Organization { get; set; } + + /// + /// Erstellungsjahr + /// + public int CreationYear { get; set; } + + /// + /// Beschreibung + /// + public string Description { get; set; } } public class Logging @@ -43,6 +108,9 @@ namespace FSI.BT.IR.Plc.TimeSync.Settings public Loglevel LogLevel { get; set; } } + /// + /// Logging-Einstellungen + /// public class Loglevel { public string Default { get; set; } @@ -51,45 +119,119 @@ namespace FSI.BT.IR.Plc.TimeSync.Settings public string MicrosoftHostingLifetime { get; set; } } + /// + /// Daten von config.json + /// public record Cfg { + /// + /// Spsen, deren Zeit mit NTP-Server syncronsiert werden sollen + /// public List Plcs { get; set; } + /// + /// NTP-Server Adresse + /// public string NtpServer { get; set; } } + /// + /// SPS-Daten + /// public record Plc : IPlc { + /// + /// Name + /// public string Name { get; set; } + + /// + /// Beschreibung + /// public string Description { get; set; } + + /// + /// IP-Adresse + /// public string Adress { get; set; } + + /// + /// Rack-Nummer + /// public int Rack { get; set; } + + /// + /// Slot-Nummer + /// public int Slot { get; set; } + + /// + /// Update-Intervall, in der die Zeit überprüft werden soll. + /// public int UpdateIntervall { get; set; } + + /// + /// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll. + /// public int TimeDifference { get; set; } + + /// + /// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit + /// public bool LocalTime { get; set; } + + /// + /// Soll SPS-Zeit synchronisiert werden + /// public bool Enable { get; set; } } public interface IPlc { + /// + /// Name + /// public string Name { get; set; } - + + /// + /// Beschreibung + /// public string Description { get; set; } + /// + /// IP-Adresse + /// public string Adress { get; set; } + /// + /// Rack-Nummer + /// public int Rack { get; set; } + /// + /// Slot-Nummer + /// public int Slot { get; set; } + /// + /// Update-Intervall, in der die Zeit überprüft werden soll. + /// public int UpdateIntervall { get; set; } + /// + /// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll. + /// public int TimeDifference { get; set; } + /// + /// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit + /// public bool LocalTime { get; set; } + /// + /// Soll SPS-Zeit synchronisiert werden + /// public bool Enable { get; set; } } } diff --git a/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs b/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs index 89492fe..7163db6 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs +++ b/scr/FSI.BT.IR.Plc.TimeSync/SyncPlcTime.cs @@ -3,7 +3,6 @@ using Sharp7; using GuerrillaNtp; using static FSI.BT.IR.Plc.TimeSync.Settings.Context; - namespace FSI.BT.IR.Plc.TimeSync { internal class SyncPlcTime : IPlc @@ -25,10 +24,15 @@ namespace FSI.BT.IR.Plc.TimeSync } public string Name { get; set; } + public string Description { get; set; } + public string Adress { get; set; } + public int Rack { get; set; } + public int Slot { get; set; } + public int UpdateIntervall { get; set; } public int TimeDifference { get; set; } @@ -39,7 +43,6 @@ namespace FSI.BT.IR.Plc.TimeSync public string NtpServer { get; set; } - public async Task Snyc(CancellationToken cancellationToken) => await Task.Run(async () => { @@ -57,7 +60,8 @@ namespace FSI.BT.IR.Plc.TimeSync { _log.Error(Name + " Verbindung nicht hergestellt."); _log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt)); - return; + await Task.Delay(UpdateIntervall, cancellationToken); + continue; } var plcDateTime = new DateTime(); diff --git a/scr/FSI.BT.IR.Plc.TimeSync/Worker.cs b/scr/FSI.BT.IR.Plc.TimeSync/Worker.cs index 574a551..ea93646 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/Worker.cs +++ b/scr/FSI.BT.IR.Plc.TimeSync/Worker.cs @@ -10,22 +10,34 @@ namespace FSI.BT.IR.Plc.TimeSync private CancellationTokenSource _tokenSource; private CancellationToken _stoppingToken; + /// + /// Standard Taks + /// + /// + /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - - settings.PropertyChanged += Settings_PropertyChanged; + settings.PropertyChanged += Settings_PropertyChanged; // Event, bei Änderungen an der Config-Datei - StartTasks(); + StartTasks(); // Tasks, die beim Start ausgeführt werden } + /// + /// Event, bei Änderungen an der config.json. + /// + /// + /// private void Settings_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) { StopTasks(); StartTasks(); } + /// + /// Tasks, die beim Start ausgeführt werden sollen. + /// private void StartTasks() { @@ -36,10 +48,10 @@ namespace FSI.BT.IR.Plc.TimeSync _tokenSource = new CancellationTokenSource(); _stoppingToken = _tokenSource.Token; + // Schleife über alle Spsen foreach (var plc in settings.Cfg.Plcs) { - - if (plc.Enable) + if (plc.Enable) { var timeSync = new SyncPlcTime(plc); timeSync.NtpServer = settings.Cfg.NtpServer; @@ -51,11 +63,15 @@ namespace FSI.BT.IR.Plc.TimeSync } + /// + /// alle Taks werden gestoppt. + /// private async void StopTasks() { _tokenSource.Cancel(); _stoppingToken = _tokenSource.Token; + // Schleife über alle Spsen foreach (var task in _taskList) { task.Snyc(_stoppingToken); @@ -65,7 +81,5 @@ namespace FSI.BT.IR.Plc.TimeSync _taskList?.Clear(); _taskList = null; } - - } } diff --git a/scr/FSI.BT.IR.Plc.TimeSync/appsettings.json b/scr/FSI.BT.IR.Plc.TimeSync/appsettings.json index de26e29..6884de1 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/appsettings.json +++ b/scr/FSI.BT.IR.Plc.TimeSync/appsettings.json @@ -3,7 +3,13 @@ "Major": 0, "Minor": 0, "Patch": 0, - "Optional": "alpha" + "Optional": "alpha" + }, + "Build": { + "Creator": "Stephan Maier", + "Organization": "Fondium Singen GmbH", + "CreationYear": "2024", + "Description": "" }, "Logging": { "LogLevel": { diff --git a/scr/FSI.BT.IR.Plc.TimeSync/config.json b/scr/FSI.BT.IR.Plc.TimeSync/config.json index eb46f81..040a7c8 100644 --- a/scr/FSI.BT.IR.Plc.TimeSync/config.json +++ b/scr/FSI.BT.IR.Plc.TimeSync/config.json @@ -1,5 +1,4 @@ { - "Plcs": [ { "Name": "PL1 FA", @@ -25,5 +24,4 @@ } ], "NtpServer": "10.10.199.41" - }