PatientModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms.DataVisualization.Charting;
  9. namespace FietsClient
  10. {
  11. class PatientModel
  12. {
  13. private static PatientModel _patientModel;
  14. public PatientForm patientform { get; set; }
  15. public static PatientModel patientModel { get { return _patientModel ?? (_patientModel = new PatientModel()); } }
  16. private DataHandler dataHandler;
  17. private Thread workerThread;
  18. private string powerLog;
  19. public Boolean askdata;
  20. public string CurrentDoctorID { get; set; }
  21. public PatientModel()
  22. {
  23. dataHandler = new DataHandler();
  24. DataHandler.IncomingDataEvent += HandleBikeData; //initialize event
  25. }
  26. public void startComPort(string portname)
  27. {
  28. dataHandler.initComm(portname);
  29. }
  30. public void startAskingData()
  31. {
  32. askdata = true;
  33. speedPoints.Clear();
  34. bpmPoints.Clear();
  35. rpmPoints.Clear();
  36. workerThread = new Thread(() => workerThreadLoop());
  37. workerThread.Start();
  38. }
  39. public void stopAskingData()
  40. {
  41. askdata = false;
  42. dataHandler.sendData(DataHandler.RESET);
  43. }
  44. private void workerThreadLoop()
  45. {
  46. while (askdata)
  47. {
  48. Thread.Sleep(1000);
  49. if( (patientform.actualBox.Text != powerLog) && (powerLog != null) && (Int32.Parse(powerLog) >= 0) )
  50. {
  51. setPower(powerLog);
  52. }
  53. try
  54. {
  55. dataHandler.sendData(DataHandler.STATUS);
  56. }
  57. catch (Exception)
  58. {
  59. dataHandler.closeComm();
  60. }
  61. }
  62. }
  63. //event handler
  64. public List<DataPoint> speedPoints { get; set; } = new List<DataPoint>();
  65. public List<DataPoint> bpmPoints { get; set; } = new List<DataPoint>();
  66. public List<DataPoint> rpmPoints { set; get; } = new List<DataPoint>();
  67. private void HandleBikeData(string[] data)
  68. {
  69. if (patientform.InvokeRequired)
  70. {
  71. patientform.Invoke((new Action(() => HandleBikeData(data))));
  72. }
  73. else
  74. {
  75. //fill fields
  76. patientform.pulseBox.Text = data[0];
  77. patientform.rpmInfoBox.Text = data[1];
  78. patientform.speedInfoBox.Text = data[2];
  79. patientform.distanceInfoBox.Text = data[3];
  80. patientform.requestedBox.Text = data[4];
  81. patientform.energyInfoBox.Text = data[5];
  82. patientform.timeBox.Text = data[6];
  83. patientform.actualBox.Text = data[7];
  84. //fill graph speed
  85. speedPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[2])));
  86. patientform.speedChart.Series[0].Points.Clear();
  87. for (int i = 0; i < speedPoints.Count; i++)
  88. patientform.speedChart.Series[0].Points.Add(speedPoints[i]);
  89. if (speedPoints.Count > 25)
  90. speedPoints.RemoveAt(0);
  91. patientform.speedChart.Update();
  92. //fill graph pulse
  93. bpmPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[0])));
  94. patientform.bpmChart.Series[0].Points.Clear();
  95. for (int i = 0; i < bpmPoints.Count; i++)
  96. patientform.bpmChart.Series[0].Points.Add(bpmPoints[i]);
  97. if (bpmPoints.Count > 25)
  98. bpmPoints.RemoveAt(0);
  99. patientform.speedChart.Update();
  100. //fill graph rpm
  101. rpmPoints.Add(new DataPoint(Convert.ToDateTime(data[6]).ToOADate(), Convert.ToDouble(data[1])));
  102. patientform.rpmChart.Series[0].Points.Clear();
  103. for (int i = 0; i < rpmPoints.Count; i++)
  104. patientform.rpmChart.Series[0].Points.Add(rpmPoints[i]);
  105. if (rpmPoints.Count > 25)
  106. rpmPoints.RemoveAt(0);
  107. patientform.rpmChart.Update();
  108. }
  109. }
  110. public void closeComPort()
  111. {
  112. stopAskingData();
  113. if (workerThread != null)
  114. workerThread.Interrupt();
  115. dataHandler.closeComm();
  116. }
  117. //change bike values
  118. public void setTimeMode(string time)
  119. {
  120. if (!dataHandler.checkBikeState(false)) return;
  121. dataHandler.sendData("CM");
  122. dataHandler.sendData("PT " + time);
  123. }
  124. public void setPower(string power)
  125. {
  126. powerLog = power;
  127. if (!dataHandler.checkBikeState(false)) return;
  128. dataHandler.sendData("CM");
  129. dataHandler.sendData("PW " + power);
  130. }
  131. public void setDistanceMode(string distance)
  132. {
  133. if (!dataHandler.checkBikeState(false)) return;
  134. dataHandler.sendData("CM");
  135. dataHandler.sendData("PD " + distance);
  136. }
  137. public void reset()
  138. {
  139. if (!dataHandler.checkBikeState(false)) return;
  140. dataHandler.sendData("RS");
  141. }
  142. }
  143. }