From 689897626bffb217195e235a36dcc9cfdda327a3 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Mon, 19 Oct 2015 22:24:58 +0200 Subject: [PATCH] Rebuild connecting, recreated active sessions, bugfixes --- .../{ => Client}/ChartPanel.cs | 6 +- .../{ => Client}/ClientThread.cs | 24 +++---- .../{ => Client}/PanelClientChat.cs | 0 .../{ => Client}/PanelClientData.cs | 0 .../{ => Client}/PanelGraphView.cs | 27 +++----- .../{ => Client}/SessionWindow.Designer.cs | 2 +- .../{ => Client}/SessionWindow.cs | 38 ++++++++++- .../{ => Client}/SessionWindow.resx | 0 .../ConActiveSessions.cs | 52 ++++++++------- .../ErgometerDoctorApplication.csproj | 22 +++---- ErgometerDoctorApplication/MainClient.cs | 17 +++-- .../MainWindow.Designer.cs | 2 +- ErgometerDoctorApplication/MainWindow.cs | 13 ++-- ErgometerDoctorApplication/SessionPanel.cs | 66 +++++++++++++++++++ ErgometerDoctorApplication/SessionTable.cs | 19 ------ 15 files changed, 183 insertions(+), 105 deletions(-) rename ErgometerDoctorApplication/{ => Client}/ChartPanel.cs (93%) rename ErgometerDoctorApplication/{ => Client}/ClientThread.cs (74%) rename ErgometerDoctorApplication/{ => Client}/PanelClientChat.cs (100%) rename ErgometerDoctorApplication/{ => Client}/PanelClientData.cs (100%) rename ErgometerDoctorApplication/{ => Client}/PanelGraphView.cs (69%) rename ErgometerDoctorApplication/{ => Client}/SessionWindow.Designer.cs (99%) rename ErgometerDoctorApplication/{ => Client}/SessionWindow.cs (63%) rename ErgometerDoctorApplication/{ => Client}/SessionWindow.resx (100%) create mode 100644 ErgometerDoctorApplication/SessionPanel.cs delete mode 100644 ErgometerDoctorApplication/SessionTable.cs diff --git a/ErgometerDoctorApplication/ChartPanel.cs b/ErgometerDoctorApplication/Client/ChartPanel.cs similarity index 93% rename from ErgometerDoctorApplication/ChartPanel.cs rename to ErgometerDoctorApplication/Client/ChartPanel.cs index beb5602..5639ef1 100644 --- a/ErgometerDoctorApplication/ChartPanel.cs +++ b/ErgometerDoctorApplication/Client/ChartPanel.cs @@ -15,10 +15,12 @@ namespace ErgometerDoctorApplication private MetingType type; private ChartArea chartArea; private Series series; + private SeriesChartType ChartType; - public ChartPanel(MetingType type) : base() + public ChartPanel(MetingType type, SeriesChartType charttype) : base() { this.type = type; + this.ChartType = charttype; this.chart = new Chart(); this.chartArea = new ChartArea(); @@ -57,7 +59,7 @@ namespace ErgometerDoctorApplication { Series serie = new Series(); serie.Name = "series"; - serie.ChartType = SeriesChartType.Line; + serie.ChartType = ChartType; serie.ChartArea = "chartArea"; serie.BorderWidth = 3; return serie; diff --git a/ErgometerDoctorApplication/ClientThread.cs b/ErgometerDoctorApplication/Client/ClientThread.cs similarity index 74% rename from ErgometerDoctorApplication/ClientThread.cs rename to ErgometerDoctorApplication/Client/ClientThread.cs index 7cac651..dbb6e4c 100644 --- a/ErgometerDoctorApplication/ClientThread.cs +++ b/ErgometerDoctorApplication/Client/ClientThread.cs @@ -8,7 +8,7 @@ using System.Windows.Forms; namespace ErgometerDoctorApplication { - class ClientThread + public class ClientThread { public int Session { get; } public string Name { get; } @@ -23,7 +23,7 @@ namespace ErgometerDoctorApplication Name = name; Session = session; - window = new SessionWindow(Name, true, Session); + window = new SessionWindow(Name, true, Session, this); window.FormClosed += Window_FormClosed; Metingen = new List(); @@ -40,7 +40,11 @@ namespace ErgometerDoctorApplication switch (command.Type) { case NetCommand.CommandType.DATA: - SaveMeting(command.Meting); + lock(Metingen) + { + Metingen.Add(command.Meting); + } + window.Invoke(window.updateMetingen, new Object[] { command.Meting }); break; case NetCommand.CommandType.CHAT: ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, false); @@ -65,18 +69,6 @@ namespace ErgometerDoctorApplication { MainClient.SendNetCommand(command); } - - public void SaveMeting(Meting m) - { - Metingen.Add(m); - - window.heartBeat.updateValue(m.HeartBeat); - window.RPM.updateValue(m.RPM); - window.speed.updateValue(m.Speed); - window.distance.updateValue(m.Distance); - window.power.updateValue(m.Power); - window.energy.updateValue(m.Energy); - window.actualpower.updateValue(m.ActualPower); - } + } } diff --git a/ErgometerDoctorApplication/PanelClientChat.cs b/ErgometerDoctorApplication/Client/PanelClientChat.cs similarity index 100% rename from ErgometerDoctorApplication/PanelClientChat.cs rename to ErgometerDoctorApplication/Client/PanelClientChat.cs diff --git a/ErgometerDoctorApplication/PanelClientData.cs b/ErgometerDoctorApplication/Client/PanelClientData.cs similarity index 100% rename from ErgometerDoctorApplication/PanelClientData.cs rename to ErgometerDoctorApplication/Client/PanelClientData.cs diff --git a/ErgometerDoctorApplication/PanelGraphView.cs b/ErgometerDoctorApplication/Client/PanelGraphView.cs similarity index 69% rename from ErgometerDoctorApplication/PanelGraphView.cs rename to ErgometerDoctorApplication/Client/PanelGraphView.cs index 56daa59..119c367 100644 --- a/ErgometerDoctorApplication/PanelGraphView.cs +++ b/ErgometerDoctorApplication/Client/PanelGraphView.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; namespace ErgometerDoctorApplication { @@ -32,16 +33,7 @@ namespace ErgometerDoctorApplication } List metingen = new List(); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); - metingen.Add(new Meting(23, 25, 12, 46, 13, 25, 13, 1, 32)); + metingen.Add(new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0)); updateAllCharts(metingen); // @@ -59,14 +51,13 @@ namespace ErgometerDoctorApplication public void createCharts() { charts = new List(); - charts.Add(new ChartPanel(ChartPanel.MetingType.HEARTBEAT)); - charts.Add(new ChartPanel(ChartPanel.MetingType.RPM)); - charts.Add(new ChartPanel(ChartPanel.MetingType.SPEED)); - charts.Add(new ChartPanel(ChartPanel.MetingType.DISTANCE)); - charts.Add(new ChartPanel(ChartPanel.MetingType.ENERGY)); - charts.Add(new ChartPanel(ChartPanel.MetingType.SECONDS)); - charts.Add(new ChartPanel(ChartPanel.MetingType.POWER)); - charts.Add(new ChartPanel(ChartPanel.MetingType.ACTUALPOWER)); + charts.Add(new ChartPanel(ChartPanel.MetingType.HEARTBEAT, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.RPM, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.SPEED, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.DISTANCE, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.ENERGY, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.POWER, SeriesChartType.Line)); + charts.Add(new ChartPanel(ChartPanel.MetingType.ACTUALPOWER, SeriesChartType.Line)); } public void updateAllCharts(List metingen) diff --git a/ErgometerDoctorApplication/SessionWindow.Designer.cs b/ErgometerDoctorApplication/Client/SessionWindow.Designer.cs similarity index 99% rename from ErgometerDoctorApplication/SessionWindow.Designer.cs rename to ErgometerDoctorApplication/Client/SessionWindow.Designer.cs index 24557cf..0c60b08 100644 --- a/ErgometerDoctorApplication/SessionWindow.Designer.cs +++ b/ErgometerDoctorApplication/Client/SessionWindow.Designer.cs @@ -146,7 +146,7 @@ namespace ErgometerDoctorApplication #endregion private System.Windows.Forms.Panel panelClientContainer; public PanelClientChat panelClientChat; - private PanelGraphView panelGraphView; + public PanelGraphView panelGraphView; private System.Windows.Forms.Panel panelDataViewLeft; private System.Windows.Forms.Panel panelTopBar; private System.Windows.Forms.Label labelUsername; diff --git a/ErgometerDoctorApplication/SessionWindow.cs b/ErgometerDoctorApplication/Client/SessionWindow.cs similarity index 63% rename from ErgometerDoctorApplication/SessionWindow.cs rename to ErgometerDoctorApplication/Client/SessionWindow.cs index 917d23f..ed6b367 100644 --- a/ErgometerDoctorApplication/SessionWindow.cs +++ b/ErgometerDoctorApplication/Client/SessionWindow.cs @@ -1,4 +1,5 @@ -using System; +using ErgometerLibrary; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -17,11 +18,24 @@ namespace ErgometerDoctorApplication private string ClientName { get; set; } public int Session { get; } - public SessionWindow(string ClientName, bool ActiveSessionm, int session) + private ClientThread client; + + private int count; + + public delegate void UpdateMetingen(Meting m); + public UpdateMetingen updateMetingen; + + public SessionWindow(string ClientName, bool ActiveSessionm, int session, ClientThread parentthread) { this.ActiveSession = ActiveSession; this.ClientName = ClientName; + this.client = parentthread; Session = session; + + count = 0; + + updateMetingen = new UpdateMetingen(this.SaveMeting); + InitializeComponent(); Form.CheckForIllegalCrossThreadCalls = false; @@ -50,5 +64,25 @@ namespace ErgometerDoctorApplication panelDataViewLeft.Dock = DockStyle.Left; } } + + public void SaveMeting(Meting m) + { + + heartBeat.updateValue(m.HeartBeat); + RPM.updateValue(m.RPM); + speed.updateValue(m.Speed); + distance.updateValue(m.Distance); + power.updateValue(m.Power); + energy.updateValue(m.Energy); + actualpower.updateValue(m.ActualPower); + + if (count >= 10) + { + count = 0; + panelGraphView.updateAllCharts(client.Metingen); + } + + count++; + } } } diff --git a/ErgometerDoctorApplication/SessionWindow.resx b/ErgometerDoctorApplication/Client/SessionWindow.resx similarity index 100% rename from ErgometerDoctorApplication/SessionWindow.resx rename to ErgometerDoctorApplication/Client/SessionWindow.resx diff --git a/ErgometerDoctorApplication/ConActiveSessions.cs b/ErgometerDoctorApplication/ConActiveSessions.cs index 52a8552..ebea44e 100644 --- a/ErgometerDoctorApplication/ConActiveSessions.cs +++ b/ErgometerDoctorApplication/ConActiveSessions.cs @@ -10,31 +10,13 @@ namespace ErgometerDoctorApplication { public class ConActiveSessions : Panel { - public DataGridView data; + + private FlowLayoutPanel flowlayout; + public ConActiveSessions() : base() { - this.data = new DataGridView(); - ((System.ComponentModel.ISupportInitialize)(this.data)).BeginInit(); - this.labelActiveSessions = new System.Windows.Forms.Label(); - // - // SongsTableView - // - this.data.AllowUserToAddRows = false; - this.data.AllowUserToDeleteRows = false; - this.data.AllowUserToResizeRows = false; - this.data.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.data.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; - this.data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.data.Location = new System.Drawing.Point(0, 0); - this.data.MultiSelect = false; - this.data.Name = "Active Sessions"; - this.data.ReadOnly = true; - this.data.RowHeadersVisible = false; - this.data.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.data.Size = new System.Drawing.Size(760, 172); - this.data.TabIndex = 0; + labelActiveSessions = new Label(); + // // ConActiveSessions // @@ -53,11 +35,33 @@ namespace ErgometerDoctorApplication this.labelActiveSessions.Name = "labelActiveSessions"; this.labelActiveSessions.Size = new System.Drawing.Size(103, 21); this.labelActiveSessions.TabIndex = 3; - this.labelActiveSessions.Text = "Geen actieve sessies."; + this.labelActiveSessions.Text = "Er zijn geen actieve sessies."; + + flowlayout = new FlowLayoutPanel(); + + flowlayout.Dock = DockStyle.Fill; + flowlayout.BackColor = System.Drawing.Color.DarkGray; + flowlayout.Location = new System.Drawing.Point(0, 0); + flowlayout.Name = "flowlayout"; + flowlayout.Padding = new Padding(15); + flowlayout.AutoScroll = true; this.Controls.Add(labelActiveSessions); + this.Controls.Add(flowlayout); + } public System.Windows.Forms.Label labelActiveSessions; + + public void updateActiveSessions(Dictionary actives) + { + flowlayout.Controls.Clear(); + + foreach (KeyValuePair pair in actives) + { + + flowlayout.Controls.Add(new SessionPanel(pair.Key, pair.Value)); + } + } } } diff --git a/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj b/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj index ca7f403..6f46a08 100644 --- a/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj +++ b/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj @@ -52,26 +52,23 @@ - + Component - + + Component - - + Component - + Component - - Component - - + Form - + SessionWindow.cs @@ -98,6 +95,9 @@ + + Component + MainWindow.cs @@ -111,7 +111,7 @@ Resources.resx True - + SessionWindow.cs diff --git a/ErgometerDoctorApplication/MainClient.cs b/ErgometerDoctorApplication/MainClient.cs index 1b34877..fec7f5c 100644 --- a/ErgometerDoctorApplication/MainClient.cs +++ b/ErgometerDoctorApplication/MainClient.cs @@ -12,7 +12,7 @@ namespace ErgometerDoctorApplication class MainClient { - public static TcpClient Server { get; } + public static TcpClient Server { get; set; } public static bool loggedin; @@ -41,8 +41,6 @@ namespace ErgometerDoctorApplication { Server = new TcpClient(); - t = new Thread(run); - loggedin = false; clients = new List(); @@ -55,8 +53,11 @@ namespace ErgometerDoctorApplication { error = "Succes"; - if (!Server.Connected) + if (Server == null || !Server.Connected) { + if (Server == null) + Server = new TcpClient(); + try { Server.Connect(HOST, PORT); @@ -74,6 +75,7 @@ namespace ErgometerDoctorApplication throw new Exception("Session not assigned"); running = true; + t = new Thread(run); t.IsBackground = true; t.Start(); } @@ -106,7 +108,9 @@ namespace ErgometerDoctorApplication { NetHelper.SendNetCommand(Server, new NetCommand(NetCommand.CommandType.LOGOUT, Session)); loggedin = false; - running = false; + running = false; + Server.Close(); + Server = null; } } @@ -159,6 +163,9 @@ namespace ErgometerDoctorApplication throw new FormatException("Error in NetCommand: Length type is not recognised"); } break; + case NetCommand.CommandType.ERROR: + Console.WriteLine("An error occured, ignoring"); + break; default: HandToClient(command); break; diff --git a/ErgometerDoctorApplication/MainWindow.Designer.cs b/ErgometerDoctorApplication/MainWindow.Designer.cs index 3dfe380..cbd8913 100644 --- a/ErgometerDoctorApplication/MainWindow.Designer.cs +++ b/ErgometerDoctorApplication/MainWindow.Designer.cs @@ -136,7 +136,7 @@ namespace ErgometerDoctorApplication // // updateTimer // - this.updateTimer.Interval = 10000; + this.updateTimer.Interval = 6000; this.updateTimer.Tick += new EventHandler(this.updateTimer_tick); // // label1 diff --git a/ErgometerDoctorApplication/MainWindow.cs b/ErgometerDoctorApplication/MainWindow.cs index c037572..11ed1d5 100644 --- a/ErgometerDoctorApplication/MainWindow.cs +++ b/ErgometerDoctorApplication/MainWindow.cs @@ -25,14 +25,15 @@ namespace ErgometerDoctorApplication this.HeaderLabel.Text = "Actieve Sessies"; conActiveSessions.BringToFront(); - string str = ""; - foreach(KeyValuePair client in MainClient.activesessions) + if(MainClient.activesessions.Count > 0) { - str += client.Value + ", "; - MainClient.StartNewCLient(client.Value, client.Key); + conActiveSessions.labelActiveSessions.Text = ""; + conActiveSessions.updateActiveSessions(MainClient.activesessions); + } + else + { + conActiveSessions.labelActiveSessions.Text = "Er zijn geen actieve sessies."; } - conActiveSessions.labelActiveSessions.Text = str; - Console.WriteLine(str); } private void BtnSessionLibrary_Click(object sender, EventArgs e) diff --git a/ErgometerDoctorApplication/SessionPanel.cs b/ErgometerDoctorApplication/SessionPanel.cs new file mode 100644 index 0000000..d584380 --- /dev/null +++ b/ErgometerDoctorApplication/SessionPanel.cs @@ -0,0 +1,66 @@ +using System.Windows.Forms; + +namespace ErgometerDoctorApplication +{ + class SessionPanel : Panel + { + private int Session; + private string Name; + + public SessionPanel(int session, string name) : base() + { + + Session = session; + Name = name; + + this.Location = new System.Drawing.Point(0, 0); + this.Size = new System.Drawing.Size(180, 100); + this.BackColor = System.Drawing.Color.White; + + this.Click += SessionPanel_Click; + this.MouseEnter += SessionPanel_MouseEnter; + this.MouseLeave += SessionPanel_MouseLeave; + this.Cursor = Cursors.Hand; + + this.labelName = new System.Windows.Forms.Label(); + + // + // labelActiveSessions + // + this.labelName.Anchor = System.Windows.Forms.AnchorStyles.None; + this.labelName.AutoSize = true; + this.labelName.Font = new System.Drawing.Font("Segoe UI Semibold", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.labelName.Location = new System.Drawing.Point(10, 10); + this.labelName.ForeColor = System.Drawing.Color.Gray; + this.labelName.Name = "labelActiveSessions"; + this.labelName.Size = new System.Drawing.Size(103, 21); + this.labelName.TabIndex = 3; + this.labelName.Text = name; + this.labelName.Click += SessionPanel_Click; + this.labelName.MouseEnter += SessionPanel_MouseEnter; + this.labelName.MouseLeave += SessionPanel_MouseLeave; + this.labelName.Cursor = Cursors.Hand; + + this.Controls.Add(labelName); + } + + private void SessionPanel_MouseEnter(object sender, System.EventArgs e) + { + this.BackColor = System.Drawing.Color.LightGray; + this.labelName.ForeColor = System.Drawing.Color.DarkGray; + } + + private void SessionPanel_MouseLeave(object sender, System.EventArgs e) + { + this.BackColor = System.Drawing.Color.White; + this.labelName.ForeColor = System.Drawing.Color.Gray; + } + + private void SessionPanel_Click(object sender, System.EventArgs e) + { + MainClient.StartNewCLient(Name, Session); + } + + public System.Windows.Forms.Label labelName; + } +} \ No newline at end of file diff --git a/ErgometerDoctorApplication/SessionTable.cs b/ErgometerDoctorApplication/SessionTable.cs deleted file mode 100644 index 5114b7f..0000000 --- a/ErgometerDoctorApplication/SessionTable.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ErgometerDoctorApplication -{ - public class SessionTable : DataTable - { - public SessionTable() : base() - { - this.Columns.Clear(); - this.Columns.Add("User", typeof (string)); - this.Columns.Add("SessionId", typeof (string)); - } - } -}