415 lines
13 KiB
C#
415 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DakotaSender
|
|
{
|
|
class Program
|
|
{
|
|
const int RC_DAKOTA_ALREADY_LOADED = 18;
|
|
|
|
static readonly string TEST_TENANT = "7375324044";
|
|
|
|
static int[] AcceptStatusCodes = new int[] { 0 };
|
|
static int[] AcceptSendCodes = new int[] { 0 };
|
|
|
|
static bool time_in_line = false;
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
FileStream filestream = new FileStream(GetLogPath(), FileMode.Append);
|
|
var streamwriter = new StreamWriter(filestream);
|
|
streamwriter.AutoFlush = true;
|
|
|
|
Console.SetOut(streamwriter);
|
|
Console.SetError(streamwriter);
|
|
|
|
try
|
|
{
|
|
PrintTitle();
|
|
|
|
CheckDakotaStatus();
|
|
|
|
ProcessFiles();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
PrintLine(e.ToString());
|
|
}
|
|
|
|
PrintEnd();
|
|
}
|
|
|
|
static void PrintTitle()
|
|
{
|
|
PrintWithTime("\n", false);
|
|
PrintLine("-------------------");
|
|
PrintLine("Dakota Sender Modul");
|
|
PrintLine("-------------------");
|
|
}
|
|
static void PrintEnd()
|
|
{
|
|
PrintLine("Ende");
|
|
PrintLine("-------------------");
|
|
}
|
|
|
|
static void PrintLine() => PrintLine(null);
|
|
static void PrintLine(string line)
|
|
{
|
|
PrintWithTime(line + "\n", !time_in_line);
|
|
|
|
time_in_line = false;
|
|
}
|
|
static void Print(string line)
|
|
{
|
|
PrintWithTime(line, !time_in_line);
|
|
|
|
time_in_line = true;
|
|
}
|
|
static void PrintWithTime(string line, bool showTime)
|
|
{
|
|
if (showTime)
|
|
Console.Write($"[{DateTime.Now:dd.MM.yy HH:mm:ss.fff}] {line}");
|
|
else
|
|
Console.Write(line);
|
|
}
|
|
static void PrintFileInfo(DakotaSystemFile file)
|
|
{
|
|
PrintLine($"Verarbeite Datei: {file.SourceNutzdatendatei.Name}");
|
|
PrintLine($"- Tenant: {file.Tenant}");
|
|
PrintLine($"- Protokoll Oid: {file.ProtokollOid}");
|
|
PrintLine($"- Datenannahmestelle: {file.Datenannahmestelle}");
|
|
}
|
|
|
|
static void CheckDakotaStatus()
|
|
{
|
|
PrintLine("Überprüfe Dakota Status");
|
|
|
|
var code = ExecuteDakota("-s");
|
|
|
|
if (AcceptStatusCodes.Contains(code))
|
|
{
|
|
PrintLine("Ok");
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Statuscode {code} wird nicht akzeptiert");
|
|
}
|
|
}
|
|
static void ProcessFiles()
|
|
{
|
|
PrintLine("Scanne Dakota Dateien");
|
|
|
|
var toCheck = ScanQueueFolder();
|
|
|
|
if (toCheck is null)
|
|
{
|
|
PrintLine("Keine Dakota Dateien gefunden.");
|
|
return;
|
|
}
|
|
|
|
var testCheck = toCheck.TryGetValue(TEST_TENANT, out var files);
|
|
|
|
if (!testCheck)
|
|
{
|
|
PrintLine($"Keine Dakota Dateien für {TEST_TENANT} gefunden.");
|
|
return;
|
|
}
|
|
|
|
foreach (var file in files)
|
|
{
|
|
TryProcessFile(file);
|
|
|
|
CheckDakotaStatus();
|
|
}
|
|
}
|
|
|
|
static Dictionary<string, List<DakotaSystemFile>> ScanQueueFolder()
|
|
{
|
|
var queue = GetQueueFolder();
|
|
|
|
var files = queue.GetFiles("*.auf", SearchOption.TopDirectoryOnly);
|
|
|
|
var toCheck = new Dictionary<string, List<DakotaSystemFile>>();
|
|
foreach (var auft in files)
|
|
{
|
|
var nutz_fullname = auft.FullName.Substring(0, auft.FullName.Length - 4);
|
|
var nutz = new FileInfo(nutz_fullname);
|
|
|
|
if (!nutz.Exists)
|
|
{
|
|
PrintLine($"{auft.FullName} wird ignoriert. {nutz.FullName} nicht vorhanden");
|
|
continue;
|
|
}
|
|
|
|
PrintLine($"{auft.FullName} gefunden.");
|
|
|
|
var split = nutz.Name.Split('_');
|
|
var tenant = split[0];
|
|
var p_oid = long.Parse(split[1]);
|
|
var datenannahmestelle = split[2];
|
|
var transfername = split[3];
|
|
|
|
if (tenant != TEST_TENANT && tenant != "demo")
|
|
{
|
|
PrintLine($"{auft.FullName} wird ignoriert. Tenant {tenant} nicht gültig.");
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
tenant = TEST_TENANT;
|
|
}
|
|
|
|
var file = new DakotaSystemFile()
|
|
{
|
|
SourceAuftragsdatei = auft,
|
|
SourceNutzdatendatei = nutz,
|
|
Tenant = tenant,
|
|
ProtokollOid = p_oid,
|
|
Datenannahmestelle = datenannahmestelle,
|
|
Transfername = transfername,
|
|
};
|
|
|
|
if (!toCheck.ContainsKey(tenant))
|
|
toCheck.Add(tenant, new List<DakotaSystemFile>());
|
|
|
|
toCheck[tenant].Add(file);
|
|
}
|
|
|
|
return toCheck.Any() ? toCheck : null;
|
|
}
|
|
|
|
static void TryProcessFile(DakotaSystemFile file)
|
|
{
|
|
string error;
|
|
bool copied = false;
|
|
try
|
|
{
|
|
error = ProcessFile(file, out copied);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
error = e.ToString();
|
|
}
|
|
|
|
if (error == string.Empty)
|
|
{
|
|
// Dakota läuft bereits.
|
|
// Nächster Run
|
|
}
|
|
else if (error is string)
|
|
{
|
|
PrintLine(error);
|
|
MoveQueueToFailed(file);
|
|
|
|
if (copied)
|
|
DeleteDakotaFile(file);
|
|
|
|
CreateResultFehlerXmlFile(file, error);
|
|
}
|
|
else
|
|
{
|
|
CreateResultSuccessXmlFile(file);
|
|
}
|
|
}
|
|
static string ProcessFile(DakotaSystemFile file, out bool copied)
|
|
{
|
|
copied = false;
|
|
|
|
PrintFileInfo(file);
|
|
|
|
if (!IsDakotaFolderDatenannahmestelleValid(file.Datenannahmestelle))
|
|
return "Datenannahmestelle unbekannt.";
|
|
|
|
CopyFileToDestination(file);
|
|
copied = true;
|
|
|
|
Print("Sende via Dakota. ");
|
|
|
|
var code = ExecuteDakota("-x");
|
|
|
|
if (code == RC_DAKOTA_ALREADY_LOADED)
|
|
{
|
|
PrintLine($"Verarbeitung fehlgeschlagen. Dakota läuft bereits.");
|
|
|
|
PrintLine("Ignoriere Verarbeitung aktuell. Versuche im nächsten Programm Aufruf erneut.");
|
|
|
|
DeleteDakotaFile(file);
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
if (!AcceptSendCodes.Contains(code))
|
|
return $"Verarbeitung fehlgeschlagen. Statuscode {code} wird nicht akzeptiert.";
|
|
|
|
PrintLine($"Ok.");
|
|
DeleteQueueFile(file);
|
|
return null;
|
|
}
|
|
|
|
static void CreateResultSuccessXmlFile(DakotaSystemFile file)
|
|
=> CreateResultXmlFile(file, "Dakota Erfolg", "", 0);
|
|
static void CreateResultFehlerXmlFile(DakotaSystemFile file, string error)
|
|
=> CreateResultXmlFile(file, "Dakota Fehler", error, 1);
|
|
static void CreateResultXmlFile(DakotaSystemFile file, string title, string nachricht, int fehler_level)
|
|
{
|
|
Print($"Speichere {title} als Xml. ");
|
|
|
|
var result = new ResultXml();
|
|
|
|
result.Datum = DateTime.Now;
|
|
result.Titel = title;
|
|
result.Nachricht = nachricht;
|
|
result.Level = fehler_level;
|
|
result.Oid = file.ProtokollOid;
|
|
|
|
var folder = GetResultFolderTenant(file.Tenant).FullName;
|
|
var path = Path.Combine(folder, $"P{file.ProtokollOid}.xml");
|
|
|
|
Print($"Path: {path}. ");
|
|
|
|
XmlFileConverter.Save(result, path);
|
|
|
|
PrintLine("Ok.");
|
|
}
|
|
|
|
static void MoveQueueToFailed(DakotaSystemFile file)
|
|
{
|
|
CopyFileToFailed(file);
|
|
|
|
DeleteQueueFile(file);
|
|
}
|
|
|
|
static void CopyFileToFailed(DakotaSystemFile file)
|
|
{
|
|
var failed_folder = GetFailedFolder();
|
|
|
|
CopyFileTo(file.SourceAuftragsdatei, failed_folder.FullName);
|
|
CopyFileTo(file.SourceNutzdatendatei, failed_folder.FullName);
|
|
}
|
|
static void CopyFileToDestination(DakotaSystemFile file)
|
|
{
|
|
var folder = GetDakotaFolderDatenannahmestelle(file.Datenannahmestelle);
|
|
|
|
CopyFileTo(file.SourceAuftragsdatei, folder.FullName, file.TransfernameAuf);
|
|
CopyFileTo(file.SourceNutzdatendatei, folder.FullName, file.Transfername);
|
|
}
|
|
static void CopyFileTo(FileSystemInfo file, string destination_folder, string file_name = null)
|
|
{
|
|
if (file_name is null)
|
|
file_name = file.Name;
|
|
|
|
CopyFile(file.FullName, Path.Combine(destination_folder, file_name));
|
|
}
|
|
static void CopyFile(string source, string destination)
|
|
{
|
|
Print($"Kopiere \"{source}\" nach \"{destination}\". ");
|
|
|
|
File.Copy(source, destination);
|
|
|
|
PrintLine("Ok.");
|
|
}
|
|
|
|
static void DeleteQueueFile(DakotaSystemFile file)
|
|
{
|
|
DeleteFile(file.SourceAuftragsdatei.FullName);
|
|
DeleteFile(file.SourceNutzdatendatei.FullName);
|
|
}
|
|
static void DeleteDakotaFile(DakotaSystemFile file)
|
|
{
|
|
var folder = GetDakotaFolderDatenannahmestelle(file.Datenannahmestelle).FullName;
|
|
|
|
DeleteFile(Path.Combine(folder, file.TransfernameAuf));
|
|
DeleteFile(Path.Combine(folder, file.Transfername));
|
|
}
|
|
static void DeleteFile(string file)
|
|
{
|
|
Print($"Lösche {file}. ");
|
|
|
|
File.Delete(file);
|
|
|
|
PrintLine("Ok.");
|
|
}
|
|
|
|
static int ExecuteDakota(string arg)
|
|
{
|
|
var fileinfo = GetDakotaExe();
|
|
|
|
PrintLine($"{fileinfo.Name} {arg} wird ausgeführt");
|
|
|
|
var process = Process.Start(GetDakotaExe().FullName, arg);
|
|
|
|
var success = process.WaitForExit(GetProcessWaitForExitMs());
|
|
|
|
if (success)
|
|
{
|
|
var exitcode = process.ExitCode;
|
|
|
|
PrintLine($"Returncode {exitcode}");
|
|
|
|
return exitcode;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Timeout");
|
|
}
|
|
}
|
|
|
|
static bool IsDakotaFolderDatenannahmestelleValid(string datenannahmestelle)
|
|
{
|
|
try
|
|
{
|
|
_ = GetDakotaFolderDatenannahmestelle(datenannahmestelle);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
static DirectoryInfo GetDakotaFolderDatenannahmestelle(string datenannahmestelle)
|
|
{
|
|
var folder = new DirectoryInfo(Path.Combine(GetDakotaFolder().FullName, "TP5Daten", datenannahmestelle));
|
|
|
|
if (!folder.Exists)
|
|
folder = new DirectoryInfo(Path.Combine(GetDakotaFolder().FullName, "TP5Daten", datenannahmestelle.Substring(0, datenannahmestelle.Length - 1)));
|
|
|
|
if (!folder.Exists)
|
|
throw new Exception($"Datenannahmestelle {datenannahmestelle} konnte nicht gefunden werden.");
|
|
|
|
return folder;
|
|
}
|
|
static DirectoryInfo GetResultFolderTenant(string tenant)
|
|
{
|
|
var dirinfo = new DirectoryInfo(Path.Combine(GetResultFolder().FullName, tenant));
|
|
|
|
if (!dirinfo.Exists)
|
|
dirinfo.Create();
|
|
|
|
return dirinfo;
|
|
}
|
|
|
|
static string GetLogPath()
|
|
=> ConfigurationManager.AppSettings.Get("LogPath") ?? @"C:\GkvAbrechnung\Sender\log.txt";
|
|
static int GetProcessWaitForExitMs()
|
|
=> int.Parse(ConfigurationManager.AppSettings.Get("ProcessWaitForExitMs") ?? "10000");
|
|
static FileInfo GetDakotaExe()
|
|
=> new FileInfo(ConfigurationManager.AppSettings.Get("DakotaExePath") ?? @"C:\Program Files (x86)\ITSG\dakotale\dakota30.exe");
|
|
static DirectoryInfo GetDakotaFolder()
|
|
=> new DirectoryInfo(ConfigurationManager.AppSettings.Get("DakotaFolder") ?? @"C:\dakotale\");
|
|
static DirectoryInfo GetQueueFolder()
|
|
=> new DirectoryInfo(ConfigurationManager.AppSettings.Get("GkvAbrechnungQueueFolderPath") ?? @"C:\GkvAbrechnung\Queue\");
|
|
static DirectoryInfo GetResultFolder()
|
|
=> new DirectoryInfo(ConfigurationManager.AppSettings.Get("GkvAbrechnungResultFolderPath") ?? @"C:\GkvAbrechnung\Results\");
|
|
static DirectoryInfo GetFailedFolder()
|
|
=> new DirectoryInfo(ConfigurationManager.AppSettings.Get("GkvAbrechnungFailedFolderPath") ?? @"C:\GkvAbrechnung\Failed\");
|
|
}
|
|
}
|