Tagessicherung
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="Config.Net" Version="5.2.0" />
|
||||
<PackageReference Include="GuerrillaNtp" Version="3.1.0" />
|
||||
<PackageReference Include="Json.Net" Version="1.0.33" />
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Context.Settings>();
|
||||
|
||||
var tmo = Settings.Logging.LogLevel.MicrosoftHostingLifetime;
|
||||
var verstion = Settings.Version.ToString();
|
||||
}
|
||||
|
||||
private void LoadCfg()
|
||||
|
||||
12
scr/FSI.BT.IR.Plc.TimeSync/Settings/ConsoleArgs.cs
Normal file
12
scr/FSI.BT.IR.Plc.TimeSync/Settings/ConsoleArgs.cs
Normal file
@@ -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; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Datenbasis von config.json und appsettings.json
|
||||
/// Einstellungen und Konfigurationsdaten
|
||||
/// </summary>
|
||||
public class Context
|
||||
{
|
||||
public interface ISettings : INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Daten von appsettings.json
|
||||
/// </summary>
|
||||
public Settings Settings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Daten von config.json
|
||||
/// </summary>
|
||||
public Cfg Cfg { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daten von appsettings.json
|
||||
/// </summary>
|
||||
public record Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// Versions-Informationen
|
||||
/// </summary>
|
||||
public Version Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Build-Informationen
|
||||
/// </summary>
|
||||
public Build Build { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Logging-Einstellungen
|
||||
/// </summary>
|
||||
public Logging Logging { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Versions-Informationen
|
||||
/// </summary>
|
||||
public record Version
|
||||
{
|
||||
/// <summary>
|
||||
/// Haupt-Versionsnummer
|
||||
/// </summary>
|
||||
public uint Major { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Unter-Versionsnummer
|
||||
/// </summary>
|
||||
public uint Minor { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Patch/Hotfix
|
||||
/// </summary>
|
||||
public uint Patch { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// optinoale Versionsinformationen
|
||||
/// </summary>
|
||||
public string? Optional { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// gibt die Versions-Nummer zurück
|
||||
/// </summary>
|
||||
/// <returns>Versionsnummer</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
{
|
||||
return Major.ToString() + "." + Minor.ToString() + "." + Patch.ToString() + ((Optional == string.Empty || Optional == null) ? "" : "-" + Optional);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Build-Informationen
|
||||
/// </summary>
|
||||
public record Build
|
||||
{
|
||||
/// <summary>
|
||||
/// Ersteller
|
||||
/// </summary>
|
||||
public string Creator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Organisation
|
||||
/// </summary>
|
||||
public string Organization { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Erstellungsjahr
|
||||
/// </summary>
|
||||
public int CreationYear { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Beschreibung
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
||||
public class Logging
|
||||
@@ -43,6 +108,9 @@ namespace FSI.BT.IR.Plc.TimeSync.Settings
|
||||
public Loglevel LogLevel { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logging-Einstellungen
|
||||
/// </summary>
|
||||
public class Loglevel
|
||||
{
|
||||
public string Default { get; set; }
|
||||
@@ -51,45 +119,119 @@ namespace FSI.BT.IR.Plc.TimeSync.Settings
|
||||
public string MicrosoftHostingLifetime { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daten von config.json
|
||||
/// </summary>
|
||||
public record Cfg
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Spsen, deren Zeit mit NTP-Server syncronsiert werden sollen
|
||||
/// </summary>
|
||||
public List<Plc> Plcs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// NTP-Server Adresse
|
||||
/// </summary>
|
||||
public string NtpServer { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SPS-Daten
|
||||
/// </summary>
|
||||
public record Plc : IPlc
|
||||
{
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Beschreibung
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// IP-Adresse
|
||||
/// </summary>
|
||||
public string Adress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Rack-Nummer
|
||||
/// </summary>
|
||||
public int Rack { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Slot-Nummer
|
||||
/// </summary>
|
||||
public int Slot { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Update-Intervall, in der die Zeit überprüft werden soll.
|
||||
/// </summary>
|
||||
public int UpdateIntervall { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll.
|
||||
/// </summary>
|
||||
public int TimeDifference { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit
|
||||
/// </summary>
|
||||
public bool LocalTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Soll SPS-Zeit synchronisiert werden
|
||||
/// </summary>
|
||||
public bool Enable { get; set; }
|
||||
}
|
||||
|
||||
public interface IPlc
|
||||
{
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Beschreibung
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// IP-Adresse
|
||||
/// </summary>
|
||||
public string Adress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Rack-Nummer
|
||||
/// </summary>
|
||||
public int Rack { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Slot-Nummer
|
||||
/// </summary>
|
||||
public int Slot { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Update-Intervall, in der die Zeit überprüft werden soll.
|
||||
/// </summary>
|
||||
public int UpdateIntervall { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll.
|
||||
/// </summary>
|
||||
public int TimeDifference { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit
|
||||
/// </summary>
|
||||
public bool LocalTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Soll SPS-Zeit synchronisiert werden
|
||||
/// </summary>
|
||||
public bool Enable { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -10,22 +10,34 @@ namespace FSI.BT.IR.Plc.TimeSync
|
||||
private CancellationTokenSource _tokenSource;
|
||||
private CancellationToken _stoppingToken;
|
||||
|
||||
/// <summary>
|
||||
/// Standard Taks
|
||||
/// </summary>
|
||||
/// <param name="stoppingToken"></param>
|
||||
/// <returns></returns>
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
|
||||
settings.PropertyChanged += Settings_PropertyChanged;
|
||||
settings.PropertyChanged += Settings_PropertyChanged; // Event, bei <20>nderungen an der Config-Datei
|
||||
|
||||
StartTasks();
|
||||
StartTasks(); // Tasks, die beim Start ausgef<65>hrt werden
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event, bei <20>nderungen an der config.json.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void Settings_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||
{
|
||||
StopTasks();
|
||||
StartTasks();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tasks, die beim Start ausgef<65>hrt werden sollen.
|
||||
/// </summary>
|
||||
private void StartTasks()
|
||||
{
|
||||
|
||||
@@ -36,10 +48,10 @@ namespace FSI.BT.IR.Plc.TimeSync
|
||||
_tokenSource = new CancellationTokenSource();
|
||||
_stoppingToken = _tokenSource.Token;
|
||||
|
||||
// Schleife <20>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
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// alle Taks werden gestoppt.
|
||||
/// </summary>
|
||||
private async void StopTasks()
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
_stoppingToken = _tokenSource.Token;
|
||||
|
||||
// Schleife <20>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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
{
|
||||
|
||||
"Plcs": [
|
||||
{
|
||||
"Name": "PL1 FA",
|
||||
@@ -25,5 +24,4 @@
|
||||
}
|
||||
],
|
||||
"NtpServer": "10.10.199.41"
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user