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.

108 lines
2.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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.workoutStatus = {
  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. setTimeout(() => {
  24. this.events.emit("maxLevel", 32);
  25. })
  26. return Promise.resolve();
  27. }
  28. start() {
  29. if (this.started) {
  30. return;
  31. }
  32. this.started = true;
  33. this.interval = setInterval(() => {
  34. this.seconds++;
  35. this.workoutStatus.minutes = Math.floor(this.seconds / 60);
  36. this.workoutStatus.seconds = this.seconds % 60;
  37. this.workoutStatus.speed = Math.floor(300 + Math.random() * 200) / 10;
  38. this.workoutStatus.rpm = Math.floor(57 + Math.random() * 5);
  39. this.workoutStatus.distance += (this.seconds % 5 == 0) ? 0.1 : 0;
  40. this.workoutStatus.calories += (this.seconds % 2 == 0) ? 1 : 0;
  41. this.workoutStatus.pulse = null;
  42. this.workoutStatus.watt = Math.floor(100 + Math.random() * 20);
  43. this.workoutStatus.level = this.level;
  44. this.events.emit("workoutStatus", {...this.workoutStatus});
  45. }, 1000);
  46. this.workoutStatus.level = 1;
  47. this.workoutStatus.speed = 0;
  48. this.workoutStatus.rpm = 0;
  49. this.workoutStatus.watt = 0;
  50. if (this.seconds === 0) {
  51. this.events.emit("workoutStatus", {...this.workoutStatus});
  52. }
  53. return Promise.resolve();
  54. }
  55. setLevel(n) {
  56. if (n < 0 || n > 32) {
  57. return Promise.reject(new Error("Level is out of range (0..=32)"))
  58. }
  59. this.level = n;
  60. return Promise.resolve();
  61. }
  62. pause() {
  63. clearInterval(this.interval);
  64. this.started = false;
  65. return Promise.resolve();
  66. }
  67. stop() {
  68. this.pause();
  69. this.workoutStatus = {
  70. minutes: 0,
  71. seconds: 0,
  72. speed: 0,
  73. rpm: 0,
  74. distance: 0,
  75. calories: 0,
  76. pulse: 0,
  77. watt: 0,
  78. level: 1,
  79. };
  80. this.seconds = 0;
  81. return Promise.resolve();
  82. }
  83. destroy() {
  84. this.stop();
  85. this.events.emit("destroy");
  86. this.events.removeAllListeners();
  87. }
  88. }
  89. module.exports = MockDriver;