git_bsmd/nsw/Source/bsmd.herberg.FormService/FormService.cs
2015-04-23 06:02:43 +00:00

171 lines
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using log4net;
using bsmd.database;
using bsmd.herberg.FormService.WebReference;
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.EventLog.BeginInit();
if (!EventLog.SourceExists(this.EventLog.Source, this.EventLog.Log))
EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);
this.EventLog.EndInit();
this.Init(args);
this.EventLog.WriteEntry("NSW Send Service started.", EventLogEntryType.Information);
}
protected override void OnStop()
{
}
public void Init(string[] args)
{
this._timer = new Timer();
this._timer.Interval = 5000; // 5 sec
this._timer.Elapsed += _timer_Elapsed;
this._timer.Enabled = 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(30, 0, 0, 0); // last 30 days
body.timeFrameRequestFilter.startDateSpecified = true;
body.timeFrameRequestFilter.endDate = DateTime.Now + new TimeSpan(30, 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
// 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;
GetFormDataResponseData formResponse = client.GetFormData(formBody);
if (formResponse.success)
{
// abgefragtes Formular in die DB speichern
MessageCore aMessageCore = DBManager.Instance.GetHerbergFormMessage(new Guid(formBody.formGuid));
if (aMessageCore == null)
{
// neuen Anlaufeintrag (=MessageCore) erstellen
aMessageCore = new MessageCore();
aMessageCore.HerbergFormGuid = new Guid(formBody.formGuid);
// aMessageCore.HerbergFormTemplateGuid = formResponse.
aMessageCore.IMO = formResponse.imoNumber.ToString();
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);
}
// Änderungen im Formular übertragen und speichern
Util.UpdateFormCore(aMessageCore);
}
else
{
_log.ErrorFormat("Request of form {0} failed", dataSet.formGuid);
}
}
}
else
{
_log.Info("formDataInfoDatasets null");
}
}
else
{
_log.Error("Request of form list failed");
}
}
catch (Exception ex)
{
_log.ErrorFormat("Exception occurred: {0}", ex.ToString());
}
}
else
{
this.EventLog.WriteEntry("FormService stopped: DB connection failed", EventLogEntryType.Error);
this.Stop();
}
lock (this._timerlock)
{
this.processRunning = false;
}
}
}
}