From 0c34586358f9ece45722eef7373939a8d3f7afcd Mon Sep 17 00:00:00 2001 From: Simone Tesini Date: Sat, 2 Aug 2025 10:06:31 +0200 Subject: [PATCH] add room scanner logic --- backend/src/app.py | 19 ++++--- .../src/lib/components/RoomComponent.svelte | 7 +-- frontend/src/lib/types.ts | 7 +-- frontend/src/lib/utils.ts | 19 ++++--- frontend/src/routes/+page.svelte | 49 ++++++------------- 5 files changed, 45 insertions(+), 56 deletions(-) diff --git a/backend/src/app.py b/backend/src/app.py index 0331aa6..436d5d7 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -1,18 +1,17 @@ import uuid from dataclasses import asdict + import dotenv +from connect import get_connection from flask import Flask, Response, jsonify, request from flask_cors import CORS from flask_socketio import SocketIO, join_room, leave_room - -from state import State -from connect import get_connection -from room import Room -from song import Song, init_db, get_song_by_title_artist, add_song_in_db, get_song_by_uuid -from song_fetch import query_search, yt_get_audio_url, yt_search_song +from gps import Coordinates, distance_between_coords from qrcode_gen import generate_qr -from gps import is_within_range, distance_between_coords, Coordinates - +from room import Room +from song import Song, add_song_in_db, get_song_by_title_artist, get_song_by_uuid, init_db +from song_fetch import query_search, yt_get_audio_url, yt_search_song +from state import State dotenv.load_dotenv() @@ -29,11 +28,11 @@ init_db(state.db) state.rooms[1000] = Room( id=1000, - coord=Coordinates(1, 5), + coord=Coordinates(46.6769043, 11.1851585), name="Test Room", pin=None, tags=set(), - range_size=100, + range_size=150, songs={}, history=[], playing=[], diff --git a/frontend/src/lib/components/RoomComponent.svelte b/frontend/src/lib/components/RoomComponent.svelte index e98e000..4e742db 100644 --- a/frontend/src/lib/components/RoomComponent.svelte +++ b/frontend/src/lib/components/RoomComponent.svelte @@ -3,8 +3,9 @@ let { room }: { room: Room } = $props() - + diff --git a/frontend/src/lib/types.ts b/frontend/src/lib/types.ts index 0c2e8f5..c0d4155 100644 --- a/frontend/src/lib/types.ts +++ b/frontend/src/lib/types.ts @@ -37,11 +37,12 @@ export const parseSuggestion = async function(sugg: any): Promise { } const RoomSchema = z.object({ - id: z.string(), + id: z.number(), name: z.string(), private: z.boolean(), - coords: z.tuple([z.number(), z.number()]), - range: z.number().int() + coords: z.object({ latitude: z.number(), longitude: z.number() }), + range: z.number().int(), + distance: z.number() }) export type Room = z.infer diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index 1b806c5..29fc5fc 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -1,16 +1,21 @@ +import { get_coords } from "./gps" import { parseSong, parseSuggestion, type FetchError, type Song, type Suggestion } from "./types" -export const joinRoom = async function (roomId: string): Promise<[FetchError | null, string]> { - let resp = await fetch("/api/join?room=" + roomId) +export const joinRoom = async function(roomId: string): Promise<[FetchError | null, string]> { + let { coords, error } = await get_coords() + if (error != null) return [{ code: 400, message: "Cannot join the room due to GPS error" }, ""] + if (coords == null) return [{ code: 400, message: "Cannot join the room due to GPS error" }, ""] - if (resp.status != 200) { + let res = await fetch(`/api/join?room=${roomId}&lat=${coords.latitude}&lon=${coords.longitude}`) + + if (res.status != 200) { return [{ code: 400, message: "Cannot join the room" }, ""] } return [null, "test"] } -export const getSuggestions = async function (roomId: string): Promise<[FetchError | null, Suggestion[]]> { +export const getSuggestions = async function(roomId: string): Promise<[FetchError | null, Suggestion[]]> { let resp = await fetch("/api/room/suggestions?room=" + roomId) if (resp.status != 200) { @@ -31,7 +36,7 @@ export const getSuggestions = async function (roomId: string): Promise<[FetchErr return [null, suggestions] } -export const getQueueSongs = async function (roomId: string): Promise<[FetchError | null, Song[], number]> { +export const getQueueSongs = async function(roomId: string): Promise<[FetchError | null, Song[], number]> { let resp = await fetch("/api/queue?room=" + roomId) if (resp.status != 200) { return [{ code: 400, message: "Failed to load queue songs" }, [], 0] @@ -49,7 +54,7 @@ export const getQueueSongs = async function (roomId: string): Promise<[FetchErro return [null, songs, playingId] } -export const triggerPlayNext = async function (roomId: string): Promise<[FetchError | null, Song[], number]> { +export const triggerPlayNext = async function(roomId: string): Promise<[FetchError | null, Song[], number]> { let resp = await fetch("/api/queue/next?room=" + roomId, { method: "POST" }) if (resp.status != 200) { @@ -66,7 +71,7 @@ export const triggerPlayNext = async function (roomId: string): Promise<[FetchEr return [null, songs, json["index"]] } -export const getStreamingUrl = async function (uuid: string) { +export const getStreamingUrl = async function(uuid: string) { let resp = await fetch("/api/song/audio?song=" + uuid) let json = await resp.json() diff --git a/frontend/src/routes/+page.svelte b/frontend/src/routes/+page.svelte index 319ce8d..c110c6a 100644 --- a/frontend/src/routes/+page.svelte +++ b/frontend/src/routes/+page.svelte @@ -1,9 +1,21 @@
@@ -21,37 +33,8 @@
- - - - - - - - - - - - + {#each rooms as room} + + {/each}
- - - - - - - - - - - - - - - - - - - -