ClientApplicatie.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. private int count;
  24. public ClientApplicatie()
  25. {
  26. InitializeComponent();
  27. MainClient.Init(this);
  28. count = 0;
  29. }
  30. private void updateTimer_Tick(object sender, EventArgs e)
  31. {
  32. if(MainClient.Doctor.Connected)
  33. {
  34. MainClient.ComPort.Write("ST");
  35. string response = MainClient.ComPort.Read();
  36. if (response != "err")
  37. {
  38. Meting m = MainClient.SaveMeting(response);
  39. heartBeat.updateValue(m.HeartBeat);
  40. RPM.updateValue(m.RPM);
  41. speed.updateValue(m.Speed);
  42. distance.updateValue(m.Distance);
  43. power.updateValue(m.Power);
  44. energy.updateValue(m.Energy);
  45. actualpower.updateValue(m.ActualPower);
  46. time.updateValue(m.Seconds);
  47. if (count >= 10)
  48. {
  49. count = 0;
  50. panelGraphView.updateAllCharts(MainClient.Metingen);
  51. }
  52. count++;
  53. }
  54. else
  55. {
  56. logout("De Ergometer is niet meer verbonden.", Color.Red);
  57. }
  58. }
  59. }
  60. public void validateLogin(string username, string password)
  61. {
  62. if (username.Length > 0)
  63. {
  64. if (password.Length == 0)
  65. {
  66. panelLogin.lblVerification.Text = "Vul een wachtwoord in.";
  67. panelLogin.lblVerification.ForeColor = Color.Red;
  68. panelLogin.lblVerification.Visible = true;
  69. }
  70. if (password.Length > 0)
  71. {
  72. string error = "";
  73. bool connect = MainClient.Connect(SerialPort.GetPortNames()[0], username, password, out error);
  74. if (connect)
  75. {
  76. panelClientContainer.BringToFront();
  77. chat = panelClientChat;
  78. this.labelUsername.Text = panelLogin.textBoxUsername.Text;
  79. panelTopBar.Visible = true;
  80. updateTimer.Start();
  81. }
  82. else
  83. {
  84. panelLogin.lblVerification.Text = error;
  85. panelLogin.lblVerification.ForeColor = Color.Red;
  86. panelLogin.lblVerification.Visible = true;
  87. }
  88. }
  89. }
  90. else
  91. {
  92. panelLogin.lblVerification.Text = "Vul een gebruikersnaam in.";
  93. panelLogin.lblVerification.ForeColor = Color.Red;
  94. panelLogin.lblVerification.Visible = true;
  95. }
  96. }
  97. private void ClientApplicatie_Resize(object sender, System.EventArgs e)
  98. {
  99. Control control = (Control)sender;
  100. if (control.Size.Width < 980)
  101. {
  102. panelGraphView.Visible = false;
  103. panelClientChat.Width = 400;
  104. panelDataViewLeft.Dock = DockStyle.Fill;
  105. }
  106. if (control.Size.Width >= 980 && control.Size.Width < 1368)
  107. {
  108. panelGraphView.Visible = true;
  109. panelDataViewLeft.Width = 250;
  110. panelClientChat.Width = 400;
  111. panelDataViewLeft.Dock = DockStyle.Left;
  112. }
  113. if (control.Size.Width >= 1368)
  114. {
  115. panelGraphView.Visible = true;
  116. panelDataViewLeft.Width = 400;
  117. panelDataViewLeft.Dock = DockStyle.Left;
  118. }
  119. }
  120. private void buttonLogOff_Click(object sender, EventArgs e)
  121. {
  122. logout("U bent uitgelogd.", Color.Blue);
  123. }
  124. private void logout(string message, System.Drawing.Color cl)
  125. {
  126. panelLogin.BringToFront();
  127. panelTopBar.Visible = false;
  128. panelLogin.lblVerification.Text = message;
  129. panelLogin.lblVerification.ForeColor = cl;
  130. panelLogin.lblVerification.Visible = true;
  131. panelLogin.textBoxUsername.Text = "";
  132. panelLogin.textBoxPassword.Text = "";
  133. MainClient.Disconnect();
  134. updateTimer.Stop();
  135. }
  136. }
  137. }