205 lines
7.9 KiB
C#
205 lines
7.9 KiB
C#
// Copyright (c) 2020-present schick Informatik
|
|
// Description: Manager zum Senden/Empfangen von Daten mit dbh
|
|
|
|
using log4net;
|
|
using System;
|
|
using System.IO;
|
|
|
|
using bsmd.database;
|
|
using System.Linq;
|
|
|
|
namespace bsmd.dbh
|
|
{
|
|
public static class MessageController
|
|
{
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(MessageController));
|
|
private static int? _fileSequenceCounter = null;
|
|
|
|
#region send single message
|
|
|
|
public static bool SendMessage(MessageCore core, Message message)
|
|
{
|
|
bool result = true;
|
|
try
|
|
{
|
|
if (message == null) return false;
|
|
|
|
if (message.ReportingParty == null)
|
|
{
|
|
_log.ErrorFormat("Reporting party not set on message {0}", message.Id);
|
|
return false;
|
|
}
|
|
|
|
if ((message.MessageNotificationClass != Message.NotificationClass.VISIT) &&
|
|
(message.MessageNotificationClass != Message.NotificationClass.TRANSIT) &&
|
|
(message.Elements.Count == 0))
|
|
{
|
|
_log.ErrorFormat("trying to send message {0} class {1} but there are no depending record elements",
|
|
message.Id, message.MessageNotificationClass);
|
|
return false;
|
|
}
|
|
|
|
if (!_fileSequenceCounter.HasValue)
|
|
_fileSequenceCounter = DBManager.Instance.GetMessageFileMaxNum();
|
|
_fileSequenceCounter += 1;
|
|
message.FileSequenceNumber = _fileSequenceCounter;
|
|
|
|
string messageFile = RequestUtil.CreateMessageFile(core, message);
|
|
|
|
if (messageFile != null)
|
|
{
|
|
string onlyFileName = Path.GetFileName(messageFile);
|
|
string moveTarget = Path.Combine(Properties.Settings.Default.OutgoingFolder, onlyFileName);
|
|
// move file to output directory
|
|
File.Move(messageFile, moveTarget);
|
|
}
|
|
else
|
|
{
|
|
result = false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex.ToString());
|
|
result = false;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region send cancel core messaage
|
|
|
|
public static bool SendCancelCore(MessageCore core)
|
|
{
|
|
bool result = true;
|
|
|
|
Message cancelMessage = null;
|
|
foreach(Message aMessage in DBManager.Instance.GetMessagesForCore(core, DBManager.MessageLoad.ALL))
|
|
{
|
|
if(core.IsTransit && (aMessage.MessageNotificationClass == Message.NotificationClass.TRANSIT))
|
|
{
|
|
cancelMessage = aMessage; break;
|
|
}
|
|
if(!core.IsTransit && (aMessage.MessageNotificationClass == Message.NotificationClass.VISIT))
|
|
{
|
|
cancelMessage = aMessage; break;
|
|
}
|
|
}
|
|
|
|
string messageFile = RequestUtil.CreateMessageFile(core, cancelMessage);
|
|
|
|
if (messageFile != null)
|
|
{
|
|
|
|
if (!_fileSequenceCounter.HasValue)
|
|
_fileSequenceCounter = DBManager.Instance.GetMessageFileMaxNum();
|
|
_fileSequenceCounter += 1;
|
|
cancelMessage.FileSequenceNumber = _fileSequenceCounter;
|
|
|
|
string onlyFileName = Path.GetFileName(messageFile);
|
|
string moveTarget = Path.Combine(Properties.Settings.Default.OutgoingFolder, onlyFileName);
|
|
// move file to output directory
|
|
File.Move(messageFile, moveTarget);
|
|
}
|
|
else
|
|
{
|
|
result = false;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region send and receive files (SFTP)
|
|
|
|
public static void SendAndReceive()
|
|
{
|
|
// sent unsent messages in output folder
|
|
bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteIncomingFolder, Properties.Settings.Default.OutgoingFolder, dakosy.SFtp.Direction.OUTGOING, Properties.Settings.Default.SFTPSessionName);
|
|
// move files from output folder to archive folder
|
|
foreach(string sentFile in Directory.GetFiles(Properties.Settings.Default.OutgoingFolder))
|
|
{
|
|
_log.InfoFormat("sent {0}", sentFile);
|
|
string onlyFileName = Path.GetFileName(sentFile);
|
|
string moveTarget = Path.Combine(Properties.Settings.Default.OutgoingArchiveFolder, onlyFileName);
|
|
File.Copy(sentFile, moveTarget, true);
|
|
File.Delete(sentFile);
|
|
}
|
|
|
|
// receive files from remote host
|
|
// SFTP verbindung öffnen und alle Dateien herunterladen
|
|
bsmd.dakosy.SFtp.TransmitAll(Properties.Settings.Default.RemoteOutgoingFolder, Properties.Settings.Default.IncomingFolder, dakosy.SFtp.Direction.INCOMING, Properties.Settings.Default.SFTPSessionName);
|
|
|
|
foreach (string inputFile in Directory.GetFiles(Properties.Settings.Default.IncomingFolder))
|
|
{
|
|
string justFilename = Path.GetFileName(inputFile);
|
|
// lokale Dateien verarbeiten
|
|
if (!ResponseUtil.Read(inputFile))
|
|
{
|
|
_log.ErrorFormat("Error reading input file {0}", justFilename);
|
|
string errorPath = Path.Combine(Properties.Settings.Default.IncomingErrorFolder, justFilename);
|
|
File.Copy(inputFile, errorPath, true);
|
|
File.Delete(inputFile);
|
|
}
|
|
else
|
|
{
|
|
_log.InfoFormat("Incoming file {0} processed", justFilename);
|
|
string archivePath = Path.Combine(Properties.Settings.Default.IncomingArchiveFolder, justFilename);
|
|
File.Copy(inputFile, archivePath, true);
|
|
File.Delete(inputFile);
|
|
}
|
|
// remote Dateien löschen
|
|
bsmd.dakosy.SFtp.RemoveProcessedFile(Properties.Settings.Default.RemoteOutgoingFolder, Path.GetFileName(inputFile), Properties.Settings.Default.SFTPSessionName);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Purge old files
|
|
|
|
public static void PurgeOldFiles(int maxAgeDays)
|
|
{
|
|
try
|
|
{
|
|
int cnt = 0;
|
|
DirectoryInfo info = new DirectoryInfo(Properties.Settings.Default.IncomingArchiveFolder);
|
|
FileInfo[] files = info.GetFiles();
|
|
foreach (FileInfo file in files)
|
|
{
|
|
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
|
|
{
|
|
_log.Debug($"deleting {file.Name}");
|
|
file.Delete();
|
|
cnt++;
|
|
}
|
|
}
|
|
_log.Info($"deleted {cnt} files from {Properties.Settings.Default.IncomingArchiveFolder}");
|
|
|
|
cnt = 0;
|
|
info = new DirectoryInfo(Properties.Settings.Default.OutgoingArchiveFolder);
|
|
files = info.GetFiles();
|
|
foreach (FileInfo file in files)
|
|
{
|
|
if (file.CreationTime < DateTime.Now.AddDays(-maxAgeDays))
|
|
{
|
|
_log.Debug($"deleting {file.Name}");
|
|
file.Delete();
|
|
cnt++;
|
|
}
|
|
}
|
|
_log.Info($"deleted {cnt} files from {Properties.Settings.Default.OutgoingArchiveFolder}");
|
|
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_log.ErrorFormat("Error deleting old files: {0}", ex.Message);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|