PatientModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace FietsClientV2
  8. {
  9. class PatientModel
  10. {
  11. private static PatientModel _patientModel;
  12. public PatientForm patientform { private get; set; }
  13. public static PatientModel patientModel { get { return _patientModel ?? (_patientModel = new PatientModel()); } }
  14. private DataHandler dataHandler;
  15. private Thread workerThread;
  16. private PatientModel()
  17. {
  18. dataHandler = new DataHandler();
  19. DataHandler.IncomingDataEvent += HandleBikeData; //initialize event
  20. }
  21. public void startComPort(string portname)
  22. {
  23. dataHandler.initComm(portname);
  24. }
  25. public void startAskingData()
  26. {
  27. workerThread = new Thread(() => workerThreadLoop());
  28. workerThread.Start();
  29. }
  30. private void workerThreadLoop()
  31. {
  32. while (true)
  33. {
  34. Thread.Sleep(1000);
  35. dataHandler.sendData(DataHandler.STATUS);
  36. }
  37. }
  38. //event handler
  39. private void HandleBikeData(string[] data)
  40. {
  41. if (patientform.InvokeRequired)
  42. {
  43. patientform.Invoke((new Action(() => HandleBikeData(data))));
  44. }
  45. else
  46. {
  47. patientform.pulseBox.Text = data[0];
  48. patientform.rpmInfoBox.Text = data[1];
  49. patientform.speedInfoBox.Text = data[2];
  50. patientform.distanceInfoBox.Text = data[3];
  51. patientform.requestedBox.Text = data[4];
  52. patientform.energyInfoBox.Text = data[5];
  53. patientform.timeBox.Text = data[6];
  54. patientform.actualBox.Text = data[7];
  55. }
  56. }
  57. public void closeComPort()
  58. {
  59. if (workerThread != null)
  60. workerThread.Interrupt();
  61. dataHandler.closeComm();
  62. }
  63. //change bike values
  64. public void setTimeMode(string time)
  65. {
  66. if (!dataHandler.checkBikeState(false)) return;
  67. dataHandler.sendData("CM");
  68. dataHandler.sendData("PT " + time);
  69. }
  70. public void setPower(string power)
  71. {
  72. if (!dataHandler.checkBikeState(false)) return;
  73. dataHandler.sendData("CM");
  74. dataHandler.sendData("PW " + power);
  75. }
  76. public void setDistanceMode(string distance)
  77. {
  78. if (!dataHandler.checkBikeState(false)) return;
  79. dataHandler.sendData("CM");
  80. dataHandler.sendData("PD " + distance);
  81. }
  82. public void reset()
  83. {
  84. if (!dataHandler.checkBikeState(false)) return;
  85. dataHandler.sendData("RS");
  86. }
  87. }
  88. }