PatientForm.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 FietsClient.JSONObjecten;
  13. using System.Windows.Forms.DataVisualization.Charting;
  14. namespace FietsClient
  15. {
  16. public partial class PatientForm : Form
  17. {
  18. private TcpConnection _connection;
  19. private PatientModel patienModel;
  20. public PatientForm(TcpConnection connection)
  21. {
  22. this._connection = connection;
  23. InitializeComponent();
  24. patienModel = PatientModel.patientModel;
  25. patienModel.patientform = this;
  26. DataHandler.IncomingErrorEvent += HandleError; //initialize event
  27. _connection.IncomingChatmessageEvent += new TcpConnection.ChatmassegeDelegate(printMessage);
  28. }
  29. private void HandleError(string error)
  30. {
  31. switch (error)
  32. {
  33. case "WrongComPort":
  34. toolStripComboBox1.Text = "";
  35. MessageBox.Show("ERROR: Comport not initialized... trying to close the comport", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
  36. break;
  37. case "NotConnectedToBike":
  38. MessageBox.Show("ERROR: Not connected to bike.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. private void Form1_Load(object sender, EventArgs e)
  45. {
  46. string[] ports = SerialPort.GetPortNames();
  47. toolStripComboBox1.Items.AddRange(ports);
  48. MenuSessionItems();
  49. }
  50. private void requestDataToolStripMenuItem_Click(object sender, EventArgs e)
  51. {
  52. patienModel.startAskingData();
  53. }
  54. private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
  55. {
  56. patienModel.closeComPort();
  57. }
  58. private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
  59. {
  60. patienModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
  61. requestDataToolStripMenuItem.Enabled = true;
  62. closePortToolStripMenuItem.Enabled = true;
  63. }
  64. private void confirmDistanceBox_Click(object sender, EventArgs e)
  65. {
  66. int n;
  67. if (int.TryParse(distanceBox.Text, out n))
  68. {
  69. patienModel.setDistanceMode(distanceBox.Text);
  70. }
  71. else
  72. {
  73. MessageBox.Show("Distance is not a valid number.");
  74. }
  75. }
  76. private void confirmTimeBox_Click(object sender, EventArgs e)
  77. {
  78. int minutes, seconds;
  79. bool isNumericS = int.TryParse(minuteBox.Text, out minutes);
  80. bool isNumericM = int.TryParse(secondBox.Text, out seconds);
  81. if (isNumericM)
  82. {
  83. if (isNumericS)
  84. patienModel.setTimeMode(minutes + ":" + seconds);
  85. else MessageBox.Show("Minutes is not a valid number.");
  86. }
  87. else MessageBox.Show("Seconds is not a valid number.");
  88. }
  89. private void stopTrainingToolStripMenuItem_Click(object sender, EventArgs e)
  90. {
  91. patienModel.reset();
  92. }
  93. private void setPower_Click(object sender, EventArgs e)
  94. {
  95. int n;
  96. if (int.TryParse(powerBox.Text, out n))
  97. patienModel.setPower(powerBox.Text);
  98. else
  99. MessageBox.Show("Power is not a valid number.");
  100. }
  101. private void sendButton_Click(object sender, EventArgs e)
  102. {
  103. if (messageBox.Text != null)
  104. {
  105. string message = messageBox.Text;
  106. messageBox.Clear();
  107. _connection.SendChatMessage(message);
  108. }
  109. }
  110. private void messageBox_KeyPress(object sender, KeyPressEventArgs e)
  111. {
  112. sendButton_Click(sender, e);
  113. }
  114. private void MenuSessionItems()
  115. {
  116. foreach (Session s in _connection.currentData.GetSessions())
  117. {
  118. selectSessionToolStripMenuItem.DropDownItems.Add(
  119. new ToolStripMenuItem(s.id.ToString(), null, delegate
  120. {
  121. this.sessionBox.Text = s.id.ToString();
  122. this.nameBox.Text = _connection.userID;
  123. //get measurments
  124. List<Measurement> measurments = s.session;
  125. //fill boxes
  126. this.pulseBox.Text = measurments[measurments.Count - 1].bpm.ToString();
  127. this.rpmInfoBox.Text = measurments[measurments.Count - 1].rpm.ToString();
  128. this.speedInfoBox.Text = measurments[measurments.Count - 1].speed.ToString();
  129. this.distanceInfoBox.Text = measurments[measurments.Count - 1].distance.ToString();
  130. this.requestedBox.Text = measurments[measurments.Count - 1].requestedPower.ToString();
  131. this.energyInfoBox.Text = measurments[measurments.Count - 1].energy.ToString();
  132. this.timeBox.Text = measurments[measurments.Count - 1].time.ToString();
  133. this.actualBox.Text = measurments[measurments.Count - 1].actualPower.ToString();
  134. //fill speedpoints
  135. patienModel.speedPoints = new List<DataPoint>();
  136. for (int i = 0; i < measurments.Count; i++)
  137. {
  138. patienModel.speedPoints.Add(new DataPoint(measurments[i].time, measurments[i].speed));
  139. }
  140. //fill speedgraph
  141. this.speedChart.Series[0].Points.Clear();
  142. for (int i = 0; i < patienModel.speedPoints.Count; i++)
  143. this.speedChart.Series[0].Points.Add(patienModel.speedPoints[i]);
  144. this.speedChart.Update();
  145. //fill bpm
  146. patienModel.bpmPoints = new List<DataPoint>();
  147. for (int i = 0; i < measurments.Count; i++)
  148. {
  149. patienModel.bpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].bpm));
  150. }
  151. //fill bpmgraph
  152. this.bpmChart.Series[0].Points.Clear();
  153. for (int i = 0; i < patienModel.bpmPoints.Count; i++)
  154. this.bpmChart.Series[0].Points.Add(patienModel.bpmPoints[i]);
  155. this.bpmChart.Update();
  156. //fill rpm
  157. patienModel.rpmPoints = new List<DataPoint>();
  158. for (int i = 0; i < measurments.Count; i++)
  159. {
  160. patienModel.rpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].rpm));
  161. }
  162. //fill rpmgraph
  163. this.rpmChart.Series[0].Points.Clear();
  164. for (int i = 0; i < patienModel.rpmPoints.Count; i++)
  165. this.rpmChart.Series[0].Points.Add(patienModel.rpmPoints[i]);
  166. this.rpmChart.Update();
  167. })
  168. );
  169. }
  170. }
  171. private void printMessage(string[] data)
  172. {
  173. string finalMessage = data[1] + ":\t\t" + data[3] + "\r\n";
  174. chatBox.AppendText(finalMessage);
  175. }
  176. }
  177. }