Working mysql connection icon

This commit is contained in:
szporowolik
2019-09-13 13:31:03 +02:00
parent ddb2527eb8
commit 238f6e29ae
6 changed files with 76 additions and 53 deletions

View File

@@ -3,11 +3,13 @@ using MySql.Data.MySqlClient;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
internal class DatabaseController public class DatabaseController
{ {
public static string strMySQLConnectionString; // MySQL connection string public string strMySQLConnectionString; // MySQL connection string
public MySqlConnection connMySQL;
public bool bStatus;
public static void SendToMySql(string strRawUDPFrame) public void SendToMySql(string strRawUDPFrame)
{ {
// Main function to send data to mysql // Main function to send data to mysql
dynamic strUDPFrame = JsonConvert.DeserializeObject(strRawUDPFrame); // Deserialize raw data dynamic strUDPFrame = JsonConvert.DeserializeObject(strRawUDPFrame); // Deserialize raw data
@@ -84,12 +86,12 @@ internal class DatabaseController
} }
// Connect to mysql and execute sql // Connect to mysql and execute sql
MySqlConnection connMySQL = new MySqlConnection(DatabaseController.strMySQLConnectionString); connMySQL = new MySqlConnection(strMySQLConnectionString);
try try
{ {
Console.WriteLine("Sending data to MySQL - Begin"); Console.WriteLine("Sending data to MySQL - Begin");
connMySQL.Open(); connMySQL.Open();
bStatus = true;
MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL); MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL);
MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader(); MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader();
@@ -105,6 +107,7 @@ internal class DatabaseController
// General exception found // General exception found
Console.WriteLine(a_ex.ToString()); Console.WriteLine(a_ex.ToString());
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
bStatus = false;
} }
catch (MySqlException m_ex) catch (MySqlException m_ex)
{ {
@@ -122,6 +125,7 @@ internal class DatabaseController
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number);
break; break;
} }
bStatus = false;
} }
connMySQL.Close(); connMySQL.Close();
Console.WriteLine("Sending data to MySQL - Done"); Console.WriteLine("Sending data to MySQL - Done");

View File

@@ -1,7 +1,10 @@
// This class gathers all global variables // This class gathers all global variable
using MySql.Data.MySqlClient;
internal class Globals internal class Globals
{ {
public static string strPerunVersion = "DEBUG"; // Helper for pulling version definition public static string strPerunVersion = "DEBUG"; // Helper for pulling version definition
public static string[] arrLogHistory = new string[10]; // Log history for GUI public static string[] arrLogHistory = new string[10]; // Log history for GUI
} }

View File

@@ -46,12 +46,9 @@ internal class TCPController
{ {
try try
{ {
// Start listening to UDP // Start listening to TCP
tcpServer = new TcpListener(IPAddress.Any, intListenPort); tcpServer = new TcpListener(IPAddress.Any, intListenPort);
string strReceivedData; string strReceivedData;
byte[] arrReceiveByteArray;
// Start the main loop // Start the main loop
TCPController.boolDone = false; TCPController.boolDone = false;

View File

@@ -65,18 +65,18 @@
this.con_txt_dcs_instance = new System.Windows.Forms.MaskedTextBox(); this.con_txt_dcs_instance = new System.Windows.Forms.MaskedTextBox();
this.label7 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label();
this.con_txt_dcs_server_port = new System.Windows.Forms.MaskedTextBox(); this.con_txt_dcs_server_port = new System.Windows.Forms.MaskedTextBox();
this.con_img_db = new System.Windows.Forms.PictureBox();
this.con_img_dcs = new System.Windows.Forms.PictureBox();
this.con_img_srs = new System.Windows.Forms.PictureBox();
this.con_img_lotATC = new System.Windows.Forms.PictureBox(); this.con_img_lotATC = new System.Windows.Forms.PictureBox();
this.con_img_srs = new System.Windows.Forms.PictureBox();
this.con_img_dcs = new System.Windows.Forms.PictureBox();
this.con_img_db = new System.Windows.Forms.PictureBox();
this.con_GroupBox_1.SuspendLayout(); this.con_GroupBox_1.SuspendLayout();
this.con_GroupBox_2.SuspendLayout(); this.con_GroupBox_2.SuspendLayout();
this.con_GroupBox_3.SuspendLayout(); this.con_GroupBox_3.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.con_img_db)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_dcs)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_srs)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_lotATC)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.con_img_lotATC)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_srs)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_dcs)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_db)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// con_List_Received // con_List_Received
@@ -383,37 +383,42 @@
this.con_txt_dcs_server_port.Size = new System.Drawing.Size(207, 20); this.con_txt_dcs_server_port.Size = new System.Drawing.Size(207, 20);
this.con_txt_dcs_server_port.TabIndex = 3; this.con_txt_dcs_server_port.TabIndex = 3;
// //
// con_img_db // con_img_lotATC
// //
this.con_img_db.Location = new System.Drawing.Point(12, 335); this.con_img_lotATC.Location = new System.Drawing.Point(307, 335);
this.con_img_db.Name = "con_img_db"; this.con_img_lotATC.Name = "con_img_lotATC";
this.con_img_db.Size = new System.Drawing.Size(29, 28); this.con_img_lotATC.Size = new System.Drawing.Size(29, 28);
this.con_img_db.TabIndex = 10; this.con_img_lotATC.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.con_img_db.TabStop = false; this.con_img_lotATC.TabIndex = 13;
// this.con_img_lotATC.TabStop = false;
// con_img_dcs
//
this.con_img_dcs.Location = new System.Drawing.Point(47, 335);
this.con_img_dcs.Name = "con_img_dcs";
this.con_img_dcs.Size = new System.Drawing.Size(29, 28);
this.con_img_dcs.TabIndex = 11;
this.con_img_dcs.TabStop = false;
// //
// con_img_srs // con_img_srs
// //
this.con_img_srs.Location = new System.Drawing.Point(82, 335); this.con_img_srs.Location = new System.Drawing.Point(272, 335);
this.con_img_srs.Name = "con_img_srs"; this.con_img_srs.Name = "con_img_srs";
this.con_img_srs.Size = new System.Drawing.Size(29, 28); this.con_img_srs.Size = new System.Drawing.Size(29, 28);
this.con_img_srs.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.con_img_srs.TabIndex = 12; this.con_img_srs.TabIndex = 12;
this.con_img_srs.TabStop = false; this.con_img_srs.TabStop = false;
// //
// con_img_lotATC // con_img_dcs
// //
this.con_img_lotATC.Location = new System.Drawing.Point(117, 335); this.con_img_dcs.Location = new System.Drawing.Point(237, 335);
this.con_img_lotATC.Name = "con_img_lotATC"; this.con_img_dcs.Name = "con_img_dcs";
this.con_img_lotATC.Size = new System.Drawing.Size(29, 28); this.con_img_dcs.Size = new System.Drawing.Size(29, 28);
this.con_img_lotATC.TabIndex = 13; this.con_img_dcs.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.con_img_lotATC.TabStop = false; this.con_img_dcs.TabIndex = 11;
this.con_img_dcs.TabStop = false;
//
// con_img_db
//
this.con_img_db.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.con_img_db.Location = new System.Drawing.Point(202, 335);
this.con_img_db.Name = "con_img_db";
this.con_img_db.Size = new System.Drawing.Size(29, 28);
this.con_img_db.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.con_img_db.TabIndex = 10;
this.con_img_db.TabStop = false;
// //
// form_Main // form_Main
// //
@@ -449,10 +454,10 @@
this.con_GroupBox_3.PerformLayout(); this.con_GroupBox_3.PerformLayout();
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.con_img_db)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_dcs)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_srs)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_lotATC)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.con_img_lotATC)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_srs)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_dcs)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.con_img_db)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -11,6 +12,7 @@ namespace Perun_v1
// Variable definitions // Variable definitions
public string[] arrSendBuffer = new string[65534]; // Mysql send buffer public string[] arrSendBuffer = new string[65534]; // Mysql send buffer
public bool boolLetMeOut = false; // Helper to handle system tray public bool boolLetMeOut = false; // Helper to handle system tray
public DatabaseController dbConnection = new DatabaseController();
// ################################ Main ################################ // ################################ Main ################################
private void form_Main_Load(object sender, EventArgs e) private void form_Main_Load(object sender, EventArgs e)
@@ -94,7 +96,7 @@ namespace Perun_v1
con_txt_mysql_database.Enabled = true; con_txt_mysql_database.Enabled = true;
con_txt_mysql_username.Enabled = true; con_txt_mysql_username.Enabled = true;
con_txt_mysql_password.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_port.Enabled = true;
con_txt_mysql_server.Enabled = true; con_txt_mysql_server.Enabled = true;
con_txt_3rd_lotatc.Enabled = true; con_txt_3rd_lotatc.Enabled = true;
con_txt_3rd_srs.Enabled = true; con_txt_3rd_srs.Enabled = true;
@@ -109,12 +111,7 @@ namespace Perun_v1
private void con_Button_Listen_ON_Click(object sender, EventArgs e) private void con_Button_Listen_ON_Click(object sender, EventArgs e)
{ {
// Start listening // Start listening
//UDPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer); TCPController.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer);
//UDPController.thrUDPListener = new Thread(UDPController.StartListen);
//UDPController.thrUDPListener.Start();
//UDPController.thrUDPListener.Name = "UDPThread";
TCPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer);
TCPController.thrTCPListener = new Thread(TCPController.StartListen); TCPController.thrTCPListener = new Thread(TCPController.StartListen);
TCPController.thrTCPListener.Start(); TCPController.thrTCPListener.Start();
TCPController.thrTCPListener.Name = "TCPThread"; TCPController.thrTCPListener.Name = "TCPThread";
@@ -123,7 +120,7 @@ namespace Perun_v1
form_Main_SaveSettings(); // Save settings form_Main_SaveSettings(); // Save settings
// Prepare connection string // Prepare connection string
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; dbConnection.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 // Start timmers
tim_200ms.Enabled = true; tim_200ms.Enabled = true;
@@ -136,7 +133,6 @@ namespace Perun_v1
// Stop listening // Stop listening
try try
{ {
//UDPController.StopListen();
TCPController.StopListen(); TCPController.StopListen();
} }
catch (Exception ex) catch (Exception ex)
@@ -283,10 +279,22 @@ namespace Perun_v1
{ {
if (arrSendBuffer[i] != null) if (arrSendBuffer[i] != null)
{ {
DatabaseController.SendToMySql(arrSendBuffer[i]); dbConnection.SendToMySql(arrSendBuffer[i]);
arrSendBuffer[i] = null; arrSendBuffer[i] = null;
} }
} }
// Update icons
if (dbConnection.bStatus)
{
con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_db");
} else
{
con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error");
}
con_img_dcs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_game");
con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_srs");
con_img_lotATC.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_lotatc");
} }
private void tim_10000ms_Tick(object sender, EventArgs e) private void tim_10000ms_Tick(object sender, EventArgs e)
@@ -361,7 +369,7 @@ namespace Perun_v1
{ {
strSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}"; strSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}";
} }
DatabaseController.SendToMySql(strSRSJson); dbConnection.SendToMySql(strSRSJson);
// Handle LotATC - TBD clean code for multiinstance // Handle LotATC - TBD clean code for multiinstance
if (con_check_3rd_lotatc.Checked) if (con_check_3rd_lotatc.Checked)
@@ -386,7 +394,7 @@ namespace Perun_v1
{ {
strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}"; // No LotATC controller connected strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}"; // No LotATC controller connected
} }
DatabaseController.SendToMySql(strLotATCJson); dbConnection.SendToMySql(strLotATCJson);
} }
} }
} }

View File

@@ -181,6 +181,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="03_Resources\perun_logo.ico" /> <Content Include="03_Resources\perun_logo.ico" />
<None Include="Resources\ico_error.png" />
<None Include="Resources\ico_srs.png" />
<None Include="Resources\ico_lotatc.png" />
<None Include="Resources\ico_game.png" />
<None Include="Resources\ico_db.png" />
<None Include="Resources\img_db.png" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />