-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (41 loc) · 1.7 KB
/
script.js
File metadata and controls
44 lines (41 loc) · 1.7 KB
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
// Bring up websocket and perform device handshake
let deviceWebsocket = new WebSocket("ws://127.0.0.1:54817");
let deviceAddress = "A9816725B";
let handshakePacket = {
identifier: "LVS-Test",
address: deviceAddress,
version: 0
};
// When we open, send handshake and setup event handlers
deviceWebsocket.addEventListener("open", (socket) => {
console.log("Connected");
deviceWebsocket.send(JSON.stringify(handshakePacket));
console.log("Handshake Sent");
deviceWebsocket.addEventListener("message", async (event) => {
let msg = await event.data.text();
console.log(msg);
if (msg.indexOf("DeviceType;") !== -1) {
console.log("got device type");
console.log(`Z:10:${deviceAddress};`);
// Lovense initialization request
deviceWebsocket.send(`Z:10:${deviceAddress};`);
} else if (msg.indexOf("Battery;") !== -1) {
console.log("Got battery");
// Buttplug will wait for a response to Battery so just make something up.
deviceWebsocket.send("90;");
} else {
console.log(`Lovense command: ${msg}`);
// If it's a vibrate message, get the vibrate level, which will be 0-20.
let regex = /Vibrate:([0-9]+)/;
let match = msg.match(regex);
if (match.length > 1) {
console.log(match[1]);
let rotateValue = ((match[1] / 20.0) * 240) - 120;
document.getElementById("rotatepin").style.transform = `rotate(${rotateValue}deg)`;
document.getElementById("rotateline").style.transform = `rotate(${rotateValue}deg)`;
}
//document.getElementById("lovense-output").innerHTML = msg;
// If we wanted to conform with the Lovense protocol we'd send "OK;" here, but Buttplug doesn't care.
}
});
});