PatientModel.cs 5.1 KB

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