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.

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