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.

72 lines
1.6 KiB

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