buttplug 3.2.1 → 4.0.0

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 -1
  6. package/CHANGELOG.md +577 -571
  7. package/LICENSE +27 -27
  8. package/README.md +97 -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 +227 -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 +118 -228
  33. package/dist/main/src/core/Messages.js +51 -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 +118 -227
  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 +209 -480
  95. package/src/core/index.d.ts +4 -4
  96. package/src/index.ts +21 -19
  97. package/src/utils/ButtplugBrowserWebsocketConnector.ts +89 -92
  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,112 @@
1
+ import { ButtplugDeviceError } from "../core/Exceptions";
2
+ import { OutputType } from "../core/Messages";
3
+
4
+ class PercentOrSteps {
5
+ private _percent: number | undefined;
6
+ private _steps: number | undefined;
7
+
8
+ public get percent() {
9
+ return this._percent;
10
+ }
11
+
12
+ public get steps() {
13
+ return this._steps;
14
+ }
15
+
16
+ public static createSteps(s: number): PercentOrSteps {
17
+ let v = new PercentOrSteps;
18
+ v._steps = s;
19
+ return v;
20
+ }
21
+
22
+ public static createPercent(p: number): PercentOrSteps {
23
+ if (p < 0 || p > 1.0) {
24
+ throw new ButtplugDeviceError(`Percent value ${p} is not in the range 0.0 <= x <= 1.0`);
25
+ }
26
+
27
+ let v = new PercentOrSteps;
28
+ v._percent = p;
29
+ return v;
30
+ }
31
+ }
32
+
33
+ export class DeviceOutputCommand {
34
+ public constructor(
35
+ private _outputType: OutputType,
36
+ private _value: PercentOrSteps,
37
+ private _duration?: number,
38
+ )
39
+ {}
40
+
41
+ public get outputType() {
42
+ return this._outputType;
43
+ }
44
+
45
+ public get value() {
46
+ return this._value;
47
+ }
48
+
49
+ public get duration() {
50
+ return this._duration;
51
+ }
52
+ }
53
+
54
+ export class DeviceOutputValueConstructor {
55
+ public constructor(
56
+ private _outputType: OutputType)
57
+ {}
58
+
59
+ public steps(steps: number): DeviceOutputCommand {
60
+ return new DeviceOutputCommand(this._outputType, PercentOrSteps.createSteps(steps), undefined);
61
+ }
62
+
63
+ public percent(percent: number): DeviceOutputCommand {
64
+ return new DeviceOutputCommand(this._outputType, PercentOrSteps.createPercent(percent), undefined);
65
+ }
66
+ }
67
+
68
+ export class DeviceOutputPositionWithDurationConstructor {
69
+ public steps(steps: number, duration: number): DeviceOutputCommand {
70
+ return new DeviceOutputCommand(OutputType.Position, PercentOrSteps.createSteps(steps), duration);
71
+ }
72
+
73
+ public percent(percent: number, duration: number): DeviceOutputCommand {
74
+ return new DeviceOutputCommand(OutputType.HwPositionWithDuration, PercentOrSteps.createPercent(percent), duration);
75
+ }
76
+ }
77
+
78
+ export class DeviceOutput {
79
+ private constructor() {}
80
+
81
+ public static get Vibrate() {
82
+ return new DeviceOutputValueConstructor(OutputType.Vibrate);
83
+ }
84
+ public static get Rotate() {
85
+ return new DeviceOutputValueConstructor(OutputType.Rotate);
86
+ }
87
+ public static get Oscillate() {
88
+ return new DeviceOutputValueConstructor(OutputType.Oscillate);
89
+ }
90
+ public static get Constrict() {
91
+ return new DeviceOutputValueConstructor(OutputType.Constrict);
92
+ }
93
+ public static get Inflate() {
94
+ return new DeviceOutputValueConstructor(OutputType.Inflate);
95
+ }
96
+ public static get Temperature() {
97
+ return new DeviceOutputValueConstructor(OutputType.Temperature);
98
+ }
99
+ public static get Led() {
100
+ return new DeviceOutputValueConstructor(OutputType.Led);
101
+ }
102
+ public static get Spray() {
103
+ return new DeviceOutputValueConstructor(OutputType.Spray);
104
+ }
105
+ public static get Position() {
106
+ return new DeviceOutputValueConstructor(OutputType.Position);
107
+ }
108
+ public static get PositionWithDuration() {
109
+ return new DeviceOutputPositionWithDurationConstructor();
110
+ }
111
+ }
112
+
@@ -0,0 +1,138 @@
1
+ import { ButtplugDeviceError, ButtplugError, ButtplugMessageError } from "../core/Exceptions";
2
+ import * as Messages from "../core/Messages";
3
+ import { DeviceOutputCommand } from "./ButtplugClientDeviceCommand";
4
+
5
+ export class ButtplugClientDeviceFeature {
6
+
7
+ constructor(
8
+ private _deviceIndex: number,
9
+ private _deviceName: string,
10
+ private _feature: Messages.DeviceFeature,
11
+ private _sendClosure: (
12
+ msg: Messages.ButtplugMessage
13
+ ) => Promise<Messages.ButtplugMessage>) {
14
+ }
15
+
16
+ protected send = async (msg: Messages.ButtplugMessage): Promise<Messages.ButtplugMessage> => {
17
+ return await this._sendClosure(msg);
18
+ }
19
+
20
+ protected sendMsgExpectOk = async (
21
+ msg: Messages.ButtplugMessage
22
+ ): Promise<void> => {
23
+ const response = await this.send(msg);
24
+ if (response.Ok !== undefined) {
25
+ return;
26
+ } else if (response.Error !== undefined) {
27
+ throw ButtplugError.FromError(response as Messages.Error);
28
+ } else {
29
+ throw new ButtplugMessageError("Expected Ok or Error, and didn't get either!");
30
+ }
31
+ };
32
+
33
+ protected isOutputValid(type: Messages.OutputType) {
34
+ if (this._feature.Output !== undefined && !this._feature.Output.hasOwnProperty(type)) {
35
+ throw new ButtplugDeviceError(`Feature index ${this._feature.FeatureIndex} does not support type ${type} for device ${this._deviceName}`);
36
+ }
37
+ }
38
+
39
+ protected isInputValid(type: Messages.InputType) {
40
+ if (this._feature.Input !== undefined && !this._feature.Input.hasOwnProperty(type)) {
41
+ throw new ButtplugDeviceError(`Feature index ${this._feature.FeatureIndex} does not support type ${type} for device ${this._deviceName}`);
42
+ }
43
+ }
44
+
45
+ protected async sendOutputCmd(command: DeviceOutputCommand): Promise<void> {
46
+ // Make sure the requested feature is valid
47
+ this.isOutputValid(command.outputType);
48
+ if (command.value === undefined) {
49
+ throw new ButtplugDeviceError(`${command.outputType} requires value defined`);
50
+ }
51
+
52
+ let type = command.outputType;
53
+ let duration: undefined | number = undefined;
54
+ if (type == Messages.OutputType.HwPositionWithDuration) {
55
+ if (command.duration === undefined) {
56
+ throw new ButtplugDeviceError("PositionWithDuration requires duration defined");
57
+ }
58
+ duration = command.duration;
59
+ }
60
+ let value: number;
61
+ let p = command.value;
62
+ if (p.percent === undefined) {
63
+ // TODO Check step limits here
64
+ value = command.value.steps!;
65
+ } else {
66
+ value = Math.ceil(this._feature.Output[type]!.Value![1] * p.percent);
67
+ }
68
+ let newCommand: Messages.DeviceFeatureOutput = { Value: value, Duration: duration };
69
+ let outCommand = {};
70
+ outCommand[type.toString()] = newCommand;
71
+
72
+ let cmd: Messages.ButtplugMessage = {
73
+ OutputCmd: {
74
+ Id: 1,
75
+ DeviceIndex: this._deviceIndex,
76
+ FeatureIndex: this._feature.FeatureIndex,
77
+ Command: outCommand
78
+ }
79
+ };
80
+ await this.sendMsgExpectOk(cmd);
81
+ }
82
+
83
+ public hasOutput(type: Messages.OutputType): boolean {
84
+ if (this._feature.Output !== undefined) {
85
+ return this._feature.Output.hasOwnProperty(type.toString());
86
+ }
87
+ return false;
88
+ }
89
+
90
+ public hasInput(type: Messages.InputType): boolean {
91
+ if (this._feature.Input !== undefined) {
92
+ return this._feature.Input.hasOwnProperty(type.toString());
93
+ }
94
+ return false;
95
+ }
96
+
97
+
98
+ public async runOutput(cmd: DeviceOutputCommand): Promise<void> {
99
+ if (this._feature.Output !== undefined && this._feature.Output.hasOwnProperty(cmd.outputType.toString())) {
100
+ return this.sendOutputCmd(cmd);
101
+ }
102
+ throw new ButtplugDeviceError(`Output type ${cmd.outputType} not supported by feature.`);
103
+ }
104
+
105
+ public async runInput(inputType: Messages.InputType, inputCommand: Messages.InputCommandType): Promise<Messages.InputReading | undefined> {
106
+ // Make sure the requested feature is valid
107
+ this.isInputValid(inputType);
108
+ let inputAttributes = this._feature.Input[inputType];
109
+ console.log(this._feature.Input);
110
+ if ((inputCommand === Messages.InputCommandType.Unsubscribe && !inputAttributes.Command.includes(Messages.InputCommandType.Subscribe)) && !inputAttributes.Command.includes(inputCommand)) {
111
+ throw new ButtplugDeviceError(`${inputType} does not support command ${inputCommand}`);
112
+ }
113
+
114
+ let cmd: Messages.ButtplugMessage = {
115
+ InputCmd: {
116
+ Id: 1,
117
+ DeviceIndex: this._deviceIndex,
118
+ FeatureIndex: this._feature.FeatureIndex,
119
+ Type: inputType,
120
+ Command: inputCommand,
121
+ }
122
+ };
123
+ if (inputCommand == Messages.InputCommandType.Read) {
124
+ const response = await this.send(cmd);
125
+ if (response.InputReading !== undefined) {
126
+ return response.InputReading;
127
+ } else if (response.Error !== undefined) {
128
+ throw ButtplugError.FromError(response as Messages.Error);
129
+ } else {
130
+ throw new ButtplugMessageError("Expected InputReading or Error, and didn't get either!");
131
+ }
132
+ } else {
133
+ console.log(`Sending subscribe message: ${JSON.stringify(cmd)}`);
134
+ await this.sendMsgExpectOk(cmd);
135
+ console.log("Got back ok?");
136
+ }
137
+ }
138
+ }
@@ -1,17 +1,17 @@
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 { ButtplugBrowserWebsocketClientConnector } from './ButtplugBrowserWebsocketClientConnector';
12
- import { WebSocket as NodeWebSocket } from 'ws';
13
-
14
- export class ButtplugNodeWebsocketClientConnector extends ButtplugBrowserWebsocketClientConnector {
15
- protected _websocketConstructor =
16
- NodeWebSocket as unknown as typeof WebSocket;
17
- }
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 { ButtplugBrowserWebsocketClientConnector } from './ButtplugBrowserWebsocketClientConnector';
12
+ import { WebSocket as NodeWebSocket } from 'ws';
13
+
14
+ export class ButtplugNodeWebsocketClientConnector extends ButtplugBrowserWebsocketClientConnector {
15
+ protected _websocketConstructor =
16
+ NodeWebSocket as unknown as typeof WebSocket;
17
+ }
@@ -1,18 +1,18 @@
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 { ButtplugMessage } from '../core/Messages';
10
- import { EventEmitter } from 'eventemitter3';
11
-
12
- export interface IButtplugClientConnector extends EventEmitter {
13
- connect: () => Promise<void>;
14
- disconnect: () => Promise<void>;
15
- initialize: () => Promise<void>;
16
- send: (msg: ButtplugMessage) => void;
17
- readonly Connected: boolean;
18
- }
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 { ButtplugMessage } from '../core/Messages';
10
+ import { EventEmitter } from 'eventemitter3';
11
+
12
+ export interface IButtplugClientConnector extends EventEmitter {
13
+ connect: () => Promise<void>;
14
+ disconnect: () => Promise<void>;
15
+ initialize: () => Promise<void>;
16
+ send: (msg: ButtplugMessage) => void;
17
+ readonly Connected: boolean;
18
+ }
@@ -1,101 +1,107 @@
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 * as Messages from './Messages';
10
- import { ButtplugLogger } from './Logging';
11
-
12
- export class ButtplugError extends Error {
13
- public get ErrorClass(): Messages.ErrorClass {
14
- return this.errorClass;
15
- }
16
-
17
- public get InnerError(): Error | undefined {
18
- return this.innerError;
19
- }
20
-
21
- public get Id(): number | undefined {
22
- return this.messageId;
23
- }
24
-
25
- public get ErrorMessage(): Messages.ButtplugMessage {
26
- return new Messages.Error(this.message, this.ErrorClass, this.Id);
27
- }
28
-
29
- public static LogAndError<T extends ButtplugError>(
30
- constructor: new (str: string, num: number) => T,
31
- logger: ButtplugLogger,
32
- message: string,
33
- id: number = Messages.SYSTEM_MESSAGE_ID
34
- ): T {
35
- logger.Error(message);
36
- return new constructor(message, id);
37
- }
38
-
39
- public static FromError(error: Messages.Error) {
40
- switch (error.ErrorCode) {
41
- case Messages.ErrorClass.ERROR_DEVICE:
42
- return new ButtplugDeviceError(error.ErrorMessage, error.Id);
43
- case Messages.ErrorClass.ERROR_INIT:
44
- return new ButtplugInitError(error.ErrorMessage, error.Id);
45
- case Messages.ErrorClass.ERROR_UNKNOWN:
46
- return new ButtplugUnknownError(error.ErrorMessage, error.Id);
47
- case Messages.ErrorClass.ERROR_PING:
48
- return new ButtplugPingError(error.ErrorMessage, error.Id);
49
- case Messages.ErrorClass.ERROR_MSG:
50
- return new ButtplugMessageError(error.ErrorMessage, error.Id);
51
- default:
52
- throw new Error(`Message type ${error.ErrorCode} not handled`);
53
- }
54
- }
55
-
56
- public errorClass: Messages.ErrorClass = Messages.ErrorClass.ERROR_UNKNOWN;
57
- public innerError: Error | undefined;
58
- public messageId: number | undefined;
59
-
60
- protected constructor(
61
- message: string,
62
- errorClass: Messages.ErrorClass,
63
- id: number = Messages.SYSTEM_MESSAGE_ID,
64
- inner?: Error
65
- ) {
66
- super(message);
67
- this.errorClass = errorClass;
68
- this.innerError = inner;
69
- this.messageId = id;
70
- }
71
- }
72
-
73
- export class ButtplugInitError extends ButtplugError {
74
- public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
75
- super(message, Messages.ErrorClass.ERROR_INIT, id);
76
- }
77
- }
78
-
79
- export class ButtplugDeviceError extends ButtplugError {
80
- public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
81
- super(message, Messages.ErrorClass.ERROR_DEVICE, id);
82
- }
83
- }
84
-
85
- export class ButtplugMessageError extends ButtplugError {
86
- public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
87
- super(message, Messages.ErrorClass.ERROR_MSG, id);
88
- }
89
- }
90
-
91
- export class ButtplugPingError extends ButtplugError {
92
- public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
93
- super(message, Messages.ErrorClass.ERROR_PING, id);
94
- }
95
- }
96
-
97
- export class ButtplugUnknownError extends ButtplugError {
98
- public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
99
- super(message, Messages.ErrorClass.ERROR_UNKNOWN, id);
100
- }
101
- }
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 * as Messages from './Messages';
10
+ import { ButtplugLogger } from './Logging';
11
+
12
+ export class ButtplugError extends Error {
13
+ public get ErrorClass(): Messages.ErrorClass {
14
+ return this.errorClass;
15
+ }
16
+
17
+ public get InnerError(): Error | undefined {
18
+ return this.innerError;
19
+ }
20
+
21
+ public get Id(): number | undefined {
22
+ return this.messageId;
23
+ }
24
+
25
+ public get ErrorMessage(): Messages.ButtplugMessage {
26
+ return {
27
+ Error: {
28
+ Id: this.Id,
29
+ ErrorCode: this.ErrorClass,
30
+ ErrorMessage: this.message
31
+ }
32
+ }
33
+ }
34
+
35
+ public static LogAndError<T extends ButtplugError>(
36
+ constructor: new (str: string, num: number) => T,
37
+ logger: ButtplugLogger,
38
+ message: string,
39
+ id: number = Messages.SYSTEM_MESSAGE_ID
40
+ ): T {
41
+ logger.Error(message);
42
+ return new constructor(message, id);
43
+ }
44
+
45
+ public static FromError(error: Messages.Error) {
46
+ switch (error.ErrorCode) {
47
+ case Messages.ErrorClass.ERROR_DEVICE:
48
+ return new ButtplugDeviceError(error.ErrorMessage, error.Id);
49
+ case Messages.ErrorClass.ERROR_INIT:
50
+ return new ButtplugInitError(error.ErrorMessage, error.Id);
51
+ case Messages.ErrorClass.ERROR_UNKNOWN:
52
+ return new ButtplugUnknownError(error.ErrorMessage, error.Id);
53
+ case Messages.ErrorClass.ERROR_PING:
54
+ return new ButtplugPingError(error.ErrorMessage, error.Id);
55
+ case Messages.ErrorClass.ERROR_MSG:
56
+ return new ButtplugMessageError(error.ErrorMessage, error.Id);
57
+ default:
58
+ throw new Error(`Message type ${error.ErrorCode} not handled`);
59
+ }
60
+ }
61
+
62
+ public errorClass: Messages.ErrorClass = Messages.ErrorClass.ERROR_UNKNOWN;
63
+ public innerError: Error | undefined;
64
+ public messageId: number | undefined;
65
+
66
+ protected constructor(
67
+ message: string,
68
+ errorClass: Messages.ErrorClass,
69
+ id: number = Messages.SYSTEM_MESSAGE_ID,
70
+ inner?: Error
71
+ ) {
72
+ super(message);
73
+ this.errorClass = errorClass;
74
+ this.innerError = inner;
75
+ this.messageId = id;
76
+ }
77
+ }
78
+
79
+ export class ButtplugInitError extends ButtplugError {
80
+ public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
81
+ super(message, Messages.ErrorClass.ERROR_INIT, id);
82
+ }
83
+ }
84
+
85
+ export class ButtplugDeviceError extends ButtplugError {
86
+ public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
87
+ super(message, Messages.ErrorClass.ERROR_DEVICE, id);
88
+ }
89
+ }
90
+
91
+ export class ButtplugMessageError extends ButtplugError {
92
+ public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
93
+ super(message, Messages.ErrorClass.ERROR_MSG, id);
94
+ }
95
+ }
96
+
97
+ export class ButtplugPingError extends ButtplugError {
98
+ public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
99
+ super(message, Messages.ErrorClass.ERROR_PING, id);
100
+ }
101
+ }
102
+
103
+ export class ButtplugUnknownError extends ButtplugError {
104
+ public constructor(message: string, id: number = Messages.SYSTEM_MESSAGE_ID) {
105
+ super(message, Messages.ErrorClass.ERROR_UNKNOWN, id);
106
+ }
107
+ }