initial commit
init
This commit is contained in:
25
.gitignore
vendored
Normal file
25
.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
# build output
|
||||
dist/
|
||||
# generated types
|
||||
.astro/
|
||||
|
||||
# dependencies
|
||||
node_modules/
|
||||
|
||||
# logs
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
|
||||
|
||||
# environment variables
|
||||
.env
|
||||
.env.production
|
||||
|
||||
# macOS-specific files
|
||||
.DS_Store
|
||||
|
||||
# jetbrains setting folder
|
||||
.idea/
|
||||
.tags
|
||||
3
.prettierrc
Normal file
3
.prettierrc
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["prettier-plugin-astro","prettier-plugin-svelte","prettier-plugin-tailwindcss"]
|
||||
}
|
||||
4
.vscode/extensions.json
vendored
Normal file
4
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"recommendations": ["astro-build.astro-vscode"],
|
||||
"unwantedRecommendations": []
|
||||
}
|
||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "./node_modules/.bin/astro dev",
|
||||
"name": "Development server",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
||||
FROM alpine:3.20
|
||||
|
||||
RUN apk add --no-cache g++ python3 python3-dev py3-aiohttp git
|
||||
|
||||
RUN git clone --recursive https://github.com/vyv/psn-py.git
|
||||
WORKDIR /psn-py/vendors/pybind11
|
||||
# We need a newer version of pybind11 to compile agains python 3.12
|
||||
RUN git fetch --tags && git checkout v2.13.6
|
||||
WORKDIR /psn-py
|
||||
|
||||
RUN g++ -O3 -Wall -shared -fPIC $(python3-config --includes) \
|
||||
-Ivendors/psn/include -Ivendors/pybind11/include src/main.cpp \
|
||||
-o psn$(python3-config --extension-suffix)
|
||||
|
||||
COPY backend /backend
|
||||
WORKDIR /backend
|
||||
RUN cp /psn-py/psn$(python3-config --extension-suffix) .
|
||||
|
||||
ENTRYPOINT ["python3", "psn_server.py"]
|
||||
12
astro.config.mjs
Normal file
12
astro.config.mjs
Normal file
@@ -0,0 +1,12 @@
|
||||
// @ts-check
|
||||
// @ts-check
|
||||
import { defineConfig } from 'astro/config';
|
||||
|
||||
import svelte from '@astrojs/svelte';
|
||||
|
||||
import tailwind from '@astrojs/tailwind';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
integrations: [svelte(), tailwind()]
|
||||
});
|
||||
109
backend/psn_server.py
Normal file
109
backend/psn_server.py
Normal file
@@ -0,0 +1,109 @@
|
||||
import psn
|
||||
import time
|
||||
import socket
|
||||
import json
|
||||
from aiohttp import web
|
||||
import logging
|
||||
from dataclasses import dataclass, make_dataclass
|
||||
|
||||
PSN_DEFAULT_UDP_PORT = 56565
|
||||
PSN_DEFAULT_UDP_MCAST_ADDRESS = "236.10.10.10"
|
||||
PORT = 8000
|
||||
IP = "0.0.0.0"
|
||||
NUM_TRACKERS = 3
|
||||
|
||||
@dataclass
|
||||
class TrackerData:
|
||||
id: int
|
||||
x: float
|
||||
y: float
|
||||
|
||||
|
||||
trackers = {}
|
||||
|
||||
|
||||
for i in NUM_TRACKERS:
|
||||
trackers[i] = psn.Tracker(i, f"Tracker {i}")
|
||||
trackers[i].set_pos(psn.Float3(0, 0, 0))
|
||||
|
||||
def update_trackers(tracker_data_json: str):
|
||||
global trackers
|
||||
tracker_data = [TrackerData(**data) for data in json.loads(tracker_data_json)]
|
||||
|
||||
for tracker in tracker_data:
|
||||
trackers[tracker.id].set_pos(psn.Float3(tracker.x, tracker.y, 0))
|
||||
|
||||
def trackers_to_json():
|
||||
return json.dumps([{"id": tracker.id, "x": tracker.pos.x, "y": tracker.pos.y} for tracker in trackers.values()])
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
def get_time_ms():
|
||||
return int(time.time() * 1000)
|
||||
|
||||
|
||||
START_TIME = get_time_ms()
|
||||
|
||||
|
||||
def get_elapsed_time_ms():
|
||||
return get_time_ms() - START_TIME
|
||||
|
||||
|
||||
def pic_to_scene_coords(x, y):
|
||||
return x / 200, y / 200
|
||||
|
||||
|
||||
def send_positions():
|
||||
encoder = psn.Encoder("Server 1")
|
||||
|
||||
packets = encoder.encode_data(trackers, get_elapsed_time_ms())
|
||||
for packet in packets:
|
||||
sock.sendto(packet, (PSN_DEFAULT_UDP_MCAST_ADDRESS, PSN_DEFAULT_UDP_PORT))
|
||||
|
||||
|
||||
async def handle_websocket(request):
|
||||
ws = web.WebSocketResponse()
|
||||
logging.debug("Websocket connection starting")
|
||||
await ws.prepare(request)
|
||||
logging.debug("Websocket connection ready")
|
||||
|
||||
ws.send_str(trackers_to_json())
|
||||
|
||||
try:
|
||||
async for msg in ws:
|
||||
logging.debug(f"Websocket data: {msg}")
|
||||
if msg.type == web.WSMsgType.TEXT:
|
||||
logging.debug("Received message: %s" % msg.data)
|
||||
update_trackers(msg.data)
|
||||
send_positions()
|
||||
|
||||
elif msg.type == web.WSMsgType.ERROR:
|
||||
logging.error("ws connection closed with exception %s" % ws.exception())
|
||||
print("ws connection closed with exception %s" % ws.exception())
|
||||
except Exception as e:
|
||||
logging.error(f"Websocket exception: {e}")
|
||||
|
||||
finally:
|
||||
logging.debug("Websocket connection closing")
|
||||
await ws.close()
|
||||
|
||||
return ws
|
||||
|
||||
|
||||
|
||||
|
||||
async def handle_root(request):
|
||||
return web.FileResponse("./static/index.html")
|
||||
|
||||
|
||||
def create_app():
|
||||
app = web.Application()
|
||||
app.router.add_get("/", handle_root)
|
||||
app.router.add_static("/", "./static")
|
||||
app.router.add_get("/ws", handle_websocket)
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
web.run_app(create_app(), host=IP, port=PORT)
|
||||
BIN
backend/static/favicon.ico
Normal file
BIN
backend/static/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
backend/static/scene_drawing.png
Normal file
BIN
backend/static/scene_drawing.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 138 KiB |
5
docker-compose.yaml
Normal file
5
docker-compose.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
services:
|
||||
followspot-psn:
|
||||
build: .
|
||||
ports:
|
||||
- "8000:8000"
|
||||
26
package.json
Normal file
26
package.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "",
|
||||
"type": "module",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"dev": "astro dev",
|
||||
"build": "astro check && astro build",
|
||||
"preview": "astro preview",
|
||||
"astro": "astro"
|
||||
},
|
||||
"dependencies": {
|
||||
"@astrojs/check": "^0.9.4",
|
||||
"@astrojs/svelte": "^6.0.2",
|
||||
"@astrojs/tailwind": "^5.1.2",
|
||||
"astro": "^4.16.16",
|
||||
"svelte": "^5.2.12",
|
||||
"tailwindcss": "^3.4.15",
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^3.4.1",
|
||||
"prettier-plugin-astro": "^0.14.1",
|
||||
"prettier-plugin-svelte": "^3.3.2",
|
||||
"prettier-plugin-tailwindcss": "^0.6.9"
|
||||
}
|
||||
}
|
||||
4793
pnpm-lock.yaml
generated
Normal file
4793
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
9
public/favicon.svg
Normal file
9
public/favicon.svg
Normal file
@@ -0,0 +1,9 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 128 128">
|
||||
<path d="M50.4 78.5a75.1 75.1 0 0 0-28.5 6.9l24.2-65.7c.7-2 1.9-3.2 3.4-3.2h29c1.5 0 2.7 1.2 3.4 3.2l24.2 65.7s-11.6-7-28.5-7L67 45.5c-.4-1.7-1.6-2.8-2.9-2.8-1.3 0-2.5 1.1-2.9 2.7L50.4 78.5Zm-1.1 28.2Zm-4.2-20.2c-2 6.6-.6 15.8 4.2 20.2a17.5 17.5 0 0 1 .2-.7 5.5 5.5 0 0 1 5.7-4.5c2.8.1 4.3 1.5 4.7 4.7.2 1.1.2 2.3.2 3.5v.4c0 2.7.7 5.2 2.2 7.4a13 13 0 0 0 5.7 4.9v-.3l-.2-.3c-1.8-5.6-.5-9.5 4.4-12.8l1.5-1a73 73 0 0 0 3.2-2.2 16 16 0 0 0 6.8-11.4c.3-2 .1-4-.6-6l-.8.6-1.6 1a37 37 0 0 1-22.4 2.7c-5-.7-9.7-2-13.2-6.2Z" />
|
||||
<style>
|
||||
path { fill: #000; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
path { fill: #FFF; }
|
||||
}
|
||||
</style>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 749 B |
24
src/components/Container.svelte
Normal file
24
src/components/Container.svelte
Normal file
@@ -0,0 +1,24 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import type { TrackerData } from "../utils/types";
|
||||
import Drag from "./Drag.svelte";
|
||||
|
||||
let ws: WebSocket | null = $state(null);
|
||||
|
||||
let trackers: TrackerData[] = $state([
|
||||
{ id: 1, x: 0, y: 0 },
|
||||
{ id: 2, x: 400, y: 40 },
|
||||
]);
|
||||
|
||||
onMount(() => {
|
||||
ws = new WebSocket("/ws");
|
||||
ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
trackers = data;
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
{#each trackers as tracker}
|
||||
<Drag bind:id={tracker.id} bind:x={tracker.x} bind:y={tracker.y} {ws} />
|
||||
{/each}
|
||||
58
src/components/Drag.svelte
Normal file
58
src/components/Drag.svelte
Normal file
@@ -0,0 +1,58 @@
|
||||
<script lang="ts">
|
||||
type Props = {
|
||||
id: number;
|
||||
x: number;
|
||||
y: number;
|
||||
ws: WebSocket | null;
|
||||
};
|
||||
|
||||
let {
|
||||
id = $bindable(),
|
||||
x = $bindable(),
|
||||
y = $bindable(),
|
||||
ws = $bindable(),
|
||||
}: Props = $props();
|
||||
|
||||
let element: HTMLElement;
|
||||
|
||||
let capturedPointerId: number | null = $state(null);
|
||||
|
||||
function onPointerDown(e: PointerEvent) {
|
||||
element.setPointerCapture(e.pointerId);
|
||||
capturedPointerId = e.pointerId;
|
||||
}
|
||||
function onPointerUp(e: PointerEvent) {
|
||||
capturedPointerId = null;
|
||||
element.releasePointerCapture(e.pointerId);
|
||||
}
|
||||
function onPointerMove(e: PointerEvent) {
|
||||
if (capturedPointerId != e.pointerId) return;
|
||||
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
x += e.movementX;
|
||||
y += e.movementY;
|
||||
|
||||
if (ws) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
id,
|
||||
x,
|
||||
y,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div
|
||||
bind:this={element}
|
||||
onpointerdown={onPointerDown}
|
||||
onpointerup={onPointerUp}
|
||||
onpointermove={onPointerMove}
|
||||
style={`transform: translate(${x}px, ${y}px)`}
|
||||
class="absolute flex h-40 w-40 touch-none items-center justify-center rounded-full border-red-400 bg-red-400"
|
||||
>
|
||||
dragable
|
||||
</div>
|
||||
1
src/env.d.ts
vendored
Normal file
1
src/env.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/// <reference path="../.astro/types.d.ts" />
|
||||
16
src/pages/index.astro
Normal file
16
src/pages/index.astro
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
import Container from "../components/Container.svelte";
|
||||
---
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<meta name="generator" content={Astro.generator} />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
|
||||
<title>Astro</title>
|
||||
</head>
|
||||
<body class="h-dvh w-screen">
|
||||
<Container client:load />
|
||||
</body>
|
||||
</html>
|
||||
5
src/utils/types.ts
Normal file
5
src/utils/types.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export interface TrackerData {
|
||||
id: number;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
5
svelte.config.js
Normal file
5
svelte.config.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import { vitePreprocess } from '@astrojs/svelte';
|
||||
|
||||
export default {
|
||||
preprocess: vitePreprocess(),
|
||||
}
|
||||
8
tailwind.config.mjs
Normal file
8
tailwind.config.mjs
Normal file
@@ -0,0 +1,8 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
17
tsconfig.json
Normal file
17
tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "astro/tsconfigs/strict",
|
||||
"compilerOptions": {
|
||||
// ...
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"$lib/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
".astro/types.d.ts",
|
||||
"**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user