buttplug 3.2.2 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/.eslintrc.js +25 -25
  2. package/.jscsrc +2 -2
  3. package/.jshintrc +5 -5
  4. package/.prettierrc.json +3 -3
  5. package/.yarnrc.yml +5 -5
  6. package/CHANGELOG.md +591 -577
  7. package/LICENSE +27 -27
  8. package/README.md +105 -97
  9. package/dist/main/src/client/ButtplugBrowserWebsocketClientConnector.js +6 -9
  10. package/dist/main/src/client/ButtplugBrowserWebsocketClientConnector.js.map +1 -1
  11. package/dist/main/src/client/{Client.d.ts → ButtplugClient.d.ts} +3 -4
  12. package/dist/main/src/client/ButtplugClient.js +232 -0
  13. package/dist/main/src/client/ButtplugClient.js.map +1 -0
  14. package/dist/main/src/client/ButtplugClientConnectorException.js +17 -7
  15. package/dist/main/src/client/ButtplugClientConnectorException.js.map +1 -1
  16. package/dist/main/src/client/ButtplugClientDevice.d.ts +13 -28
  17. package/dist/main/src/client/ButtplugClientDevice.js +105 -247
  18. package/dist/main/src/client/ButtplugClientDevice.js.map +1 -1
  19. package/dist/main/src/client/ButtplugClientDeviceCommand.d.ts +42 -0
  20. package/dist/main/src/client/ButtplugClientDeviceCommand.js +105 -0
  21. package/dist/main/src/client/ButtplugClientDeviceCommand.js.map +1 -0
  22. package/dist/main/src/client/ButtplugClientDeviceFeature.d.ts +18 -0
  23. package/dist/main/src/client/ButtplugClientDeviceFeature.js +166 -0
  24. package/dist/main/src/client/ButtplugClientDeviceFeature.js.map +1 -0
  25. package/dist/main/src/client/ButtplugNodeWebsocketClientConnector.d.ts +1 -8
  26. package/dist/main/src/client/ButtplugNodeWebsocketClientConnector.js +1 -4
  27. package/dist/main/src/client/ButtplugNodeWebsocketClientConnector.js.map +1 -1
  28. package/dist/main/src/core/Exceptions.js +27 -9
  29. package/dist/main/src/core/Exceptions.js.map +1 -1
  30. package/dist/main/src/core/Logging.js +12 -6
  31. package/dist/main/src/core/Logging.js.map +1 -1
  32. package/dist/main/src/core/Messages.d.ts +119 -230
  33. package/dist/main/src/core/Messages.js +50 -404
  34. package/dist/main/src/core/Messages.js.map +1 -1
  35. package/dist/main/src/index.d.ts +2 -2
  36. package/dist/main/src/index.js +4 -2
  37. package/dist/main/src/index.js.map +1 -1
  38. package/dist/main/src/utils/ButtplugBrowserWebsocketConnector.js +40 -52
  39. package/dist/main/src/utils/ButtplugBrowserWebsocketConnector.js.map +1 -1
  40. package/dist/main/src/utils/ButtplugMessageSorter.js +27 -15
  41. package/dist/main/src/utils/ButtplugMessageSorter.js.map +1 -1
  42. package/dist/main/src/utils/Utils.js +1 -2
  43. package/dist/main/src/utils/Utils.js.map +1 -1
  44. package/dist/web/buttplug.js +1 -38
  45. package/dist/web/buttplug.mjs +595 -1984
  46. package/dist/web/client/ButtplugBrowserWebsocketClientConnector.d.ts +0 -7
  47. package/dist/web/client/{Client.d.ts → ButtplugClient.d.ts} +3 -11
  48. package/dist/web/client/ButtplugClientConnectorException.d.ts +0 -7
  49. package/dist/web/client/ButtplugClientDevice.d.ts +14 -29
  50. package/dist/web/client/ButtplugClientDeviceCommand.d.ts +42 -0
  51. package/dist/web/client/ButtplugClientDeviceFeature.d.ts +18 -0
  52. package/dist/web/client/ButtplugNodeWebsocketClientConnector.d.ts +1 -15
  53. package/dist/web/client/IButtplugClientConnector.d.ts +0 -7
  54. package/dist/web/core/Exceptions.d.ts +1 -1
  55. package/dist/web/core/Logging.d.ts +0 -7
  56. package/dist/web/core/Messages.d.ts +119 -229
  57. package/dist/web/index.d.ts +2 -2
  58. package/dist/web/utils/ButtplugBrowserWebsocketConnector.d.ts +0 -7
  59. package/examples/node/SYNC_MANIFEST.md +105 -0
  60. package/examples/node/application-example.ts +213 -0
  61. package/examples/node/async-example.ts +124 -0
  62. package/examples/node/connection-example.ts +76 -0
  63. package/examples/node/device-control-example.ts +131 -0
  64. package/examples/node/device-enumeration-example.ts +86 -0
  65. package/examples/node/device-info-example.ts +131 -0
  66. package/examples/node/errors-example.ts +166 -0
  67. package/examples/node/package-lock.json +281 -0
  68. package/examples/node/package.json +25 -0
  69. package/examples/node/remote-connector-example.ts +84 -0
  70. package/examples/node/tsconfig.json +14 -0
  71. package/examples/web/application-example.js +197 -0
  72. package/examples/web/async-example.js +90 -0
  73. package/examples/web/device-control-example.js +87 -0
  74. package/examples/web/device-enumeration-example.js +49 -0
  75. package/examples/web/device-info-example.js +100 -0
  76. package/examples/web/errors-example.js +110 -0
  77. package/examples/web/index.html +55 -0
  78. package/examples/web/logging.js +42 -0
  79. package/examples/web/ping-timeout-example.js +59 -0
  80. package/examples/web/remote-connector-example.js +68 -0
  81. package/node-test.js +24 -0
  82. package/node-test.ts +23 -5
  83. package/package.json +85 -87
  84. package/src/client/ButtplugBrowserWebsocketClientConnector.ts +25 -25
  85. package/src/client/ButtplugClient.ts +242 -0
  86. package/src/client/ButtplugClientConnectorException.ts +16 -16
  87. package/src/client/ButtplugClientDevice.ts +178 -401
  88. package/src/client/ButtplugClientDeviceCommand.ts +112 -0
  89. package/src/client/ButtplugClientDeviceFeature.ts +138 -0
  90. package/src/client/ButtplugNodeWebsocketClientConnector.ts +17 -17
  91. package/src/client/IButtplugClientConnector.ts +18 -18
  92. package/src/core/Exceptions.ts +107 -101
  93. package/src/core/Logging.ts +197 -197
  94. package/src/core/Messages.ts +205 -480
  95. package/src/core/index.d.ts +4 -4
  96. package/src/index.ts +21 -19
  97. package/src/utils/ButtplugBrowserWebsocketConnector.ts +89 -89
  98. package/src/utils/ButtplugMessageSorter.ts +66 -65
  99. package/src/utils/Utils.ts +3 -3
  100. package/tsconfig.json +22 -22
  101. package/tsfmt.json +14 -14
  102. package/tslint.json +27 -27
  103. package/typedocconfig.js +6 -6
  104. package/vite.config.ts +26 -26
  105. package/dist/main/src/client/Client.js +0 -242
  106. package/dist/main/src/client/Client.js.map +0 -1
  107. package/dist/main/src/core/MessageUtils.d.ts +0 -10
  108. package/dist/main/src/core/MessageUtils.js +0 -65
  109. package/dist/main/src/core/MessageUtils.js.map +0 -1
  110. package/dist/web/core/MessageUtils.d.ts +0 -10
  111. package/doc/.nojekyll +0 -1
  112. package/doc/assets/highlight.css +0 -22
  113. package/doc/assets/main.js +0 -58
  114. package/doc/assets/search.js +0 -1
  115. package/doc/assets/style.css +0 -1280
  116. package/doc/classes/ButtplugBrowserWebsocketClientConnector.html +0 -234
  117. package/doc/classes/ButtplugClient.html +0 -331
  118. package/doc/classes/ButtplugClientConnectorException.html +0 -216
  119. package/doc/classes/ButtplugClientDevice.html +0 -489
  120. package/doc/classes/ButtplugDeviceError.html +0 -218
  121. package/doc/classes/ButtplugDeviceMessage.html +0 -165
  122. package/doc/classes/ButtplugError.html +0 -220
  123. package/doc/classes/ButtplugInitError.html +0 -218
  124. package/doc/classes/ButtplugLogger.html +0 -288
  125. package/doc/classes/ButtplugMessage.html +0 -147
  126. package/doc/classes/ButtplugMessageError.html +0 -218
  127. package/doc/classes/ButtplugMessageSorter.html +0 -128
  128. package/doc/classes/ButtplugNodeWebsocketClientConnector.html +0 -239
  129. package/doc/classes/ButtplugPingError.html +0 -218
  130. package/doc/classes/ButtplugSystemMessage.html +0 -150
  131. package/doc/classes/ButtplugUnknownError.html +0 -218
  132. package/doc/classes/DeviceAdded.html +0 -186
  133. package/doc/classes/DeviceInfo.html +0 -114
  134. package/doc/classes/DeviceList.html +0 -160
  135. package/doc/classes/DeviceRemoved.html +0 -158
  136. package/doc/classes/Error.html +0 -179
  137. package/doc/classes/GenericDeviceMessageAttributes.html +0 -107
  138. package/doc/classes/GenericMessageSubcommand.html +0 -90
  139. package/doc/classes/LinearCmd.html +0 -187
  140. package/doc/classes/LogMessage.html +0 -134
  141. package/doc/classes/MessageAttributes.html +0 -160
  142. package/doc/classes/Ok.html +0 -151
  143. package/doc/classes/Ping.html +0 -151
  144. package/doc/classes/RawDeviceMessageAttributes.html +0 -86
  145. package/doc/classes/RawReadCmd.html +0 -188
  146. package/doc/classes/RawReading.html +0 -179
  147. package/doc/classes/RawSubscribeCmd.html +0 -170
  148. package/doc/classes/RawUnsubscribeCmd.html +0 -170
  149. package/doc/classes/RawWriteCmd.html +0 -188
  150. package/doc/classes/RequestDeviceList.html +0 -151
  151. package/doc/classes/RequestServerInfo.html +0 -169
  152. package/doc/classes/RotateCmd.html +0 -187
  153. package/doc/classes/RotateSubcommand.html +0 -108
  154. package/doc/classes/ScalarCmd.html +0 -170
  155. package/doc/classes/ScalarSubcommand.html +0 -108
  156. package/doc/classes/ScanningFinished.html +0 -146
  157. package/doc/classes/SensorDeviceMessageAttributes.html +0 -107
  158. package/doc/classes/SensorReadCmd.html +0 -179
  159. package/doc/classes/SensorReading.html +0 -188
  160. package/doc/classes/ServerInfo.html +0 -178
  161. package/doc/classes/StartScanning.html +0 -151
  162. package/doc/classes/StopAllDevices.html +0 -151
  163. package/doc/classes/StopDeviceCmd.html +0 -161
  164. package/doc/classes/StopScanning.html +0 -151
  165. package/doc/classes/VectorSubcommand.html +0 -108
  166. package/doc/enums/ActuatorType.html +0 -104
  167. package/doc/enums/ButtplugLogLevel.html +0 -97
  168. package/doc/enums/ErrorClass.html +0 -90
  169. package/doc/enums/SensorType.html +0 -90
  170. package/doc/functions/FromJSON.html +0 -113
  171. package/doc/index.html +0 -184
  172. package/doc/interfaces/IButtplugClientConnector.html +0 -137
  173. package/doc/modules.html +0 -176
  174. package/doc/variables/DEFAULT_MESSAGE_ID.html +0 -104
  175. package/doc/variables/MAX_ID.html +0 -104
  176. package/doc/variables/MESSAGE_SPEC_VERSION.html +0 -104
  177. package/doc/variables/SYSTEM_MESSAGE_ID.html +0 -104
  178. package/src/client/Client.ts +0 -276
  179. package/src/core/MessageUtils.ts +0 -48
@@ -0,0 +1,242 @@
1
+ /*!
2
+ * Buttplug JS Source Code File - Visit https://buttplug.io for more info about
3
+ * the project. Licensed under the BSD 3-Clause license. See LICENSE file in the
4
+ * project root for full license information.
5
+ *
6
+ * @copyright Copyright (c) Nonpolynomial Labs LLC. All rights reserved.
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ import { ButtplugLogger } from '../core/Logging';
12
+ import { EventEmitter } from 'eventemitter3';
13
+ import { ButtplugClientDevice } from './ButtplugClientDevice';
14
+ import { IButtplugClientConnector } from './IButtplugClientConnector';
15
+ import { ButtplugMessageSorter } from '../utils/ButtplugMessageSorter';
16
+ import * as Messages from '../core/Messages';
17
+ import {
18
+ ButtplugError,
19
+ ButtplugInitError,
20
+ ButtplugMessageError,
21
+ } from '../core/Exceptions';
22
+ import { ButtplugClientConnectorException } from './ButtplugClientConnectorException';
23
+
24
+ export class ButtplugClient extends EventEmitter {
25
+ protected _pingTimer: NodeJS.Timeout | null = null;
26
+ protected _connector: IButtplugClientConnector | null = null;
27
+ protected _devices: Map<number, ButtplugClientDevice> = new Map();
28
+ protected _clientName: string;
29
+ protected _logger = ButtplugLogger.Logger;
30
+ protected _isScanning = false;
31
+ private _sorter: ButtplugMessageSorter = new ButtplugMessageSorter(true);
32
+
33
+ constructor(clientName = 'Generic Buttplug Client') {
34
+ super();
35
+ this._clientName = clientName;
36
+ this._logger.Debug(`ButtplugClient: Client ${clientName} created.`);
37
+ }
38
+
39
+ public get connected(): boolean {
40
+ return this._connector !== null && this._connector.Connected;
41
+ }
42
+
43
+ public get devices(): Map<number, ButtplugClientDevice> {
44
+ // While this function doesn't actually send a message, if we don't have a
45
+ // connector, we shouldn't have devices.
46
+ this.checkConnector();
47
+ return this._devices;
48
+ }
49
+
50
+ public get isScanning(): boolean {
51
+ return this._isScanning;
52
+ }
53
+
54
+ public connect = async (connector: IButtplugClientConnector) => {
55
+ this._logger.Info(
56
+ `ButtplugClient: Connecting using ${connector.constructor.name}`
57
+ );
58
+ await connector.connect();
59
+ this._connector = connector;
60
+ this._connector.addListener('message', this.parseMessages);
61
+ this._connector.addListener('disconnect', this.disconnectHandler);
62
+ await this.initializeConnection();
63
+ };
64
+
65
+ public disconnect = async () => {
66
+ this._logger.Debug('ButtplugClient: Disconnect called');
67
+ this._devices.clear();
68
+ this.checkConnector();
69
+ await this.shutdownConnection();
70
+ await this._connector!.disconnect();
71
+ };
72
+
73
+ public startScanning = async () => {
74
+ this._logger.Debug('ButtplugClient: StartScanning called');
75
+ this._isScanning = true;
76
+ await this.sendMsgExpectOk({ StartScanning: { Id: 1 } });
77
+ };
78
+
79
+ public stopScanning = async () => {
80
+ this._logger.Debug('ButtplugClient: StopScanning called');
81
+ this._isScanning = false;
82
+ await this.sendMsgExpectOk({ StopScanning: { Id: 1 } });
83
+ };
84
+
85
+ public stopAllDevices = async () => {
86
+ this._logger.Debug('ButtplugClient: StopCmd (all devices)');
87
+ await this.sendMsgExpectOk({ StopCmd: { Id: 1, DeviceIndex: undefined, FeatureIndex: undefined, Inputs: true, Outputs: true } });
88
+ };
89
+
90
+ protected disconnectHandler = () => {
91
+ this._logger.Info('ButtplugClient: Disconnect event receieved.');
92
+ this.emit('disconnect');
93
+ };
94
+
95
+ protected parseMessages = (msgs: Messages.ButtplugMessage[]) => {
96
+ const leftoverMsgs = this._sorter.ParseIncomingMessages(msgs);
97
+ for (const x of leftoverMsgs) {
98
+ if (x.DeviceList !== undefined) {
99
+ this.parseDeviceList(x.DeviceList!);
100
+ break;
101
+ } else if (x.ScanningFinished !== undefined) {
102
+ this._isScanning = false;
103
+ this.emit('scanningfinished', x);
104
+ } else if (x.InputReading !== undefined) {
105
+ // TODO this should be emitted from the device or feature, not the client
106
+ this.emit('inputreading', x);
107
+ } else {
108
+ console.log(`Unhandled message: ${x}`);
109
+ }
110
+ }
111
+ };
112
+
113
+ protected initializeConnection = async (): Promise<boolean> => {
114
+ this.checkConnector();
115
+ const msg = await this.sendMessage(
116
+ {
117
+ RequestServerInfo: {
118
+ ClientName: this._clientName,
119
+ Id: 1,
120
+ ProtocolVersionMajor: Messages.MESSAGE_SPEC_VERSION_MAJOR,
121
+ ProtocolVersionMinor: Messages.MESSAGE_SPEC_VERSION_MINOR
122
+ }
123
+ }
124
+ );
125
+ if (msg.ServerInfo !== undefined) {
126
+ const serverinfo = msg as Messages.ServerInfo;
127
+ this._logger.Info(
128
+ `ButtplugClient: Connected to Server ${serverinfo.ServerName}`
129
+ );
130
+ // TODO: maybe store server name, do something with message template version?
131
+ const ping = serverinfo.MaxPingTime;
132
+ // If the server version is lower than the client version, the server will disconnect here.
133
+ if (ping > 0) {
134
+ /*
135
+ this._pingTimer = setInterval(async () => {
136
+ // If we've disconnected, stop trying to ping the server.
137
+ if (!this.Connected) {
138
+ await this.ShutdownConnection();
139
+ return;
140
+ }
141
+ await this.SendMessage(new Messages.Ping());
142
+ } , Math.round(ping / 2));
143
+ */
144
+ }
145
+ await this.requestDeviceList();
146
+ return true;
147
+ } else if (msg.Error !== undefined) {
148
+ // Disconnect and throw an exception with the error message we got back.
149
+ // This will usually only error out if we have a version mismatch that the
150
+ // server has detected.
151
+ await this._connector!.disconnect();
152
+ const err = msg.Error as Messages.Error;
153
+ throw ButtplugError.LogAndError(
154
+ ButtplugInitError,
155
+ this._logger,
156
+ `Cannot connect to server. ${err.ErrorMessage}`
157
+ );
158
+ }
159
+ return false;
160
+ }
161
+
162
+ private parseDeviceList = (list: Messages.DeviceList) => {
163
+ for (let [_, d] of Object.entries(list.Devices)) {
164
+ if (!this._devices.has(d.DeviceIndex)) {
165
+ const device = ButtplugClientDevice.fromMsg(
166
+ d,
167
+ this.sendMessageClosure
168
+ );
169
+ this._logger.Debug(`ButtplugClient: Adding Device: ${device}`);
170
+ this._devices.set(d.DeviceIndex, device);
171
+ this.emit('deviceadded', device);
172
+ } else {
173
+ this._logger.Debug(`ButtplugClient: Device already added: ${d}`);
174
+ }
175
+ }
176
+ for (let [index, device] of this._devices.entries()) {
177
+ if (!list.Devices.hasOwnProperty(index.toString())) {
178
+ this._devices.delete(index);
179
+ this.emit('deviceremoved', device);
180
+ }
181
+ }
182
+ }
183
+
184
+ protected requestDeviceList = async () => {
185
+ this.checkConnector();
186
+ this._logger.Debug('ButtplugClient: ReceiveDeviceList called');
187
+ const response = (await this.sendMessage(
188
+ {
189
+ RequestDeviceList: { Id: 1 }
190
+ }
191
+ ));
192
+ this.parseDeviceList(response.DeviceList!);
193
+ };
194
+
195
+ protected shutdownConnection = async () => {
196
+ await this.stopAllDevices();
197
+ if (this._pingTimer !== null) {
198
+ clearInterval(this._pingTimer);
199
+ this._pingTimer = null;
200
+ }
201
+ };
202
+
203
+ protected async sendMessage(
204
+ msg: Messages.ButtplugMessage
205
+ ): Promise<Messages.ButtplugMessage> {
206
+ this.checkConnector();
207
+ const p = this._sorter.PrepareOutgoingMessage(msg);
208
+ await this._connector!.send(msg);
209
+ return await p;
210
+ }
211
+
212
+ protected checkConnector() {
213
+ if (!this.connected) {
214
+ throw new ButtplugClientConnectorException(
215
+ 'ButtplugClient not connected'
216
+ );
217
+ }
218
+ }
219
+
220
+ protected sendMsgExpectOk = async (
221
+ msg: Messages.ButtplugMessage
222
+ ): Promise<void> => {
223
+ const response = await this.sendMessage(msg);
224
+ if (response.Ok !== undefined) {
225
+ return;
226
+ } else if (response.Error !== undefined) {
227
+ throw ButtplugError.FromError(response as Messages.Error);
228
+ } else {
229
+ throw ButtplugError.LogAndError(
230
+ ButtplugMessageError,
231
+ this._logger,
232
+ `Message ${response} not handled by SendMsgExpectOk`
233
+ );
234
+ }
235
+ };
236
+
237
+ protected sendMessageClosure = async (
238
+ msg: Messages.ButtplugMessage
239
+ ): Promise<Messages.ButtplugMessage> => {
240
+ return await this.sendMessage(msg);
241
+ };
242
+ }
@@ -1,16 +1,16 @@
1
- /*!
2
- * Buttplug JS Source Code File - Visit https://buttplug.io for more info about
3
- * the project. Licensed under the BSD 3-Clause license. See LICENSE file in the
4
- * project root for full license information.
5
- *
6
- * @copyright Copyright (c) Nonpolynomial Labs LLC. All rights reserved.
7
- */
8
-
9
- import { ButtplugError } from '../core/Exceptions';
10
- import * as Messages from '../core/Messages';
11
-
12
- export class ButtplugClientConnectorException extends ButtplugError {
13
- public constructor(message: string) {
14
- super(message, Messages.ErrorClass.ERROR_UNKNOWN);
15
- }
16
- }
1
+ /*!
2
+ * Buttplug JS Source Code File - Visit https://buttplug.io for more info about
3
+ * the project. Licensed under the BSD 3-Clause license. See LICENSE file in the
4
+ * project root for full license information.
5
+ *
6
+ * @copyright Copyright (c) Nonpolynomial Labs LLC. All rights reserved.
7
+ */
8
+
9
+ import { ButtplugError } from '../core/Exceptions';
10
+ import * as Messages from '../core/Messages';
11
+
12
+ export class ButtplugClientConnectorException extends ButtplugError {
13
+ public constructor(message: string) {
14
+ super(message, Messages.ErrorClass.ERROR_UNKNOWN);
15
+ }
16
+ }