using Newtonsoft.Json; using PdfSharp.Drawing; using PdfSharp.Pdf; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace PrimarySystemSimulator { public partial class Form1 : Form { private string _Path = @"c:\pba\private"; private List _Patients = new List(); private string patient_path = ""; private bool IsLoading = false; private bool IsWatching = false; private bool PatientOpen = false; private delegate void LoadingFinishedDelegate(); private delegate void PatientSavedDelegate(Patient patient, string ack); private delegate void ShowPatientDialogDelegate(string ack); public Form1() { InitializeComponent(); label1.Text = _Path; Initialize(); } private void Initialize() { try { patient_path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "tmp_db"); if(!Directory.Exists(patient_path)) { Directory.CreateDirectory(patient_path); } this.LoadPatients(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void WatchRegisterAcks() { if(!IsWatching) { Thread thread = new Thread(WatchRegisterAcksInBG); thread.Start(); } } private void WatchRegisterAcksInBG() { try { IsWatching = true; while(IsWatching) { if(!PatientOpen) { string[] acks = Directory.GetFiles(Path.Combine(_Path, "ack"), "*.log"); string ack_done_dir = Path.Combine(Path.Combine(_Path, "ack"), "done"); if (acks != null && acks.Length > 0) { string type = ""; string pgs = ""; string udid = ""; Patient patien = null; string[] lines = File.ReadAllLines(acks[0]); if (lines != null && lines.Length > 0) { foreach (string line in lines) { if (line.StartsWith("TYPE:")) { type = line.Replace("TYPE:", "").Trim(' ').Replace("\r\n", "").Replace("\n", ""); } else if (line.StartsWith("PGS:")) { pgs = line.Replace("PGS:", "").Trim(' ').Replace("\r\n", "").Replace("\n", ""); } else if (line.StartsWith("UDID:")) { udid = line.Replace("UDID:", "").Trim(' ').Replace("\r\n", "").Replace("\n", ""); } } } if (_Patients != null && _Patients.Count > 0) { patien = _Patients.FirstOrDefault(m => m.PGS == pgs); } if(type == "SUBSCRIBE") { if (patien == null) { ShowPatientDialog(acks[0]); } else if (!string.IsNullOrWhiteSpace(pgs) && !string.IsNullOrEmpty(udid)) { patien.PGS = pgs; patien.UDID = udid; PatientSaved(patien, acks[0]); } } string done_file = Path.Combine(ack_done_dir, Path.GetFileName(acks[0] + ".imp")); File.Move(acks[0], done_file); } } Thread.Sleep(200); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void SavePatientInDb(Patient patient) { try { string json = JsonConvert.SerializeObject(patient); if(!String.IsNullOrEmpty(json)) { string file = Path.Combine(patient_path, patient.PatId + ".json"); if(File.Exists(file)) { File.Delete(file); } File.WriteAllText(file, json); LoadPatients(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void ShowPatientDialog(string ack) { if(this.send.InvokeRequired) { this.send.Invoke(new ShowPatientDialogDelegate(ShowPatientDialog), ack); } else { PatientOpen = true; Register register = new Register(ack); DialogResult result = register.ShowDialog(); PatientSaved(register.Patient, ack); } } private void PatientSaved(Patient patient, string ack) { if(this.send.InvokeRequired) { this.send.Invoke(new PatientSavedDelegate(PatientSaved), patient, ack); } else { if(patient != null) { if (patient.Status == "REJECTED") { string filename = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "test.pdf"); if (File.Exists(filename)) { File.Delete(filename); } string pgs = SHA512HASH(patient.Zip + patient.Birthday + sampleid.Text); string testhl7 = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "befund.hl7"); string content = File.ReadAllText(testhl7); content = content.Replace("[STATUS]", "REJECTED"); content = content.Replace("[UDID]", patient.UDID); content = content.Replace("[PGS]", patient.PGS); string newFile = Path.Combine(_Path, SHA512HASH(patient.PGS + patient.UDID) + ".hl7"); File.WriteAllText(newFile, content); lblStatus.Text = "Auftrag wurde abgelehnt"; } else { SavePatientInDb(patient); } } PatientOpen = false; this.LoadPatients(); } } private void LoadPatients() { try { if(send.InvokeRequired) { send.Invoke(new LoadingFinishedDelegate(LoadPatients)); } else { if (!IsLoading) { send.Visible = true; Thread thread = new Thread(LoadPatientsAsync); thread.Start(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void LoadPatientsAsync() { try { _Patients = null; IsLoading = true; string[] files = Directory.GetFiles(patient_path, "*.json"); if (files != null && files.Length > 0) { _Patients = new List(); foreach (string file in files) { if(!string.IsNullOrEmpty(file) && File.Exists(file)) { string json = File.ReadAllText(file); if(!string.IsNullOrEmpty(json)) { Patient patient = JsonConvert.DeserializeObject(json); if(patient != null && _Patients != null) { _Patients.Add(patient); } } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { LoadFinished(); } } private void LoadFinished() { if(this.send.InvokeRequired) { this.send.Invoke(new LoadingFinishedDelegate(LoadFinished)); } else { comboBox1.DataSource = null; if(_Patients != null && _Patients.Count > 0) { comboBox1.DataSource = _Patients; comboBox1.DisplayMember = "Name"; } IsLoading = false; if(!IsWatching) { this.WatchRegisterAcks(); } } } private void send_Click(object sender, EventArgs e) { lblStatus.Text = ""; try { if (string.IsNullOrEmpty(this._Path)) { MessageBox.Show("Bitte wählen Sie den Pfad von der Freigabe, wo der ServiceInside die Dateien bearbeitet"); } else if (string.IsNullOrEmpty(this.sampleid.Text)) { MessageBox.Show("Bitte geben Sie eine Auftragsnummer ein"); } else if (string.IsNullOrEmpty(this.resultsText.Text)) { MessageBox.Show("Bitte geben Sie einen Text als Befund ein"); } else if(comboBox1.SelectedValue == null) { MessageBox.Show("Bitte wählen Sie einen Patient aus"); } else { Patient patient = (comboBox1.SelectedValue as Patient); PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Verdana", 20, XFontStyle.Bold); gfx.DrawString("Patient: " + patient.Name + " (" + patient.PatId + ")", new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 100); gfx.DrawString("PLZ:" + patient.Zip, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 120); gfx.DrawString("Geburtsdatum: " + patient.Birthday, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 140); gfx.DrawString("Auftragsnummer: " + sampleid.Text, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 160); gfx.DrawString("BEFUND: " + sampleid.Text, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 200); gfx.DrawString(resultsText.Text, new XFont("Arial", 16, XFontStyle.Bold), XBrushes.Black, 100, 220); string filename = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "test.pdf"); if (File.Exists(filename)) { File.Delete(filename); } document.Save(filename); string base64 = Convert.ToBase64String(File.ReadAllBytes(filename)); string pgs = SHA512HASH(patient.Zip + patient.Birthday + sampleid.Text); string testhl7 = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "befund.hl7"); string content = File.ReadAllText(testhl7); content = content.Replace("[BIRTHDAY]", patient.Birthday); content = content.Replace("[ZIP]", patient.Zip); content = content.Replace("[SAMPLEID]", sampleid.Text); content = content.Replace("[PATID]", patient.PatId); content = content.Replace("[UDID]", patient.UDID); content = content.Replace("[RESULTS]", base64); content = content.Replace("[STATUS]", "COMPLETED"); content = content.Replace("[PGS]", pgs); string newFile = Path.Combine(_Path, pgs + ".hl7"); File.WriteAllText(newFile, content); lblStatus.Text = "Test Befund wurde erfolgreich erstellt"; } } catch (Exception ex) { lblStatus.Text = ex.Message; } } private void pathSelector_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); if(dialog.ShowDialog() == DialogResult.OK) { this._Path = dialog.SelectedPath; label1.Text = dialog.SelectedPath; } } public static string SHA512HASH(string value) { string result = null; try { byte[] bytes = Encoding.UTF8.GetBytes(value); using (System.Security.Cryptography.SHA512 hash = System.Security.Cryptography.SHA512.Create()) { byte[] hashedInputBytes = hash.ComputeHash(bytes); StringBuilder hashedInputStringBuilder = new StringBuilder(128); foreach (var b in hashedInputBytes) { hashedInputStringBuilder.Append(b.ToString("X2")); } result = hashedInputStringBuilder.ToString().ToLower(); } } catch { result = null; } return result; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { IsWatching = false; } private void button1_Click(object sender, EventArgs e) { LoadPatients(); } } }