Sicherung

This commit is contained in:
Maier Stephan SI
2024-08-27 07:31:12 +02:00
parent bc23a8514f
commit b7055bc31c
2 changed files with 93 additions and 36 deletions

View File

@@ -7,8 +7,13 @@ namespace FSI.BT.IR.Plc.TimeSync
{
internal class SyncPlcTime : IPlc
{
private Logger _log = LogManager.GetCurrentClassLogger();
private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss.fff";
#region Constants
private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss.fff"; // Zeitformat
#endregion
private Logger _log = LogManager.GetCurrentClassLogger(); // Nlog
public SyncPlcTime(IPlc plc)
{
@@ -23,85 +28,137 @@ namespace FSI.BT.IR.Plc.TimeSync
Enable = plc.Enable;
}
/// <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; }
/// <summary>
/// NTP-Server Adresse
/// </summary>
public string NtpServer { get; set; }
public async Task Snyc(CancellationToken cancellationToken) =>
await Task.Run(async () =>
{
var plc = new S7Client(); // SPS-Verbindung
var client = new NtpClient(NtpServer); // NTP-Client
NtpClock clock = client.Query(); // NTP-Client Uhrzeit
var plcDateTime = new DateTime(); // Uhrzeit SPS
DateTime ntpDateTime; // Uhrzeit NTP-Client
while (!cancellationToken.IsCancellationRequested)
{
var plc = new S7Client();
var connectionRslt = plc.ConnectTo(Adress, Rack, Slot);
if (connectionRslt == 0)
try
{
_log.Debug(Name + " Verbindung hergestellt.");
// Verbindung mit SPS-Aufbauen
var connectionRslt = plc.ConnectTo(Adress, Rack, Slot);
// Verbindungsstatus überprüfen
if (connectionRslt == 0) // Verbindung i.O.
{
_log.Debug(Name + " Verbindung hergestellt.");
}
else // Verbindung n.i.O.
{
_log.Error(Name + " Verbindung nicht hergestellt.");
_log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt));
await Task.Delay(UpdateIntervall, cancellationToken); // Warten bis zum nächsten Verbindungsversuch (Zeiten aus config.json)
continue;
}
}
else
catch (Exception ex)
{
_log.Error(Name + " Verbindung nicht hergestellt.");
_log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt));
await Task.Delay(UpdateIntervall, cancellationToken);
continue;
_log.Error(Name + " " + ex.Message);
}
var plcDateTime = new DateTime();
plc.GetPlcDateTime(ref plcDateTime);
plc.GetPlcDateTime(ref plcDateTime); // Uhrzeit aus SPS auslesen
_log.Debug(Name + " SPS Zeit (aktuell): " + plcDateTime.ToString(DATE_TIME_FORMAT));
var client = new NtpClient(NtpServer);
NtpClock clock = client.Query();
DateTime ntpDateTime;
if (LocalTime)
if (LocalTime) // lokale Zeit/Ortszeit
{
_log.Debug(Name + " UTC Zeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
ntpDateTime = clock.Now.DateTime;
_log.Debug(Name + " Ortszeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
ntpDateTime = clock.Now.DateTime; // lokale Zeit/Ortszeit von NTP-Server
}
else
else // UTC - Zeit
{
_log.Debug(Name + " Ortszeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
ntpDateTime = clock.UtcNow.DateTime;
_log.Debug(Name + " UTC-Zeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
ntpDateTime = clock.UtcNow.DateTime; // UTC-Zeit von NTP-Server
}
// Zeitdiffernz zwischen SPS-Zeit und Zeit von NTP-Server berechnen
var timeSpan = Math.Abs((plcDateTime - ntpDateTime).TotalMilliseconds);
if (TimeDifference > 0 && timeSpan >= TimeDifference)
if (
TimeDifference > 0 // Zeitdifferenz aus Einstellungen > 0 ms
&& timeSpan >= TimeDifference // Zeitdifferenz überprüfung
)
{
_log.Debug(Name + " Zeitdifferenz " + timeSpan + " ms überschritten");
var temp = plc.SetPlcDateTime(ntpDateTime); // Zeit an SPS senden
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
var temp = plc.SetPlcDateTime(ntpDateTime);
}
else if (TimeDifference == 0)
{
var temp = plc.SetPlcDateTime(ntpDateTime); // Zeit an SPS senden
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
var temp = plc.SetPlcDateTime(ntpDateTime);
}
plc.Disconnect();
plc.Disconnect(); // Verbindung zur SPS trennen
_log.Debug(Name + " Verbindung getrennt.");
await Task.Delay(UpdateIntervall, cancellationToken);
_log.Debug(Name + " Start Task-Wartezeit");
try
{
await Task.Delay(UpdateIntervall, cancellationToken); // Warten bis zum nächsten Verbindungsversuch (Zeiten aus config.json)
}
catch (Exception ex)
{
_log.Error(Name + " " + ex.Message);
}
_log.Debug(Name + " Ende Task-Wartezeit");
}
}, cancellationToken);
}, cancellationToken);
}
}

View File

@@ -12,7 +12,7 @@
<target xsi:type="File"
name="logfile"
fileName="d:/logs/${appName}/${appName}.log"
archiveFileName ="d:/logs/{#}_${appName}.log"
archiveFileName ="d:/logs/${appName}/{#}_${appName}.log"
archiveNumbering ="Date" archiveEvery="Day"
archiveDateFormat="yyyyMMdd"/>