git_bsmd/nsw/Source/SendNSWMessageService/NSWSendService.cs

311 lines
16 KiB
C#

using bsmd.database;
using bsmd.status;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace SendNSWMessageService
{
public partial class NSWSendService : ServiceBase
{
private Timer _timer;
private object _timerlock = new object();
private bool processRunning = false;
private ILog _log = LogManager.GetLogger(typeof(NSWSendService));
public NSWSendService()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
InitializeComponent();
System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true; // **** Immer OK weil wir nur mit einem dedizierten Endpoint reden..
};
}
public void Commandline(string[] args)
{
// Dieser Modus dient allein dazu, eine separate Instanz "nebenher" laufen zu lassen, ohne
// sie als Service zu installieren. Dies ist sinnvoll um für spezielle HIS Einstellungen Tests zu machen
// ohne den laufenden Betrieb zu stören (hoffentlich!)
this.Init(args); // setup timer
this.DoOnce();
while (true)
System.Threading.Thread.Sleep(1000); // sleep until someone kills me :)
}
protected override void OnStart(string[] args)
{
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
this.Init(args);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
this.EventLog.WriteEntry("NSW Send Service started", EventLogEntryType.Information);
_log.InfoFormat("NSW Send Service started. v.{0}", version);
this.DoOnce();
}
public void Init(string[] args)
{
this._timer = new Timer();
this._timer.Interval = Properties.Settings.Default.SleepSeconds * 1000;
this._timer.Elapsed += _timer_Elapsed;
this._timer.Enabled = true;
}
public void DoOnce()
{
this._timer_Elapsed(null, null);
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this._timerlock)
{
if (this.processRunning) return;
else this.processRunning = true;
}
bool sendSucceeded;
if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString))
{
// Datenbank auf zu sendende Objekte überprüfen und laden
List<MessageCore> coresMarkedForSending = DBManager.Instance.GetMessageCoresByStatus(MessageCore.BSMDStatus.TOSEND);
foreach (MessageCore core in coresMarkedForSending)
{
// TODO: Hier sollten jeweils die "neuesten" Nachrichten geladen werden, wenn es mehrere geben sollte!!
List<Message> messages = DBManager.Instance.GetMessagesForCore(core, DBManager.MessageLoad.ALL);
//
// if (core.InitialHIS != Message.NSWProvider.DBH_TEST) continue; // ignore everything else
if (core.InitialHIS == Message.NSWProvider.DUDR)
{
// HIS-NORD: alles auf einmal
sendSucceeded = bsmd.hisnord.Request.Send(messages);
if (!sendSucceeded) core.BSMDStatusInternal = MessageCore.BSMDStatus.FAILURE;
else core.BSMDStatusInternal = MessageCore.BSMDStatus.SENT;
DBManager.Instance.Save(core);
}
else
{
List<Message> toSendMessageList = new List<Message>();
if ((core.IsTransit && core.TransitId.IsNullOrEmpty()) ||
(!core.IsTransit && core.VisitId.IsNullOrEmpty()))
{
foreach (Message message in messages)
{
if ((message.MessageNotificationClass == Message.NotificationClass.VISIT) ||
(message.MessageNotificationClass == Message.NotificationClass.TRANSIT))
{
if ((message.InternalStatus == Message.BSMDStatus.UNDEFINED) ||
(message.InternalStatus == Message.BSMDStatus.PREPARE) ||
(message.InternalStatus == Message.BSMDStatus.TOSEND))
{
_log.Debug("Visit/Transit not found, SENDING VISIT/TRANSIT message");
if (message.HIS == Message.NSWProvider.UNDEFINED)
message.HIS = core.InitialHIS;
if (core.DefaultReportingPartyId.HasValue)
message.ReportingPartyId = core.DefaultReportingPartyId;
toSendMessageList.Add(message);
}
}
}
}
else // eine VISIT/TRANSIT Id ist vorhanden, die Daten können gesendet werden
{
// Änderung Sept 15: versendet werden alle Nachrichten, die *nicht* bestätigt sind
// also auch alles was auf in bearbeitung / Korrektur etc steht
// Einzig bleibt die Frage offen, was mit Nachrichten passiert, die über das UI
// nachträglich geändert werden. Ich denke hier muss in dashface ggf. der Status
// wieder auf PREPARE gesetzt werden
_log.Debug("Visit/Transit found, SENDING DATA messages");
foreach (Message message in messages)
{
// Eine Nachricht mit dem Status "Suspended / Zurückgestellt" wird so lange nicht
// versendet, bis im ENI der Status wieder explizit zurückgesetzt wird (16.4.16)
if (message.InternalStatus == Message.BSMDStatus.SUSPENDED) continue;
// "virtuelle" Messages nicht versenden (DK, EU)
if ((message.MessageNotificationClass == Message.NotificationClass.STO) ||
(message.MessageNotificationClass == Message.NotificationClass.CREWD) ||
(message.MessageNotificationClass == Message.NotificationClass.PASD))
continue;
// Wenn das ein Transit ist, werden nicht erforderliche Meldeklassen übersprungen
if (core.IsTransit)
{
if ((message.MessageNotificationClass == Message.NotificationClass.BKRD) ||
(message.MessageNotificationClass == Message.NotificationClass.PRE72H) ||
(message.MessageNotificationClass == Message.NotificationClass.TIEFD) ||
(message.MessageNotificationClass == Message.NotificationClass.NAME) ||
(message.MessageNotificationClass == Message.NotificationClass.INFO) ||
(message.MessageNotificationClass == Message.NotificationClass.ATA) ||
(message.MessageNotificationClass == Message.NotificationClass.ATD) ||
(message.MessageNotificationClass == Message.NotificationClass.LADG) ||
(message.MessageNotificationClass == Message.NotificationClass.SERV) ||
(message.MessageNotificationClass == Message.NotificationClass.WAS) ||
(message.MessageNotificationClass == Message.NotificationClass.TOWD))
continue;
}
// Visit/Transit Meldeklassen werden nicht erneut übertragen
if ((message.MessageNotificationClass == Message.NotificationClass.VISIT) ||
(message.MessageNotificationClass == Message.NotificationClass.TRANSIT))
continue;
if ((message.InternalStatus != Message.BSMDStatus.CONFIRMED) &&
(message.InternalStatus != Message.BSMDStatus.SENT))
{
// 28.12.2015: Das über "Overview" eingestellte HIS ist immer "führend" (zumindest aktuell zum Testen)
// if (message.HIS == Message.NSWProvider.UNDEFINED)
message.HIS = core.InitialHIS;
if (core.DefaultReportingPartyId.HasValue)
{
message.ReportingPartyId = core.DefaultReportingPartyId; // Referenz umbiegen
if (DBManager.Instance.GetReportingPartyDict().ContainsKey(core.DefaultReportingPartyId.Value)) // geladenes Objekt ersetzen
message.ReportingParty = DBManager.Instance.GetReportingPartyDict()[core.DefaultReportingPartyId.Value];
}
toSendMessageList.Add(message);
}
}
}
foreach (Message message in toSendMessageList)
{
try
{
_log.InfoFormat("Sending {0} message to {1}",
message.MessageNotificationClass.ToString(), message.HIS.ToString());
sendSucceeded = false;
// switch über passendes HIS / Schnittstelle
switch (message.HIS)
{
case Message.NSWProvider.DBH:
case Message.NSWProvider.DBH_TEST:
sendSucceeded = bsmd.dbh.Request.SendMessage(message, (message.HIS == Message.NSWProvider.DBH_TEST));
if (!sendSucceeded)
message.InternalStatus = Message.BSMDStatus.SEND_FAILED;
break;
case Message.NSWProvider.DAKOSY:
case Message.NSWProvider.DAKOSY_TEST:
sendSucceeded = bsmd.dakosy.Request.Send(message, true);
if (!sendSucceeded) message.InternalStatus = Message.BSMDStatus.SEND_FAILED;
break;
default:
_log.WarnFormat("Initial HIS not specified for message {0}", message.Id);
break;
}
if (sendSucceeded)
{
// alte Fehlerliste entfernen (die Antwort kann praktisch noch nicht da sein)
// vor dem Versenden zu entfernen halte ich für doof, wenn das Versenden scheitert
foreach (MessageError messageError in message.ErrorList)
DBManager.Instance.Delete(messageError);
foreach (MessageViolation messageViolation in message.ViolationList)
DBManager.Instance.Delete(messageViolation);
_log.Info("send successful, saving message.");
message.InternalStatus = Message.BSMDStatus.SENT;
message.SentAt = DateTime.Now;
}
DBManager.Instance.Save(message);
}
catch (Exception ex)
{
_log.ErrorFormat("SENDING message {0}: {1}", message.Id.ToString(), ex.Message);
}
}
if (toSendMessageList.Count > 0)
{
core.BSMDStatusInternal = MessageCore.BSMDStatus.SENT;
}
else
{
core.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE;
}
DBManager.Instance.Save(core);
}
}
List<MessageCore> coresMarkedForStatusQuery = DBManager.Instance.GetMessageCoresWithNSWStatusFlag();
foreach (MessageCore core in coresMarkedForStatusQuery)
{
core.QueryNSWStatus = false; // reset flag
Status aStatus = new Status(core);
aStatus.PerformQuery();
}
// Auf erhaltene Visit-Ids prüfen (HIS-NORD)
// TODO
// bsmd.hisnord.Request.ReadAnswers();
DBManager.Instance.Disconnect();
}
else
{
// _log.Fatal("database connection failure, stopping service");
this.EventLog.WriteEntry("NSW Send Service DB connection failure", EventLogEntryType.Warning);
// this.Stop();
}
lock (this._timerlock)
{
this.processRunning = false;
}
}
protected override void OnPause()
{
this._timer.Stop();
}
protected override void OnContinue()
{
this._timer.Start();
}
protected override void OnStop()
{
this._timer.Enabled = false;
this.EventLog.WriteEntry("NSW Send Service stopped.", EventLogEntryType.Information);
_log.Info("NSW Send Service stopped");
}
}
}