ClientApplicatie.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. power.updateValue(m.Power);
  45. actualpower.updateValue(m.ActualPower);
  46. time.updateValue(m.Seconds);
  47. if(ergotest != null)
  48. ergotest.timerTick();
  49. }
  50. else
  51. {
  52. logout("De Ergometer is niet meer verbonden.", Color.Red);
  53. }
  54. }
  55. }
  56. public void validateLogin(string username, string password)
  57. {
  58. if (username.Length > 0)
  59. {
  60. if (password.Length == 0)
  61. {
  62. panelLogin.lblVerification.Text = "Vul een wachtwoord in.";
  63. panelLogin.lblVerification.ForeColor = Color.Red;
  64. panelLogin.lblVerification.Visible = true;
  65. }
  66. if (password.Length > 0)
  67. {
  68. if (SerialPort.GetPortNames().Length > 0)
  69. {
  70. string error = "";
  71. bool connect = false;
  72. foreach(string comport in SerialPort.GetPortNames().Reverse())
  73. {
  74. connect = MainClient.Connect(comport, username, password, out error);
  75. if (connect)
  76. break;
  77. }
  78. if (connect)
  79. {
  80. panelGatherInfo.BringToFront();
  81. panelTopBar.Visible = true;
  82. this.labelUsername.Text = panelLogin.textBoxUsername.Text;
  83. }
  84. else
  85. {
  86. panelLogin.lblVerification.Text = error;
  87. panelLogin.lblVerification.ForeColor = Color.Red;
  88. panelLogin.lblVerification.Visible = true;
  89. }
  90. }
  91. else
  92. {
  93. panelLogin.lblVerification.Text = "De Ergometer is niet verbonden.";
  94. panelLogin.lblVerification.ForeColor = Color.Red;
  95. panelLogin.lblVerification.Visible = true;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. panelLogin.lblVerification.Text = "Vul een gebruikersnaam in.";
  102. panelLogin.lblVerification.ForeColor = Color.Red;
  103. panelLogin.lblVerification.Visible = true;
  104. }
  105. }
  106. public void updateStepsText(string text)
  107. {
  108. steps.setText(text);
  109. MainClient.SendNetCommand(new NetCommand(MainClient.Session, text));
  110. }
  111. public void CreateNewTest(char geslacht, int leeftijd, int gewicht, int lengte)
  112. {
  113. panelTopBar.Visible = true;
  114. panelClientContainer.BringToFront();
  115. chat = panelClientChat;
  116. MainClient.SendNetCommand(new NetCommand(gewicht, lengte, leeftijd, geslacht, MainClient.Session));
  117. MainClient.ComPort.Write("RS");
  118. MainClient.ComPort.Read();
  119. Thread.Sleep(200);
  120. MainClient.ComPort.Write("CM");
  121. MainClient.ComPort.Read();
  122. ergotest = new ErgometerTest(gewicht, lengte, leeftijd, geslacht, this);
  123. updateTimer.Start();
  124. beeptimer.Start();
  125. }
  126. private void buttonLogOff_Click(object sender, EventArgs e)
  127. {
  128. logout("U bent uitgelogd.", Color.Blue);
  129. }
  130. private void logout(string message, System.Drawing.Color cl)
  131. {
  132. MainClient.Disconnect();
  133. updateTimer.Stop();
  134. beeptimer.Stop();
  135. ergotest = null;
  136. panelTopBar.Visible = false;
  137. panelLogin.lblVerification.Text = message;
  138. panelLogin.lblVerification.ForeColor = cl;
  139. panelLogin.lblVerification.Visible = true;
  140. panelLogin.textBoxUsername.Text = "";
  141. panelLogin.textBoxPassword.Text = "";
  142. panelLogin.BringToFront();
  143. }
  144. }
  145. }