🚧 Some progress with ClassQuizController
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
|
#* {
|
||||||
:8080 {
|
:8080 {
|
||||||
|
# tls /home/mawoka/certs/cert.pem /home/mawoka/certs/key.pem
|
||||||
reverse_proxy /* localhost:3000
|
reverse_proxy /* localhost:3000
|
||||||
reverse_proxy /api* localhost:8000
|
reverse_proxy /api* localhost:8000
|
||||||
reverse_proxy /rapidoc* localhost:8000
|
reverse_proxy /rapidoc* localhost:8000
|
||||||
|
|||||||
Generated
+786
-618
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
|||||||
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -29,7 +30,7 @@ class JoinGameResponse(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/join")
|
@router.post("/join")
|
||||||
async def join_game(data: JoinGameInput):
|
async def join_game(data: JoinGameInput) -> JoinGameResponse:
|
||||||
controller = await Controllers.objects.get_or_none(id=data.id, secret_key=data.secret_key)
|
controller = await Controllers.objects.get_or_none(id=data.id, secret_key=data.secret_key)
|
||||||
game_pin = await redis.get(f"game:cqc:code:{data.code}")
|
game_pin = await redis.get(f"game:cqc:code:{data.code}")
|
||||||
if game_pin is None:
|
if game_pin is None:
|
||||||
@@ -65,4 +66,17 @@ async def register_with_code(data: RegisterWithCodeInput) -> RegisterWithCodeRes
|
|||||||
await redis.delete(f"controller_setup:{data.code}")
|
await redis.delete(f"controller_setup:{data.code}")
|
||||||
c_id = uuid.UUID(c_id)
|
c_id = uuid.UUID(c_id)
|
||||||
controller = await Controllers.objects.get(id=c_id)
|
controller = await Controllers.objects.get(id=c_id)
|
||||||
|
controller.first_seen = datetime.now()
|
||||||
|
controller.last_seen = datetime.now()
|
||||||
|
await controller.update()
|
||||||
return RegisterWithCodeResponse(id=controller.id, secret_key=controller.secret_key)
|
return RegisterWithCodeResponse(id=controller.id, secret_key=controller.secret_key)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/ping")
|
||||||
|
async def ping_server(id: uuid.UUID, secret_key: str, version: str):
|
||||||
|
controller = await Controllers.objects.get_or_none(id=id, secret_key=secret_key)
|
||||||
|
if controller is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Key and/or id invalid")
|
||||||
|
controller.last_seen = datetime.now()
|
||||||
|
controller.os_version = version
|
||||||
|
await controller.update()
|
||||||
|
|||||||
@@ -78,16 +78,16 @@ class WebSocketRequest(BaseModel):
|
|||||||
wss_clients = {}
|
wss_clients = {}
|
||||||
|
|
||||||
|
|
||||||
@router.websocket("/{id}")
|
@router.websocket("/{game_id}")
|
||||||
async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
|
async def websocket_endpoint(ws: WebSocket, game_id: str):
|
||||||
try:
|
try:
|
||||||
if id in wss_clients.keys():
|
if game_id in wss_clients.keys():
|
||||||
await ws.close(code=status.WS_1001_GOING_AWAY)
|
await ws.close(code=status.WS_1001_GOING_AWAY)
|
||||||
print("Client {} already exists.".format(id))
|
print("Client {} already exists.".format(game_id))
|
||||||
return
|
return
|
||||||
|
print("hI!")
|
||||||
await ws.accept()
|
await ws.accept()
|
||||||
wss_clients[id] = ws
|
wss_clients[game_id] = ws
|
||||||
|
|
||||||
player_id, game_pin = game_id.split(":")
|
player_id, game_pin = game_id.split(":")
|
||||||
if player_id is None or game_pin is None:
|
if player_id is None or game_pin is None:
|
||||||
@@ -105,7 +105,10 @@ async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
|
|||||||
raw_data = await ws.receive_text()
|
raw_data = await ws.receive_text()
|
||||||
try:
|
try:
|
||||||
data = WebSocketRequest.parse_raw(raw_data)
|
data = WebSocketRequest.parse_raw(raw_data)
|
||||||
except ValidationError:
|
except ValidationError as e:
|
||||||
|
print("ValError")
|
||||||
|
print(e)
|
||||||
|
print(raw_data)
|
||||||
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="ValidationError").json())
|
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="ValidationError").json())
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -116,10 +119,10 @@ async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
|
|||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
|
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
|
||||||
continue
|
continue
|
||||||
|
print(data)
|
||||||
await submit_answer_fn(answer_index, game_pin, player_id, now)
|
await submit_answer_fn(answer_index, game_pin, player_id, now)
|
||||||
print("Data from client {}: {}".format(id, data))
|
print("Data from client {}: {}".format(game_id, data))
|
||||||
|
|
||||||
except WebSocketDisconnect as ex:
|
except WebSocketDisconnect as ex:
|
||||||
print("Client {} is disconnected: {}".format(id, ex))
|
print("Client {} is disconnected: {}".format(game_id, ex))
|
||||||
wss_clients.pop(id, None)
|
wss_clients.pop(game_id, None)
|
||||||
|
|||||||
@@ -75,3 +75,14 @@ async def modify_controller(
|
|||||||
controller.name = data.name
|
controller.name = data.name
|
||||||
await controller.update()
|
await controller.update()
|
||||||
return GetControllerResponse(**controller.dict())
|
return GetControllerResponse(**controller.dict())
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/list")
|
||||||
|
async def get_all_controllers(user: User = Depends(get_current_user)) -> list[GetControllerResponse]:
|
||||||
|
controllers = await Controllers.objects.all(user=user.id)
|
||||||
|
if len(controllers) == 0:
|
||||||
|
return []
|
||||||
|
return_list = []
|
||||||
|
for controller in controllers:
|
||||||
|
return_list.append(GetControllerResponse(**controller.dict()))
|
||||||
|
return return_list
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from fastapi import APIRouter
|
|||||||
from classquiz.config import settings, meilisearch
|
from classquiz.config import settings, meilisearch
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from typing import Optional, List, Any
|
from typing import Optional, List, Any
|
||||||
from meilisearch.errors import MeiliSearchApiError
|
from meilisearch.errors import MeilisearchApiError
|
||||||
from classquiz.helpers import meilisearch_init
|
from classquiz.helpers import meilisearch_init
|
||||||
|
|
||||||
settings = settings()
|
settings = settings()
|
||||||
@@ -53,7 +53,7 @@ async def _perform_search(query: str, params: dict) -> dict[str, Any]:
|
|||||||
try:
|
try:
|
||||||
index = meilisearch.get_index(settings.meilisearch_index)
|
index = meilisearch.get_index(settings.meilisearch_index)
|
||||||
return index.search(query, params)
|
return index.search(query, params)
|
||||||
except MeiliSearchApiError:
|
except MeilisearchApiError:
|
||||||
await meilisearch_init()
|
await meilisearch_init()
|
||||||
index = meilisearch.get_index(settings.meilisearch_index)
|
index = meilisearch.get_index(settings.meilisearch_index)
|
||||||
return index.search(query, params)
|
return index.search(query, params)
|
||||||
|
|||||||
Generated
+917
-840
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
let color_map = {
|
||||||
|
r: 'red',
|
||||||
|
g: 'green',
|
||||||
|
y: 'yellow',
|
||||||
|
b: 'blue'
|
||||||
|
};
|
||||||
|
export let code: string;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="flex flex-row gap-2 mx-auto">
|
||||||
|
{#each code as c}
|
||||||
|
<div class="flex flex-col">
|
||||||
|
<p class="text-center">{c}</p>
|
||||||
|
<span
|
||||||
|
style="background-color: {color_map[
|
||||||
|
c.toLowerCase()
|
||||||
|
]}; width: 2rem; height: {c.toLowerCase() == c ? '2' : '4'}rem"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
@@ -149,12 +149,7 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex justify-center w-full my-auto">
|
<div class="flex justify-center w-full my-auto">
|
||||||
<label
|
<label for="cqc-toggle" class="inline-flex relative items-center cursor-pointer">
|
||||||
for="cqc-toggle"
|
|
||||||
class="inline-flex relative items-center cursor-pointer"
|
|
||||||
class:pointer-events-none={!captcha_enabled}
|
|
||||||
class:opacity-50={!captcha_enabled}
|
|
||||||
>
|
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={cqcs_enabled}
|
bind:checked={cqcs_enabled}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
-->
|
-->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import AudioPlayer from '$lib/play/audio_player.svelte';
|
import AudioPlayer from '$lib/play/audio_player.svelte';
|
||||||
|
import ControllerCodeDisplay from '$lib/components/controller/code.svelte';
|
||||||
import { getLocalization } from '$lib/i18n';
|
import { getLocalization } from '$lib/i18n';
|
||||||
|
|
||||||
export let game_pin: string;
|
export let game_pin: string;
|
||||||
@@ -30,12 +31,6 @@
|
|||||||
}
|
}
|
||||||
players = players;
|
players = players;
|
||||||
};
|
};
|
||||||
const color_map = {
|
|
||||||
r: 'red',
|
|
||||||
g: 'green',
|
|
||||||
y: 'yellow',
|
|
||||||
b: 'blue'
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="w-full h-full">
|
<div class="w-full h-full">
|
||||||
@@ -55,18 +50,7 @@
|
|||||||
<div class="m-auto">
|
<div class="m-auto">
|
||||||
<div class="flex-col flex justify-center">
|
<div class="flex-col flex justify-center">
|
||||||
<p class="mx-auto">Join by entering the following code</p>
|
<p class="mx-auto">Join by entering the following code</p>
|
||||||
<div class="flex flex-row gap-2 mx-auto">
|
<ControllerCodeDisplay code={cqc_code} />
|
||||||
{#each cqc_code as c}
|
|
||||||
<div class="flex flex-col">
|
|
||||||
<p class="text-center">{c}</p>
|
|
||||||
<span
|
|
||||||
style="background-color: {color_map[
|
|
||||||
c.toLowerCase()
|
|
||||||
]}; width: 2rem; height: {c.toLowerCase() == c ? '2' : '4'}rem"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
{/each}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { PageData } from './$types';
|
||||||
|
import BrownButton from '$lib/components/buttons/brown.svelte';
|
||||||
|
|
||||||
|
export let data: PageData;
|
||||||
|
|
||||||
|
console.log(data.controllers);
|
||||||
|
const controllers: [] = data.controllers;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="w-full h-full">
|
||||||
|
{#if controllers.length === 0}
|
||||||
|
<div class="w-full h-full flex">
|
||||||
|
<div class="m-auto">
|
||||||
|
<BrownButton>Add new controller</BrownButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="p-2">
|
||||||
|
<table class="w-full">
|
||||||
|
<tr class="border-b-2 dark:border-gray-500 text-left border-gray-300">
|
||||||
|
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300">Name</th>
|
||||||
|
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||||
|
>Player name</th
|
||||||
|
>
|
||||||
|
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||||
|
>First seen</th
|
||||||
|
>
|
||||||
|
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||||
|
>Last seen</th
|
||||||
|
>
|
||||||
|
<th class="mx-auto p-1">Version</th>
|
||||||
|
</tr>
|
||||||
|
{#each data.controllers as controller}
|
||||||
|
<tr class="text-left">
|
||||||
|
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||||
|
><a
|
||||||
|
href="/account/controllers/{controller.id}"
|
||||||
|
class="underline text-lg">{controller.name}</a
|
||||||
|
></td
|
||||||
|
>
|
||||||
|
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||||
|
>{controller.player_name}</td
|
||||||
|
>
|
||||||
|
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||||
|
>{controller.first_seen
|
||||||
|
? new Date(controller.first_seen).toLocaleString()
|
||||||
|
: 'Never'}</td
|
||||||
|
>
|
||||||
|
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||||
|
>{controller.last_seen
|
||||||
|
? new Date(controller.last_seen).toLocaleString()
|
||||||
|
: 'Never'}</td
|
||||||
|
>
|
||||||
|
<td class="mx-auto p-1">{controller.os_version ?? 'Unknown'}</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
|
export const load = (async ({ fetch }) => {
|
||||||
|
const resp = await fetch('/api/v1/box-controller/web/list');
|
||||||
|
// const resp = await fetch("https://localhost/api/v1/box-controller/web/list")
|
||||||
|
const controllers = await resp.json();
|
||||||
|
return {
|
||||||
|
controllers
|
||||||
|
};
|
||||||
|
}) satisfies PageLoad;
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
|
import type { PageData } from './$types';
|
||||||
|
|
||||||
|
export let data: PageData;
|
||||||
|
let input_data = {
|
||||||
|
player_name: data.username,
|
||||||
|
name: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
let isValid = false;
|
||||||
|
let isSubmitting = false;
|
||||||
|
|
||||||
|
$: isValid = input_data.name.length !== 0 && input_data.player_name.length !== 0;
|
||||||
|
|
||||||
|
const submit = async () => {
|
||||||
|
if (!isValid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const res = await fetch('/api/v1/box-controller/web/setup', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(input_data)
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
const json = await res.json();
|
||||||
|
goto(`/account/controllers/add/wait?id=${json.id}&code=${json.code}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-center h-full px-4">
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
class="w-full max-w-sm mx-auto overflow-hidden bg-white rounded-lg shadow-md dark:bg-gray-800"
|
||||||
|
>
|
||||||
|
<div class="px-6 py-4">
|
||||||
|
<h2 class="text-3xl font-bold text-center text-gray-700 dark:text-white">
|
||||||
|
ClassQuiz
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<h3 class="mt-1 text-xl font-medium text-center text-gray-600 dark:text-gray-200">
|
||||||
|
Add a controller
|
||||||
|
</h3>
|
||||||
|
|
||||||
|
<form on:submit|preventDefault={submit}>
|
||||||
|
<div class="w-full mt-4">
|
||||||
|
<div class="dark:bg-gray-800 bg-white p-4 rounded-lg">
|
||||||
|
<div class="relative bg-inherit w-full">
|
||||||
|
<input
|
||||||
|
id="player_name"
|
||||||
|
name="player_name"
|
||||||
|
type="text"
|
||||||
|
class="w-full peer bg-transparent h-10 rounded-lg text-gray-700 dark:text-white placeholder-transparent ring-2 px-2 ring-gray-500 focus:ring-sky-600 focus:outline-none focus:border-rose-600"
|
||||||
|
class:ring-red-700={input_data.player_name.length === 0}
|
||||||
|
class:ring-green-600={input_data.player_name.length !== 0}
|
||||||
|
bind:value={input_data.player_name}
|
||||||
|
/>
|
||||||
|
<label
|
||||||
|
for="player_name"
|
||||||
|
class="absolute cursor-text left-0 -top-3 text-sm text-gray-700 dark:text-white bg-inherit mx-1 px-1 peer-placeholder-shown:text-base peer-placeholder-shown:text-gray-500 peer-placeholder-shown:top-2 peer-focus:-top-3 peer-focus:text-sky-600 peer-focus:text-sm transition-all"
|
||||||
|
>
|
||||||
|
Player name
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="dark:bg-gray-800 bg-white p-4 rounded-lg">
|
||||||
|
<div class="relative bg-inherit w-full">
|
||||||
|
<input
|
||||||
|
id="name"
|
||||||
|
name="name"
|
||||||
|
type="text"
|
||||||
|
class="w-full peer bg-transparent h-10 rounded-lg text-gray-700 dark:text-white placeholder-transparent ring-2 px-2 ring-gray-500 focus:ring-sky-600 focus:outline-none focus:border-rose-600"
|
||||||
|
placeholder="Name"
|
||||||
|
class:ring-red-700={input_data.name.length === 0}
|
||||||
|
class:ring-green-600={input_data.name.length !== 0}
|
||||||
|
bind:value={input_data.name}
|
||||||
|
/>
|
||||||
|
<label
|
||||||
|
for="name"
|
||||||
|
class="absolute cursor-text left-0 -top-3 text-sm text-gray-700 dark:text-white bg-inherit mx-1 px-1 peer-placeholder-shown:text-base peer-placeholder-shown:text-gray-500 peer-placeholder-shown:top-2 peer-focus:-top-3 peer-focus:text-sky-600 peer-focus:text-sm transition-all"
|
||||||
|
>
|
||||||
|
Name
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-center mt-4">
|
||||||
|
<button
|
||||||
|
class="px-4 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded hover:bg-gray-600 focus:outline-none"
|
||||||
|
disabled={!isValid || isSubmitting}
|
||||||
|
class:cursor-not-allowed={!isValid || isSubmitting}
|
||||||
|
class:opacity-50={!isValid || isSubmitting}
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
{#if isSubmitting}
|
||||||
|
<svg class="h-4 w-4 animate-spin" viewBox="3 3 18 18">
|
||||||
|
<path
|
||||||
|
class="fill-blue-800"
|
||||||
|
d="M12 5C8.13401 5 5 8.13401 5 12C5 15.866 8.13401 19 12 19C15.866 19 19 15.866 19 12C19 8.13401 15.866 5 12 5ZM3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12Z"
|
||||||
|
/>
|
||||||
|
<path
|
||||||
|
class="fill-blue-100"
|
||||||
|
d="M16.9497 7.05015C14.2161 4.31648 9.78392 4.31648 7.05025 7.05015C6.65973 7.44067 6.02656 7.44067 5.63604 7.05015C5.24551 6.65962 5.24551 6.02646 5.63604 5.63593C9.15076 2.12121 14.8492 2.12121 18.364 5.63593C18.7545 6.02646 18.7545 6.65962 18.364 7.05015C17.9734 7.44067 17.3403 7.44067 16.9497 7.05015Z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
{:else}
|
||||||
|
Add
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="flex items-center justify-center py-4 text-center bg-gray-50 dark:bg-gray-700"
|
||||||
|
>
|
||||||
|
<span class="text-sm text-gray-600 dark:text-gray-200"
|
||||||
|
>Don't know what this is?
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<a
|
||||||
|
href="/account/login"
|
||||||
|
class="mx-2 text-sm font-bold text-blue-500 dark:text-blue-400 hover:underline"
|
||||||
|
>Read more here.</a
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
|
export const load = (async ({ fetch }) => {
|
||||||
|
const resp = await fetch('/api/v1/users/me');
|
||||||
|
const json = await resp.json();
|
||||||
|
return {
|
||||||
|
username: json.username
|
||||||
|
};
|
||||||
|
}) satisfies PageLoad;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
|
export const load = (async ({ url }) => {
|
||||||
|
const code = url.searchParams.get('code');
|
||||||
|
const id = url.searchParams.get('id');
|
||||||
|
if (!id || !code) {
|
||||||
|
throw error(404, JSON.stringify({ detail: 'id and/or code are/is missing' }));
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
code
|
||||||
|
};
|
||||||
|
}) satisfies PageServerLoad;
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { PageData } from './$types';
|
||||||
|
import CodeDisplay from '$lib/components/controller/code.svelte';
|
||||||
|
import Spinner from '$lib/Spinner.svelte';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
|
export let data: PageData;
|
||||||
|
let controller_seen = false;
|
||||||
|
let check_tick = 0;
|
||||||
|
let interval;
|
||||||
|
|
||||||
|
const check_if_controller_was_seen = async () => {
|
||||||
|
const res = await fetch(`/api/v1/box-controller/web/controller?id=${data.id}`);
|
||||||
|
const json = await res.json();
|
||||||
|
controller_seen = Boolean(json.first_seen);
|
||||||
|
if (controller_seen) {
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
await check_if_controller_was_seen();
|
||||||
|
interval = setInterval(async () => {
|
||||||
|
check_tick += 1;
|
||||||
|
if (check_tick === 5) {
|
||||||
|
await check_if_controller_was_seen();
|
||||||
|
check_tick = 0;
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="w-full h-full flex">
|
||||||
|
<div class="m-auto flex flex-col">
|
||||||
|
<div class="block">
|
||||||
|
<CodeDisplay code={data.code} />
|
||||||
|
</div>
|
||||||
|
{#if controller_seen}
|
||||||
|
<div class="mt-10">
|
||||||
|
<p class="text-center">Controller set up successfully!</p>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="mt-10 flex-col flex gap-2 justify-center">
|
||||||
|
<p>Checking if controller has been connected in {5 - check_tick} seconds.</p>
|
||||||
|
<Spinner my_20={false} />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user