ClientApplicatie.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using Newtonsoft.Json;
  13. using System.IO;
  14. using System.Net.Sockets;
  15. using System.Net;
  16. using System.Timers;
  17. using ErgometerLibrary;
  18. namespace ErgometerApplication
  19. {
  20. public partial class ClientApplicatie : Form
  21. {
  22. public PanelClientChat chat;
  23. public ErgometerTest ergotest;
  24. public ClientApplicatie()
  25. {
  26. InitializeComponent();
  27. MainClient.Init(this);
  28. }
  29. private void updateBeep(object sender, EventArgs e)
  30. {
  31. Console.Beep(1000,5);
  32. }
  33. private void updateTimer_Tick(object sender, EventArgs e)
  34. {
  35. if (MainClient.Doctor.Connected)
  36. {
  37. MainClient.ComPort.Write("ST");
  38. string response = MainClient.ComPort.Read();
  39. if (response != "err")
  40. {
  41. Meting m = MainClient.SaveMeting(response);
  42. heartBeat.updateValue(m.HeartBeat);
  43. RPM.updateValue(m.RPM);
  44. speed.updateValue(m.Speed);
  45. distance.updateValue(m.Distance);
  46. power.updateValue(m.Power);
  47. energy.updateValue(m.Energy);
  48. actualpower.updateValue(m.ActualPower);
  49. time.updateValue(m.Seconds);
  50. if(ergotest != null)
  51. ergotest.timerTick();
  52. }
  53. else
  54. {
  55. logout("De Ergometer is niet meer verbonden.", Color.Red);
  56. }
  57. }
  58. }
  59. public void validateLogin(string username, string password)
  60. {
  61. if (username.Length > 0)
  62. {
  63. if (password.Length == 0)
  64. {
  65. panelLogin.lblVerification.Text = "Vul een wachtwoord in.";
  66. panelLogin.lblVerification.ForeColor = Color.Red;
  67. panelLogin.lblVerification.Visible = true;
  68. }
  69. if (password.Length > 0)
  70. {
  71. string error = "";
  72. bool connect = MainClient.Connect(SerialPort.GetPortNames()[0], username, password, out error);
  73. if (connect)
  74. {
  75. panelGatherInfo.BringToFront();
  76. panelTopBar.Visible = true;
  77. this.labelUsername.Text = panelLogin.textBoxUsername.Text;
  78. }
  79. else
  80. {
  81. panelLogin.lblVerification.Text = error;
  82. panelLogin.lblVerification.ForeColor = Color.Red;
  83. panelLogin.lblVerification.Visible = true;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. panelLogin.lblVerification.Text = "Vul een gebruikersnaam in.";
  90. panelLogin.lblVerification.ForeColor = Color.Red;
  91. panelLogin.lblVerification.Visible = true;
  92. }
  93. }
  94. public void updateStepsText(string text)
  95. {
  96. steps.setText(text);
  97. MainClient.SendNetCommand(new NetCommand(MainClient.Session, text));
  98. }
  99. public void CreateNewTest(char geslacht, int leeftijd, int gewicht, int lengte)
  100. {
  101. panelTopBar.Visible = true;
  102. panelClientContainer.BringToFront();
  103. chat = panelClientChat;
  104. MainClient.SendNetCommand(new NetCommand(gewicht, lengte, leeftijd, geslacht, MainClient.Session));
  105. MainClient.ComPort.Write("RS");
  106. MainClient.ComPort.Read();
  107. Thread.Sleep(200);
  108. MainClient.ComPort.Write("CM");
  109. MainClient.ComPort.Read();
  110. ergotest = new ErgometerTest(gewicht, lengte, leeftijd, geslacht, this);
  111. updateTimer.Start();
  112. beeptimer.Start();
  113. }
  114. private void buttonLogOff_Click(object sender, EventArgs e)
  115. {
  116. logout("U bent uitgelogd.", Color.Blue);
  117. }
  118. private void logout(string message, System.Drawing.Color cl)
  119. {
  120. MainClient.Disconnect();
  121. updateTimer.Stop();
  122. beeptimer.Stop();
  123. ergotest = null;
  124. panelTopBar.Visible = false;
  125. panelLogin.lblVerification.Text = message;
  126. panelLogin.lblVerification.ForeColor = cl;
  127. panelLogin.lblVerification.Visible = true;
  128. panelLogin.textBoxUsername.Text = "";
  129. panelLogin.textBoxPassword.Text = "";
  130. panelLogin.BringToFront();
  131. }
  132. }
  133. }