PatientForm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. closePortToolStripMenuItem.Enabled = true;
  67. }
  68. private void confirmDistanceBox_Click(object sender, EventArgs e)
  69. {
  70. int n;
  71. if (patientModel.askdata)
  72. {
  73. MessageBox.Show("Er is nog een sessie bezig, deze moet eerst gestopt worden");
  74. }
  75. else if (int.TryParse(distanceBox.Text, out n))
  76. {
  77. patientModel.setDistanceMode(distanceBox.Text);
  78. this.label19.Text = "Huidige sessie: Afstand: " + n;
  79. }
  80. else
  81. {
  82. MessageBox.Show("Distance is not a valid number.");
  83. }
  84. }
  85. private void confirmTimeBox_Click(object sender, EventArgs e)
  86. {
  87. int minutes, seconds;
  88. bool isNumericS = int.TryParse(minuteBox.Text, out minutes);
  89. bool isNumericM = int.TryParse(secondBox.Text, out seconds);
  90. if (patientModel.askdata)
  91. {
  92. MessageBox.Show("Er is nog een sessie bezig, deze moet eerst gestopt worden");
  93. }
  94. else if (isNumericM)
  95. {
  96. if (isNumericS)
  97. {
  98. patientModel.setTimeMode($"{ minutes:00}{seconds:00}");
  99. this.label19.Text = "Huidige sessie: Tijd: " + minutes + ":" + seconds;
  100. }
  101. else MessageBox.Show("Minutes is not a valid number.");
  102. }
  103. else MessageBox.Show("Seconds is not a valid number.");
  104. }
  105. private void stopTrainingToolStripMenuItem_Click(object sender, EventArgs e)
  106. {
  107. patientModel.reset();
  108. }
  109. private void setPower_Click(object sender, EventArgs e)
  110. {
  111. int n;
  112. if (int.TryParse(powerBox.Text, out n))
  113. patientModel.setPower(powerBox.Text);
  114. else
  115. MessageBox.Show("Power is not a valid number.");
  116. }
  117. private void sendButton_Click(object sender, EventArgs e)
  118. {
  119. if (messageBox.Text != null && patientModel.CurrentDoctorID != "")
  120. {
  121. String[] data = new String[2];
  122. data[0] = messageBox.Text;
  123. //receiver ID of doctor
  124. data[1] = patientModel.CurrentDoctorID;
  125. messageBox.Clear();
  126. _connection.SendChatMessage(data);
  127. }
  128. }
  129. private void messageBox_KeyPress(object sender, KeyPressEventArgs e)
  130. {
  131. if (e.KeyChar == '\r')
  132. {
  133. sendButton_Click(sender, e);
  134. }
  135. }
  136. private void MenuSessionItems()
  137. {
  138. foreach (Session s in _connection.currentData.GetSessions())
  139. {
  140. selectSessionToolStripMenuItem.DropDownItems.Add(
  141. new ToolStripMenuItem(s.id.ToString(), null, delegate
  142. {
  143. patientModel.stopAskingData();
  144. this.sessionBox.Text = s.id.ToString();
  145. this.nameBox.Text = _connection.userID;
  146. //get measurments
  147. List<Measurement> measurments = s.measurements;
  148. //fill boxes
  149. this.pulseBox.Text = measurments[measurments.Count - 1].pulse.ToString();
  150. this.rpmInfoBox.Text = measurments[measurments.Count - 1].rpm.ToString();
  151. this.speedInfoBox.Text = measurments[measurments.Count - 1].speed.ToString();
  152. this.distanceInfoBox.Text = measurments[measurments.Count - 1].distance.ToString();
  153. this.requestedBox.Text = measurments[measurments.Count - 1].requestedPower.ToString();
  154. this.energyInfoBox.Text = measurments[measurments.Count - 1].energy.ToString();
  155. this.timeBox.Text = measurments[measurments.Count - 1].time.ToString();
  156. this.actualBox.Text = measurments[measurments.Count - 1].actualPower.ToString();
  157. //fill speedpoints
  158. patientModel.speedPoints = new List<DataPoint>();
  159. for (int i = 0; i < measurments.Count; i++)
  160. {
  161. patientModel.speedPoints.Add(new DataPoint(measurments[i].time, measurments[i].speed));
  162. }
  163. //fill speedgraph
  164. this.speedChart.Series[0].Points.Clear();
  165. for (int i = 0; i < patientModel.speedPoints.Count; i++)
  166. this.speedChart.Series[0].Points.Add(patientModel.speedPoints[i]);
  167. this.speedChart.Update();
  168. //fill bpm
  169. patientModel.bpmPoints = new List<DataPoint>();
  170. for (int i = 0; i < measurments.Count; i++)
  171. {
  172. patientModel.bpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].pulse));
  173. }
  174. //fill bpmgraph
  175. this.bpmChart.Series[0].Points.Clear();
  176. for (int i = 0; i < patientModel.bpmPoints.Count; i++)
  177. this.bpmChart.Series[0].Points.Add(patientModel.bpmPoints[i]);
  178. this.bpmChart.Update();
  179. //fill rpm
  180. patientModel.rpmPoints = new List<DataPoint>();
  181. for (int i = 0; i < measurments.Count; i++)
  182. {
  183. patientModel.rpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].rpm));
  184. }
  185. //fill rpmgraph
  186. this.rpmChart.Series[0].Points.Clear();
  187. for (int i = 0; i < patientModel.rpmPoints.Count; i++)
  188. this.rpmChart.Series[0].Points.Add(patientModel.rpmPoints[i]);
  189. this.rpmChart.Update();
  190. })
  191. );
  192. }
  193. }
  194. private void printMessage(string[] data)
  195. {
  196. if (data[2].StartsWith("This is a broadcast: "))
  197. {
  198. string finalMessage = data[2] + "\r\n";
  199. chatBox.Invoke((MethodInvoker)delegate ()
  200. {
  201. chatBox.AppendText(finalMessage);
  202. });
  203. }
  204. else
  205. {
  206. if (data[0] != _connection.userID)
  207. patientModel.CurrentDoctorID = data[0];
  208. string finalMessage = data[0] + ":\t" + data[2] + "\r\n";
  209. chatBox.Invoke((MethodInvoker)delegate ()
  210. {
  211. chatBox.AppendText(finalMessage);
  212. });
  213. }
  214. }
  215. private void PatientForm_FormClosing(object sender, FormClosingEventArgs e)
  216. {
  217. _connection.disconnect();
  218. Application.Exit();
  219. }
  220. private void button1_Click(object sender, EventArgs e)
  221. {
  222. _connection.StartNewSession();
  223. }
  224. private void button2_Click(object sender, EventArgs e)
  225. {
  226. _connection.StopSessoin();
  227. }
  228. private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
  229. {
  230. Form activeForm = Form.ActiveForm;
  231. if (activeForm != null)
  232. {
  233. activeForm.Invoke((MethodInvoker)delegate ()
  234. {
  235. Login login = new Login(_connection);
  236. activeForm.Hide();
  237. login.Show();
  238. });
  239. }
  240. }
  241. }
  242. }