AppGlobal.cs 1.9 KB

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