public ADTLookupResult( ADTLookupStatus _status, ADTMessage _message) { status = _status; message = _message; }
//____ _ _ _ ___ ___ _ _ _ ___ //|___ | |\ | | \ |__] \_/ | | \ //| | | \| |__/ |__] | | |__/ ////////////////////////////////////////////////////////////////////////////////////////////////////////// public static ADTLookupResult FindByID(String ID_toQuery) { ADTMessage adt_message; using (var db = new ADT_ModelContainer1()) { if (ID_toQuery == "") { return(new ADTLookupResult(ADTLookupStatus.Blank, null)); } else { try { // Check database for ADTMessage adt_message = db.ADTMessages.First(adt => adt.MessageID == ID_toQuery); // ADTMessage was found... // If AllowDuplicateMessageIDs is set, recursively find the next available MessageID if (Global.AllowDuplicateMessageIDs) { string[] full_id_split = ID_toQuery.Split(new string[] { "--" }, StringSplitOptions.None); if (full_id_split.Length == 1) { return(FindByID(ID_toQuery + "--1")); } else { return(FindByID(full_id_split[0] + "--" + (Int32.Parse(full_id_split[1]) + 1))); } } // Otherwise, return that it was found else { return(new ADTLookupResult(ADTLookupStatus.Found, adt_message)); } } // TargetInvocationException catch (Exception) { adt_message = new ADTMessage(ID_toQuery); return(new ADTLookupResult(ADTLookupStatus.CreatedNew, adt_message)); } } } }
//___ ____ ____ ____ ____ //|__] |__| |__/ [__ |___ //| | | | \ ___] |___ ////////////////////////////////////////////////////////////////////////////////////////////////////////// public void parse() { // LocationLookupStatus: // ---------------------- // Blank - location = null - existingPatient = null // Found_NoExistingPatient - location = <a location> - existingPatient = null // Found_ExistingPatientKicked - location = <a location> - existingPatient = <a patient> // NotFound_Created - location = <a location> - existingPatient = null // NotFound_NotCreated - location = null - existingPatient = null // PatientLookupStatus: // --------------------- // Blank - patient = null // Found - patient = <a patient> // NotFound - patient = null // MessageType // A01 - Admit - Need location - Need patient // A02 - Transfer - Need location - Need patient // A03 - Discharge - Do not Need location - Need patient // A06 - OutpatientToIn - Need location - Need patient // A07 - InpatientToOut - Do not Need location - Need patient // A08 - Update - Need location - Need patient // A11 - CancelAdmit - Do not Need location - Need patient // A12 - CancelTransfer - Do not Need location - Need patient // A13 - CancelDischarge - Do not Need location - Need patient // PatientStatus: // ---------------------- // AdmittedWithLocation // AdmittedWithoutLocation // Discharged // Query the database for the identifiers in the message adtLookupResult = ADTMessage.FindByID(MessageID); locationLookupResult = Location.FindByID(LocationID); patientLookupResult = Patient.FindByID(PatientID); switch (adtLookupResult.status) { case ADTLookupStatus.Blank: warnList.Add("SKIP: Blank Message ID"); skip = true; return; case ADTLookupStatus.Found: warnList.Add("SKIP: Message Already Exists"); skip = true; return; case ADTLookupStatus.CreatedNew: MessageID = adtLookupResult.message.MessageID; break; } switch (patientLookupResult.status) { // If there is no patient: WARN AND EXIT case PatientLookupStatus.Blank: warnList.Add("SKIP: No Patient ID"); skip = true; return; // If there is no patient: INITIALIZE BASIC INFO case PatientLookupStatus.CreatedNew: patientLookupResult.patient.FirstName = FirstName; patientLookupResult.patient.LastName = LastName; break; } switch (MessageType) { // These messages can update a patient's location case "A01": case "A02": case "A06": case "A08": int h = 5; if (MessageType == "A06") { h = 4; } // If there is no location // WARN AND EXIT if (locationLookupResult.location == null) { warnList.Add("SKIP: There was no location in " + MessageType); skip = true; return; } // If there is an existing patient // WARN AND KICK if (locationLookupResult.existingPatient != null && locationLookupResult.existingPatient.PatientID != PatientID) { warnList.Add("Kicking existing Patient"); KickPatient(); } // Update the patient, and ADT message Update(); break; // These messages can discharge a patient case "A03": case "A07": // If there is no location // WARN if (locationLookupResult.location == null) { warnList.Add("There was no location in " + MessageType); } // If there is an existing patient // WARN if (locationLookupResult.existingPatient != null && locationLookupResult.existingPatient.PatientID != PatientID) { warnList.Add(MessageType + " indicated different patient in location"); } // Discharge the patient Discharge(); break; //case "A11": if (Global.Process_A11) CancelAdmit(); break; //case "A12": if (Global.Process_A12) CancelTransfer(); break; //case "A13": if (Global.Process_A13) CancelDischarge(); break; default: warnList.Add("SKIP: " + MessageType + " messages are not handled"); skip = true; return; } // Write adt_message to the database CreateInDatabase(); }
// // ______ __ ____ ____ ___ __ ___ ____ __ __ ___ ____ // | T / ]| \ | \ / _] / ] / _]l j| T | / _]| \ // | | / / | o ) | D ) / [_ / / / [_ | T | | | / [_ | D ) // l_j l_j/ / | _/ | / Y _] / / Y _] | | | | |Y _]| / // | | / \_ | | | \ | [_ / \_ | [_ | | l : !| [_ | \ // | | \ || | | . Y| T\ || T j l \ / | T| . Y // l__j \____jl__j l__j\_jl_____j \____jl_____j|____j \_/ l_____jl__j\_j // // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region TCP Receiver private static void TCP_Listen_Thread_Function() { TcpListener server = null; ADTMessage adt_message; try { initDatabase(); Int32 port = Convert.ToInt32(Global.TCP_PORT); if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { MLog.Error("No network available... Press any key to exit."); Console.ReadKey(); return; } IPAddress localAddr = IPAddress.Parse(LocalIPAddress()); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data byte[] raw; string rawString = ""; // Enter the listening loop. while (true) { MLog.Info("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); MLog.Info("Connected!"); NetworkStream stream = client.GetStream(); int i = 0; while (client.Connected) { // If this truly loops around 1 time per message // then let the info about the message be known // MLog.Info("Yo, whatup!" + i++); try { // this reads exactly 1 message raw = blockingEOMRead(stream, 1000, 500, 1000, Global.EOM); //MLog.Info("Test: " + i); if (raw.Length == 0 || raw.Length == 1) { continue; } rawString = Tool.ByteArrayToString(raw); MLog.MsgCapture("Received: " + rawString); // This extracts data from the message string adt_message = new ADTMessage(rawString, DateTime.Now); // Read from/Write to Patient and Location tables in database if (!adt_message.skip) { adt_message.parse(); } if (!adt_message.skip || Global.logIfSkipped) { adt_message.Log(); } // Send an acknowledge sendAck(stream); } catch (Exception ex) { MLog.Error("Exception Detected in msg: |" + rawString + "| Closing Connection.\n" + ex.Message); client.Close(); break; } } } } catch (SocketException ex) { MLog.Error("FATAL: SocketException: " + ex); } catch (Exception ex) { MLog.Error("FATAL: " + ex); } finally { MLog.Info("Exiting"); MLog.saveLog(null, null); // Stop listening for new clients. server.Stop(); } }