General cleanup

This commit is contained in:
szporowolik
2019-09-13 15:13:12 +02:00
parent 238f6e29ae
commit 9d42ccc1d4
6 changed files with 165 additions and 111 deletions

View File

@@ -16,9 +16,9 @@ public class DatabaseController
string strUDPFrameType = strUDPFrame.type; string strUDPFrameType = strUDPFrame.type;
string strUDPFrameTimestamp = strUDPFrame.timestamp; string strUDPFrameTimestamp = strUDPFrame.timestamp;
string strUDPFrameInstance = strUDPFrame.instance; string strUDPFrameInstance = strUDPFrame.instance;
string strUDPFramePayload = ""; string strUDPFramePayload;
string strUDPFramePayload_Perun = ""; string strUDPFramePayload_Perun;
string strSQLQueryTxt = ""; string strSQLQueryTxt;
// Some frames may come without timestamp, use database currrent timestampe then // Some frames may come without timestamp, use database currrent timestampe then
@@ -86,7 +86,10 @@ public class DatabaseController
} }
// Connect to mysql and execute sql // Connect to mysql and execute sql
try
{
connMySQL = new MySqlConnection(strMySQLConnectionString); connMySQL = new MySqlConnection(strMySQLConnectionString);
try try
{ {
Console.WriteLine("Sending data to MySQL - Begin"); Console.WriteLine("Sending data to MySQL - Begin");
@@ -100,7 +103,7 @@ public class DatabaseController
Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]); Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]);
} }
rdrMySQL.Close(); rdrMySQL.Close();
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#"+strUDPFrameInstance + " > MySQL updated, package type: " + strUDPFrameType); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > MySQL updated, package type: " + strUDPFrameType);
} }
catch (ArgumentException a_ex) catch (ArgumentException a_ex)
{ {
@@ -130,4 +133,11 @@ public class DatabaseController
connMySQL.Close(); connMySQL.Close();
Console.WriteLine("Sending data to MySQL - Done"); Console.WriteLine("Sending data to MySQL - Done");
} }
catch (ArgumentException)
{
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - wrong connection parameters");
}
}
} }

View File

@@ -17,7 +17,6 @@ namespace Perun_v1
static void Main() static void Main()
{ {
// Only instance
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit); Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

View File

@@ -7,67 +7,63 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
internal class TCPController public class TCPController
{ {
// Main class for TCP listener // Main class for TCP listener
public static int intListenPort; // Port to connect to public int intListenPort; // Port to connect to
public static bool boolDone; // Helper to exit main loop without killing thread public bool bDone; // Helper to exit main loop without killing thread
public static TcpListener tcpServer; // Listener object public string[] arrLogHistory; // Log history for GUI
public static string[] arrLogHistory; // Log history for GUI public string[] arrSendBuffer; // Mysql send buffer
public static string[] arrSendBuffer; // Mysql send buffer public Thread thrTCPListener; // Seperate thread for TCP
public static Thread thrTCPListener; // Seperate thread for UDP public bool bStatus; // TCP connecion status
public static void Create(int intListenPort, ref string[] arrLogHistory, ref string[] arrSendBuffer) public void Create(int par_intListenPort, ref string[] par_arrLogHistory, ref string[] par_arrSendBuffer)
{ {
// Create class // Create class
TCPController.intListenPort = intListenPort; intListenPort = par_intListenPort;
TCPController.arrLogHistory = arrLogHistory; arrLogHistory = par_arrLogHistory;
TCPController.arrSendBuffer = arrSendBuffer; arrSendBuffer = par_arrSendBuffer;
bDone = false;
bStatus = false;
} }
public static void StopListen() public void StopListen()
{ {
// FInish listening // Finish listening
TCPController.boolDone = true; bDone = true;
TCPController.tcpServer.Stop(); bStatus = false;
TCPController.tcpServer = null;
for (int i = 0; i < arrSendBuffer.Length - 1; i++) for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{ {
arrSendBuffer[i] = null; arrSendBuffer[i] = null;
} }
} }
public static void StartListen() public void StartListen()
{
while (!bDone)
{ {
Console.WriteLine("TCP Listen start"); Console.WriteLine("TCP Listen start");
while (!TCPController.boolDone) TcpListener tcpServer = new TcpListener(IPAddress.Any, intListenPort); ; // Listener object
{
try try
{ {
// Start listening to TCP // Start listening to TCP
tcpServer = new TcpListener(IPAddress.Any, intListenPort);
string strReceivedData; string strReceivedData;
// Start the main loop // Start the main loop
TCPController.boolDone = false;
tcpServer.Start(); tcpServer.Start();
while (!TCPController.boolDone) bStatus = true;
while (!bDone)
{ {
// Start listening // Start listening
Console.WriteLine("TCP: Waiting for packet"); Console.WriteLine("TCP: Waiting for packet");
TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
//tcpClient.ReceiveBufferSize = 65534;
while (tcpClient.Connected && !TCPController.boolDone) //while the client is connected, we look for incoming messages while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
{ {
//arrReceiveByteArray = new byte[tcpClient.ReceiveBufferSize]; //the messages arrive as byte array
//ns.Read(arrReceiveByteArray, 0, arrReceiveByteArray.Length); //the same networkstream reads the message sent by the client
//strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length);
StringBuilder CompleteMessage = new StringBuilder(); StringBuilder CompleteMessage = new StringBuilder();
if (ns.CanRead) if (ns.CanRead)
@@ -87,6 +83,8 @@ internal class TCPController
strReceivedData = CompleteMessage.ToString(); strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
try
{
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type; string strRawTCPFrameType = dynamicRawTCPFrame.type;
@@ -102,9 +100,16 @@ internal class TCPController
} }
} }
} }
catch(Exception e)
{
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP ERROR incorrect JSON");
}
}
} }
tcpServer.Stop(); tcpServer.Stop();
bStatus = false;
} }
catch (Exception e) catch (Exception e)
{ {
@@ -114,8 +119,14 @@ internal class TCPController
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP error - connection closed or port in use"); PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP error - connection closed or port in use");
} }
} }
//Console.WriteLine("TCP listen stop"); if (!(tcpServer is null))
{
tcpServer.Stop();
}
bStatus = false;
Console.WriteLine("TCP listen stop");
} }
} }
} }

View File

@@ -96,7 +96,7 @@
this.con_Button_Listen_ON.Name = "con_Button_Listen_ON"; this.con_Button_Listen_ON.Name = "con_Button_Listen_ON";
this.con_Button_Listen_ON.Size = new System.Drawing.Size(86, 39); this.con_Button_Listen_ON.Size = new System.Drawing.Size(86, 39);
this.con_Button_Listen_ON.TabIndex = 2; this.con_Button_Listen_ON.TabIndex = 2;
this.con_Button_Listen_ON.Text = "Connect"; this.con_Button_Listen_ON.Text = "Start";
this.con_Button_Listen_ON.UseVisualStyleBackColor = true; this.con_Button_Listen_ON.UseVisualStyleBackColor = true;
this.con_Button_Listen_ON.Click += new System.EventHandler(this.con_Button_Listen_ON_Click); this.con_Button_Listen_ON.Click += new System.EventHandler(this.con_Button_Listen_ON_Click);
// //
@@ -107,7 +107,7 @@
this.con_Button_Listen_OFF.Name = "con_Button_Listen_OFF"; this.con_Button_Listen_OFF.Name = "con_Button_Listen_OFF";
this.con_Button_Listen_OFF.Size = new System.Drawing.Size(86, 39); this.con_Button_Listen_OFF.Size = new System.Drawing.Size(86, 39);
this.con_Button_Listen_OFF.TabIndex = 3; this.con_Button_Listen_OFF.TabIndex = 3;
this.con_Button_Listen_OFF.Text = "Disconnect"; this.con_Button_Listen_OFF.Text = "Stop";
this.con_Button_Listen_OFF.UseVisualStyleBackColor = true; this.con_Button_Listen_OFF.UseVisualStyleBackColor = true;
this.con_Button_Listen_OFF.Click += new System.EventHandler(this.con_Button_Listen_OFF_Click); this.con_Button_Listen_OFF.Click += new System.EventHandler(this.con_Button_Listen_OFF_Click);
// //
@@ -156,7 +156,6 @@
// //
// con_txt_mysql_port // con_txt_mysql_port
// //
this.con_txt_mysql_port.Enabled = false;
this.con_txt_mysql_port.Location = new System.Drawing.Point(106, 45); this.con_txt_mysql_port.Location = new System.Drawing.Point(106, 45);
this.con_txt_mysql_port.Name = "con_txt_mysql_port"; this.con_txt_mysql_port.Name = "con_txt_mysql_port";
this.con_txt_mysql_port.Size = new System.Drawing.Size(207, 20); this.con_txt_mysql_port.Size = new System.Drawing.Size(207, 20);

View File

@@ -10,15 +10,19 @@ namespace Perun_v1
public partial class form_Main : Form public partial class form_Main : Form
{ {
// 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 bLetMeOut = false; // Helper to handle system tray
public DatabaseController dbConnection = new DatabaseController();
public DatabaseController dcConnection = new DatabaseController(); // MySQL controller
public TCPController tcpcServer=new TCPController(); // TCP controller
public bool bSRSStatus; // Use empty/default SRS status
public bool bLotATCStatus; // Use empty/default LotATC status
// ################################ Main ################################ // ################################ Main ################################
private void form_Main_Load(object sender, EventArgs e) private void form_Main_Load(object sender, EventArgs e)
{ {
// Form loaded - fill controls with default values // Form loaded - fill controls with default values
Globals.arrLogHistory[0] = DateTime.Now.ToString("HH:mm:ss") + " > " + "Perun started"; Globals.arrLogHistory[0] = DateTime.Now.ToString("HH:mm:ss") + " > " + "Perun started";
form_Main_LoadSettings(); // Load settings form_Main_LoadSettings(); // Load settings
this.Text = PerunHelper.GetAppVersion(this.Text + " - "); // Display build version in title bar this.Text = PerunHelper.GetAppVersion(this.Text + " - "); // Display build version in title bar
@@ -62,7 +66,6 @@ namespace Perun_v1
Properties.Settings.Default.OTHER_SRS_FILE = con_txt_3rd_srs.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_LOTATC_USE = con_check_3rd_lotatc.Checked;
Properties.Settings.Default.OTHER_SRS_USE = con_check_3rd_srs.Checked; Properties.Settings.Default.OTHER_SRS_USE = con_check_3rd_srs.Checked;
Properties.Settings.Default.DCS_Server_Port = Int32.Parse(con_txt_dcs_server_port.Text); Properties.Settings.Default.DCS_Server_Port = Int32.Parse(con_txt_dcs_server_port.Text);
Properties.Settings.Default.DCS_Instance = Int32.Parse(con_txt_dcs_instance.Text); Properties.Settings.Default.DCS_Instance = Int32.Parse(con_txt_dcs_instance.Text);
@@ -92,7 +95,6 @@ namespace Perun_v1
// Enables controls // Enables controls
con_Button_Listen_ON.Enabled = true; con_Button_Listen_ON.Enabled = true;
con_Button_Listen_OFF.Enabled = false; con_Button_Listen_OFF.Enabled = false;
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;
@@ -104,28 +106,30 @@ namespace Perun_v1
con_check_3rd_srs.Enabled = true; con_check_3rd_srs.Enabled = true;
con_txt_dcs_server_port.Enabled = true; con_txt_dcs_server_port.Enabled = true;
con_txt_dcs_instance.Enabled = true; con_txt_dcs_instance.Enabled = true;
} }
// ################################ User input ################################ // ################################ User input ################################
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
TCPController.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer); tcpcServer.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer);
TCPController.thrTCPListener = new Thread(TCPController.StartListen); tcpcServer.thrTCPListener = new Thread(tcpcServer.StartListen);
TCPController.thrTCPListener.Start(); tcpcServer.thrTCPListener.Start();
TCPController.thrTCPListener.Name = "TCPThread"; tcpcServer.thrTCPListener.Name = "TCPThread";
form_Main_DisableControls(); // Disable controlls form_Main_DisableControls(); // Disable controlls
form_Main_SaveSettings(); // Save settings form_Main_SaveSettings(); // Save settings
// Prepare connection string // Prepare connection string
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; dcConnection.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;
tim_1000ms.Enabled = true; tim_1000ms.Enabled = true;
tim_10000ms.Enabled = true; tim_10000ms.Enabled = true;
// Send initial data
tim_10000ms_Tick(null, null);
} }
private void con_Button_Listen_OFF_Click(object sender, EventArgs e) private void con_Button_Listen_OFF_Click(object sender, EventArgs e)
@@ -133,7 +137,7 @@ namespace Perun_v1
// Stop listening // Stop listening
try try
{ {
TCPController.StopListen(); tcpcServer.StopListen();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -142,6 +146,11 @@ namespace Perun_v1
form_Main_EnableControls(); // Enable controls form_Main_EnableControls(); // Enable controls
con_img_db.Image = null;
con_img_dcs.Image = null;
con_img_lotATC.Image = null;
con_img_srs.Image = null;
// Stop timmers // Stop timmers
tim_1000ms.Enabled = false; tim_1000ms.Enabled = false;
tim_10000ms.Enabled = false; tim_10000ms.Enabled = false;
@@ -194,7 +203,7 @@ namespace Perun_v1
{ {
form_Main_SaveSettings(); form_Main_SaveSettings();
boolLetMeOut = true; // Save settings on exit bLetMeOut = true; // Save settings on exit
this.Close(); // Allow to exit application this.Close(); // Allow to exit application
} }
else if (dialogResult == DialogResult.No) else if (dialogResult == DialogResult.No)
@@ -244,7 +253,7 @@ namespace Perun_v1
private void form_Main_FormClosing(object sender, FormClosingEventArgs e) private void form_Main_FormClosing(object sender, FormClosingEventArgs e)
{ {
// Minimize to try on clicking "X" // Minimize to try on clicking "X"
if (e.CloseReason == CloseReason.UserClosing && !boolLetMeOut) if (e.CloseReason == CloseReason.UserClosing && !bLetMeOut)
{ {
e.Cancel = true; e.Cancel = true;
form_Main_SendToTray(); // Send app to system tray form_Main_SendToTray(); // Send app to system tray
@@ -279,23 +288,46 @@ namespace Perun_v1
{ {
if (arrSendBuffer[i] != null) if (arrSendBuffer[i] != null)
{ {
dbConnection.SendToMySql(arrSendBuffer[i]); dcConnection.SendToMySql(arrSendBuffer[i]);
arrSendBuffer[i] = null; arrSendBuffer[i] = null;
} }
} }
// Update icons // Update status icons at main form
if (dbConnection.bStatus) if (dcConnection.bStatus)
{ {
con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_db"); con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_db");
} else } else
{ {
con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error"); con_img_db.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error");
} }
if (tcpcServer.bStatus)
{
con_img_dcs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_game"); con_img_dcs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_game");
}
else
{
con_img_dcs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error");
}
if (bSRSStatus)
{
con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_srs"); con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_srs");
}
else
{
con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error");
}
if (bLotATCStatus)
{
con_img_lotATC.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_lotatc"); con_img_lotATC.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_lotatc");
} }
else
{
con_img_lotATC.Image = (Image)Properties.Resources.ResourceManager.GetObject("ico_error");
}
}
private void tim_10000ms_Tick(object sender, EventArgs e) private void tim_10000ms_Tick(object sender, EventArgs e)
{ {
@@ -356,11 +388,12 @@ namespace Perun_v1
} }
boolSRSdefault = false; boolSRSdefault = false;
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data loaded"); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data loaded");
bSRSStatus = true;
} }
catch catch
{ {
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data ERROR"); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data ERROR");
bSRSStatus = false;
} }
@@ -369,7 +402,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'}}";
} }
dbConnection.SendToMySql(strSRSJson); dcConnection.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)
@@ -382,10 +415,12 @@ namespace Perun_v1
strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + strLotATCJson + "'}"; strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + strLotATCJson + "'}";
boolLotATCdefault = false; boolLotATCdefault = false;
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data loaded"); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data loaded");
bLotATCStatus = true;
} }
catch catch
{ {
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data ERROR"); PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data ERROR");
bLotATCStatus = false;
} }
@@ -394,7 +429,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
} }
dbConnection.SendToMySql(strLotATCJson); dcConnection.SendToMySql(strLotATCJson);
} }
} }
} }

View File

@@ -75,7 +75,7 @@
<GenerateManifests>true</GenerateManifests> <GenerateManifests>true</GenerateManifests>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SignManifests>true</SignManifests> <SignManifests>false</SignManifests>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SignAssembly>false</SignAssembly> <SignAssembly>false</SignAssembly>