From 3ff2fff72841b0d7955240719bd416c27a78fda5 Mon Sep 17 00:00:00 2001 From: Szymon Porwolik Date: Fri, 1 Mar 2019 01:57:00 +0100 Subject: [PATCH] v0.4.0 major code cleanup --- .../Perun_v1/01_Classes/DatabaseController.cs | 125 ++++ 02_Windows_App/Perun_v1/01_Classes/Globals.cs | 7 + .../Perun_v1/01_Classes/NativeMethods.cs | 2 +- .../Perun_v1/01_Classes/PerunHelper.cs | 26 + 02_Windows_App/Perun_v1/01_Classes/Program.cs | 19 +- .../Perun_v1/01_Classes/UDPController.cs | 91 +++ .../Perun_v1/02_Forms/form_Main.Designer.cs | 18 +- 02_Windows_App/Perun_v1/02_Forms/form_Main.cs | 552 ++++++------------ .../Perun_v1/02_Forms/form_Main.resx | 3 + 02_Windows_App/Perun_v1/Perun_v1.csproj | 4 + 10 files changed, 453 insertions(+), 394 deletions(-) create mode 100644 02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs create mode 100644 02_Windows_App/Perun_v1/01_Classes/Globals.cs create mode 100644 02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs create mode 100644 02_Windows_App/Perun_v1/01_Classes/UDPController.cs diff --git a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs new file mode 100644 index 0000000..8058257 --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs @@ -0,0 +1,125 @@ +// This class handles MySQL communication +using MySql.Data.MySqlClient; +using Newtonsoft.Json; +using System; + +internal class DatabaseController +{ + public static string strMySQLConnectionString; // MySQL connection string + + public static void SendToMySql(string strRawUDPFrame) + { + // Main function to send data to mysql + dynamic strUDPFrame = JsonConvert.DeserializeObject(strRawUDPFrame); // Deserialize raw data + string strUDPFrameType = strUDPFrame.type; + string strUDPFrameTimestamp = strUDPFrame.timestamp; + string strUDPFramePayload = ""; + string strSQLQueryTxt = ""; + + + // Some frames may come without timestamp, use database currrent timestampe then + if (strUDPFrameTimestamp != null) + { + strUDPFrameTimestamp = "'" + strUDPFrameTimestamp + "'"; + } + else + { + strUDPFrameTimestamp = "CURRENT_TIMESTAMP()"; + } + + // Modify specific types + if (strUDPFrameType == "1") + { + strUDPFrame.payload["v_win"] = "v" + Globals.strPerunVersion; // Inject app version information + } + + // Specific SQL + if (strUDPFrameType == "50") + { + // Add entry to chat log + strSQLQueryTxt = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.ucid + "' );"; + strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated` = " + strUDPFrameTimestamp + ",`pe_DataPlayers_lastname`='" + strUDPFrame.payload.player + "' WHERE `pe_DataPlayers_ucid`='" + strUDPFrame.payload.ucid + "' ;"; + strSQLQueryTxt += "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` ='" + strUDPFrame.payload.missionhash + "' );"; + strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.missionhash + "';"; + strSQLQueryTxt += "INSERT INTO `pe_LogChat` (`pe_LogChat_id`,`pe_LogChat_datetime`, `pe_LogChat_playerid`, `pe_LogChat_msg`, `pe_LogChat_all`,`pe_LogChat_missionhash_id`) VALUES (NULL,'" + strUDPFrame.payload.datetime + "', (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.ucid + "'), '" + strUDPFrame.payload.msg + "', '" + strUDPFrame.payload.all + "',(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.missionhash + "'));"; + } + else if (strUDPFrameType == "51") + { + // Add entry to event log + strSQLQueryTxt = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.log_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "');"; + strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "';"; + strSQLQueryTxt += "INSERT INTO `pe_LogEvent` (`pe_LogEvent_id`, `pe_LogEvent_datetime`, `pe_LogEvent_type`, `pe_LogEvent_content`,`pe_LogEvent_missionhash_id`) VALUES ( NULL, '" + strUDPFrame.payload.log_datetime + "', '" + strUDPFrame.payload.log_type + "', '" + strUDPFrame.payload.log_content + "', (SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "'));"; + } + else if (strUDPFrameType == "52") + { + // Update user stats + strUDPFramePayload = JsonConvert.SerializeObject(strUDPFrame.payload.stat_data); // Deserialize payload + + strSQLQueryTxt = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.stat_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "');"; + strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "';"; + strSQLQueryTxt += "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.stat_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "');"; + strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated`=" + strUDPFrameTimestamp + " WHERE `pe_DataPlayers_ucid`='" + strUDPFrame.payload.stat_ucid + "';"; + strSQLQueryTxt += "INSERT INTO `pe_LogStats` (`pe_LogStats_playerid`,`pe_LogStats_missionhash_id`) SELECT (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "'), (SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_LogStats` WHERE `pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "') );"; + strSQLQueryTxt += "UPDATE `pe_LogStats` SET `pe_LogStats_datetime`='" + strUDPFrame.payload.stat_datetime + "',`pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "'),`pe_LogStats_debug`=JSON_QUOTE('" + strUDPFramePayload + "'),`pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') WHERE `pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "');"; + } + else if (strUDPFrameType == "53") + { + // User logged in to DCS server + + strSQLQueryTxt = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.login_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where pe_DataPlayers_ucid='" + strUDPFrame.payload.login_ucid + "');"; + strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET pe_DataPlayers_lastip='" + strUDPFrame.payload.login_ipaddr + "', pe_DataPlayers_lastname='" + strUDPFrame.payload.login_name + "',pe_DataPlayers_updated='" + strUDPFrame.payload.login_datetime + "' WHERE `pe_DataPlayers_ucid`= '" + strUDPFrame.payload.login_ucid + "';"; + strSQLQueryTxt += "INSERT INTO `pe_LogLogins` (`pe_LogLogins_datetime`, `pe_LogLogins_playerid`, `pe_LogLogins_name`, `pe_LogLogins_ip`) VALUES ('" + strUDPFrame.payload.login_datetime + "', (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + strUDPFrame.payload.login_ucid + "'), '" + strUDPFrame.payload.login_name + "', '" + strUDPFrame.payload.login_ipaddr + "');"; + } + else + { + // General definition used for 1-10 type packets + strUDPFramePayload = JsonConvert.SerializeObject(strUDPFrame.payload); // Deserialize payload + + strSQLQueryTxt = "INSERT INTO `pe_DataRaw` (`pe_dataraw_type`) SELECT '" + strUDPFrameType + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataRaw` WHERE pe_dataraw_type = '" + strUDPFrameType + "' );"; + strSQLQueryTxt += "UPDATE `pe_DataRaw` SET `pe_dataraw_payload` = JSON_QUOTE('" + strUDPFramePayload + "'), `pe_dataraw_updated`=" + strUDPFrameTimestamp + " WHERE `pe_dataraw_type`=" + strUDPFrameType + ";"; + } + + // Connect to mysql and execute sql + MySqlConnection connMySQL = new MySqlConnection(DatabaseController.strMySQLConnectionString); + try + { + Console.WriteLine("Sending data to MySQL - Begin"); + connMySQL.Open(); + + MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL); + MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader(); + + while (rdrMySQL.Read()) + { + Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]); + } + rdrMySQL.Close(); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "MySQL updated, package type: " + strUDPFrameType); + } + catch (ArgumentException a_ex) + { + // General exception found + Console.WriteLine(a_ex.ToString()); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType); + } + catch (MySqlException m_ex) + { + // MySQL exception found + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType); + switch (m_ex.Number) + { + case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port) + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - unable to connect"); + break; + case 0: // Access denied (Check DB name,username,password) + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - access denied"); + break; + default: + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number); + break; + } + } + connMySQL.Close(); + Console.WriteLine("Sending data to MySQL - Done"); + } +} \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/01_Classes/Globals.cs b/02_Windows_App/Perun_v1/01_Classes/Globals.cs new file mode 100644 index 0000000..2ccb6ec --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/Globals.cs @@ -0,0 +1,7 @@ +// This class gathers all global variables + +internal class Globals +{ + public static string strPerunVersion = "DEBUG"; // Helper for pulling version definition + public static string[] arrLogHistory = new string[10]; // Log history for GUI +} \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs b/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs index 4372f27..6815b57 100644 --- a/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs +++ b/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs @@ -1,4 +1,4 @@ -// this class just wraps some Win32 stuff that we're going to use for blocking of multiple instances +// This class just wraps some Win32 stuff that we're going to use for blocking of multiple instances using System; using System.Runtime.InteropServices; diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs new file mode 100644 index 0000000..c77dbf2 --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -0,0 +1,26 @@ +// This class gathers all helper functions +using System; + +internal class PerunHelper +{ + public static void LogHistoryAdd(ref string[] arrLogHistory, string strEntryToAdd) + { + // Add entry to log history and rotate + for (int i = 0; i < arrLogHistory.Length - 1; i++) + { + arrLogHistory[i] = arrLogHistory[i + 1]; // Shift one down + } + + arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " > " + strEntryToAdd; // Add entry at the last position + } + public static string GetAppVersion(string strBeginning) + { + // Gets build version + if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) + { + System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment; + Globals.strPerunVersion = cd.CurrentVersion.ToString(); + } + return strBeginning+"v" + Globals.strPerunVersion; + } +} \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/01_Classes/Program.cs b/02_Windows_App/Perun_v1/01_Classes/Program.cs index 2476321..001bb6d 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Program.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Program.cs @@ -4,45 +4,44 @@ using System.Windows.Forms; namespace Perun_v1 { - static class Program { /// - /// Główny punkt wejścia dla aplikacji. + /// Main entry point to app /// // Mutex to block multiple instances static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}"); + [STAThread] static void Main() { + // Check if no other instances running if (mutex.WaitOne(TimeSpan.Zero, true)) { + // Only instance Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ApplicationExit += new EventHandler(Application_ApplicationExit); void Application_ApplicationExit(object sender, EventArgs e) { - Properties.Settings.Default.Save(); + Properties.Settings.Default.Save(); // We will save settings on exit } - Application.Run(new form_Main()); - mutex.ReleaseMutex(); + Application.Run(new form_Main()); // Run main form + mutex.ReleaseMutex(); // Release mutex } else { - // send our Win32 message to make the currently running instance - // jump on top of all the other windows + // Multiple instance - send our Win32 message to make the currently running instance jump to front NativeMethods.PostMessage( (IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); - MessageBox.Show("Only one instance is allowed."); + MessageBox.Show("Only one Perun instance is allowed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } } - - } } diff --git a/02_Windows_App/Perun_v1/01_Classes/UDPController.cs b/02_Windows_App/Perun_v1/01_Classes/UDPController.cs new file mode 100644 index 0000000..1dc1375 --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/UDPController.cs @@ -0,0 +1,91 @@ +// This class handles UDP communication +using Newtonsoft.Json; +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Windows.Forms; + +internal class UDPController +{ + // Main class for UDP listener + public static int intListenPort; // Port to listen at + public static bool boolDone; // Helper to exit main loop without killing thread + public static UdpClient udpListener; // Listener object + public static string[] arrLogHistory; // Log history for GUI + public static string[] arrSendBuffer; // Mysql send buffer + public static Thread thrUDPListener; // Seperate thread for UDP + + public static void Create(int intListenPort, ref string[] arrLogHistory, ref string[] arrSendBuffer) + { + // Create class + UDPController.intListenPort = intListenPort; + UDPController.arrLogHistory = arrLogHistory; + UDPController.arrSendBuffer = arrSendBuffer; + } + + public static void StopListen() + { + // FInish listening + UDPController.boolDone = true; + UDPController.udpListener.Close(); + UDPController.udpListener = null; + + for (int i = 0; i < arrSendBuffer.Length - 1; i++) + { + arrSendBuffer[i] = null; + } + + } + + public static void StartListen() + { + Console.WriteLine("UDP Listen start"); + try + { + // Start listening to UDP + + udpListener = new UdpClient(intListenPort); + IPEndPoint ipendpointGroupEP = new IPEndPoint(IPAddress.Loopback, intListenPort); + string strReceivedData; + byte[] arrReceiveByteArray; + + // Start the main loop + UDPController.boolDone = false; + while (!UDPController.boolDone) + { + // Start listening + Console.WriteLine("UDP: Waiting for packet"); + arrReceiveByteArray = udpListener.Receive(ref ipendpointGroupEP); + strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length); + Console.WriteLine("Sender: {0} Payload: {1}", ipendpointGroupEP.ToString(), strReceivedData); + + // Add to log history and rotate + dynamic dynamicRawUDPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame + string strRawUDPFrameType = dynamicRawUDPFrame.type; + PerunHelper.LogHistoryAdd(ref arrLogHistory, "UDP packet received, type: " + strRawUDPFrameType); + + // Add to mySQL send buffer (find first empty slot) + for (int i = 0; i < arrSendBuffer.Length - 1; i++) + { + if (arrSendBuffer[i] == null) + { + arrSendBuffer[i] = strReceivedData; + break; + } + } + } + udpListener.Close(); // Close port + } + catch (Exception e) + { + // General exception found + if (e.HResult != -2147467259) { + Console.WriteLine(e.ToString()); + PerunHelper.LogHistoryAdd(ref arrLogHistory, "ERROR UDP - port may be in use"); + } + } + Console.WriteLine("UDP listen stop"); + } +} \ No newline at end of file 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 01fcb31..ba881ff 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 @@ -59,6 +59,7 @@ this.openFileDialog_LotATC = new System.Windows.Forms.OpenFileDialog(); this.tim_10000ms = new System.Windows.Forms.Timer(this.components); this.tim_200ms = new System.Windows.Forms.Timer(this.components); + this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.con_GroupBox_1.SuspendLayout(); this.con_GroupBox_2.SuspendLayout(); this.con_GroupBox_3.SuspendLayout(); @@ -69,16 +70,7 @@ this.con_List_Received.Enabled = false; this.con_List_Received.FormattingEnabled = true; this.con_List_Received.Items.AddRange(new object[] { - "Loading", - "Loading", - "Loading", - "Loading", - "Loading", - "Loading", - "Loading", - "Loading", - "Loading", - "Loading"}); + "Welcome to Perun for DCS World!"}); this.con_List_Received.Location = new System.Drawing.Point(6, 19); this.con_List_Received.Name = "con_List_Received"; this.con_List_Received.Size = new System.Drawing.Size(307, 134); @@ -327,6 +319,11 @@ // this.tim_200ms.Tick += new System.EventHandler(this.tim_200ms_Tick); // + // notifyIcon1 + // + this.notifyIcon1.Text = "notifyIcon1"; + this.notifyIcon1.Visible = true; + // // form_Main // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -390,6 +387,7 @@ private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox con_txt_mysql_port; private System.Windows.Forms.Timer tim_200ms; + private System.Windows.Forms.NotifyIcon notifyIcon1; } } 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 18de3ce..426aa92 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -1,222 +1,25 @@ using System; using System.Diagnostics; -using System.Net; -using System.Net.Sockets; -using System.Reflection; -using System.Text; using System.Threading; using System.Windows.Forms; - -using MySql.Data.MySqlClient; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace Perun_v1 { public partial class form_Main : Form { - // Variable definitions - public Thread thread_UDPListener; // Seperate thread for UDP - public class_UDPListener UDPListener; // Helper class for UDP comunication - public string[] LogHistory = new string[10]; // Log history for GUI - public string[] SendBuffer = new string[100]; // Mysql send buffer - public bool LetMeOut = false; // Helper to handle system tray - public string MySql_connStr; // MySQL connection string - public string publishVersion = "DEBUG"; // Helper for pulling version definition + public string[] arrSendBuffer = new string[100]; // Mysql send buffer + public bool boolLetMeOut = false; // Helper to handle system tray - public static void LogHistoryAdd(ref string[] LogHistory, string Comment) + // ################################ Main ################################ + private void form_Main_Load(object sender, EventArgs e) { - // Add to log history and rotate - for (int i = 0; i < LogHistory.Length - 1; i++) - { - LogHistory[i] = LogHistory[i + 1]; - } - LogHistory[9] = DateTime.Now.ToString("HH:mm:ss") + " > " + Comment; - } + // Form loaded - fill controls with default values - public void SendToMySql(string raw_udp_frame) - { - // Main function to send data to mysql - dynamic udp_frame = JsonConvert.DeserializeObject(raw_udp_frame); - - // Cut raw data to type and paylod for proper mysql insert - string type = udp_frame.type; - string payload = ""; - string timestamp = udp_frame.timestamp; - string sql = ""; - - if (timestamp != null) - { - timestamp = "'" + timestamp + "'"; - } - else - { - timestamp = "CURRENT_TIMESTAMP()"; - } - - // Modify specific - if (type == "1") // Inject app version information - { - udp_frame.payload["v_win"] = "v" + publishVersion; - } - - // Specific SQL - if (type == "50") - { - // Add entry to chat log - //sql = "INSERT IGNORE INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) VALUES ('" + udp_frame.payload.ucid + "');"; - sql = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + udp_frame.payload.ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where pe_DataPlayers_ucid = '" + udp_frame.payload.ucid + "' );"; - sql = sql + "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated` = " + timestamp + ",`pe_DataPlayers_lastname`='" + udp_frame.payload.player + "' WHERE `pe_DataPlayers_ucid`='"+ udp_frame.payload.ucid + "' ;"; - sql = sql + "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + udp_frame.payload.missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where pe_DataMissionHashes_hash ='" + udp_frame.payload.missionhash + "' );"; - sql = sql + "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + timestamp + " WHERE `pe_DataMissionHashes_hash` = '" + udp_frame.payload.missionhash + "';"; - sql = sql + "INSERT INTO `pe_LogChat` (`pe_LogChat_id`,`pe_LogChat_datetime`, `pe_LogChat_playerid`, `pe_LogChat_msg`, `pe_LogChat_all`,`pe_LogChat_missionhash_id`) VALUES (NULL,'" + udp_frame.payload.datetime + "', (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '"+ udp_frame.payload.ucid + "'), '" + udp_frame.payload.msg + "', '" + udp_frame.payload.all + "',(SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.missionhash + "'));"; - } - else if (type == "51") - { - // Add entry to event log - sql = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + udp_frame.payload.log_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where pe_DataMissionHashes_hash = '" + udp_frame.payload.log_missionhash + "');"; - sql = sql + "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + timestamp + " WHERE `pe_DataMissionHashes_hash` = '" + udp_frame.payload.log_missionhash + "';"; - sql = sql + "INSERT INTO `pe_LogEvent` (`pe_LogEvent_id`, `pe_LogEvent_datetime`, `pe_LogEvent_type`, `pe_LogEvent_content`,`pe_LogEvent_missionhash_id`) VALUES ( NULL, '" + udp_frame.payload.log_datetime + "', '" + udp_frame.payload.log_type + "', '" + udp_frame.payload.log_content + "', (SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.log_missionhash + "'));"; - } - else if (type == "52") - { - // Update user stats - payload = JsonConvert.SerializeObject(udp_frame.payload.stat_data); - - sql = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + udp_frame.payload.stat_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where pe_DataMissionHashes_hash = '" + udp_frame.payload.stat_missionhash + "');"; - sql = sql + "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + timestamp + " WHERE `pe_DataMissionHashes_hash` = '" + udp_frame.payload.stat_missionhash + "';"; - sql = sql + "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + udp_frame.payload.stat_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where pe_DataPlayers_ucid = '" + udp_frame.payload.stat_ucid + "');"; - sql = sql + "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated`=" + timestamp + " WHERE `pe_DataPlayers_ucid`='" + udp_frame.payload.stat_ucid + "';"; - sql = sql + "INSERT INTO `pe_LogStats` (`pe_LogStats_playerid`,`pe_LogStats_missionhash_id`) SELECT (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + udp_frame.payload.stat_ucid + "'), (SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.stat_missionhash + "') FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_LogStats` WHERE `pe_LogStats_missionhash_id`=(SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + udp_frame.payload.stat_ucid + "') );"; - sql = sql + "UPDATE `pe_LogStats` SET `pe_LogStats_datetime`='" + udp_frame.payload.stat_datetime + "',`pe_LogStats_playerid` = (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + udp_frame.payload.stat_ucid + "'),`pe_LogStats_debug`=JSON_QUOTE('" + payload + "'),`pe_LogStats_missionhash_id`=(SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.stat_missionhash + "') WHERE `pe_LogStats_missionhash_id`=(SELECT pe_DataMissionHashes_id FROM pe_DataMissionHashes WHERE pe_DataMissionHashes_hash = '" + udp_frame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + udp_frame.payload.stat_ucid + "');"; - } - else if (type == "53") - { - // User logged in to DCS server - payload = JsonConvert.SerializeObject(udp_frame.payload.stat_data); - - sql = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + udp_frame.payload.login_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where pe_DataPlayers_ucid='" + udp_frame.payload.login_ucid + "');"; - sql = sql + "UPDATE `pe_DataPlayers` SET pe_DataPlayers_lastip='" + udp_frame.payload.login_ipaddr + "', pe_DataPlayers_lastname='" + udp_frame.payload.login_name + "',pe_DataPlayers_updated='" + udp_frame.payload.login_datetime + "' WHERE `pe_DataPlayers_ucid`= '" + udp_frame.payload.login_ucid + "';"; - sql = sql + "INSERT INTO `pe_LogLogins` (`pe_LogLogins_datetime`, `pe_LogLogins_playerid`, `pe_LogLogins_name`, `pe_LogLogins_ip`) VALUES ('" + udp_frame.payload.login_datetime + "', (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + udp_frame.payload.login_ucid + "'), '" + udp_frame.payload.login_name + "', '" + udp_frame.payload.login_ipaddr + "');"; - } - else - { - // General definition used for 1-10 packets - payload = JsonConvert.SerializeObject(udp_frame.payload); - sql = "INSERT INTO `pe_DataRaw` (`pe_dataraw_type`) SELECT '" + type + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataRaw` WHERE pe_dataraw_type = '" + type + "' );"; - sql = sql + "UPDATE `pe_DataRaw` SET `pe_dataraw_payload` = JSON_QUOTE('" + payload + "'), `pe_dataraw_updated`="+ timestamp + " WHERE `pe_dataraw_type`=" + type + ";"; - } - - // Connect to mysql and execute sql - MySqlConnection conn = new MySqlConnection(MySql_connStr); - try - { - Console.WriteLine("Sending data to MySQL - Begin"); - conn.Open(); - - MySqlCommand cmd = new MySqlCommand(sql, conn); - MySqlDataReader rdr = cmd.ExecuteReader(); - - while (rdr.Read()) - { - Console.WriteLine(rdr[0] + " -- " + rdr[1]); - } - rdr.Close(); - LogHistoryAdd(ref LogHistory, "MySQL updated, package type: " + type); - } - catch (ArgumentException a_ex) - { - Console.WriteLine(a_ex.ToString()); - LogHistoryAdd(ref LogHistory, "ERROR MySQL - package type " + type); - } - catch (MySqlException ex) - { - LogHistoryAdd(ref LogHistory, "ERROR MySQL - package type " + type); - switch (ex.Number) - { - case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port) - LogHistoryAdd(ref LogHistory, "ERROR MySQL - unable to connect"); - break; - case 0: // Access denied (Check DB name,username,password) - LogHistoryAdd(ref LogHistory, "ERROR MySQL - access denied"); - break; - default: - LogHistoryAdd(ref LogHistory, "ERROR MySQL - " + ex.Number); - break; - } - } - conn.Close(); - Console.WriteLine("Sending data to MySQL - Done"); - } - - - - public class class_UDPListener - { - // Main class for UDP listener - int listenPort; // Port to listen at - public bool done; // Helper to exit main loop without killing thread - public UdpClient listener; // Listener object - public string[] LogHistory; // Log history for GUI - public string[] SendBuffer; // Mysql send buffer - - public class_UDPListener(int port, ref string[] LogHistory, ref string[] SendBuffer) - { - // Create clas - NOTE that there is reference passing - this.listenPort = port; - this.LogHistory = LogHistory; - this.SendBuffer = SendBuffer; - } - - public void StartListen() - { - Console.WriteLine("UDP Listen start"); - try - { - // Start listening to UDP - this.done = false; - listener = new UdpClient(listenPort); - IPEndPoint groupEP = new IPEndPoint(IPAddress.Loopback, listenPort); - string received_data; - byte[] receive_byte_array; - - - while (!done) - { - Console.WriteLine("UDP: Waiting for packet"); - receive_byte_array = listener.Receive(ref groupEP); - received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length); - - Console.WriteLine("Sender: {0} Payload: {1}", groupEP.ToString(), received_data); - - // Add to log history and rotate - dynamic udp_frame = JsonConvert.DeserializeObject(received_data); - string type = udp_frame.type; - LogHistoryAdd(ref LogHistory, "UDP packet received, type: " + type); - - // Add to mySQL send buffer and rotate - for (int i = 0; i < SendBuffer.Length - 1; i++) - { - if (SendBuffer[i]==null) - { - SendBuffer[i] = received_data; - break; - } - - } - - } - listener.Close(); - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - LogHistoryAdd(ref LogHistory, "ERROR UDP - port may be in use"); - } - Console.WriteLine("UDP listen stop"); - - } + Globals.arrLogHistory[0] = DateTime.Now.ToString("HH:mm:ss") + " > " + "Perun started"; + form_Main_LoadSettings(); // Load settings + this.Text = PerunHelper.GetAppVersion(this.Text + " - "); // Display build version in title bar } public form_Main() @@ -225,12 +28,10 @@ namespace Perun_v1 InitializeComponent(); } - private void form_Main_Load(object sender, EventArgs e) + // ################################ Helpers ################################ + private void form_Main_LoadSettings() { - // Form loaded - fill controls with default values - LogHistory[0] = DateTime.Now.ToString("HH:mm:ss") + " > " + "Perun loaded"; - - // Load settings + // Loads settings con_txt_mysql_database.Text = Properties.Settings.Default.MYSQL_DB; con_txt_mysql_username.Text = Properties.Settings.Default.MYSQL_User; con_txt_mysql_password.Text = Properties.Settings.Default.MYSQL_Password; @@ -240,37 +41,11 @@ namespace Perun_v1 con_txt_3rd_srs.Text = Properties.Settings.Default.OTHER_SRS_FILE; con_check_3rd_lotatc.Checked = Properties.Settings.Default.OTHER_LOTATC_USE; con_check_3rd_srs.Checked = Properties.Settings.Default.OTHER_SRS_USE; - - // Version to title header - if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) - { - System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment; - publishVersion = cd.CurrentVersion.ToString(); - } - this.Text = this.Text + " - v" + publishVersion; } - private void con_Button_Listen_ON_Click(object sender, EventArgs e) + private void form_Main_SaveSettings() { - // Start listening - UDPListener = new class_UDPListener(48620, ref LogHistory, ref SendBuffer); - thread_UDPListener = new Thread(UDPListener.StartListen); - thread_UDPListener.Start(); - - // Update form controls - con_Button_Listen_ON.Enabled = false; - con_Button_Listen_OFF.Enabled = true; - con_txt_mysql_database.Enabled = false; - con_txt_mysql_username.Enabled = false; - con_txt_mysql_password.Enabled = false; - con_txt_mysql_server.Enabled = false; - con_txt_mysql_port.Enabled = false; - con_txt_3rd_lotatc.Enabled = false; - con_txt_3rd_srs.Enabled = false; - con_check_3rd_lotatc.Enabled = false; - con_check_3rd_srs.Enabled = false; - - // Save settings + // Saves settings Properties.Settings.Default.MYSQL_Server = con_txt_mysql_database.Text; Properties.Settings.Default.MYSQL_DB = con_txt_mysql_database.Text; Properties.Settings.Default.MYSQL_User = con_txt_mysql_username.Text; @@ -283,14 +58,61 @@ namespace Perun_v1 Properties.Settings.Default.OTHER_LOTATC_USE = con_check_3rd_lotatc.Checked; Properties.Settings.Default.OTHER_SRS_USE = con_check_3rd_srs.Checked; + Properties.Settings.Default.Save(); + } + + private void form_Main_DisableControls() + { + // Disables controls + con_Button_Listen_ON.Enabled = false; + con_Button_Listen_OFF.Enabled = true; + con_txt_mysql_database.Enabled = false; + con_txt_mysql_username.Enabled = false; + con_txt_mysql_password.Enabled = false; + con_txt_mysql_server.Enabled = false; + con_txt_mysql_port.Enabled = false; + con_txt_3rd_lotatc.Enabled = false; + con_txt_3rd_srs.Enabled = false; + con_check_3rd_lotatc.Enabled = false; + con_check_3rd_srs.Enabled = false; + } + + private void form_Main_EnableControls() + { + // Enables controls + con_Button_Listen_ON.Enabled = true; + con_Button_Listen_OFF.Enabled = false; + + con_txt_mysql_database.Enabled = true; + con_txt_mysql_username.Enabled = true; + con_txt_mysql_password.Enabled = true; + //con_txt_mysql_port.Enabled = true; // TBD - protect against writting text in the port field + con_txt_mysql_server.Enabled = true; + con_txt_3rd_lotatc.Enabled = true; + con_txt_3rd_srs.Enabled = true; + con_check_3rd_lotatc.Enabled = true; + con_check_3rd_srs.Enabled = true; + } + + // ################################ User input ################################ + private void con_Button_Listen_ON_Click(object sender, EventArgs e) + { + // Start listening + UDPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer); + UDPController.thrUDPListener = new Thread(UDPController.StartListen); + UDPController.thrUDPListener.Start(); + UDPController.thrUDPListener.Name = "UDPThread"; + + form_Main_DisableControls(); // Disable controlls + form_Main_SaveSettings(); // Save settings + // Prepare connection string - MySql_connStr = "server=" + con_txt_mysql_server.Text + ";user=" + con_txt_mysql_username.Text + ";database=" + con_txt_mysql_database.Text + ";port=" + con_txt_mysql_port.Text + ";password=" + con_txt_mysql_password.Text; + DatabaseController.strMySQLConnectionString = "server=" + con_txt_mysql_server.Text + ";user=" + con_txt_mysql_username.Text + ";database=" + con_txt_mysql_database.Text + ";port=" + con_txt_mysql_port.Text + ";password=" + con_txt_mysql_password.Text; // Start timmers + tim_200ms.Enabled = true; tim_1000ms.Enabled = true; tim_10000ms.Enabled = true; - tim_200ms.Enabled = true; - } private void con_Button_Listen_OFF_Click(object sender, EventArgs e) @@ -298,25 +120,14 @@ namespace Perun_v1 // Stop listening try { - UDPListener.listener.Close(); + UDPController.StopListen(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } - // Update form controls - con_Button_Listen_ON.Enabled = true; - con_Button_Listen_OFF.Enabled = false; - con_txt_mysql_database.Enabled = true; - con_txt_mysql_username.Enabled = true; - con_txt_mysql_password.Enabled = true; - //con_txt_mysql_port.Enabled = true; - con_txt_mysql_server.Enabled = true; - con_txt_3rd_lotatc.Enabled = true; - con_txt_3rd_srs.Enabled = true; - con_check_3rd_lotatc.Enabled = true; - con_check_3rd_srs.Enabled = true; + form_Main_EnableControls(); // Enable controls // Stop timmers tim_1000ms.Enabled = false; @@ -324,21 +135,6 @@ namespace Perun_v1 tim_200ms.Enabled = false; } - private void timer1_Tick(object sender, EventArgs e) - { - // Main timer to sync GUI with background tasks and flush buffers - // Refresh Log Window - con_List_Received.Items.Clear(); - foreach (string i in LogHistory) - { - if (i != null) - { - con_List_Received.Items.Add(i); - } - } - - } - private void con_lab_github_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // Open default browser with link to Perun repo @@ -346,60 +142,6 @@ namespace Perun_v1 Process.Start(sInfo); } - private void con_Button_Quit_Click(object sender, EventArgs e) - { - // Close app - - DialogResult dialogResult = MessageBox.Show("Are you sure to exit Perun?", "Question", MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question); - if (dialogResult == DialogResult.Yes) - { - // Save settings on exit - Properties.Settings.Default.MYSQL_Server = con_txt_mysql_database.Text; - Properties.Settings.Default.MYSQL_DB = con_txt_mysql_database.Text; - Properties.Settings.Default.MYSQL_User = con_txt_mysql_username.Text; - Properties.Settings.Default.MYSQL_Password = con_txt_mysql_password.Text; - Properties.Settings.Default.MYSQL_Port = con_txt_mysql_port.Text; - Properties.Settings.Default.MYSQL_Server = con_txt_mysql_server.Text; - Properties.Settings.Default.MYSQL_Port = con_txt_mysql_port.Text; - Properties.Settings.Default.OTHER_LOTATC_FILE = con_txt_3rd_lotatc.Text; - Properties.Settings.Default.OTHER_SRS_FILE = con_txt_3rd_srs.Text; - Properties.Settings.Default.OTHER_LOTATC_USE = con_check_3rd_lotatc.Checked; - Properties.Settings.Default.OTHER_SRS_USE = con_check_3rd_srs.Checked; - - Properties.Settings.Default.Save(); - - LetMeOut = true; - this.Close(); - } - else if (dialogResult == DialogResult.No) - { - //do nothing - } - } - - private void form_Main_FormClosing(object sender, FormClosingEventArgs e) - { - // Minimize to try - if (e.CloseReason == CloseReason.UserClosing && !LetMeOut) - { - e.Cancel = true; - trayIconMain.Visible = true; - this.WindowState = FormWindowState.Minimized; - this.ShowInTaskbar = false; - //this.Hide(); - - } - } - - private void trayIconMain_MouseDoubleClick(object sender, MouseEventArgs e) - { - // Maximize from try - trayIconMain.Visible = false; - this.WindowState = FormWindowState.Normal; - this.ShowInTaskbar = true; - - } - private void con_txt_3rd_srs_Click(object sender, MouseEventArgs e) { // Chose SRS Json file @@ -430,14 +172,114 @@ namespace Perun_v1 } } + // ################################ Form state ################################ + private void con_Button_Quit_Click(object sender, EventArgs e) + { + // Close app + DialogResult dialogResult = MessageBox.Show("Are you sure to exit Perun?", "Question", MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question); + if (dialogResult == DialogResult.Yes) + { + form_Main_SaveSettings(); + + boolLetMeOut = true; // Save settings on exit + this.Close(); // Allow to exit application + } + else if (dialogResult == DialogResult.No) + { + //do nothing + } + } + + protected override void WndProc(ref Message m) + { + // Try to run of 2nd instance + if (m.Msg == NativeMethods.WM_SHOWME) + { + form_Main_BringFromTray(); + } + base.WndProc(ref m); + } + + private void form_Main_SendToTray() + { + // Sends app to system tray + trayIconMain.Visible = true; + this.WindowState = FormWindowState.Minimized; + this.ShowInTaskbar = false; + } + + private void form_Main_BringFromTray() + { + // Sends app from system tray + trayIconMain.Visible = false; + this.WindowState = FormWindowState.Normal; + this.ShowInTaskbar = true; + + if (WindowState == FormWindowState.Minimized) + { + WindowState = FormWindowState.Normal; + } + // get our current "TopMost" value (ours will always be false though) + bool top = TopMost; + // make our form jump to the top of everything + TopMost = true; + // set it back to whatever it was + TopMost = top; + } + + + private void form_Main_FormClosing(object sender, FormClosingEventArgs e) + { + // Minimize to try on clicking "X" + if (e.CloseReason == CloseReason.UserClosing && !boolLetMeOut) + { + e.Cancel = true; + form_Main_SendToTray(); // Send app to system tray + } + } + + private void trayIconMain_MouseDoubleClick(object sender, MouseEventArgs e) + { + // Maximize from tray + form_Main_BringFromTray(); // Bring app from system tray + } + + // ################################ Timers ################################ + private void timer1_Tick(object sender, EventArgs e) + { + // Main timer to sync GUI with background tasks and flush buffers + // Refresh Log Window + con_List_Received.Items.Clear(); + foreach (string i in Globals.arrLogHistory) + { + if (i != null) + { + con_List_Received.Items.Add(i); + } + } + } + + private void tim_200ms_Tick(object sender, EventArgs e) + { + // Send buffer to MySQL + for (int i = 0; i < arrSendBuffer.Length - 1; i++) + { + if (arrSendBuffer[i] != null) + { + DatabaseController.SendToMySql(arrSendBuffer[i]); + arrSendBuffer[i] = null; + } + } + } + private void tim_10000ms_Tick(object sender, EventArgs e) { // Main timer to send JSON files to MySQL string strSRSJson = ""; string strLotATCJson = ""; - bool SRSdefault = true; - bool LotATCdefault = true; + bool boolSRSdefault = true; + bool boolLotATCdefault = true; // Handle SRS if (con_check_3rd_srs.Checked) @@ -485,24 +327,24 @@ namespace Perun_v1 } else { - strSRSJson = "{'type':'100','payload':{'ignore':'false'}}"; + strSRSJson = "{'type':'100','payload':{'ignore':'false'}}"; // No SRS clients connected } - SRSdefault = false; - LogHistoryAdd(ref LogHistory, "SRS data loaded"); + boolSRSdefault = false; + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "SRS data loaded"); } catch { - LogHistoryAdd(ref LogHistory, "SRS data ERROR"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "SRS data ERROR"); } } - if (SRSdefault) + if (boolSRSdefault) { strSRSJson = "{'type':'100','payload':{'ignore':'true'}}"; } - SendToMySql(strSRSJson); + DatabaseController.SendToMySql(strSRSJson); // Handle LotATC if (con_check_3rd_lotatc.Checked) @@ -513,58 +355,22 @@ namespace Perun_v1 dynamic raw_srs = JsonConvert.DeserializeObject(strLotATCJson); strLotATCJson = "{'type':'101','payload':'" + strLotATCJson + "'}"; - LotATCdefault = false; - LogHistoryAdd(ref LogHistory, "LotATC data loaded"); + boolLotATCdefault = false; + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "LotATC data loaded"); } catch { - LogHistoryAdd(ref LogHistory, "LotATC data ERROR"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "LotATC data ERROR"); } } - if (LotATCdefault) + if (boolLotATCdefault) { - strLotATCJson = "{'type':'101','payload':{'ignore':'true'}}"; + strLotATCJson = "{'type':'101','payload':{'ignore':'true'}}"; // No LotATC controller connected } - SendToMySql(strLotATCJson); - - } - - private void tim_200ms_Tick(object sender, EventArgs e) - { - // Send buffer to MySQL - for (int i = 0; i < SendBuffer.Length - 1; i++) - { - if (SendBuffer[i] != null) - { - SendToMySql(SendBuffer[i]); - SendBuffer[i] = null; - } - } - } - - protected override void WndProc(ref Message m) - { - if (m.Msg == NativeMethods.WM_SHOWME) - { - ShowMe(); - } - base.WndProc(ref m); - } - private void ShowMe() - { - if (WindowState == FormWindowState.Minimized) - { - WindowState = FormWindowState.Normal; - } - // get our current "TopMost" value (ours will always be false though) - bool top = TopMost; - // make our form jump to the top of everything - TopMost = true; - // set it back to whatever it was - TopMost = top; + DatabaseController.SendToMySql(strLotATCJson); } } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.resx b/02_Windows_App/Perun_v1/02_Forms/form_Main.resx index 1ce1382..3a3403e 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.resx +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.resx @@ -419,6 +419,9 @@ 687, 19 + + 797, 19 + AAABAAEAQD8AAAEAIAAgQQAAFgAAACgAAABAAAAAfgAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA diff --git a/02_Windows_App/Perun_v1/Perun_v1.csproj b/02_Windows_App/Perun_v1/Perun_v1.csproj index bcda4e7..d204ff0 100644 --- a/02_Windows_App/Perun_v1/Perun_v1.csproj +++ b/02_Windows_App/Perun_v1/Perun_v1.csproj @@ -116,6 +116,10 @@ + + + + Form