AppGlobal.cs 2.1 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 Fietsclient
  8. {
  9. public class AppGlobal
  10. {
  11. private static AppGlobal _instance;
  12. public int[] StatusItemsChecked;
  13. Thread workerThread;
  14. public static AppGlobal Instance
  15. {
  16. get { return _instance ?? (_instance = new AppGlobal()); }
  17. }
  18. private KettlerBikeComm _bikeComm;
  19. private AppGlobal()
  20. {
  21. _bikeComm = new KettlerBikeComm();
  22. KettlerBikeComm.IncomingDataEvent += HandleBikeData; //initialize event
  23. }
  24. public void setTimeMode(string time)
  25. {
  26. _bikeComm.sendData("CU");
  27. _bikeComm.sendData("PT " + time);
  28. }
  29. public void setPower(string power)
  30. {
  31. _bikeComm.sendData("CU");
  32. _bikeComm.sendData("PW " + power);
  33. }
  34. public void setDistanceMode(string distance)
  35. {
  36. _bikeComm.sendData("CU");
  37. _bikeComm.sendData("PD " + distance);
  38. }
  39. public void reset()
  40. {
  41. _bikeComm.sendData("RS");
  42. }
  43. public void startComPort()
  44. {
  45. startComPort("COM4");
  46. }
  47. public void startComPort(string portname)
  48. {
  49. _bikeComm.initComm(portname);
  50. }
  51. public void startAskingData()
  52. {
  53. workerThread = new Thread(() => workerThreadLoop());
  54. workerThread.Start();
  55. }
  56. private void workerThreadLoop()
  57. {
  58. while(true)
  59. {
  60. Thread.Sleep(1000);
  61. _bikeComm.sendData(KettlerBikeComm.STATUS);
  62. }
  63. }
  64. //event handler
  65. private void HandleBikeData(string[] data)
  66. {
  67. //doe iets ermee...
  68. }
  69. public void closeComPort()
  70. {
  71. if (workerThread != null)
  72. workerThread.Suspend();
  73. _bikeComm.closeComm();
  74. }
  75. }
  76. }