You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.1 KiB

5 years ago
  1. const EventEmitter = require("events");
  2. const _ = require("underscore");
  3. class MockDriver {
  4. constructor() {
  5. this.started = false;
  6. this.events = new EventEmitter();
  7. this.seconds = 0;
  8. this.level = 1;
  9. this.workoutState = {
  10. minutes: 0,
  11. seconds: 0,
  12. speed: 0,
  13. rpm: 0,
  14. distance: 0,
  15. calories: 0,
  16. pulse: 0,
  17. watt: 0,
  18. level: 1,
  19. };
  20. this.interval = null;
  21. }
  22. connect() {
  23. return Promise.resolve();
  24. }
  25. start() {
  26. if (this.started) {
  27. return;
  28. }
  29. this.started = true;
  30. this.interval = setInterval(() => {
  31. this.seconds++;
  32. this.workoutState.minutes = Math.floor(this.seconds / 60);
  33. this.workoutState.seconds = this.seconds % 60;
  34. this.workoutState.speed = Math.floor(300 + Math.random() * 200) / 10;
  35. this.workoutState.rpm = Math.floor(57 + Math.random() * 5);
  36. this.workoutState.distance += (this.seconds % 5 == 0) ? 0.1 : 0;
  37. this.workoutState.calories += (this.seconds % 2 == 0) ? 1 : 0;
  38. this.workoutState.pulse = null;
  39. this.workoutState.watt = Math.floor(100 + Math.random() * 20);
  40. this.workoutState.level = this.level;
  41. this.events.emit("workoutState", {...this.workoutState});
  42. }, 1000);
  43. this.workoutState.level = 1;
  44. this.workoutState.speed = 0;
  45. this.workoutState.rpm = 0;
  46. this.workoutState.watt = 0;
  47. if (this.seconds === 0) {
  48. this.events.emit("workoutState", {...this.workoutState});
  49. }
  50. return Promise.resolve();
  51. }
  52. setLevel(n) {
  53. if (n < 0 || n > 32) {
  54. return Promise.reject(new Error("Level is out of range (0..=32)"))
  55. }
  56. this.level = n;
  57. }
  58. pause() {
  59. clearInterval(this.interval);
  60. this.started = false;
  61. return Promise.resolve();
  62. }
  63. stop() {
  64. this.pause();
  65. this.workoutState = {
  66. minutes: 0,
  67. seconds: 0,
  68. speed: 0,
  69. rpm: 0,
  70. distance: 0,
  71. calories: 0,
  72. pulse: 0,
  73. watt: 0,
  74. level: 1,
  75. };
  76. this.seconds = 0;
  77. return Promise.resolve();
  78. }
  79. destroy() {
  80. this.stop();
  81. this.events.emit("destroy");
  82. this.events.removeAllListeners();
  83. }
  84. }
  85. module.exports = MockDriver;