228 lines
8.4 KiB
C#
228 lines
8.4 KiB
C#
// Copyright (c) 2020-present schick Informatik
|
|
// Description:
|
|
|
|
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.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Timers;
|
|
using bsmd.email;
|
|
using bsmd.database;
|
|
using log4net;
|
|
|
|
namespace bsmd.POService
|
|
{
|
|
public partial class POService : ServiceBase
|
|
{
|
|
|
|
#region Fields
|
|
|
|
private Timer _timer;
|
|
private readonly ILog _log = LogManager.GetLogger(typeof(POService));
|
|
|
|
#endregion
|
|
|
|
#region Construction
|
|
|
|
public POService()
|
|
{
|
|
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
|
|
InitializeComponent();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Start/Stop Service
|
|
|
|
protected override void OnStart(string[] args)
|
|
{
|
|
this.EventLog.Source = this.ServiceName;
|
|
this.EventLog.Log = "Application";
|
|
this.Init();
|
|
this.EventLog.WriteEntry("NSW PO Number Service started.", EventLogEntryType.Information);
|
|
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
|
string version = fvi.FileVersion;
|
|
_log.InfoFormat("Starting PO Number Service. v.{0} -------------- ", version);
|
|
this.DoOnce();
|
|
}
|
|
|
|
protected override void OnStop()
|
|
{
|
|
this._log.Info("Stopping PO Number Service");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region helper
|
|
|
|
private void Init()
|
|
{
|
|
this._timer = new Timer
|
|
{
|
|
Interval = Properties.Settings.Default.SleepSeconds * 1000
|
|
};
|
|
this._timer.Elapsed += _timer_Elapsed;
|
|
this._timer.Enabled = true;
|
|
}
|
|
|
|
internal void DoOnce()
|
|
{
|
|
this._timer_Elapsed(null, null);
|
|
}
|
|
|
|
internal static bool ParsePO(string inputValue)
|
|
{
|
|
bool result = false;
|
|
|
|
// Betreffzeile parsen. Ein Beispiel:
|
|
// "WG: PO:8204730095 DEWVNTM-ADELINA D-005E-310120";
|
|
// Hier kann man designen: https://regex101.com/
|
|
const string poPattern = @"PO:(\d+) ([A-Z]+)-(.*?)-(.*?)-(\d{6})";
|
|
Regex poRegex = new Regex(poPattern);
|
|
Match aMatch = poRegex.Match(inputValue);
|
|
if (aMatch.Success)
|
|
{
|
|
string poNummer = aMatch.Groups[1].Captures[0].Value;
|
|
string hafen = aMatch.Groups[2].Captures[0].Value;
|
|
string name = aMatch.Groups[3].Captures[0].Value;
|
|
string irgendwas = aMatch.Groups[4].Captures[0].Value;
|
|
string datumString = aMatch.Groups[5].Captures[0].Value;
|
|
result = true;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
|
|
{
|
|
|
|
if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString))
|
|
{
|
|
|
|
try
|
|
{
|
|
string messageId = "";
|
|
string attachmentLocalPath = null;
|
|
string mailSender = "";
|
|
const string receiptSubject = "PO number service INFO";
|
|
string mailSubject = "";
|
|
string body = "";
|
|
|
|
using (BSMDPopClient bsmdPopClient = new BSMDPopClient())
|
|
{
|
|
if (bsmdPopClient.IsConnected)
|
|
{
|
|
// check and download next e-Mail, saving attachment
|
|
while (bsmdPopClient.ReceiveSingleMailText(out messageId, out mailSender, out mailSubject, out body))
|
|
{
|
|
|
|
bool readResult = false;
|
|
string readMessage = "";
|
|
string receiptText = "";
|
|
MessageCore messageCore = null;
|
|
|
|
if (attachmentLocalPath == null)
|
|
{
|
|
receiptText = "incoming E-Mail did not contain an Excel attachment!";
|
|
_log.WarnFormat(receiptText);
|
|
}
|
|
else
|
|
{
|
|
// only a valid sender gets a reply
|
|
bool isValidSender = false;
|
|
foreach (string aValidSender in Properties.Settings.Default.ValidSender)
|
|
{
|
|
if (mailSender.Equals(aValidSender, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
isValidSender = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!isValidSender)
|
|
{
|
|
receiptText = string.Format("ignored e - mail from illegal sender: {0}", mailSender);
|
|
_log.Warn(receiptText);
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
if(!ParsePO(mailSubject))
|
|
{
|
|
|
|
}
|
|
|
|
|
|
// aus (ungefährem) Datum, Name und Hafen den MessageCore suchen
|
|
|
|
|
|
// PO-Nummer und Schiffs/Anlaufklassifizierung eintragen
|
|
|
|
|
|
}
|
|
catch (Exception someException)
|
|
{
|
|
receiptText = string.Format("Error processing po mail: {0}", someException.Message);
|
|
_log.Error(receiptText);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (receiptText.Length > 0)
|
|
{
|
|
_log.Debug("sending system info email");
|
|
BSMDMail.SendSystemInfo(receiptSubject, receiptText, mailSender);
|
|
}
|
|
|
|
// remove e-Mail
|
|
_log.InfoFormat("deleting mail with messageId {0}", messageId);
|
|
_log.InfoFormat("mail delete {0}", bsmdPopClient.DeleteMessageByMessageId(messageId) ? "successful" : "failed");
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Phase II - Excel Sheets auf Anforderung erzeugen
|
|
|
|
List<MessageCore> excelMessageCoreList = DBManager.Instance.GetMessageCoresForExcelCreate();
|
|
if (excelMessageCoreList.Count > 0)
|
|
_log.InfoFormat("{0} excel sheets to create from database", excelMessageCoreList.Count);
|
|
foreach (MessageCore excelCore in excelMessageCoreList)
|
|
{
|
|
// load messages
|
|
List<Message> messages = DBManager.Instance.GetMessagesForCore(excelCore, DBManager.MessageLoad.ALL);
|
|
|
|
// template
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
DBManager.Instance.Disconnect();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.ErrorFormat("Exception occurred: {0}", ex.ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_log.Error("DB Connection failure");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|