PatientModel.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. dataHandler.sendData("CM");
  67. dataHandler.sendData("PT " + time);
  68. }
  69. public void setPower(string power)
  70. {
  71. dataHandler.sendData("CM");
  72. dataHandler.sendData("PW " + power);
  73. }
  74. public void setDistanceMode(string distance)
  75. {
  76. dataHandler.sendData("CM");
  77. dataHandler.sendData("PD " + distance);
  78. }
  79. public void reset()
  80. {
  81. dataHandler.sendData("RS");
  82. }
  83. }
  84. }