patbef-Simulator/Simulator/BackgroundService.cs

191 lines
7.4 KiB
C#

using Simulator.Controllers;
using Simulator.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
namespace Simulator
{
public class BackgroundService
{
private static SimulatorDbContext db = new SimulatorDbContext("Server=localhost;Database=simulator;Uid=simulator;Pwd=W5Cok8HAy1XoSa63qaRiM5BA8i1E6I;");
private static string _PrivateDirectory = @"C:\pba\private";
private static string _PrivateDirectoryAck = @"C:\pba\private\ack";
private static string _PrivateDirectoryAckDone = @"C:\pba\private\ack\done";
private static bool _Running = false;
private static Thread _workerThread;
public static void Start()
{
try
{
if (_Running)
{
Stop();
}
_workerThread = new Thread(WorkAsync);
_workerThread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void Stop()
{
try
{
_Running = false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void WorkAsync()
{
try
{
_Running = true;
while (_Running)
{
string[] acks = Directory.GetFiles(_PrivateDirectoryAck, "*.log");
if (acks != null && acks.Length > 0)
{
foreach(string ack in acks)
{
string[] lines = File.ReadAllLines(ack);
if(lines != null && lines.Length > 0)
{
string type = null;
string udid = null;
string auftragsnummer = null;
string PGS = null;
string ZIP = null;
string BIRTHDATE = null;
foreach (string line in lines)
{
if(line.StartsWith("TYPE:"))
{
type = line.Replace("TYPE:", "");
}
else if (line.StartsWith("UDID:"))
{
udid = line.Replace("UDID:", "");
}
else if (line.StartsWith("PGS:"))
{
PGS = line.Replace("PGS:", "");
}
else if (line.StartsWith("ZIP:"))
{
ZIP = line.Replace("ZIP:", "");
}
else if (line.StartsWith("BIRTHDATE:"))
{
BIRTHDATE = line.Replace("BIRTHDATE:", "");
}
else if (line.StartsWith("ORDER_ID:"))
{
auftragsnummer = line.Replace("ORDER_ID:", "");
}
}
if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(udid) && !string.IsNullOrEmpty(auftragsnummer))
{
Befund befund = db.GetBefundByAuftragsnummer(auftragsnummer, ZIP, BIRTHDATE);
if(befund != null)
{
if (type == "SUBSCRIBE")
{
Patient patient = db.GetPatient(befund.PatientId);
if (patient != null)
{
patient.UDID = udid;
befund.Status = type;
befund.UDID = patient.UDID;
if (db.UpdatePatient(patient) && db.UpdateBefund(befund))
{
HomeController.SendBefund(befund, patient);
}
}
}
else
{
befund.Status = type;
db.UpdateBefund(befund);
}
}
else
{
Reject(type, udid, auftragsnummer, PGS, ZIP, BIRTHDATE);
}
}
}
string move_file = Path.Combine(_PrivateDirectoryAckDone, Path.GetFileName(ack));
if (File.Exists(move_file))
{
File.Delete(move_file);
}
File.Move(ack, move_file);
}
}
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", "Error: " + ex.Message);
}
}
public static void Reject(string type, string udid, string auftragsnummer, string PGS, string ZIP, string BIRTHDATE)
{
try
{
string testhl7 = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), "befund.hl7");
string content = System.IO.File.ReadAllText(testhl7);
content = content.Replace("[BIRTHDAY]", BIRTHDATE);
content = content.Replace("[ZIP]", ZIP);
content = content.Replace("[SAMPLEID]", auftragsnummer);
content = content.Replace("[UDID]", udid);
content = content.Replace("[STATUS]", "NOT_FOUND");
content = content.Replace("[PGS]", PGS);
string pgs = HomeController.SHA512HASH(ZIP + BIRTHDATE + auftragsnummer);
string newFile = Path.Combine(_PrivateDirectory, pgs + ".hl7");
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", "Reject: " + newFile + ", content: " + content);
System.IO.File.WriteAllText(newFile, content);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
System.IO.File.AppendAllText(@"C:\tmp\simulator.txt", ex.Message);
}
}
}
}