PatientModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using FietsLibrary.JSONObjecten;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms.DataVisualization.Charting;
  10. namespace FietsClient
  11. {
  12. class PatientModel
  13. {
  14. private static PatientModel _patientModel;
  15. public PatientForm patientform { get; set; }
  16. public static PatientModel patientModel { get { return _patientModel ?? (_patientModel = new PatientModel()); } }
  17. public DataHandler dataHandler { get; private set; }
  18. private Thread workerThread;
  19. private string powerLog;
  20. public Boolean askdata;
  21. public string CurrentDoctorID { get; set; }
  22. public PatientModel()
  23. {
  24. dataHandler = new DataHandler();
  25. DataHandler.IncomingDataEvent += HandleBikeData; //initialize event
  26. }
  27. public void startComPort(string portname)
  28. {
  29. dataHandler.initComm(portname);
  30. }
  31. public void startAskingData()
  32. {
  33. bool canStart = false;
  34. if (workerThread != null)
  35. {
  36. if (!(workerThread.ThreadState == ThreadState.Running || workerThread.ThreadState == ThreadState.Background || workerThread.ThreadState == ThreadState.WaitSleepJoin))
  37. {
  38. canStart = true;
  39. }
  40. }
  41. else
  42. canStart = true;
  43. if (canStart)
  44. {
  45. askdata = true;
  46. speedPoints.Clear();
  47. bpmPoints.Clear();
  48. rpmPoints.Clear();
  49. workerThread = new Thread(() => workerThreadLoop());
  50. workerThread.Start();
  51. }
  52. }
  53. public void stopAskingData()
  54. {
  55. askdata = false;
  56. dataHandler.sendData(DataHandler.RESET);
  57. if (patientform.InvokeRequired)
  58. {
  59. patientform.Invoke((new Action(() => patientform.sessionBox.Text = " ")));
  60. patientform.Invoke((new Action(() => patientform.label19.Text = "Sessie gestopt")));
  61. return;
  62. }
  63. }
  64. private void workerThreadLoop()
  65. {
  66. while (askdata)
  67. {
  68. Thread.Sleep(1000);
  69. if( (patientform.actualBox.Text != powerLog) && (powerLog != null) && (Int32.Parse(powerLog) >= 0) )
  70. {
  71. setPower(powerLog);
  72. }
  73. try
  74. {
  75. if(askdata)
  76. dataHandler.sendData(DataHandler.STATUS);
  77. }
  78. catch (Exception)
  79. {
  80. dataHandler.closeComm();
  81. }
  82. }
  83. }
  84. //event handler
  85. public List<DataPoint> speedPoints { get; set; } = new List<DataPoint>();
  86. public List<DataPoint> bpmPoints { get; set; } = new List<DataPoint>();
  87. public List<DataPoint> rpmPoints { set; get; } = new List<DataPoint>();
  88. private void HandleBikeData(string[] data)
  89. {
  90. if (patientform.InvokeRequired)
  91. {
  92. patientform.Invoke((new Action(() => HandleBikeData(data))));
  93. return;
  94. }
  95. else
  96. {
  97. //fill fields
  98. patientform.pulseBox.Text = data[0];
  99. patientform.rpmInfoBox.Text = data[1];
  100. patientform.speedInfoBox.Text = data[2];
  101. patientform.distanceInfoBox.Text = data[3];
  102. patientform.requestedBox.Text = data[4];
  103. patientform.energyInfoBox.Text = data[5];
  104. patientform.timeBox.Text = data[6];
  105. patientform.actualBox.Text = data[7];
  106. //fill graph speed
  107. speedPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[2])));
  108. patientform.speedChart.Series[0].Points.Clear();
  109. for (int i = 0; i < speedPoints.Count; i++)
  110. patientform.speedChart.Series[0].Points.Add(speedPoints[i]);
  111. if (speedPoints.Count > 25)
  112. speedPoints.RemoveAt(0);
  113. patientform.speedChart.Update();
  114. //fill graph pulse
  115. bpmPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[0])));
  116. patientform.bpmChart.Series[0].Points.Clear();
  117. for (int i = 0; i < bpmPoints.Count; i++)
  118. patientform.bpmChart.Series[0].Points.Add(bpmPoints[i]);
  119. if (bpmPoints.Count > 25)
  120. bpmPoints.RemoveAt(0);
  121. patientform.speedChart.Update();
  122. //fill graph rpm
  123. rpmPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[1])));
  124. patientform.rpmChart.Series[0].Points.Clear();
  125. for (int i = 0; i < rpmPoints.Count; i++)
  126. patientform.rpmChart.Series[0].Points.Add(rpmPoints[i]);
  127. if (rpmPoints.Count > 25)
  128. rpmPoints.RemoveAt(0);
  129. patientform.rpmChart.Update();
  130. }
  131. SaveAndSendData(data);
  132. }
  133. private void SaveAndSendData(string[] data)
  134. {
  135. Measurement m = new Measurement(data);
  136. patientform._connection.currentData.sessions.Last().AddMeasurement(m);
  137. patientform._connection.SendNewMeasurement();
  138. }
  139. public void closeComPort()
  140. {
  141. stopAskingData();
  142. if (workerThread != null)
  143. workerThread.Interrupt();
  144. dataHandler.closeComm();
  145. }
  146. //change bike values
  147. public void setTimeMode(string time, Boolean start)
  148. {
  149. dataHandler.sendData("CM");
  150. dataHandler.sendData("PT " + time);
  151. if (patientform.InvokeRequired)
  152. {
  153. patientform.Invoke((new Action(() => patientform.sessionBox.Text = "Tijd: " + time)));
  154. return;
  155. }
  156. if (!dataHandler.checkBikeState(false)) return;
  157. if (start)
  158. startSession();
  159. }
  160. public void setPower(string power)
  161. {
  162. powerLog = power;
  163. dataHandler.sendData("CM");
  164. dataHandler.sendData("PW " + power);
  165. if (!dataHandler.checkBikeState(false)) return;
  166. }
  167. public void setDistanceMode(string distance, Boolean start)
  168. {
  169. dataHandler.sendData("CM");
  170. dataHandler.sendData("PD " + distance);
  171. if (patientform.InvokeRequired)
  172. {
  173. patientform.Invoke((new Action(() => patientform.sessionBox.Text = "Afstand: " + distance)));
  174. return;
  175. }
  176. if (!dataHandler.checkBikeState(false)) return;
  177. if (start)
  178. startSession();
  179. }
  180. public void startSession()
  181. {
  182. patientform._connection.StartNewSession(false, patientform._connection.currentData.GetUserID());
  183. patientModel.startAskingData();
  184. if (patientform.InvokeRequired)
  185. {
  186. patientform.Invoke((new Action(() => patientform.label19.Text = "Sessie is gestart, u kunt nu gaan starten met fietsen.")));
  187. return;
  188. }
  189. }
  190. public void reset()
  191. {
  192. if (!dataHandler.checkBikeState(false)) return;
  193. dataHandler.sendData("RS");
  194. }
  195. }
  196. }