git_bsmd/bsmd.dbh/MessageController.cs
Daniel Schick 693c89e599 FILE NUM SEQUENCE
DBH will eine fortlaufende Nummer bei der Abgabe von Dateien über SFTP
Diese wird über einen zentralen Zähler vergeben und in MessageHeader gespeichert
2022-11-14 17:14:26 +01:00

107 lines
4.0 KiB
C#

// Copyright (c) 2020-present schick Informatik
// Description: Manager zum Senden/Empfangen von Daten mit dbh
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using bsmd.database;
using System.IO;
namespace bsmd.dbh
{
public static class MessageController
{
private static readonly ILog _log = LogManager.GetLogger(typeof(MessageController));
private static int? _fileSequenceCounter = null;
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)
{
// move file to output directory
File.Move(messageFile, Properties.Settings.Default.OutgoingFolder);
}
else
{
result = false;
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
result = false;
}
return result;
}
public static bool SendCancelCore(MessageCore core)
{
bool result = true;
return result;
}
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);
// move files from output folder to archive folder
foreach(string sentFile in Directory.GetFiles(Properties.Settings.Default.OutgoingFolder))
{
_log.InfoFormat("sent {0}", sentFile);
File.Move(sentFile, Properties.Settings.Default.OutgoingArchiveFolder);
}
// 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);
foreach (string inputFile in Directory.GetFiles(Properties.Settings.Default.IncomingFolder))
{
// lokale Dateien verarbeiten
if (!ResponseUtil.Read(inputFile))
{
_log.ErrorFormat("Error reading input file {0}", inputFile);
}
else
{
File.Move(inputFile, Properties.Settings.Default.IncomingArchiveFolder);
}
// remote Dateien löschen
bsmd.dakosy.SFtp.RemoveProcessedFile(Properties.Settings.Default.RemoteOutgoingFolder, Path.GetFileName(inputFile));
}
}
}
}