using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.ServiceProcess; using System.Timers; using log4net; using MigraDoc.DocumentObjectModel; using bsmd.database; using bsmd.email; 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); _log.Info("Report Service started."); 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.GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum.COMPLETE); // create report documents for each of the messages foreach (MessageCore reportCore in reportCores) { List messages = DBManager.Instance.GetMessagesForCore(reportCore); Dictionary coverInfos = new Dictionary(); // Schiffsname aus der STAT meldung fischen foreach (Message msg in messages) { if (msg.MessageNotificationClass == Message.NotificationClass.STAT) { if (msg.Elements.Count > 0) { STAT stat = msg.Elements[0] as STAT; if (stat != null) { coverInfos.Add("Ship", stat.ShipName); } } } } coverInfos.Add("IMO", reportCore.IMO); DateTime eta = reportCore.ETA ?? (reportCore.ETAKielCanal ?? new DateTime(0)); coverInfos.Add("ETA", eta.ToString()); coverInfos.Add("Port", reportCore.Portname); 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, coverInfos); // 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); string subject = string.Format("NSW message report for {0}", reportCore.IMO); BSMDMail.SendNSWReportAsAttachment(subject, fullPath); // reset report status reportCore.ReportStatus = MessageCore.ReportStatusEnum.NONE; 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() { _log.Info("Report Service stopping."); } internal void DoOnce() { this._timer_Elapsed(null, null); } } }