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); } public void Init(string[] args) { this._timer = new Timer(); this._timer.Interval = 5000; // 5 sec this._timer.Elapsed += _timer_Elapsed; this._timer.Enabled = true; } protected override void OnStop() { } 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 = "f2747e67-0043-43e4-b6a9-1ed3bf9265e4"; body.apiIdentifier.clientGuid = "76994eae-48de-44d4-a86d-6508e2e03dcf"; body.ffFolderTemplateTypeTag = "NSWAP"; body.timeFrameRequestFilter = new TimeFrameRequestFilter(); body.timeFrameRequestFilter.startDate = DateTime.Now; // TODO body.timeFrameRequestFilter.startDateSpecified = 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 = Guid.NewGuid().ToString(); // TODO formBody.apiIdentifier.clientGuid = Guid.NewGuid().ToString(); // TODO formBody.formGuid = dataSet.formGuid; GetFormDataResponseData formResponse = client.GetFormData(formBody); if (formResponse.success) { // abgefragtes Formular in die DB speichern } 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; } } } }