Move ChatItem to library. Fix bugs with connecting.

This commit is contained in:
2015-10-19 18:52:09 +02:00
parent 14c332bd16
commit f03ccad863
9 changed files with 56 additions and 110 deletions
@@ -1,63 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ErgometerApplication
{
public class ChatItem : Panel
{
private Label messageLabel, timeLabel;
public ChatItem(string message, string time, bool isDoctor) : base()
{
this.messageLabel = new Label();
this.timeLabel = new Label();
this.AutoSize = true;
this.setAppearance(isDoctor);
this.Controls.Add(this.messageLabel);
this.Controls.Add(this.timeLabel);
this.MinimumSize = new System.Drawing.Size(150, 60);
this.Name = "messageContainer";
this.Padding = new System.Windows.Forms.Padding(6);
//
// Message Label
//
this.messageLabel.AutoSize = true;
this.messageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.messageLabel.Font = new System.Drawing.Font("Segoe UI Semibold", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.messageLabel.Location = new System.Drawing.Point(6, 6);
this.messageLabel.MaximumSize = new System.Drawing.Size(250, 0);
this.messageLabel.Size = new System.Drawing.Size(121, 20);
this.messageLabel.Text = message;
//
// Time Label
//
this.timeLabel.AutoSize = true;
this.timeLabel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.timeLabel.Font = new System.Drawing.Font("Segoe UI Semilight", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.timeLabel.Location = new System.Drawing.Point(6, 26);
this.timeLabel.Margin = new System.Windows.Forms.Padding(6);
this.timeLabel.Size = new System.Drawing.Size(32, 13);
this.timeLabel.Text = "Time:" + time;
}
private void setAppearance(bool isDoctor)
{
if (isDoctor)
{
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.Dock = DockStyle.Right;
}
else
{
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
this.Dock = DockStyle.Left;
}
}
}
}
@@ -151,7 +151,8 @@ namespace ErgometerApplication
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 600);
this.ClientSize = new System.Drawing.Size(700, 500);
this.MinimumSize = new System.Drawing.Size(550, 450);
this.Controls.Add(this.panelTopBar);
this.Controls.Add(this.panelLogin);
this.Controls.Add(this.panelClientContainer);
@@ -21,10 +21,13 @@ namespace ErgometerApplication
public partial class ClientApplicatie : Form
{
public PanelClientChat chat;
private int count;
public ClientApplicatie()
{
InitializeComponent();
MainClient.Init(this);
count = 0;
}
private void updateTimer_Tick(object sender, EventArgs e)
@@ -44,6 +47,14 @@ namespace ErgometerApplication
power.updateValue(m.Power);
energy.updateValue(m.Energy);
actualpower.updateValue(m.ActualPower);
if(count >= 10)
{
count = 0;
panelGraphView.updateAllCharts(MainClient.Metingen);
}
count++;
}
}
@@ -59,37 +70,30 @@ namespace ErgometerApplication
}
if (password.Length > 0 && password.Length < 4)
{
panelLogin.lblVerification.Text = "Vul een wachtwoord in van minimaal 8 karakters.";
panelLogin.lblVerification.Text = "Vul een wachtwoord in van minimaal 4 karakters.";
panelLogin.lblVerification.ForeColor = Color.Red;
panelLogin.lblVerification.Visible = true;
}
if (password.Length >= 3)
{
/*
if(SerialPort.GetPortNames().Length <= 0)
string error = "";
bool connect = MainClient.Connect(SerialPort.GetPortNames()[0], username, password, out error);
if (connect)
{
panelLogin.lblVerification.Text = "De Ergometer is niet gevonden.";
panelClientContainer.BringToFront();
chat = panelClientChat;
this.labelUsername.Text = panelLogin.textBoxUsername.Text;
panelTopBar.Visible = true;
updateTimer.Start();
}
else
{
panelLogin.lblVerification.Text = error;
panelLogin.lblVerification.ForeColor = Color.Red;
panelLogin.lblVerification.Visible = true;
}
else
{ */
if(MainClient.Connect(SerialPort.GetPortNames()[0], username, password))
{
panelClientContainer.BringToFront();
chat = panelClientChat;
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;
}
//}
}
}
else
@@ -59,9 +59,6 @@
<Compile Include="ChartPanel.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ChatItem.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="MainClient.cs" />
<Compile Include="PanelClientData.cs">
<SubType>Component</SubType>
@@ -49,11 +49,20 @@ namespace ErgometerApplication
Client = client;
}
public static bool Connect(string comport, string name, string password)
public static bool Connect(string comport, string name, string password, out string error)
{
error = "Succes";
if (!Doctor.Connected)
{
Doctor.Connect(HOST, PORT);
try {
Doctor.Connect(HOST, PORT);
}
catch (Exception e)
{
error = "De server is niet online.";
return false;
}
Name = name;
NetCommand net = NetHelper.ReadNetCommand(Doctor);
@@ -63,6 +72,7 @@ namespace ErgometerApplication
throw new Exception("Session not assigned");
running = true;
t.IsBackground = true;
t.Start();
}
@@ -75,6 +85,7 @@ namespace ErgometerApplication
if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
{
Loggedin = false;
error = "De inloggegevens zijn onjuist.";
return false;
}
@@ -99,7 +110,10 @@ namespace ErgometerApplication
SaveMeting(response);
}
else
throw new Exception("Comport was unable to connect");
{
error = "De ergometer is niet verbonden";
return false;
}
}
return true;
@@ -159,8 +173,9 @@ namespace ErgometerApplication
ParseValueSet(command);
break;
case NetCommand.CommandType.CHAT:
Chat.Add(new ChatMessage(Name, command.ChatMessage, true));
Client.chat.Invoke(Client.chat.passChatMessage, new Object[] {command.ChatMessage, Helper.MillisecondsToTime(command.Timestamp), true});
ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, true);
Chat.Add(chat);
Client.chat.Invoke(Client.chat.passChatMessage, new Object[] { chat });
break;
case NetCommand.CommandType.RESPONSE:
Console.WriteLine(command.Response.ToString());
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ErgometerLibrary;
using ErgometerLibrary.Chat;
namespace ErgometerApplication
{
@@ -66,7 +67,7 @@ namespace ErgometerApplication
this.connectionLabel.Name = "connectionLabel";
this.connectionLabel.Size = new System.Drawing.Size(112, 13);
this.connectionLabel.TabIndex = 0;
this.connectionLabel.Text = "Now connected with:";
this.connectionLabel.Text = "Now connected with: Doctor";
//
// label2
//
@@ -100,7 +101,7 @@ namespace ErgometerApplication
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(85, 90);
this.button1.TabIndex = 1;
this.button1.Text = "Verstuur";
this.button1.Text = "Send";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
@@ -147,27 +148,17 @@ namespace ErgometerApplication
{
if (richTextBox1.TextLength > 1)
{
AddChatItem(richTextBox1.Text, Helper.MillisecondsToTime(Helper.Now), false);
AddChatItem(new ChatMessage("Anon", richTextBox1.Text, false));
MainClient.SendNetCommand(new NetCommand(richTextBox1.Text, MainClient.Session));
richTextBox1.ResetText();
}
}
public void AddChatItem(string text)
public void AddChatItem(ChatMessage chat)
{
flowLayoutPanel1.Controls.Add(new ChatItem(text, Helper.MillisecondsToTime(Helper.Now), true));
flowLayoutPanel1.Controls.Add(new ChatItem(chat));
}
public void AddChatItem(string text, bool isDoctor)
{
flowLayoutPanel1.Controls.Add(new ChatItem(text, Helper.MillisecondsToTime(Helper.Now), isDoctor));
}
public void AddChatItem(string text, string time, bool isDoctor)
{
flowLayoutPanel1.Controls.Add(new ChatItem(text,time,isDoctor));
}
private void panel3_MouseWheel(object sender, MouseEventArgs e)
{
panel3.Focus();
@@ -201,10 +192,11 @@ namespace ErgometerApplication
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, new EventArgs());
e.Handled = true;
}
}
public delegate void ChatDelegate(string text, string time, bool isDoctor);
public delegate void ChatDelegate(ChatMessage chat);
public ChatDelegate passChatMessage;
}
}
@@ -11,7 +11,7 @@ namespace ErgometerApplication
{
public PanelClientChat panelClientChat;
private PanelGraphView panelGraphView;
public PanelGraphView panelGraphView;
public PanelClientContainer() : base()
{