UI / Python Websocket
This commit is contained in:
20
PointCloudWeb.Scanner/test/index.html
Normal file
20
PointCloudWeb.Scanner/test/index.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WebSocket demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
var ws = new WebSocket("ws://127.0.0.1:5678/"),
|
||||
messages = document.createElement('ul');
|
||||
ws.onmessage = function (event) {
|
||||
var messages = document.getElementsByTagName('ul')[0],
|
||||
message = document.createElement('li'),
|
||||
content = document.createTextNode(event.data);
|
||||
message.appendChild(content);
|
||||
messages.appendChild(message);
|
||||
};
|
||||
document.body.appendChild(messages);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
21
PointCloudWeb.Scanner/test/lida.py
Normal file
21
PointCloudWeb.Scanner/test/lida.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import PyLidar3
|
||||
import time # Time module
|
||||
#Serial port to which lidar connected, Get it from device manager windows
|
||||
#In linux type in terminal -- ls /dev/tty*
|
||||
#port = input("Enter port name which lidar is connected:") #windows
|
||||
#port = "/dev/ttyUSB0" #linux
|
||||
f = open("PoinCloudWeb.Scanner\datafile.txt","wt")
|
||||
Obj = PyLidar3.YdLidarX4(port='COM6',chunk_size=20000) #PyLidar3.your_version_of_lidar(port,chunk_size)
|
||||
if(Obj.Connect()):
|
||||
print(Obj.GetDeviceInfo())
|
||||
gen = Obj.StartScanning()
|
||||
t = time.time() # start time
|
||||
data = next(gen)
|
||||
#print(data)
|
||||
for x,y in data.items():
|
||||
f.write(str(x) + " / " + str(y) + "\n")
|
||||
f.close()
|
||||
Obj.StopScanning()
|
||||
Obj.Disconnect()
|
||||
else:
|
||||
print("Error connecting to device")
|
||||
69
PointCloudWeb.Scanner/test/ws.py
Normal file
69
PointCloudWeb.Scanner/test/ws.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# WS server example that synchronizes state across clients
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import websockets
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
STATE = {"value": 0}
|
||||
|
||||
USERS = set()
|
||||
|
||||
|
||||
def state_event():
|
||||
return json.dumps({"type": "state", **STATE})
|
||||
|
||||
|
||||
def users_event():
|
||||
return json.dumps({"type": "users", "count": len(USERS)})
|
||||
|
||||
|
||||
async def notify_state():
|
||||
if USERS: # asyncio.wait doesn't accept an empty list
|
||||
message = state_event()
|
||||
await asyncio.wait([user.send(message) for user in USERS])
|
||||
|
||||
|
||||
async def notify_users():
|
||||
if USERS: # asyncio.wait doesn't accept an empty list
|
||||
message = users_event()
|
||||
await asyncio.wait([user.send(message) for user in USERS])
|
||||
|
||||
|
||||
async def register(websocket):
|
||||
USERS.add(websocket)
|
||||
await notify_users()
|
||||
|
||||
|
||||
async def unregister(websocket):
|
||||
USERS.remove(websocket)
|
||||
await notify_users()
|
||||
|
||||
|
||||
async def counter(websocket, path):
|
||||
# register(websocket) sends user_event() to websocket
|
||||
await register(websocket)
|
||||
try:
|
||||
await websocket.send(state_event())
|
||||
async for message in websocket:
|
||||
data = json.loads(message)
|
||||
if data["action"] == "minus":
|
||||
STATE["value"] -= 1
|
||||
await notify_state()
|
||||
elif data["action"] == "plus":
|
||||
STATE["value"] += 1
|
||||
await notify_state()
|
||||
else:
|
||||
logging.error("unsupported event: %s", data)
|
||||
finally:
|
||||
await unregister(websocket)
|
||||
|
||||
|
||||
start_server = websockets.serve(counter, "localhost", 6789)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
Reference in New Issue
Block a user