using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Timers; using MigraDoc; using MigraDoc.DocumentObjectModel; using log4net; using bsmd.database; namespace bsmd.ReportGenerator { public partial class ReportService : ServiceBase { private Timer _timer; private object _timerlock = new object(); private bool processRunning = false; private ILog _log = LogManager.GetLogger(typeof(ReportService)); public ReportService() { 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("Report Service started.", EventLogEntryType.Information); this.DoOnce(); } private 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; } private 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 { // load all messages with report flag set List reportCores = DBManager.Instance.GetMessageCoresByStatus(MessageCore.BSMDStatus.CREATE_RECEIVE_RECEIPT); // create report documents for each of the messages foreach (MessageCore reportCore in reportCores) { List messages = DBManager.Instance.GetMessagesForCore(reportCore); Document migraDocument = BSMDDocument.CreateDocument( string.Format("NSW Eingangsdatenübersicht für IMO {0}, ETA {1}", reportCore.IMO, reportCore.ETA), "NSW Meldung", Properties.Settings.Default.ReportAuthor); // print header area (with message core data) // print messages in subsequent tables foreach (Message message in messages) { BSMDDocument.AddNSWMessageParagraph(migraDocument, message); } // prepare and send E-Mail with generated attachment string fullPath = string.Format("{0}\\{1}.pdf", Properties.Settings.Default.OutputDirectory, reportCore.Id); BSMDDocument.RenderDocument(migraDocument, fullPath); _log.InfoFormat("Document created for MessageCoreId {0}, IMO {1}", reportCore.Id, reportCore.IMO); BSMDMail.SendNSWReportAsAttachment(reportCore, fullPath); // reset status on core message (get ready to prepare) reportCore.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE; DBManager.Instance.Save(reportCore); } DBManager.Instance.Disconnect(); } catch (Exception ex) { _log.ErrorFormat("Exception occurred: {0}", ex.ToString()); } } else { this.EventLog.WriteEntry("ReportService stopped: DB connection failed", EventLogEntryType.Error); this.Stop(); } lock (this._timerlock) { this.processRunning = false; } } protected override void OnStop() { } internal void DoOnce() { this._timer_Elapsed(null, null); } } }