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.

66 lines
1.4 KiB

  1. const Client = require("iconsole-bike-client");
  2. const _ = require("underscore");
  3. class IConsoleDriver {
  4. constructor(connect) {
  5. this.connectString = connect;
  6. this.client = null;
  7. this.maxLevel = 24;
  8. this.lastLevel = 18;
  9. this.workoutState = {};
  10. }
  11. async connect() {
  12. this.client = await Client.scan(this.connectString, 15000)
  13. this.client.events.on("maxLevel", ({maxLevel}) => {
  14. this.maxLevel = maxLevel;
  15. this.events.emit("maxLevel", maxLevel);
  16. });
  17. this.client.events.on("workoutState", ({workoutState}) => {
  18. this.workoutState = workoutState;
  19. if (!_.isEqual(this.workoutState, workoutState)) {
  20. this.workoutState = {...workoutState};
  21. this.events.emit("workoutState", {...workoutState});
  22. }
  23. });
  24. }
  25. async start() {
  26. await this.client.start({level: this.lastLevel});
  27. }
  28. async pause() {
  29. await this.client.pause();
  30. }
  31. async resume() {
  32. await this.client.resume();
  33. }
  34. async stop() {
  35. await this.client.pause();
  36. }
  37. async setLevel(n) {
  38. if (n < 0 || n > this.maxLevel) {
  39. return Promise.reject(new Error(`Level is out of range (0 - ${this.maxLevel})`))
  40. }
  41. this.lastLevel = n;
  42. return await this.client.setLevel(n);
  43. }
  44. destroy() {
  45. this.stop();
  46. this.client.disconnect();
  47. this.events.emit("destroy");
  48. this.events.removeAllListeners();
  49. }
  50. }
  51. module.exports = IConsoleDriver;