215 lines
9.0 KiB
C#
215 lines
9.0 KiB
C#
#region Namespace
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
using com.itac.mes.tools;
|
|
using com.itac.mes.proxy.business;
|
|
using Constants = com.itac.mes.tools.Constants;
|
|
using OibMonBizObj = Asm.As.Oib.Monitoring.Proxy.Business.EventArgs;
|
|
using OibClient = Asm.As.Oib.Client;
|
|
using System.Configuration;
|
|
#endregion
|
|
|
|
namespace Itac.Oib
|
|
{
|
|
public class MonitoringReceiver : IDisposable, IReceiver
|
|
{
|
|
#region Fields
|
|
|
|
public OIBEventHandler _eventHandler { get; set; }
|
|
private readonly String _lineFullPath;
|
|
private readonly OibClient.OibMonitoringEvents _oibMonitoringEvents;
|
|
private TimeSpan boardProcessedMaxAge = new TimeSpan(0, 0, 0);
|
|
private TimeSpan stationEventMaxAge = new TimeSpan(0, 0, 0);
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public MonitoringReceiver(OibClient.OibMonitoringEvents oibMonitoringEvents, string lineFullPath)
|
|
{
|
|
_oibMonitoringEvents = oibMonitoringEvents;
|
|
_lineFullPath = lineFullPath;
|
|
|
|
oibMonitoringEvents.BoardProcessed += BoardProcessedEventReceived;
|
|
oibMonitoringEvents.StationEvent += StationEventReceived;
|
|
|
|
try
|
|
{
|
|
String sMaxAge = (String)new AppSettingsReader().GetValue("BoardProcessedDataMaxAge", typeof(String));
|
|
boardProcessedMaxAge = TimeSpan.Parse(sMaxAge);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
// default Wert 1 Stunde
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "configured Value for 'BoardProcessedDataMaxAge' is invalid, use default value");
|
|
boardProcessedMaxAge = new TimeSpan(24, 0, 0);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
boardProcessedMaxAge = new TimeSpan(0, 0, 0);
|
|
}
|
|
if (boardProcessedMaxAge.Ticks > 0)
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "maximum age for BoardProcessedMessages = " + boardProcessedMaxAge.ToString());
|
|
else
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "no maximum age for BoardProcessedMessages");
|
|
try
|
|
{
|
|
String sMaxAge = (String)new AppSettingsReader().GetValue("StationEventMaxAge", typeof(String));
|
|
stationEventMaxAge = TimeSpan.Parse(sMaxAge);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
// default Wert 1 Stunde
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "configured Value for 'StationEventMaxAge' is invalid, use default value");
|
|
stationEventMaxAge = new TimeSpan(24, 0, 0);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
stationEventMaxAge = new TimeSpan(0, 0, 0);
|
|
}
|
|
if (stationEventMaxAge.Ticks > 0)
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "maximum age for StationEventMaxAge = " + stationEventMaxAge.ToString());
|
|
else
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "no maximum age for StationEventMaxAge");
|
|
}
|
|
#endregion
|
|
|
|
#region Start/Stopp
|
|
|
|
public void Start()
|
|
{
|
|
_oibMonitoringEvents.Start();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
_oibMonitoringEvents.Stop();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Handling of events
|
|
|
|
#region Board Processed
|
|
|
|
public void BoardProcessedEventReceived(object sender, OibMonBizObj.BoardProcessedEventArgs args)
|
|
{
|
|
try
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "BoardProcessedEventArgs");
|
|
if (boardProcessedMaxAge.Ticks > 0)
|
|
{
|
|
Boolean skip = false;
|
|
DateTime oldestDate = new DateTime(DateTime.Now.Ticks - boardProcessedMaxAge.Ticks).ToUniversalTime();
|
|
foreach (Asm.As.Oib.Monitoring.Proxy.Business.Objects.ProcessedBoard processedBoard in args.BoardProcessedData.ProcessedBoards)
|
|
{
|
|
if (processedBoard.StationTime != null && processedBoard.StationTime.ToUniversalTime().Ticks < oldestDate.Ticks)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, "found old BoardProcessedEvent from " + processedBoard.StationTime.ToUniversalTime().ToString() +
|
|
", oldest message to be processed is " + oldestDate.ToString());
|
|
skip = true;
|
|
}
|
|
}
|
|
if (skip)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, "ignore BoardProcessedEvent because one of its " +
|
|
"stationEvents is older than configured time " + boardProcessedMaxAge.ToString());
|
|
return;
|
|
}
|
|
}
|
|
var mmt = new BoardProcessedThread(_eventHandler, args.BoardProcessedData);
|
|
var thread = new Thread(mmt.ThreadFunction);
|
|
thread.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Error, "BoardProcessedEventArgs failed", e);
|
|
}
|
|
}
|
|
|
|
private class BoardProcessedThread : ThreadTemplate<Asm.As.Oib.Monitoring.Proxy.Business.Objects.BoardProcessedData>
|
|
{
|
|
public BoardProcessedThread(OIBEventHandler eventHandler, Asm.As.Oib.Monitoring.Proxy.Business.Objects.BoardProcessedData request) : base(eventHandler, request) { }
|
|
|
|
public void ThreadFunction()
|
|
{
|
|
var mesNamespaceConformRequest = com.itac.oib.monitoring.contracts.data.BoardProcessedDataMapper.get(_request);
|
|
getEventHandler().boardProcessedEventReceived(mesNamespaceConformRequest);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Station Event
|
|
|
|
public void StationEventReceived(object sender, OibMonBizObj.StationEventArgs args)
|
|
{
|
|
try
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "StationEventReceived");
|
|
|
|
if (stationEventMaxAge.Ticks > 0)
|
|
{
|
|
DateTime oldestDate = new DateTime(DateTime.Now.ToUniversalTime().Ticks - stationEventMaxAge.Ticks).ToUniversalTime();
|
|
if (args.StationEventComposite != null && args.StationEventComposite.StationTime.ToUniversalTime().Ticks < oldestDate.Ticks)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "ignore StationEvent because its time '" +
|
|
args.StationEventComposite.StationTime.ToUniversalTime() + "' is older than configured time " + stationEventMaxAge.ToString());
|
|
return;
|
|
}
|
|
}
|
|
|
|
var mmt = new StationEventThread(_eventHandler, args.StationEventComposite);
|
|
var thread = new Thread(mmt.ThreadFunction);
|
|
thread.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Error, "StationEventArgs failed", e);
|
|
}
|
|
}
|
|
|
|
public class StationEventThread : ThreadTemplate<Asm.As.Oib.Monitoring.Proxy.Business.Objects.StationEventComposite>
|
|
{
|
|
public StationEventThread(OIBEventHandler eventHandler, Asm.As.Oib.Monitoring.Proxy.Business.Objects.StationEventComposite request) : base(eventHandler, request) { }
|
|
|
|
public void ThreadFunction()
|
|
{
|
|
var mesNamespaceConformRequest = com.itac.oib.monitoring.contracts.data.StationEventCompositeMapper.get(_request);
|
|
getEventHandler().stationEventReceived(mesNamespaceConformRequest);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
void MonitoringSubscriptionHealthStatus(object sender, OibClient.OibServiceStatusEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (!e.SubscriptionCheckResult.Succeeded)
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Error, string.Format("OIB Monitoring Queue for line '{0}' cound not be checked.", _lineFullPath));
|
|
else
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, string.Format("OIB Monitoring Queue for line '{0}' is in state '{1}' containing '{2}' messages.", _lineFullPath, e.SubscriptionCheckResult.QueueStatus, e.SubscriptionCheckResult.MessagesInQueue));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Error, string.Format("OIB Monitoring Queue for line '{0}' checked failed: '{1}'.", _lineFullPath, ex.Message));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region IDisposeable
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_oibMonitoringEvents != null)
|
|
_oibMonitoringEvents.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
} |