PatientForm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 FietsLibrary.JSONObjecten;
  13. using FietsLibrary;
  14. using System.Windows.Forms.DataVisualization.Charting;
  15. namespace FietsClient
  16. {
  17. public partial class PatientForm : Form
  18. {
  19. public TcpConnection _connection { get; private set; }
  20. private PatientModel patientModel;
  21. public PatientForm(TcpConnection connection)
  22. {
  23. this._connection = connection;
  24. InitializeComponent();
  25. patientModel = PatientModel.patientModel;
  26. patientModel.patientform = this;
  27. DataHandler.IncomingErrorEvent += HandleError; //initialize event
  28. _connection.IncomingChatmessageEvent += new TcpConnection.ChatmassegeDelegate(printMessage);
  29. //TIJDELIJK STUK CODE OM MESSAGE TE TESTEN
  30. //_connection.SendString("6|TOM|TOM|Je bent een homo");
  31. //Console.WriteLine("Bericht versturen");
  32. //EINDE TESTCODE
  33. }
  34. private void HandleError(string error)
  35. {
  36. switch (error)
  37. {
  38. case "WrongComPort":
  39. toolStripComboBox1.Text = "";
  40. MessageBox.Show("ERROR: Comport not initialized... trying to close the comport", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
  41. break;
  42. case "NotConnectedToBike":
  43. MessageBox.Show("ERROR: Not connected to bike.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. private void Form1_Load(object sender, EventArgs e)
  50. {
  51. string[] ports = SerialPort.GetPortNames();
  52. toolStripComboBox1.Items.AddRange(ports);
  53. MenuSessionItems();
  54. }
  55. private void requestDataToolStripMenuItem_Click(object sender, EventArgs e)
  56. {
  57. patientModel.startAskingData();
  58. }
  59. private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
  60. {
  61. patientModel.closeComPort();
  62. }
  63. private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
  64. {
  65. patientModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
  66. requestDataToolStripMenuItem.Enabled = true;
  67. closePortToolStripMenuItem.Enabled = true;
  68. }
  69. private void confirmDistanceBox_Click(object sender, EventArgs e)
  70. {
  71. int n;
  72. if (patientModel.askdata)
  73. {
  74. MessageBox.Show("Er is nog een sessie bezig, deze moet eerst gestopt worden");
  75. }
  76. else if (int.TryParse(distanceBox.Text, out n))
  77. {
  78. patientModel.setDistanceMode(distanceBox.Text);
  79. this.label19.Text = "Huidige sessie: Afstand: " + n;
  80. }
  81. else
  82. {
  83. MessageBox.Show("Distance is not a valid number.");
  84. }
  85. }
  86. private void confirmTimeBox_Click(object sender, EventArgs e)
  87. {
  88. int minutes, seconds;
  89. bool isNumericS = int.TryParse(minuteBox.Text, out minutes);
  90. bool isNumericM = int.TryParse(secondBox.Text, out seconds);
  91. if (patientModel.askdata)
  92. {
  93. MessageBox.Show("Er is nog een sessie bezig, deze moet eerst gestopt worden");
  94. }
  95. else if (isNumericM)
  96. {
  97. if (isNumericS)
  98. {
  99. patientModel.setTimeMode($"{ minutes:00}{seconds:00}");
  100. this.label19.Text = "Huidige sessie: Tijd: " + minutes + ":" + seconds;
  101. }
  102. else MessageBox.Show("Minutes is not a valid number.");
  103. }
  104. else MessageBox.Show("Seconds is not a valid number.");
  105. }
  106. private void stopTrainingToolStripMenuItem_Click(object sender, EventArgs e)
  107. {
  108. patientModel.reset();
  109. }
  110. private void setPower_Click(object sender, EventArgs e)
  111. {
  112. int n;
  113. if (int.TryParse(powerBox.Text, out n))
  114. patientModel.setPower(powerBox.Text);
  115. else
  116. MessageBox.Show("Power is not a valid number.");
  117. }
  118. private void sendButton_Click(object sender, EventArgs e)
  119. {
  120. if (messageBox.Text != null && patientModel.CurrentDoctorID != "")
  121. {
  122. String[] data = new String[2];
  123. data[0] = messageBox.Text;
  124. //receiver ID of doctor
  125. data[1] = patientModel.CurrentDoctorID;
  126. messageBox.Clear();
  127. _connection.SendChatMessage(data);
  128. }
  129. }
  130. private void messageBox_KeyPress(object sender, KeyPressEventArgs e)
  131. {
  132. if (e.KeyChar == '\r')
  133. {
  134. sendButton_Click(sender, e);
  135. }
  136. }
  137. private void MenuSessionItems()
  138. {
  139. foreach (Session s in _connection.currentData.GetSessions())
  140. {
  141. selectSessionToolStripMenuItem.DropDownItems.Add(
  142. new ToolStripMenuItem(s.id.ToString(), null, delegate
  143. {
  144. patientModel.stopAskingData();
  145. this.sessionBox.Text = s.id.ToString();
  146. this.nameBox.Text = _connection.userID;
  147. //get measurments
  148. List<Measurement> measurments = s.measurements;
  149. //fill boxes
  150. this.pulseBox.Text = measurments[measurments.Count - 1].pulse.ToString();
  151. this.rpmInfoBox.Text = measurments[measurments.Count - 1].rpm.ToString();
  152. this.speedInfoBox.Text = measurments[measurments.Count - 1].speed.ToString();
  153. this.distanceInfoBox.Text = measurments[measurments.Count - 1].distance.ToString();
  154. this.requestedBox.Text = measurments[measurments.Count - 1].requestedPower.ToString();
  155. this.energyInfoBox.Text = measurments[measurments.Count - 1].energy.ToString();
  156. this.timeBox.Text = measurments[measurments.Count - 1].time.ToString();
  157. this.actualBox.Text = measurments[measurments.Count - 1].actualPower.ToString();
  158. //fill speedpoints
  159. patientModel.speedPoints = new List<DataPoint>();
  160. for (int i = 0; i < measurments.Count; i++)
  161. {
  162. patientModel.speedPoints.Add(new DataPoint(measurments[i].time, measurments[i].speed));
  163. }
  164. //fill speedgraph
  165. this.speedChart.Series[0].Points.Clear();
  166. for (int i = 0; i < patientModel.speedPoints.Count; i++)
  167. this.speedChart.Series[0].Points.Add(patientModel.speedPoints[i]);
  168. this.speedChart.Update();
  169. //fill bpm
  170. patientModel.bpmPoints = new List<DataPoint>();
  171. for (int i = 0; i < measurments.Count; i++)
  172. {
  173. patientModel.bpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].pulse));
  174. }
  175. //fill bpmgraph
  176. this.bpmChart.Series[0].Points.Clear();
  177. for (int i = 0; i < patientModel.bpmPoints.Count; i++)
  178. this.bpmChart.Series[0].Points.Add(patientModel.bpmPoints[i]);
  179. this.bpmChart.Update();
  180. //fill rpm
  181. patientModel.rpmPoints = new List<DataPoint>();
  182. for (int i = 0; i < measurments.Count; i++)
  183. {
  184. patientModel.rpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].rpm));
  185. }
  186. //fill rpmgraph
  187. this.rpmChart.Series[0].Points.Clear();
  188. for (int i = 0; i < patientModel.rpmPoints.Count; i++)
  189. this.rpmChart.Series[0].Points.Add(patientModel.rpmPoints[i]);
  190. this.rpmChart.Update();
  191. })
  192. );
  193. }
  194. }
  195. private void printMessage(string[] data)
  196. {
  197. if (data[2].StartsWith("This is a broadcast: "))
  198. {
  199. string finalMessage = data[2] + "\r\n";
  200. chatBox.Invoke((MethodInvoker)delegate ()
  201. {
  202. chatBox.AppendText(finalMessage);
  203. });
  204. }
  205. else
  206. {
  207. if (data[0] != _connection.userID)
  208. patientModel.CurrentDoctorID = data[0];
  209. string finalMessage = data[0] + ":\t" + data[2] + "\r\n";
  210. chatBox.Invoke((MethodInvoker)delegate ()
  211. {
  212. chatBox.AppendText(finalMessage);
  213. });
  214. }
  215. }
  216. private void PatientForm_FormClosing(object sender, FormClosingEventArgs e)
  217. {
  218. _connection.disconnect();
  219. Application.Exit();
  220. }
  221. private void button1_Click(object sender, EventArgs e)
  222. {
  223. _connection.StartNewSession();
  224. }
  225. private void button2_Click(object sender, EventArgs e)
  226. {
  227. _connection.StopSessoin();
  228. }
  229. private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
  230. {
  231. Form activeForm = Form.ActiveForm;
  232. if (activeForm != null)
  233. {
  234. activeForm.Invoke((MethodInvoker)delegate ()
  235. {
  236. Login login = new Login(_connection);
  237. activeForm.Hide();
  238. login.Show();
  239. });
  240. }
  241. }
  242. }
  243. }