diff --git a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs index f2e5e24..34629e4 100644 --- a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs @@ -44,7 +44,17 @@ public class DatabaseController int LogDirection = 1; MySqlCommand DatabaseCommand = null; - // Add parameters - prevent SQL injection + // For frames 1 to 10 we will put those into data raw tables + if (Int32.Parse(TCPFrameType) >= 1 && Int32.Parse(TCPFrameType) <= 10) + { + SQLQueryTxt = "INSERT INTO `pe_DataRaw` (`pe_dataraw_type`,`pe_dataraw_instance`) SELECT '" + TCPFrameType + "','" + TCPFrameInstance + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataRaw` WHERE `pe_dataraw_type` = '" + TCPFrameType + "' AND `pe_dataraw_instance` = " + TCPFrameInstance + ");"; + SQLQueryTxt += "UPDATE `pe_DataRaw` SET `pe_dataraw_payload` = @PAR_TCPFramePayload, `pe_dataraw_updated`=" + TCPFrameTimestamp + " WHERE `pe_dataraw_type`=" + TCPFrameType + " AND `pe_dataraw_instance` = " + TCPFrameInstance + ";"; + } + else + { + SQLQueryTxt = ""; + } + switch (Int32.Parse(TCPFrameType)) { case -1: @@ -58,7 +68,7 @@ public class DatabaseController break; case 1: - // General status information + // General status information (diagnostic information) LogInfo = $"Players data received"; TCPFrame.payload["v_win"] = "v" + Globals.VersionPerun; // Inject app version information (for database) @@ -68,11 +78,13 @@ public class DatabaseController SQLQueryTxt += "UPDATE `pe_OnlineStatus` SET `pe_OnlineStatus_perunversion_winapp` = '" + TCPFrame.payload.v_win + "', `pe_OnlineStatus_perunversion_dcshook` ='" + TCPFrame.payload.v_dcs_hook + "' WHERE `pe_OnlineStatus_instance` = '" + Int32.Parse(TCPFrameInstance) + "' ;"; DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); + TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload + DatabaseCommand.Parameters.AddWithValue("@PAR_TCPFramePayload", TCPFramePayload); break; case 2: - // Mission Data + // Status Data (basic mission etc) LogInfo = $"Mission data received"; SQLQueryTxt += "DELETE FROM pe_OnlinePlayers WHERE pe_OnlinePlayers_instance = " + Int32.Parse(TCPFrameInstance) + ";"; @@ -90,6 +102,8 @@ public class DatabaseController SQLQueryTxt += "UPDATE `pe_OnlineStatus` SET `pe_OnlineStatus_theatre` = '" + TCPFrame.payload.mission.theatre + "', `pe_OnlineStatus_name` = '" + TCPFrame.payload.mission.name + "' , `pe_OnlineStatus_pause` = '" + TCPFrame.payload.mission.pause + "', `pe_OnlineStatus_multiplayer` = '" + TCPFrame.payload.mission.multiplayer + "', `pe_OnlineStatus_realtime` = '" + TCPFrame.payload.mission.realtime + "', `pe_OnlineStatus_modeltime` = '" + TCPFrame.payload.mission.modeltime + "', `pe_OnlineStatus_players` = " + player_count + " WHERE `pe_OnlineStatus_instance` = '" + Int32.Parse(TCPFrameInstance) + "';"; DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); + TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload + DatabaseCommand.Parameters.AddWithValue("@PAR_TCPFramePayload", TCPFramePayload); // Save for GUI Globals.CurrentMission.Theatre = TCPFrame.payload.mission.theatre; // Mission theatre @@ -99,7 +113,15 @@ public class DatabaseController Globals.CurrentMission.ModelTime = TCPFrame.payload.mission.modeltime; // Mission time Globals.CurrentMission.RealTime = TCPFrame.payload.mission.realtime; // Since start of server break; + case 3: + // Slots Data (basic mission etc) + LogInfo = $"Slots data received"; + DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); + TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload + DatabaseCommand.Parameters.AddWithValue("@PAR_TCPFramePayload", TCPFramePayload); + + break; case 50: // Chat entry LogInfo = $"Player \"{TCPFrame.payload.player}\" -> chat message saved"; @@ -167,9 +189,6 @@ public class DatabaseController // Switch to insert correct log information switch (Int32.Parse(TCPFrameType)) { - case 3: - LogInfo = "Slots data received"; - break; case 100: LogInfo = "DCS SRS data send"; break; @@ -184,10 +203,9 @@ public class DatabaseController SQLQueryTxt = "INSERT INTO `pe_DataRaw` (`pe_dataraw_type`,`pe_dataraw_instance`) SELECT '" + TCPFrameType + "','" + TCPFrameInstance + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataRaw` WHERE `pe_dataraw_type` = '" + TCPFrameType + "' AND `pe_dataraw_instance` = " + TCPFrameInstance + ");"; SQLQueryTxt += "UPDATE `pe_DataRaw` SET `pe_dataraw_payload` = @PAR_TCPFramePayload, `pe_dataraw_updated`=" + TCPFrameTimestamp + " WHERE `pe_dataraw_type`=" + TCPFrameType + " AND `pe_dataraw_instance` = " + TCPFrameInstance + ";"; - TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload - DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); + TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload DatabaseCommand.Parameters.AddWithValue("@PAR_TCPFramePayload", TCPFramePayload); break; diff --git a/02_Windows_App/Perun_v1/01_Classes/Globals.cs b/02_Windows_App/Perun_v1/01_Classes/Globals.cs index 1261c2e..07b7ae0 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Globals.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Globals.cs @@ -81,6 +81,12 @@ internal class Globals public static string LastLogLocation = ""; // Last used log location public static bool RotateLogs = false; // Rotate logs true/false + public static float LastFrameTime = 0f; // Last recorded frame time + public static float LastFrameDelay = 0f; // Last recorded frame time + + public static string[] arrGUILogHistory; // Log history for GUI + public static string[] arrMySQLSendBuffer; // MySQL send buffer + public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information single ton public static HardwareMonitorClass HardwareMonitor = new HardwareMonitorClass(); // Hardware monitor singleton diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs index 77b367a..9161243 100644 --- a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -5,6 +5,12 @@ using System.Text.RegularExpressions; internal class PerunHelper { + public static void SetFrameRates(float frame_rate, float last_delay) + { + Globals.LastFrameTime = frame_rate; + Globals.LastFrameDelay = last_delay; + } + // Add error infomation public static void LogError(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false) { @@ -92,7 +98,7 @@ internal class PerunHelper Globals.AppUpdateGUI = true; } // Add the entry to log file - LogController.instance.WriteLog(logLevel, $"{DateTime.Now.ToString("yyyy-MM-dd ")} {DateTime.Now.ToString("HH:mm:ss.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}"); + LogController.instance.WriteLog(logLevel, $"{DateTime.Now.ToString("yyyy-MM-dd ")} {DateTime.Now.ToString("HH:mm:ss.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.LastFrameTime.ToString("0.00")} | {Globals.LastFrameDelay.ToString("0.00")} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}"); } // Gets build version diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index 1eb0330..790f7ce 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -12,16 +12,15 @@ public class TCPController // Main class for TCP listener public int intListenPort; // Port to connect to public bool bCloseConnection; // Helper to exit main loop without killing thread - public string[] arrGUILogHistory; // Log history for GUI - public string[] arrMySQLSendBuffer; // MySQL send buffer + public Thread thrTCPListener; // Seperate thread for TCP public void Create(int par_intListenPort, ref string[] par_arrLogHistory, ref string[] par_arrSendBuffer) { // Create class and map creation arguments to class intListenPort = par_intListenPort; - arrGUILogHistory = par_arrLogHistory; - arrMySQLSendBuffer = par_arrSendBuffer; + Globals.arrGUILogHistory = par_arrLogHistory; + Globals.arrMySQLSendBuffer = par_arrSendBuffer; bCloseConnection = false; } @@ -31,9 +30,9 @@ public class TCPController bCloseConnection = true; // Clear send buffer - for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) + for (int i = 0; i < Globals.arrMySQLSendBuffer.Length - 1; i++) { - arrMySQLSendBuffer[i] = null; // Empty send buffer + Globals.arrMySQLSendBuffer[i] = null; // Empty send buffer } } @@ -126,25 +125,30 @@ public class TCPController if (Int32.Parse(strRawTCPFrameType) != 0) { // Add to mySQL send buffer (find first empty slot) - PerunHelper.LogDebug(ref arrGUILogHistory, "Packet received" , 2,0, strRawTCPFrameType); + PerunHelper.LogDebug(ref Globals.arrGUILogHistory, "Packet received" , 2,0, strRawTCPFrameType); bool AddedDataToBuffer = false; - for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) + for (int i = 0; i < Globals.arrMySQLSendBuffer.Length - 1; i++) { - if (arrMySQLSendBuffer[i] == null) + if (Globals.arrMySQLSendBuffer[i] == null) { - arrMySQLSendBuffer[i] = strReceivedData; + Globals.arrMySQLSendBuffer[i] = strReceivedData; AddedDataToBuffer = true; break; } } if (!AddedDataToBuffer) { - PerunHelper.LogError(ref arrGUILogHistory, "ERROR TCP package was dropped", 1, 1, strRawTCPFrameType); + PerunHelper.LogError(ref Globals.arrGUILogHistory, "ERROR TCP package was dropped", 1, 1, strRawTCPFrameType); } } else { // Keep alive - PerunHelper.LogDebug(ref arrGUILogHistory, "Keep-alive received", 2,0,"0"); + PerunHelper.LogDebug(ref Globals.arrGUILogHistory, "Keep-alive received", 2,0,"0"); + } + + if (dynamicRawTCPFrame.dcs_frame_time != null && dynamicRawTCPFrame.dcs_current_frame_delay != null) + { + PerunHelper.SetFrameRates((float)dynamicRawTCPFrame.dcs_frame_time, (float)dynamicRawTCPFrame.dcs_current_frame_delay); } } else @@ -156,7 +160,7 @@ public class TCPController { Globals.ErrorsGame++; Console.WriteLine(e.ToString()); - PerunHelper.LogError(ref arrGUILogHistory, $"ERROR TCP while message parsing , error: {e.Message}",2,1,"?"); + PerunHelper.LogError(ref Globals.arrGUILogHistory, $"ERROR TCP while message parsing , error: {e.Message}",2,1,"?"); bTCPConnectionOnline = false; } @@ -168,7 +172,7 @@ public class TCPController catch (SocketException e) { Console.WriteLine(e.ToString()); - PerunHelper.LogError(ref arrGUILogHistory, $"ERROR TCP cannot check connection, error: {e.Message}",2,1,"?"); + PerunHelper.LogError(ref Globals.arrGUILogHistory, $"ERROR TCP cannot check connection, error: {e.Message}",2,1,"?"); } } @@ -182,7 +186,7 @@ public class TCPController { Globals.ErrorsGame++; Console.WriteLine(e.ToString()); - PerunHelper.LogError(ref arrGUILogHistory, $"ERROR TCP - connection closed or port in use, error: {e.Message}",1,1,"?"); + PerunHelper.LogError(ref Globals.arrGUILogHistory, $"ERROR TCP - connection closed or port in use, error: {e.Message}",1,1,"?"); bTCPConnectionOnline = false; } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs index eeb5344..3a9dfbf 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs @@ -34,6 +34,7 @@ this.con_Button_Listen_ON = new System.Windows.Forms.Button(); this.con_Button_Listen_OFF = new System.Windows.Forms.Button(); this.con_GroupBox_1 = new System.Windows.Forms.GroupBox(); + this.con_Button_See_log = new System.Windows.Forms.Button(); this.label15 = new System.Windows.Forms.Label(); this.con_com_loglevel = new System.Windows.Forms.ComboBox(); this.con_Button_Add_Marker = new System.Windows.Forms.Button(); @@ -83,6 +84,7 @@ this.con_img_dcs = new System.Windows.Forms.PictureBox(); this.con_img_db = new System.Windows.Forms.PictureBox(); this.con_GroupBox_5 = new System.Windows.Forms.GroupBox(); + this.con_check_delete_logs = new System.Windows.Forms.CheckBox(); this.con_GroupBox_6 = new System.Windows.Forms.GroupBox(); this.label26 = new System.Windows.Forms.Label(); this.label25 = new System.Windows.Forms.Label(); @@ -100,8 +102,10 @@ this.label27 = new System.Windows.Forms.Label(); this.label20 = new System.Windows.Forms.Label(); this.tim_HW_status = new System.Windows.Forms.Timer(this.components); - this.con_Button_See_log = new System.Windows.Forms.Button(); - this.con_check_delete_logs = new System.Windows.Forms.CheckBox(); + this.label30 = new System.Windows.Forms.Label(); + this.label31 = new System.Windows.Forms.Label(); + this.label32 = new System.Windows.Forms.Label(); + this.label33 = new System.Windows.Forms.Label(); this.con_GroupBox_1.SuspendLayout(); this.con_GroupBox_2.SuspendLayout(); this.con_GroupBox_3.SuspendLayout(); @@ -163,6 +167,18 @@ this.con_GroupBox_1.TabStop = false; this.con_GroupBox_1.Text = "Data log"; // + // con_Button_See_log + // + this.con_Button_See_log.Enabled = false; + this.con_Button_See_log.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.con_Button_See_log.Location = new System.Drawing.Point(170, 12); + this.con_Button_See_log.Name = "con_Button_See_log"; + this.con_Button_See_log.Size = new System.Drawing.Size(56, 20); + this.con_Button_See_log.TabIndex = 24; + this.con_Button_See_log.Text = "See log"; + this.con_Button_See_log.UseVisualStyleBackColor = true; + this.con_Button_See_log.Click += new System.EventHandler(this.con_Button_See_log_Click); + // // label15 // this.label15.AutoSize = true; @@ -631,6 +647,20 @@ this.con_GroupBox_5.TabStop = false; this.con_GroupBox_5.Text = "Other settings"; // + // con_check_delete_logs + // + this.con_check_delete_logs.AutoSize = true; + this.con_check_delete_logs.Checked = true; + this.con_check_delete_logs.CheckState = System.Windows.Forms.CheckState.Checked; + this.con_check_delete_logs.Location = new System.Drawing.Point(105, 19); + this.con_check_delete_logs.Name = "con_check_delete_logs"; + this.con_check_delete_logs.Size = new System.Drawing.Size(138, 17); + this.con_check_delete_logs.TabIndex = 25; + this.con_check_delete_logs.Text = "Rotate logs after 7 days"; + this.con_check_delete_logs.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; + this.con_check_delete_logs.UseVisualStyleBackColor = true; + this.con_check_delete_logs.Validated += new System.EventHandler(this.con_check_delete_logs_Validated); + // // con_GroupBox_6 // this.con_GroupBox_6.Controls.Add(this.label26); @@ -751,6 +781,10 @@ // // con_GroupBox_7 // + this.con_GroupBox_7.Controls.Add(this.label33); + this.con_GroupBox_7.Controls.Add(this.label32); + this.con_GroupBox_7.Controls.Add(this.label31); + this.con_GroupBox_7.Controls.Add(this.label30); this.con_GroupBox_7.Controls.Add(this.label29); this.con_GroupBox_7.Controls.Add(this.label28); this.con_GroupBox_7.Controls.Add(this.label27); @@ -765,7 +799,7 @@ // label29 // this.label29.AutoSize = true; - this.label29.Location = new System.Drawing.Point(82, 30); + this.label29.Location = new System.Drawing.Point(40, 34); this.label29.Name = "label29"; this.label29.Size = new System.Drawing.Size(37, 13); this.label29.TabIndex = 34; @@ -774,7 +808,7 @@ // label28 // this.label28.AutoSize = true; - this.label28.Location = new System.Drawing.Point(82, 16); + this.label28.Location = new System.Drawing.Point(40, 16); this.label28.Name = "label28"; this.label28.Size = new System.Drawing.Size(35, 13); this.label28.TabIndex = 34; @@ -783,7 +817,7 @@ // label27 // this.label27.AutoSize = true; - this.label27.Location = new System.Drawing.Point(51, 30); + this.label27.Location = new System.Drawing.Point(10, 34); this.label27.Name = "label27"; this.label27.Size = new System.Drawing.Size(34, 13); this.label27.TabIndex = 34; @@ -793,7 +827,7 @@ // label20 // this.label20.AutoSize = true; - this.label20.Location = new System.Drawing.Point(53, 16); + this.label20.Location = new System.Drawing.Point(10, 16); this.label20.Name = "label20"; this.label20.Size = new System.Drawing.Size(32, 13); this.label20.TabIndex = 34; @@ -806,31 +840,43 @@ this.tim_HW_status.Interval = 1000; this.tim_HW_status.Tick += new System.EventHandler(this.tim_HW_status_Tick); // - // con_Button_See_log + // label30 // - this.con_Button_See_log.Enabled = false; - this.con_Button_See_log.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.con_Button_See_log.Location = new System.Drawing.Point(170, 12); - this.con_Button_See_log.Name = "con_Button_See_log"; - this.con_Button_See_log.Size = new System.Drawing.Size(56, 20); - this.con_Button_See_log.TabIndex = 24; - this.con_Button_See_log.Text = "See log"; - this.con_Button_See_log.UseVisualStyleBackColor = true; - this.con_Button_See_log.Click += new System.EventHandler(this.con_Button_See_log_Click); + this.label30.AutoSize = true; + this.label30.Location = new System.Drawing.Point(92, 16); + this.label30.Name = "label30"; + this.label30.Size = new System.Drawing.Size(81, 13); + this.label30.TabIndex = 34; + this.label30.Text = "Last frame time:"; + this.label30.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // - // con_check_delete_logs + // label31 // - this.con_check_delete_logs.AutoSize = true; - this.con_check_delete_logs.Checked = true; - this.con_check_delete_logs.CheckState = System.Windows.Forms.CheckState.Checked; - this.con_check_delete_logs.Location = new System.Drawing.Point(105, 19); - this.con_check_delete_logs.Name = "con_check_delete_logs"; - this.con_check_delete_logs.Size = new System.Drawing.Size(138, 17); - this.con_check_delete_logs.TabIndex = 25; - this.con_check_delete_logs.Text = "Rotate logs after 7 days"; - this.con_check_delete_logs.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; - this.con_check_delete_logs.UseVisualStyleBackColor = true; - this.con_check_delete_logs.Validated += new System.EventHandler(this.con_check_delete_logs_Validated); + this.label31.AutoSize = true; + this.label31.Location = new System.Drawing.Point(174, 16); + this.label31.Name = "label31"; + this.label31.Size = new System.Drawing.Size(61, 13); + this.label31.TabIndex = 34; + this.label31.Text = "[frame time]"; + // + // label32 + // + this.label32.AutoSize = true; + this.label32.Location = new System.Drawing.Point(86, 34); + this.label32.Name = "label32"; + this.label32.Size = new System.Drawing.Size(87, 13); + this.label32.TabIndex = 35; + this.label32.Text = "Last frame delay:"; + this.label32.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // label33 + // + this.label33.AutoSize = true; + this.label33.Location = new System.Drawing.Point(174, 34); + this.label33.Name = "label33"; + this.label33.Size = new System.Drawing.Size(67, 13); + this.label33.TabIndex = 36; + this.label33.Text = "[frame delay]"; // // form_Main // @@ -962,6 +1008,10 @@ private System.Windows.Forms.Timer tim_HW_status; private System.Windows.Forms.Button con_Button_See_log; private System.Windows.Forms.CheckBox con_check_delete_logs; + private System.Windows.Forms.Label label31; + private System.Windows.Forms.Label label30; + private System.Windows.Forms.Label label33; + private System.Windows.Forms.Label label32; } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index 5ff0b59..b4be97d 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -581,6 +581,10 @@ namespace Perun_v1 Globals.ErrorsHistoryGame = Globals.ErrorsGame; } + // Frame times + label31.Text = $"{Globals.LastFrameTime.ToString("0.00")}µs"; + label33.Text = $"{Globals.LastFrameDelay.ToString("0.00")}µs"; + // Check if we shall rate logs Globals.RotateLogs = con_check_minimize_to_tray.Checked;