/
Lovense.ts
157 lines (139 loc) · 6.41 KB
/
Lovense.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*!
* Buttplug JS Source Code File - Visit https://buttplug.io for more info about
* the project. Licensed under the BSD 3-Clause license. See LICENSE file in the
* project root for full license information.
*
* @copyright Copyright (c) Nonpolynomial Labs LLC. All rights reserved.
*/
import * as Messages from "../../core/Messages";
import { ButtplugDeviceException } from "../../core/Exceptions";
import { ButtplugDeviceProtocol } from "../ButtplugDeviceProtocol";
import { IButtplugDeviceImpl } from "../IButtplugDeviceImpl";
import { Endpoints } from "devices/Endpoints";
export class Lovense extends ButtplugDeviceProtocol {
private static _deviceNames = {
A: "Nora",
B: "Max",
C: "Nora",
L: "Ambi",
O: "Osci",
P: "Edge",
S: "Lush",
W: "Domi",
Z: "Hush",
0: "Unknown",
};
private _initResolve: (() => void) | undefined;
private _initPromise = new Promise((res) => { this._initResolve = res; });
private _isClockwise = false;
private _specs: any = {
VibrateCmd: { FeatureCount: 1 },
SingleMotorVibrateCmd: {},
StopDeviceCmd: {},
};
public constructor(aDeviceImpl: IButtplugDeviceImpl) {
super(`Lovense ${aDeviceImpl.Name}`, aDeviceImpl);
}
public Initialize = async (): Promise<void> => {
this._device.addListener("updateReceived", this.OnValueChanged);
await this._device.SubscribeToUpdates();
// xxx This is a bogus read that is required for noble on linux to dump
// some weird characteristic value we get back on first notify. This doesn't
// seem to happen in WebBluetooth.
await this._device.ReadString();
await this._device.WriteString("DeviceType;");
this._logger.Debug(`Device ${this._device.Name} waiting for initialization return`);
await this._initPromise;
}
public get MessageSpecifications(): object {
return this._specs;
}
private ParseDeviceType(aDeviceType: string) {
// This will return 3 values, but the last one (device address) we don't
// really care about.
let [deviceLetter, deviceVersion] = aDeviceType.split(":");
if (!Lovense._deviceNames.hasOwnProperty(deviceLetter)) {
deviceLetter = "0";
}
this._name = `Lovense ${Lovense._deviceNames[deviceLetter]} v${deviceVersion}`;
this.MsgFuncs.set(Messages.StopDeviceCmd, this.HandleStopDeviceCmd);
this.MsgFuncs.set(Messages.VibrateCmd, this.HandleVibrateCmd);
this.MsgFuncs.set(Messages.SingleMotorVibrateCmd, this.HandleSingleMotorVibrateCmd);
if (deviceLetter === "P") {
// Edge has 2 motors
this._specs.VibrateCmd = { FeatureCount: 2 };
} else if (deviceLetter === "A" || deviceLetter === "C") {
// Nora has rotation
this._specs.RotateCmd = { FeatureCount: 1 };
this.MsgFuncs.set(Messages.RotateCmd, this.HandleRotateCmd);
}
}
private OnValueChanged = async ([aEndpoint, aValue]: [Endpoints, Buffer]) => {
// If we haven't initialized yet, consider this to be the first read, for the device info.
if (this._initResolve !== undefined) {
let identStr = aValue.toString('utf-8');
// For some reason, our usual tricks with subscribe/notify don't work with
// noble, meaning older devices like the Nora and Max won't ever send a
// valid ident string. In this case, we just kludge an ident based on the
// device name and hope it works. Any device named
// LVS-[devicename][firmwarename] shouldn't hit this block, this really
// only applies to the oldest firmware.
if (identStr.length === 0 || identStr.indexOf(":") === -1) {
this._logger.Debug(`Lovense Device ${this._device.Name} got invalid initialization return "${identStr}, falling back to fake init`);
identStr=`${this._device.Name.substr(4, 1)}:00:000000000000`;
}
this._logger.Debug(`Lovense Device ${this._device.Name} got initialization return ${identStr}`);
this.ParseDeviceType(identStr);
this._initResolve();
this._initResolve = undefined;
return;
}
// TODO Fill in battery/accelerometer/etc reads
}
private HandleStopDeviceCmd = async (aMsg: Messages.StopDeviceCmd): Promise<Messages.ButtplugMessage> => {
await this.HandleSingleMotorVibrateCmd(new Messages.SingleMotorVibrateCmd(0, aMsg.DeviceIndex, aMsg.Id));
if (this._specs.hasOwnProperty("RotateCmd")) {
this.HandleRotateCmd(new Messages.RotateCmd([new Messages.RotateSubcommand(0, 0, this._isClockwise)], 0, aMsg.Id));
}
return new Messages.Ok(aMsg.Id);
}
private HandleSingleMotorVibrateCmd =
async (aMsg: Messages.SingleMotorVibrateCmd): Promise<Messages.ButtplugMessage> => {
const speeds: Messages.SpeedSubcommand[] = [];
for (let i = 0; i < this._specs.VibrateCmd.FeatureCount; i++) {
speeds.push(new Messages.SpeedSubcommand(i, aMsg.Speed));
}
return await this.HandleVibrateCmd(new Messages.VibrateCmd(speeds, aMsg.DeviceIndex, aMsg.Id));
}
private HandleVibrateCmd = async (aMsg: Messages.VibrateCmd): Promise<Messages.ButtplugMessage> => {
if (aMsg.Speeds.length > this._specs.VibrateCmd.FeatureCount) {
throw new ButtplugDeviceException(`Lovense devices require VibrateCmd to have at most ` +
`${this._specs.VibrateCmd.FeatureCount} speed commands, ` +
`${aMsg.Speeds.length} sent.`,
aMsg.Id);
}
for (const cmd of aMsg.Speeds) {
const index = this._specs.VibrateCmd.FeatureCount > 1 ? (cmd.Index + 1).toString(10) : "";
const speed = Math.floor(20 * cmd.Speed);
await this._device.WriteString(`Vibrate${index}:${speed};`);
}
return new Messages.Ok(aMsg.Id);
}
private HandleRotateCmd = async (aMsg: Messages.RotateCmd): Promise<Messages.ButtplugMessage> => {
if (aMsg.Rotations.length !== 1) {
throw new ButtplugDeviceException(`Lovense devices require RotateCmd to have 1 rotate command, ` +
`${aMsg.Rotations.length} sent.`,
aMsg.Id);
}
const rotateCmd = aMsg.Rotations[0];
if (rotateCmd.Index !== 0) {
throw new ButtplugDeviceException("Rotation command sent for invalid index.");
}
if (rotateCmd.Clockwise !== this._isClockwise) {
await this._device.WriteString("RotateChange;");
}
const speed = Math.floor(20 * rotateCmd.Speed);
await this._device.WriteString(`Rotate:${speed};`);
return new Messages.Ok(aMsg.Id);
}
}