forked from doublerobotics/d3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble.py
More file actions
28 lines (24 loc) · 814 Bytes
/
double.py
File metadata and controls
28 lines (24 loc) · 814 Bytes
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
import threading
import socket
import json
class DRDoubleSDK():
def __init__(self, address='/tmp/doubleapi'):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(address)
def close(self):
self.sock.close()
def sendCommand(self, command, data=None):
packet = { 'c': command }
if data is not None: packet['d'] = data
jsonString = json.dumps(packet)
self.sock.send(jsonString.encode('utf-8'))
def recv(self):
packet = self.sock.recv(4096).decode('utf-8')
if not packet:
exit('Error: received None from D3SDK')
object = None
try:
object = json.loads(packet)
except ValueError as e:
print("JSON Parse error", packet)
return object