-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_to_intiface_central_demo.py
90 lines (79 loc) · 2.76 KB
/
connect_to_intiface_central_demo.py
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
#You can find the intiface central development manual here, and here is a simple demo for your reference.
#https://buttplug-developer-guide.docs.buttplug.io/docs/spec/architecture/
#This demo should allow you to test any linear device.(osr2 sr6 handy ssr)
#After establishing a connection with intiface central using the websocket library in any programming language.
#Tell intiface central who is connected to it and start getting available devices.
#Select one of LinearCmd, RotateCmd, or ScalarCmd control method according to the device type.
#Since I only have the LinearCmd device this demo can only be tested like this.
import websocket
import _thread
import threading
import time
import rel
import json
def on_message(ws, message):
print(f"get message: {message}")
def on_error(ws, error):
print(f"error: {error}")
def on_close(ws, close_status_code, close_msg):
print("### socket has been closed###")
def on_open(ws):
print("builded connection")
def run(*args):
# Create LinearCmd messages
handshake_msg = {
"RequestServerInfo": {
"Id": 1,
"ClientName": "Python Test Client",
"MessageVersion": 1
}
}
ws.send(json.dumps([handshake_msg]))
requestdevicelist ={
"RequestDeviceList": {
"Id": 1
}
}
ws.send(json.dumps([requestdevicelist]))
startScanning ={
"StartScanning": {
"Id": 1
}
}
ws.send(json.dumps([startScanning]))
time.sleep(1)
stopscanning ={
"StopScanning": {
"Id": 1
}
}
ws.send(json.dumps([stopscanning]))
time.sleep(1)
linearcmd = {
"LinearCmd": {
"Id": 1,
"DeviceIndex": 0,
"Vectors": [
{
"Index": 0,
"Duration": 1000,# Just make sure it's an integer greater than 0, in milliseconds.
"Position": 1,# Percentages are calculated in the range 0-100.
},
]*1
}
}
ws.send(json.dumps([linearcmd]))
ws.close()
print("websocket close")
_thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:12345",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
#ws.send("")
ws.run_forever(dispatcher=rel)
rel.signal(2, rel.abort) # Ctrl+C exit
rel.dispatch()