garmsync garmsync
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.

168 lines
3.7 KiB

3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
3 months ago
11 months ago
10 months ago
  1. import WebSocket from "ws";
  2. export default class Trimlog {
  3. constructor({username, password}) {
  4. this.username = username;
  5. this.password = password;
  6. this.socket = null;
  7. this.inbox = [];
  8. }
  9. send(key, data) {
  10. this.socket.send(JSON.stringify({
  11. [key]: data,
  12. register: {
  13. clientId: this.username,
  14. clientSecret: this.password,
  15. },
  16. }));
  17. }
  18. waitFor(key) {
  19. return new Promise((resolve, reject) => {
  20. const ws = this.socket;
  21. setInterval(() => {
  22. if (ws !== this.socket) {
  23. reject("disconnected");
  24. }
  25. for (const [i, data] of this.inbox.entries()) {
  26. if (!!data[key]) {
  27. this.inbox.splice(i, 1);
  28. return data[key];
  29. }
  30. }
  31. }, 100)
  32. })
  33. }
  34. connect() {
  35. return new Promise((resolve, reject) => {
  36. if (this.socket != null) {
  37. this.socket.close();
  38. }
  39. let answered = false;
  40. const ws = new WebSocket("wss://i.stifred.dev/");
  41. ws.on("open", () => {
  42. ws.send(JSON.stringify({register: {
  43. clientId: this.username,
  44. clientSecret: this.password,
  45. }}));
  46. const interval = setInterval(() => {
  47. if (this.socket !== ws) {
  48. clearInterval(interval);
  49. }
  50. ws.send(JSON.stringify({ping: {
  51. reference: "ping:" + new Date().toISOString(),
  52. register: {
  53. clientId: this.username,
  54. clientSecret: this.password,
  55. },
  56. }}))
  57. }, 60000)
  58. this.socket = ws;
  59. })
  60. ws.on("message", (data) => {
  61. data = JSON.parse(data);
  62. if (data.signal) {
  63. return
  64. }
  65. console.log(JSON.stringify(data, 0, 4));
  66. if (!answered) {
  67. if (data.connection?.active) {
  68. answered = true;
  69. return resolve();
  70. } else if (data.error != null) {
  71. return reject(data.error.message);
  72. }
  73. }
  74. this.inbox.push(data);
  75. })
  76. ws.on("error", (err) => {
  77. if (!answered) {
  78. answered = true;
  79. return reject(err);
  80. }
  81. })
  82. ws.on("close", () => {
  83. if (this.socket === ws) {
  84. this.socket = null;
  85. console.log("Socket closed")
  86. this.connect().then(() => {
  87. console.log("Socket reconnected")
  88. })
  89. }
  90. })
  91. })
  92. }
  93. submit(message) {
  94. return new Promise((resolve, reject) => {
  95. const reference = Date.now().toString(36) + Math.random().toString(36).replace(".", "");
  96. let answered = false;
  97. let sent = false;
  98. const ws = new WebSocket("wss://i.stifred.dev/");
  99. ws.on("open", () => {
  100. ws.send(JSON.stringify({register: {
  101. clientId: this.username,
  102. clientSecret: this.password,
  103. }}));
  104. })
  105. ws.on("message", (data) => {
  106. data = JSON.parse(data);
  107. console.log(JSON.stringify(data, 0, 4));
  108. if (data.signal?.reference === reference) {
  109. if (!answered) {
  110. answered = true;
  111. resolve();
  112. ws.close();
  113. }
  114. }
  115. if (!answered && !sent) {
  116. if (data.connection?.active) {
  117. ws.send(JSON.stringify({
  118. ...message,
  119. ping: { reference },
  120. register: {
  121. clientId: this.username,
  122. clientSecret: this.password,
  123. }
  124. }));
  125. sent = true;
  126. }
  127. }
  128. this.inbox.push(data);
  129. })
  130. ws.on("error", (err) => {
  131. if (!answered) {
  132. answered = true;
  133. return reject(err);
  134. }
  135. })
  136. ws.on("close", () => {
  137. console.log("Socket closed")
  138. })
  139. })
  140. }
  141. }