private static void initDatabase() { using (var db = new ADT_ModelContainer1()) { var objCtx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext; try { MLog.Info("Caching database..."); objCtx.ExecuteStoreCommand("SELECT TOP 1000 [MessageID] from [ADT].[dbo].[ADTMessages]"); objCtx.ExecuteStoreCommand("SELECT TOP 1000 [LocationID] from [ADT].[dbo].[Locations]"); objCtx.ExecuteStoreCommand("SELECT TOP 1000 [PatientID] from [ADT].[dbo].[Patients]"); if (Global.clearAllTables) { MLog.Info(" Clearing Tables..."); objCtx.ExecuteStoreCommand("TRUNCATE TABLE [ADT].[dbo].[ADTMessages]"); objCtx.ExecuteStoreCommand("TRUNCATE TABLE [ADT].[dbo].[Locations]"); objCtx.ExecuteStoreCommand("TRUNCATE TABLE [ADT].[dbo].[Patients]"); } } catch (Exception) { MLog.Info(" Creating Tables..."); SqlFile_Execute("ADT_Model.edmx.sql", objCtx); } } }
//_ _ _ ____ ____ _ _ //| | | |__| |__/ |\ | //|_|_| | | | \ | \| ////////////////////////////////////////////////////////////////////////////////////////////////////////// internal void Log() { string msg = LookupStatusesToString() + (skip? "Skipped! " : "STORED! "); if (warnList.Count() > 0) { msg += warnList.Count() + " Warnings: " + string.Join("; ", warnList); MLog.Warn(msg); } else { MLog.Info(msg); } }
// // ______ __ ____ ____ ___ __ ___ ____ __ __ ___ ____ // | 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(); } }