// 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) { 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; } 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, 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.Move(sentFile, moveTarget); } // 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.Move(inputFile, errorPath); } else { _log.InfoFormat("Incoming file {0} processed", justFilename); string archivePath = Path.Combine(Properties.Settings.Default.IncomingArchiveFolder, justFilename); File.Move(inputFile, archivePath); } // remote Dateien löschen bsmd.dakosy.SFtp.RemoveProcessedFile(Properties.Settings.Default.RemoteOutgoingFolder, Path.GetFileName(inputFile), Properties.Settings.Default.SFTPSessionName); } } } }