diff --git a/ErgometerApplication/.vs/ErgometerApplication/v14/.suo b/ErgometerApplication/.vs/ErgometerApplication/v14/.suo index 0457cd7..43cbfe4 100644 Binary files a/ErgometerApplication/.vs/ErgometerApplication/v14/.suo and b/ErgometerApplication/.vs/ErgometerApplication/v14/.suo differ diff --git a/ErgometerApplication/ErgometerApplication/ClientApplicatie.Designer.cs b/ErgometerApplication/ErgometerApplication/ClientApplicatie.Designer.cs index 9b704f8..53adc8c 100644 --- a/ErgometerApplication/ErgometerApplication/ClientApplicatie.Designer.cs +++ b/ErgometerApplication/ErgometerApplication/ClientApplicatie.Designer.cs @@ -33,7 +33,6 @@ namespace ErgometerApplication { this.components = new System.ComponentModel.Container(); this.updateTimer = new System.Windows.Forms.Timer(this.components); - this.writeTimer = new System.Windows.Forms.Timer(this.components); this.panelClientContainer = new System.Windows.Forms.Panel(); this.panelDataViewLeft = new System.Windows.Forms.Panel(); this.panelGraphView = new ErgometerApplication.PanelGraphView(); @@ -58,11 +57,8 @@ namespace ErgometerApplication // // updateTimer // - this.updateTimer.Interval = 1500; - // - // writeTimer - // - this.writeTimer.Interval = 10000; + this.updateTimer.Interval = 350; + this.updateTimer.Tick += new System.EventHandler(this.updateTimer_Tick); // // panelClientContainer // diff --git a/ErgometerApplication/ErgometerApplication/ClientApplicatie.cs b/ErgometerApplication/ErgometerApplication/ClientApplicatie.cs index 8e457a3..bc4f7f8 100644 --- a/ErgometerApplication/ErgometerApplication/ClientApplicatie.cs +++ b/ErgometerApplication/ErgometerApplication/ClientApplicatie.cs @@ -20,281 +20,32 @@ namespace ErgometerApplication { public partial class ClientApplicatie : Form { - private ComPort comPort; - private TcpClient doctor; - //private List _data; - //private int i = 0; - //NetCommand command; - //private ServerCommunicator communicator = new ServerCommunicator(); - //List readFile = new List(); - public ClientApplicatie() { - InitializeComponent(); - comPort = new ComPort(); - - doctor = new TcpClient("127.0.0.1", 8888); - - // _data = new List(); - // communicator.data = _data; + InitializeComponent(); } - //private void connectButton_Click(object sender, EventArgs e) - //{ - // if (!comPort.IsOpen()) - // { - // if (comPort.Connect(ComPortBox.Text)) - // { - // connectButton.Text = "Disconnect"; - // statusButton.Enabled = true; - // resetButton.Enabled = true; - // timerStatus.Enabled = true; - // inputTextBox.Enabled = true; - // timeButton.Enabled = true; - // distanceButton.Enabled = true; - // powerButton.Enabled = true; - // energyButton.Enabled = true; - // ComPortBox.Enabled = false; - - // comPort.Write("RS"); - // comPort.Read(); - // Thread.Sleep(200); - // comPort.Write("CM"); - // comPort.Read(); - // Thread.Sleep(200); - - // comPort.Write("ST"); - // string response = comPort.Read(); - // Console.WriteLine(response); - - // Meting m = Meting.Parse(response); - // SaveData(m); - // // richTextBox1.Text = m.ToString(); - // } - // } - // else - // { - // if (comPort.Disconnect()) - // { - // connectButton.Text = "Connect"; - // timerStatus.Checked = false; - // statusButton.Enabled = false; - // resetButton.Enabled = false; - // timerStatus.Enabled = false; - // inputTextBox.Enabled = false; - // timeButton.Enabled = false; - // distanceButton.Enabled = false; - // powerButton.Enabled = false; - // energyButton.Enabled = false; - // ComPortBox.Enabled = true; - // richTextBox1.Text = ""; - - // WriteFile(); - - // } - // } - // communicator.client = new TcpClient(); - // communicator.client.Connect("127.0.0.1", 8888); - // Thread thread = new Thread(ServerCommunication); - // thread.Start(communicator); - //} - - //private void statusButton_Click(object sender, EventArgs e) - //{ - // comPort.Write("ST"); - // string response = comPort.Read(); - // Console.WriteLine(response); - // Meting m = Meting.Parse(response, '\t'); - // Console.WriteLine(m); - // SaveData(m); - // communicator.data.Add(m); - // command = new NetCommand(m, communicator.sessionId); - // NetHelper.SendNetCommand(communicator.client, command); - // richTextBox1.Text = m.ToString(); - // WriteFile(); - //} - - //static void ServerCommunication(object obj) - //{ - // ServerCommunicator communicator = obj as ServerCommunicator; - - // NetCommand net = NetHelper.ReadNetCommand(communicator.client); - // if (net.Type == NetCommand.CommandType.SESSION) - // communicator.sessionId = net.Session; - - // NetCommand command = new NetCommand("name", false, "pass", communicator.sessionId); - // NetHelper.SendNetCommand(communicator.client, command); - //} - - //private void resetButton_Click(object sender, EventArgs e) - //{ - // comPort.Write("RS"); - // comPort.Read(); - // Thread.Sleep(200); - // comPort.Write("CM"); - // comPort.Read(); - //} - - //private void updateTimer_Tick(object sender, EventArgs e) - //{ - // if (timerStatus.Checked) - // { - // comPort.Write("ST"); - // string response = comPort.Read(); - // Console.WriteLine(response); - // Meting m = Meting.Parse(response); - // SaveData(m); - // communicator.data.Add(m); - // command = new NetCommand(m, communicator.sessionId); - // NetHelper.SendNetCommand(communicator.client, command); - // richTextBox1.Text = m.ToString(); - // command = new NetCommand(communicator.data[0], communicator.sessionId); - // NetHelper.SendNetCommand(communicator.client, command); - // } - //} - - //private void timeButton_Click(object sender, EventArgs e) - //{ - // comPort.Write("RS"); - // comPort.Read(); - // Thread.Sleep(200); - // string[] temp = inputTextBox.Text.Split(':'); - // comPort.Write("PT " + temp[0] + temp[1]); - // Console.WriteLine("PT " + temp[0] + temp[1]); - // inputTextBox.Text = ""; - // comPort.Read(); - //} - - //private void distanceButton_Click(object sender, EventArgs e) - //{ - // comPort.Write("RS"); - // comPort.Read(); - // Thread.Sleep(200); - // int temp = int.Parse(inputTextBox.Text); - // comPort.Write("PD " + temp.ToString()); - // inputTextBox.Text = ""; - // comPort.Read(); - //} - - //private void powerButton_Click(object sender, EventArgs e) - //{ - // int temp = int.Parse(inputTextBox.Text); - // comPort.Write("PW " + temp.ToString()); - // inputTextBox.Text = ""; - // comPort.Read(); - //} - - //private void energyButton_Click(object sender, EventArgs e) - //{ - // comPort.Write("RS"); - // comPort.Read(); - // Thread.Sleep(200); - // int temp = int.Parse(inputTextBox.Text); - // comPort.Write("PE " + temp.ToString()); - // inputTextBox.Text = ""; - // comPort.Read(); - //} - - //private void timerStatus_CheckedChanged(object sender, EventArgs e) - //{ - // if (timerStatus.Checked) - // { - // statusButton.Enabled = false; - // updateTimer.Start(); - // writeTimer.Start(); - // } - // else - // { - // statusButton.Enabled = true; - // updateTimer.Stop(); - // writeTimer.Stop(); - // } - //} - - //private void readButton_Click(object sender, EventArgs e) - //{ - // ReadFile(); - // if (readFile != null) - // { - // metingNextButton.Enabled = true; - // metingBackButton.Enabled = true; - // } - - //} - - //private void metingBackButton_Click(object sender, EventArgs e) - //{ - // if (i > 0) - // { i--; - // metingNextButton.Enabled = true; - // } - // if (i == 0) - // { - // metingBackButton.Enabled = false; - // } - // richTextBox1.Text = readFile.ElementAt(i).ToString(); - //} - - //private void metingNextButton_Click(object sender, EventArgs e) - //{ - // if (i < (readFile.Count - 1)) - // { i++; - // metingBackButton.Enabled = true; - // } - // if (i == (readFile.Count - 1)) - // { - // metingNextButton.Enabled = false; - // } - // richTextBox1.Text = readFile.ElementAt(i).ToString(); - //} - - //private void writeTimer_Tick(object sender, EventArgs e) - //{ - // WriteFile(); - //} - - - //private void WriteFile() - //{ - // string json = Newtonsoft.Json.JsonConvert.SerializeObject(_data); - // string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ErgometerSessionData.ergo"); - // System.IO.File.WriteAllText(path, json); - // Console.WriteLine("Writing file: " + path); - //} - - //private void SaveData(Meting meting) - //{ - // _data.Add(meting); - //} - - //private void ReadFile() - //{ - // string path; - // OpenFileDialog file = new OpenFileDialog(); - // file.Filter = "Ergometer files(*.ergo) | *.ergo | All files(*.*) | *.*"; - // if (file.ShowDialog() == DialogResult.OK) - // { - // path = file.FileName; - // readFile = Newtonsoft.Json.JsonConvert.DeserializeObject>(System.IO.File.ReadAllText(path)); - // richTextBox1.Text = readFile.ElementAt(i).ToString(); - // } - // else - // { - // readFile = null; - // } - //} + private void updateTimer_Tick(object sender, EventArgs e) + { + if(MainClient.Doctor.Connected) + { + MainClient.ComPort.Write("ST"); + string response = MainClient.ComPort.Read(); + MainClient.SaveMeting(response); + } + } public void validateLogin(string username, string password) { - if(username.Length >= 4) + if (username.Length >= 4) { - if(password.Length == 0) + if (password.Length == 0) { panelLogin.lblVerification.Text = "Vul een wachtwoord in."; panelLogin.lblVerification.ForeColor = Color.Red; panelLogin.lblVerification.Visible = true; } - if (password.Length > 0 && password.Length < 9) + if (password.Length > 0 && password.Length < 8) { panelLogin.lblVerification.Text = "Vul een wachtwoord in van minimaal 8 karakters."; panelLogin.lblVerification.ForeColor = Color.Red; @@ -302,7 +53,7 @@ namespace ErgometerApplication } if (password.Length >= 8) { - //password checken + MainClient.Connect(SerialPort.GetPortNames()[0], username, password); panelClientContainer.BringToFront(); this.labelUsername.Text = panelLogin.textBoxUsername.Text; panelTopBar.Visible = true; @@ -319,7 +70,7 @@ namespace ErgometerApplication private void ClientApplicatie_Resize(object sender, System.EventArgs e) { Control control = (Control)sender; - if(control.Size.Width < 980) + if (control.Size.Width < 980) { panelGraphView.Visible = false; panelClientChat.Width = 400; @@ -343,7 +94,7 @@ namespace ErgometerApplication public void sessionLogout() { - + MainClient.Disconnect(); } private void buttonLogOff_Click(object sender, EventArgs e) @@ -352,7 +103,5 @@ namespace ErgometerApplication panelTopBar.Visible = false; sessionLogout(); } - - } } \ No newline at end of file diff --git a/ErgometerApplication/ErgometerApplication/ClientApplicatie.resx b/ErgometerApplication/ErgometerApplication/ClientApplicatie.resx index 29ee7eb..26f2a51 100644 --- a/ErgometerApplication/ErgometerApplication/ClientApplicatie.resx +++ b/ErgometerApplication/ErgometerApplication/ClientApplicatie.resx @@ -120,9 +120,6 @@ 17, 17 - - 147, 19 - 25 diff --git a/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj b/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj index cc4fa12..6ff639d 100644 --- a/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj +++ b/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj @@ -54,6 +54,7 @@ + Component diff --git a/ErgometerApplication/ErgometerApplication/MainClient.cs b/ErgometerApplication/ErgometerApplication/MainClient.cs new file mode 100644 index 0000000..a6e709b --- /dev/null +++ b/ErgometerApplication/ErgometerApplication/MainClient.cs @@ -0,0 +1,109 @@ +using ErgometerLibrary; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ErgometerApplication +{ + class MainClient + { + + public static ComPort ComPort { get; } + public static TcpClient Doctor { get; } + public static List Metingen { get; } + + public static int Session; + + public static string HOST = "127.0.0.1"; + public static int PORT = 8888; + + static MainClient() + { + ComPort = new ComPort(); + Doctor = new TcpClient(); + Metingen = new List(); + + Session = 0; + } + + public static void Connect(string comport, string name, string password) + { + if (!ComPort.IsOpen()) + { + if (ComPort.Connect(comport)) + { + ComPort.Write("RS"); + ComPort.Read(); + Thread.Sleep(200); + ComPort.Write("CM"); + ComPort.Read(); + Thread.Sleep(200); + + ComPort.Write("ST"); + string response = ComPort.Read(); + Console.WriteLine(response); + + SaveMeting(response); + } + else + throw new Exception("Comport was unable to connect"); + } + else + throw new Exception("Comport is already connected"); + + + if(!Doctor.Connected) + { + Doctor.Connect(HOST, PORT); + + NetCommand net = NetHelper.ReadNetCommand(Doctor); + if (net.Type == NetCommand.CommandType.SESSION) + Session = net.Session; + else + throw new Exception("Session not assigned"); + + NetCommand command = new NetCommand(name, false, password, Session); + NetHelper.SendNetCommand(Doctor, command); + } + else + throw new Exception("Client is already connected"); + } + + public static void Disconnect() + { + if (ComPort.IsOpen()) + { + if (ComPort.Disconnect()) + { + + } + else + throw new Exception("Comport was unable to disconnect"); + } + else + throw new Exception("Comport is already closed"); + + if (Doctor.Connected) + { + NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session)); + Doctor.Close(); + } + else + throw new Exception("Client is already disconnected"); + } + + public static void SaveMeting(string meting) + { + Meting m = Meting.Parse(meting); + Metingen.Add(m); + if (Doctor.Connected) + { + NetHelper.SendNetCommand(Doctor, new NetCommand(m, Session)); + } + } + } +} diff --git a/ErgometerApplication/ErgometerApplication/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/ErgometerApplication/ErgometerApplication/obj/Debug/DesignTimeResolveAssemblyReferences.cache index e5db290..dc63a6f 100644 Binary files a/ErgometerApplication/ErgometerApplication/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/ErgometerApplication/ErgometerApplication/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ