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.

82 lines
1.7 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
  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. this.started = false;
  13. }
  14. async connect() {
  15. this.client = await Client.scan(this.connectString.toLowerCase(), 15000)
  16. await this.client.connect();
  17. this.client.events.on("data", ({kind, workoutStatus, maxLevel}) => {
  18. switch (kind) {
  19. case "workoutStatus": {
  20. if (!_.isEqual(this.workoutStatus, workoutStatus)) {
  21. this.workoutStatus = {...workoutStatus};
  22. this.events.emit("workoutStatus", {...workoutStatus});
  23. }
  24. break;
  25. }
  26. case "maxLevel": {
  27. this.events.emit("maxLevel", maxLevel);
  28. break;
  29. }
  30. }
  31. })
  32. }
  33. get state() {
  34. return this.client.state;
  35. }
  36. async start() {
  37. if (!this.started) {
  38. await this.client.start({level: this.lastLevel});
  39. this.started = true;
  40. } else {
  41. await this.client.resume();
  42. }
  43. }
  44. async pause() {
  45. await this.client.pause();
  46. }
  47. async resume() {
  48. await this.client.resume();
  49. }
  50. async stop() {
  51. await this.client.pause();
  52. }
  53. async setLevel(n) {
  54. if (n < 0 || n > this.maxLevel) {
  55. return Promise.reject(new Error(`Level is out of range (0 - ${this.maxLevel})`))
  56. }
  57. this.lastLevel = n;
  58. return await this.client.setLevel(n);
  59. }
  60. destroy() {
  61. this.stop();
  62. this.client.disconnect();
  63. this.events.emit("destroy");
  64. this.events.removeAllListeners();
  65. }
  66. }
  67. module.exports = IConsoleDriver;