Rebuild connecting, bugfixes

This commit is contained in:
2015-10-19 22:25:23 +02:00
parent f03ccad863
commit f0578091d8
7 changed files with 41 additions and 40 deletions
@@ -6,19 +6,22 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting; using System.Windows.Forms.DataVisualization.Charting;
namespace ErgometerApplication namespace ErgometerApplication
{ {
public class ChartPanel:Panel public class ChartPanel : Panel
{ {
private Chart chart; private Chart chart;
private MetingType type; private MetingType type;
private ChartArea chartArea; private ChartArea chartArea;
private Series series; private Series series;
private SeriesChartType ChartType;
public ChartPanel(MetingType type) : base() public ChartPanel(MetingType type, SeriesChartType charttype) : base()
{ {
this.type = type; this.type = type;
this.ChartType = charttype;
this.chart = new Chart(); this.chart = new Chart();
this.chartArea = new ChartArea(); this.chartArea = new ChartArea();
this.chart.Titles.Add(new Title(type.ToString())); this.chart.Titles.Add(new Title(type.ToString()));
@@ -32,7 +35,7 @@ namespace ErgometerApplication
this.chartArea.Name = "chartArea"; this.chartArea.Name = "chartArea";
this.chart.Size = new System.Drawing.Size(250, 200); this.chart.Size = new System.Drawing.Size(250, 200);
this.chart.Dock = DockStyle.Fill; this.chart.Dock = DockStyle.Fill;
this.chart.Series.Add(series); this.chart.Series.Add(series);
this.chart.Text = "chart"; this.chart.Text = "chart";
@@ -56,7 +59,7 @@ namespace ErgometerApplication
{ {
Series serie = new Series(); Series serie = new Series();
serie.Name = "series"; serie.Name = "series";
serie.ChartType = SeriesChartType.Line; serie.ChartType = ChartType;
serie.ChartArea = "chartArea"; serie.ChartArea = "chartArea";
serie.BorderWidth = 3; serie.BorderWidth = 3;
return serie; return serie;
@@ -64,7 +67,7 @@ namespace ErgometerApplication
public int getMetingType(Meting meting) public int getMetingType(Meting meting)
{ {
switch(type) switch (type)
{ {
case MetingType.HEARTBEAT: case MetingType.HEARTBEAT:
return meting.HeartBeat; return meting.HeartBeat;
@@ -83,7 +86,7 @@ namespace ErgometerApplication
case MetingType.ACTUALPOWER: case MetingType.ACTUALPOWER:
return meting.ActualPower; return meting.ActualPower;
default: default:
return 0; return 0;
} }
} }
@@ -96,7 +99,7 @@ namespace ErgometerApplication
POWER, POWER,
ENERGY, ENERGY,
SECONDS, SECONDS,
ACTUALPOWER ACTUALPOWER
} }
} }
} }
@@ -57,7 +57,7 @@ namespace ErgometerApplication
// //
// updateTimer // updateTimer
// //
this.updateTimer.Interval = 800; this.updateTimer.Interval = 600;
this.updateTimer.Tick += new System.EventHandler(this.updateTimer_Tick); this.updateTimer.Tick += new System.EventHandler(this.updateTimer_Tick);
// //
// panelClientContainer // panelClientContainer
@@ -32,10 +32,8 @@ namespace ErgometerApplication
private void updateTimer_Tick(object sender, EventArgs e) private void updateTimer_Tick(object sender, EventArgs e)
{ {
Console.WriteLine("Trying to send data....");
if(MainClient.Doctor.Connected) if(MainClient.Doctor.Connected)
{ {
Console.WriteLine("Sending data");
MainClient.ComPort.Write("ST"); MainClient.ComPort.Write("ST");
string response = MainClient.ComPort.Read(); string response = MainClient.ComPort.Read();
Meting m = MainClient.SaveMeting(response); Meting m = MainClient.SaveMeting(response);
@@ -13,7 +13,7 @@ namespace ErgometerApplication
{ {
public static ComPort ComPort { get; } public static ComPort ComPort { get; }
public static TcpClient Doctor { get; } public static TcpClient Doctor { get; set; }
public static List<Meting> Metingen { get; } public static List<Meting> Metingen { get; }
public static List<ChatMessage> Chat { get; } public static List<ChatMessage> Chat { get; }
@@ -37,8 +37,6 @@ namespace ErgometerApplication
Metingen = new List<Meting>(); Metingen = new List<Meting>();
Chat = new List<ChatMessage>(); Chat = new List<ChatMessage>();
t = new Thread(run);
Name = "Unknown"; Name = "Unknown";
Session = 0; Session = 0;
Loggedin = false; Loggedin = false;
@@ -53,16 +51,21 @@ namespace ErgometerApplication
{ {
error = "Succes"; error = "Succes";
if (!Doctor.Connected) if (Doctor == null || !Doctor.Connected)
{ {
try { if (Doctor == null)
Doctor = new TcpClient();
try
{
Doctor.Connect(HOST, PORT); Doctor.Connect(HOST, PORT);
} }
catch (Exception e) catch (Exception e)
{ {
error = "De server is niet online."; error = "Server is niet online.";
return false; return false;
} }
Name = name; Name = name;
NetCommand net = NetHelper.ReadNetCommand(Doctor); NetCommand net = NetHelper.ReadNetCommand(Doctor);
@@ -72,6 +75,8 @@ namespace ErgometerApplication
throw new Exception("Session not assigned"); throw new Exception("Session not assigned");
running = true; running = true;
t = new Thread(run);
t.IsBackground = true; t.IsBackground = true;
t.Start(); t.Start();
} }
@@ -105,7 +110,6 @@ namespace ErgometerApplication
ComPort.Write("ST"); ComPort.Write("ST");
string response = ComPort.Read(); string response = ComPort.Read();
Console.WriteLine(response);
SaveMeting(response); SaveMeting(response);
} }
@@ -136,6 +140,8 @@ namespace ErgometerApplication
NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session)); NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
Loggedin = false; Loggedin = false;
running = false; running = false;
Doctor.Close();
Doctor = null;
} }
} }
@@ -183,6 +189,9 @@ namespace ErgometerApplication
case NetCommand.CommandType.SESSION: case NetCommand.CommandType.SESSION:
Session = command.Session; Session = command.Session;
break; break;
case NetCommand.CommandType.ERROR:
Console.WriteLine("An error occured, ignoring");
break;
default: default:
throw new FormatException("Error in Netcommand: Received command not recognized"); throw new FormatException("Error in Netcommand: Received command not recognized");
} }
@@ -5,10 +5,11 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace ErgometerApplication namespace ErgometerApplication
{ {
public class PanelGraphView:Panel public class PanelGraphView : Panel
{ {
private List<ChartPanel> charts; private List<ChartPanel> charts;
@@ -16,7 +17,7 @@ namespace ErgometerApplication
public PanelGraphView() : base() public PanelGraphView() : base()
{ {
createCharts(); createCharts();
flowlayout = new FlowLayoutPanel(); flowlayout = new FlowLayoutPanel();
@@ -26,22 +27,13 @@ namespace ErgometerApplication
flowlayout.Name = "flowlayout"; flowlayout.Name = "flowlayout";
flowlayout.Padding = new Padding(15); flowlayout.Padding = new Padding(15);
flowlayout.AutoScroll = true; flowlayout.AutoScroll = true;
foreach(ChartPanel chart in charts) foreach (ChartPanel chart in charts)
{ {
flowlayout.Controls.Add(chart); flowlayout.Controls.Add(chart);
} }
List<Meting> metingen = new List<Meting>(); List<Meting> metingen = new List<Meting>();
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));
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));
updateAllCharts(metingen); updateAllCharts(metingen);
// //
@@ -59,19 +51,18 @@ namespace ErgometerApplication
public void createCharts() public void createCharts()
{ {
charts = new List<ChartPanel>(); charts = new List<ChartPanel>();
charts.Add(new ChartPanel(ChartPanel.MetingType.HEARTBEAT)); charts.Add(new ChartPanel(ChartPanel.MetingType.HEARTBEAT, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.RPM)); charts.Add(new ChartPanel(ChartPanel.MetingType.RPM, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.SPEED)); charts.Add(new ChartPanel(ChartPanel.MetingType.SPEED, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.DISTANCE)); charts.Add(new ChartPanel(ChartPanel.MetingType.DISTANCE, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.ENERGY)); charts.Add(new ChartPanel(ChartPanel.MetingType.ENERGY, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.SECONDS)); charts.Add(new ChartPanel(ChartPanel.MetingType.POWER, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.POWER)); charts.Add(new ChartPanel(ChartPanel.MetingType.ACTUALPOWER, SeriesChartType.Line));
charts.Add(new ChartPanel(ChartPanel.MetingType.ACTUALPOWER));
} }
public void updateAllCharts(List<Meting> metingen) public void updateAllCharts(List<Meting> metingen)
{ {
foreach(ChartPanel chart in charts) foreach (ChartPanel chart in charts)
{ {
chart.updateChart(metingen); chart.updateChart(metingen);
} }