git_bsmd/bsmd.herberg.FormService/FormService.cs

219 lines
10 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Timers;
using log4net;
using bsmd.database;
using bsmd.herberg.FormService.WebReference;
// temporary
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace bsmd.herberg.FormService
{
public partial class FormService : ServiceBase
{
private Timer _timer;
private object _timerlock = new object();
private bool processRunning = false;
private ILog _log = LogManager.GetLogger(typeof(FormService));
public FormService()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
this.Init(args);
this.EventLog.WriteEntry("FormService started.", EventLogEntryType.Information);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
this._log.InfoFormat("Starting EU-NOAD FormService. v.{0}", version);
this.DoOnce();
}
protected override void OnStop()
{
this._log.Info("Stopping EU-NOAD FormService.");
}
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.IgnoreSSLCertificate();
this._timer_Elapsed(null, null);
}
private void IgnoreSSLCertificate()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this._timerlock)
{
if (this.processRunning) return;
else this.processRunning = true;
}
if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString))
{
try
{
WSAPIFormData client = new WSAPIFormData();
client.Url = Properties.Settings.Default.ServerAddress;
// client.Endpoint.Name = "Fleettracker Forms"; // TODO
// Verbindung zu Herberg's Service aufnehmen und Formulare abfragen
GetFormDataInfoListRequestData body = new GetFormDataInfoListRequestData();
body.apiIdentifier = new APIIdentifier();
// von Jan am 1.4.15: (April, April..)
body.apiIdentifier.companyGuid = Properties.Settings.Default.CompanyGuid.ToString();
body.apiIdentifier.clientGuid = Properties.Settings.Default.ClientGuid.ToString();
body.ffFolderTemplateTypeTag = "NSWAD";
body.timeFrameRequestFilter = new TimeFrameRequestFilter();
body.timeFrameRequestFilter.startDate = DateTime.Now -
new TimeSpan(Properties.Settings.Default.IntervalLastDay,
Properties.Settings.Default.IntervalLastHour,
Properties.Settings.Default.IntervalLastMinute,
0);
body.timeFrameRequestFilter.startDateSpecified = true;
body.timeFrameRequestFilter.endDate = DateTime.Now + new TimeSpan(1, 0, 0, 0);
body.timeFrameRequestFilter.endDateSpecified = true;
// Liste der verfügbaren Formulare abholen
GetFormDataInfoListResponseData listReponse = client.GetFormDataInfoList(body);
if (listReponse.success)
{
if (listReponse.formDataInfoDatasets != null)
{
for (int i = 0; i < listReponse.formDataInfoDatasets.Length; i++)
{
FormDataInfoDataset dataSet = listReponse.formDataInfoDatasets[i];
/* prüfen, ob ich das Formular schon habe? -> RS Jan
> Ein Formular, identifiziert durch formGuid würde dann immer einem
> Schiffsanlauf entsprechen? d.h. der nächste Hafenanlauf hätte dann
> dieselbe formTemplateGuid, aber unterschiedliche formGuids?
Das ist korrekt. Allerdings kann sich die FormTemplate-GUID mit der Zeit
ändern, wenn wir z.B. das Template updaten. Theoretisch kann das auch
zwischen Initial und Update passieren.
*/
// Formular abholen
GetFormDataRequestData formBody = new GetFormDataRequestData();
formBody.apiIdentifier = new APIIdentifier();
formBody.apiIdentifier.companyGuid = Properties.Settings.Default.CompanyGuid.ToString();
formBody.apiIdentifier.clientGuid = Properties.Settings.Default.ClientGuid.ToString();
formBody.formGuid = dataSet.formGuid;
bool successfulGet = true;
GetFormDataResponseData formResponse = null;
try
{
formResponse = client.GetFormData(formBody);
}
catch (Exception ex)
{
_log.ErrorFormat("Exception reading body of form {0}: {1}", dataSet.formGuid, ex.ToString());
successfulGet = false;
}
if (successfulGet && formResponse.success)
{
// abgefragtes Formular in die DB speichern
MessageCore aMessageCore = DBManager.Instance.GetHerbergFormMessage(new Guid(formBody.formGuid));
if (aMessageCore == null)
{
_log.InfoFormat("Creating new MessageCore for IMO {0}", formResponse.imoNumber);
// neuen Anlaufeintrag (=MessageCore) erstellen
aMessageCore = new MessageCore();
aMessageCore.HerbergFormGuid = new Guid(formBody.formGuid);
// aMessageCore.HerbergFormTemplateGuid = formResponse.
aMessageCore.IMO = formResponse.imoNumber.ToString();
aMessageCore.ReportStatus = MessageCore.ReportStatusEnum.COMPLETE;
aMessageCore.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE;
if (aMessageCore.IMO.Length > 7)
{
_log.WarnFormat("IMO {0} is longer than 7 chars, truncating!", aMessageCore.IMO);
aMessageCore.IMO = aMessageCore.IMO.Substring(0, 7);
}
DBManager.Instance.Save(aMessageCore);
}
else
{
// TODO: Status von Core prüfen (wurde schon von BSMD verändert?)
_log.InfoFormat("Updating form data for IMO {0}", formResponse.imoNumber);
}
Util.UpdateFormCore(aMessageCore, formResponse);
}
else
{
_log.ErrorFormat("Request of form {0} failed", dataSet.formGuid);
}
}
}
else
{
_log.InfoFormat("nothing: {0} - {1}",
body.timeFrameRequestFilter.startDate,
body.timeFrameRequestFilter.endDate);
}
}
else
{
_log.Error("Request of form list failed");
}
DBManager.Instance.Disconnect();
}
catch (Exception ex)
{
_log.ErrorFormat("Exception occurred: {0}", ex.ToString());
}
}
else
{
this.EventLog.WriteEntry("FormService DB connection failure", EventLogEntryType.Warning);
}
lock (this._timerlock)
{
this.processRunning = false;
}
}
}
}