Sicherung
This commit is contained in:
@@ -7,8 +7,13 @@ namespace FSI.BT.IR.Plc.TimeSync
|
|||||||
{
|
{
|
||||||
internal class SyncPlcTime : IPlc
|
internal class SyncPlcTime : IPlc
|
||||||
{
|
{
|
||||||
private Logger _log = LogManager.GetCurrentClassLogger();
|
#region Constants
|
||||||
private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss.fff";
|
|
||||||
|
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)
|
public SyncPlcTime(IPlc plc)
|
||||||
{
|
{
|
||||||
@@ -23,85 +28,137 @@ namespace FSI.BT.IR.Plc.TimeSync
|
|||||||
Enable = plc.Enable;
|
Enable = plc.Enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Name
|
||||||
|
/// </summary>
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Beschreibung
|
||||||
|
/// </summary>
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// IP-Adresse
|
||||||
|
/// </summary>
|
||||||
public string Adress { get; set; }
|
public string Adress { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Rack-Nummer
|
||||||
|
/// </summary>
|
||||||
public int Rack { get; set; }
|
public int Rack { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Slot-Nummer
|
||||||
|
/// </summary>
|
||||||
public int Slot { get; set; }
|
public int Slot { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update-Intervall, in der die Zeit überprüft werden soll.
|
||||||
|
/// </summary>
|
||||||
public int UpdateIntervall { get; set; }
|
public int UpdateIntervall { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zeitdifferenz, ab der die SPS-Zeit angepasst werden soll.
|
||||||
|
/// </summary>
|
||||||
public int TimeDifference { get; set; }
|
public int TimeDifference { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Locale Zeit wird an die SPS gesendet - nicht UTC-Zeit
|
||||||
|
/// </summary>
|
||||||
public bool LocalTime { get; set; }
|
public bool LocalTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Soll SPS-Zeit synchronisiert werden
|
||||||
|
/// </summary>
|
||||||
public bool Enable { get; set; }
|
public bool Enable { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// NTP-Server Adresse
|
||||||
|
/// </summary>
|
||||||
public string NtpServer { get; set; }
|
public string NtpServer { get; set; }
|
||||||
|
|
||||||
public async Task Snyc(CancellationToken cancellationToken) =>
|
public async Task Snyc(CancellationToken cancellationToken) =>
|
||||||
await Task.Run(async () =>
|
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)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var plc = new S7Client();
|
try
|
||||||
|
|
||||||
var connectionRslt = plc.ConnectTo(Adress, Rack, Slot);
|
|
||||||
|
|
||||||
if (connectionRslt == 0)
|
|
||||||
{
|
{
|
||||||
_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 + " " + ex.Message);
|
||||||
_log.Error(Name + " Fehler: " + plc.ErrorText(connectionRslt));
|
|
||||||
await Task.Delay(UpdateIntervall, cancellationToken);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var plcDateTime = new DateTime();
|
plc.GetPlcDateTime(ref plcDateTime); // Uhrzeit aus SPS auslesen
|
||||||
plc.GetPlcDateTime(ref plcDateTime);
|
|
||||||
_log.Debug(Name + " SPS Zeit (aktuell): " + plcDateTime.ToString(DATE_TIME_FORMAT));
|
_log.Debug(Name + " SPS Zeit (aktuell): " + plcDateTime.ToString(DATE_TIME_FORMAT));
|
||||||
|
|
||||||
var client = new NtpClient(NtpServer);
|
if (LocalTime) // lokale Zeit/Ortszeit
|
||||||
NtpClock clock = client.Query();
|
|
||||||
|
|
||||||
DateTime ntpDateTime;
|
|
||||||
|
|
||||||
if (LocalTime)
|
|
||||||
{
|
{
|
||||||
_log.Debug(Name + " UTC Zeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
|
_log.Debug(Name + " Ortszeit: " + clock.Now.ToString(DATE_TIME_FORMAT));
|
||||||
ntpDateTime = clock.Now.DateTime;
|
ntpDateTime = clock.Now.DateTime; // lokale Zeit/Ortszeit von NTP-Server
|
||||||
}
|
}
|
||||||
else
|
else // UTC - Zeit
|
||||||
{
|
{
|
||||||
_log.Debug(Name + " Ortszeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
|
_log.Debug(Name + " UTC-Zeit: " + clock.UtcNow.ToString(DATE_TIME_FORMAT));
|
||||||
ntpDateTime = clock.UtcNow.DateTime;
|
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);
|
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");
|
_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));
|
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
|
||||||
var temp = plc.SetPlcDateTime(ntpDateTime);
|
|
||||||
}
|
}
|
||||||
else if (TimeDifference == 0)
|
else if (TimeDifference == 0)
|
||||||
{
|
{
|
||||||
|
var temp = plc.SetPlcDateTime(ntpDateTime); // Zeit an SPS senden
|
||||||
_log.Info(Name + " neue Zeit: " + ntpDateTime.ToString(DATE_TIME_FORMAT));
|
_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.");
|
_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<target xsi:type="File"
|
<target xsi:type="File"
|
||||||
name="logfile"
|
name="logfile"
|
||||||
fileName="d:/logs/${appName}/${appName}.log"
|
fileName="d:/logs/${appName}/${appName}.log"
|
||||||
archiveFileName ="d:/logs/{#}_${appName}.log"
|
archiveFileName ="d:/logs/${appName}/{#}_${appName}.log"
|
||||||
archiveNumbering ="Date" archiveEvery="Day"
|
archiveNumbering ="Date" archiveEvery="Day"
|
||||||
archiveDateFormat="yyyyMMdd"/>
|
archiveDateFormat="yyyyMMdd"/>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user