-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebsocket-Server.py
61 lines (49 loc) · 1.52 KB
/
Websocket-Server.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
#!/usr/bin/env python
import asyncio
import datetime
import random
import websockets
import isotp
import json
s = isotp.socket()
s.bind("vcan0", isotp.Address(rxid=0x7E8, txid=0x7DF))
async def bytestring(websocket, path):
while True:
# Recieve command from client
command = await websocket.recv()
if (command == "DTC"):
data = b"\x03"
print("DTC Recieved")
elif (command == "RPM"):
data = b"\x01\x0C"
print("RPM Recieved")
elif (command == "PIDS"):
data = b"\x01\x00"
print("PIDS Recieved")
elif (command == "SPEED"):
data = b"\x01\x0D"
print("SPEED Recieved")
else:
print("Unknown Command.")
continue
# Send request data to CAN Bus.
try:
s.send(data)
except:
print("Error: ISOTP: Could not send request.")
# Wait a little bit, then get the response.
await asyncio.sleep(0.1)
try:
response = s.recv()
except:
print("Error: ISOTP: Could not get reponse.")
continue
# Send response back to client.
try:
await websocket.send(json.dumps(list(response)))
except:
print("Error: Websockets: Could not send reponse.")
continue
start_server = websockets.serve(bytestring, "0.0.0.0", 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()