Added login, connecting and showing data

This commit is contained in:
2015-10-08 22:40:53 +02:00
parent 3d1bc8ebb9
commit a46f7bee93
9 changed files with 90 additions and 108 deletions
@@ -43,13 +43,13 @@ namespace ErgometerApplication
this.labelUsername = new System.Windows.Forms.Label();
this.labelHallo = new System.Windows.Forms.Label();
this.heartBeat = new PanelClientData("Hartslag");
this.RPM = new PanelClientData("RPM");
this.speed = new PanelClientData("Snelheid");
this.distance = new PanelClientData("Afstand (km)");
this.power = new PanelClientData("Weerstand");
this.energy = new PanelClientData("Energie");
this.actualpower = new PanelClientData("Absolute Weerstand");
this.heartBeat = new PanelClientData("Hartslag", 0, 250);
this.RPM = new PanelClientData("RPM", 0, 100);
this.speed = new PanelClientData("Snelheid", 0, 500);
this.distance = new PanelClientData("Afstand (km)", 0, 1000);
this.power = new PanelClientData("Weerstand", 25, 400);
this.energy = new PanelClientData("Energie", 0, 200);
this.actualpower = new PanelClientData("Absolute Weerstand", 0, 400);
this.panelClientContainer.SuspendLayout();
this.panelTopBar.SuspendLayout();
@@ -27,11 +27,21 @@ namespace ErgometerApplication
private void updateTimer_Tick(object sender, EventArgs e)
{
Console.WriteLine("Trying to send data....");
if(MainClient.Doctor.Connected)
{
Console.WriteLine("Sending data");
MainClient.ComPort.Write("ST");
string response = MainClient.ComPort.Read();
MainClient.SaveMeting(response);
Meting m = MainClient.SaveMeting(response);
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);
}
}
@@ -62,10 +72,20 @@ namespace ErgometerApplication
}
else
{ */
MainClient.Connect(SerialPort.GetPortNames()[0], username, password);
panelClientContainer.BringToFront();
this.labelUsername.Text = panelLogin.textBoxUsername.Text;
panelTopBar.Visible = true;
if(MainClient.Connect(SerialPort.GetPortNames()[0], username, password))
{
panelClientContainer.BringToFront();
this.labelUsername.Text = panelLogin.textBoxUsername.Text;
panelTopBar.Visible = true;
updateTimer.Start();
}
else
{
panelLogin.lblVerification.Text = "De inloggegevens zijn onjuist.";
panelLogin.lblVerification.ForeColor = Color.Red;
panelLogin.lblVerification.Visible = true;
}
//}
}
}
@@ -112,6 +132,7 @@ namespace ErgometerApplication
panelLogin.BringToFront();
panelTopBar.Visible = false;
sessionLogout();
updateTimer.Stop();
}
}
}
@@ -1,46 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using ErgometerLibrary;
namespace ErgometerApplication
{
class DataSaving
{
//List<Meting> local_data = null;
//private List<Meting> ReadFile()
//{
// string path;
// OpenFileDialog file = new OpenFileDialog();
// file.Filter = "Ergometer files(*.ergo) | *.ergo | All files(*.*) | *.*";
// if (file.ShowDialog() == DialogResult.OK)
// {
// path = file.FileName;
// List<Meting> readFile = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Meting>>(System.IO.File.ReadAllText(path));
// return readFile;
// }
// else
// {
// return null;
// }
//}
//private List<Meting> SaveData(Meting meting, List<Meting> _data)
//{
// List<Meting> local_data = _data;
// local_data.Add(meting);
// return local_data;
//}
//private void WriteFile(List<Meting> _data)
//{
// 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);
//}
}
}
@@ -70,8 +70,6 @@
<Compile Include="PanelLogin.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ServerCommunicator.cs" />
<Compile Include="DataSaving.cs" />
<Compile Include="ClientApplicatie.cs">
<SubType>Form</SubType>
</Compile>
@@ -17,6 +17,7 @@ namespace ErgometerApplication
public static List<Meting> Metingen { get; }
public static int Session;
public static bool Loggedin;
public static string HOST = "127.0.0.1";
public static int PORT = 8888;
@@ -28,10 +29,37 @@ namespace ErgometerApplication
Metingen = new List<Meting>();
Session = 0;
Loggedin = false;
}
public static void Connect(string comport, string name, string password)
public static bool Connect(string comport, string name, string password)
{
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");
}
if(! Loggedin)
{
NetCommand command = new NetCommand(name, false, password, Session);
NetHelper.SendNetCommand(Doctor, command);
NetCommand response = NetHelper.ReadNetCommand(Doctor);
if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
{
Loggedin = false;
return false;
}
Loggedin = true;
}
if (!ComPort.IsOpen())
{
if (ComPort.Connect(comport))
@@ -52,25 +80,8 @@ namespace ErgometerApplication
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");
return true;
}
public static void Disconnect()
@@ -84,19 +95,16 @@ namespace ErgometerApplication
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));
Loggedin = false;
Doctor.Close();
}
else
throw new Exception("Client is already disconnected");
}
public static void SaveMeting(string meting)
public static Meting SaveMeting(string meting)
{
Meting m = Meting.Parse(meting);
Metingen.Add(m);
@@ -104,6 +112,8 @@ namespace ErgometerApplication
{
NetHelper.SendNetCommand(Doctor, new NetCommand(m, Session));
}
return m;
}
}
}
@@ -13,8 +13,14 @@ namespace ErgometerApplication
public ProgressBar progressBarMeting;
public Label metingName;
public PanelClientData(string name) : base()
private int min;
private int max;
public PanelClientData(string name, int min, int max) : base()
{
this.min = min;
this.max = max;
this.metingName = new System.Windows.Forms.Label();
this.progressBarMeting = new System.Windows.Forms.ProgressBar();
this.labelMetingCurrentValue = new System.Windows.Forms.Label();
@@ -47,7 +53,7 @@ namespace ErgometerApplication
this.progressBarMeting.Name = "progressBarMeting";
this.progressBarMeting.Size = new System.Drawing.Size(183, 23);
this.progressBarMeting.TabIndex = 1;
this.progressBarMeting.Value = 50;
this.progressBarMeting.Value = 0;
//
// labelMetingCurrentValue
//
@@ -58,7 +64,7 @@ namespace ErgometerApplication
this.labelMetingCurrentValue.Name = "labelMetingCurrentValue";
this.labelMetingCurrentValue.Size = new System.Drawing.Size(57, 32);
this.labelMetingCurrentValue.TabIndex = 2;
this.labelMetingCurrentValue.Text = "255";
this.labelMetingCurrentValue.Text = "0";
}
public void setText(string text)
@@ -69,19 +75,30 @@ namespace ErgometerApplication
public void updateValue(int value)
{
this.labelMetingCurrentValue.Text = value.ToString();
this.progressBarMeting.Value = value;
this.progressBarMeting.Value = ValueToPercentage(value);
}
public void updateValue(double value)
{
this.labelMetingCurrentValue.Text = value.ToString();
this.progressBarMeting.Value = (int)value;
this.progressBarMeting.Value = ValueToPercentage((int)value);
}
public void updateValue(decimal value)
{
this.labelMetingCurrentValue.Text = value.ToString();
this.progressBarMeting.Value = (int)value;
this.progressBarMeting.Value = ValueToPercentage((int)value);
}
private int ValueToPercentage(int value)
{
if (value < min)
return 0;
if (value > max)
return 100;
return ((value - min) * 100) / (max - min);
}
}
}
@@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
//using ErgometerLibrary;
namespace ErgometerApplication
{
class ServerCommunicator
{
public TcpClient client { get; set; }
public int sessionId { get; set; }
// public List<Meting> data { get; set; }
}
}