add room scanner logic

This commit is contained in:
Simone Tesini 2025-08-02 10:06:31 +02:00
parent 244e49d311
commit 0c34586358
5 changed files with 45 additions and 56 deletions

View file

@ -1,18 +1,17 @@
import uuid import uuid
from dataclasses import asdict from dataclasses import asdict
import dotenv import dotenv
from connect import get_connection
from flask import Flask, Response, jsonify, request from flask import Flask, Response, jsonify, request
from flask_cors import CORS from flask_cors import CORS
from flask_socketio import SocketIO, join_room, leave_room from flask_socketio import SocketIO, join_room, leave_room
from gps import Coordinates, distance_between_coords
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 qrcode_gen import generate_qr 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() dotenv.load_dotenv()
@ -29,11 +28,11 @@ init_db(state.db)
state.rooms[1000] = Room( state.rooms[1000] = Room(
id=1000, id=1000,
coord=Coordinates(1, 5), coord=Coordinates(46.6769043, 11.1851585),
name="Test Room", name="Test Room",
pin=None, pin=None,
tags=set(), tags=set(),
range_size=100, range_size=150,
songs={}, songs={},
history=[], history=[],
playing=[], playing=[],

View file

@ -3,8 +3,9 @@
let { room }: { room: Room } = $props() let { room }: { room: Room } = $props()
</script> </script>
<button <a
class="flex w-82 cursor-pointer flex-row items-center rounded-md border border-dark-pine-muted bg-light-pine-overlay p-3 hover:bg-dark-pine-base/20 dark:bg-dark-pine-overlay hover:dark:bg-light-pine-base/20" class="flex w-82 cursor-pointer flex-row items-center rounded-md border border-dark-pine-muted bg-light-pine-overlay p-3 hover:bg-dark-pine-base/20 dark:bg-dark-pine-overlay hover:dark:bg-light-pine-base/20"
href="/room/{room.id}"
> >
<div class="flex flex-row"> <div class="flex flex-row">
{room.name} {room.name}
@ -12,7 +13,7 @@
</div> </div>
<div class="grow"></div> <div class="grow"></div>
<div class="flex flex-row items-center gap-2"> <div class="flex flex-row items-center gap-2">
<div class="font-mono">64m</div> <div class="font-mono">{Math.round(room.distance)}m</div>
<div class="rounded bg-light-pine-blue px-2 py-0.5 text-dark-pine-text dark:bg-dark-pine-blue">Join</div> <div class="rounded bg-light-pine-blue px-2 py-0.5 text-dark-pine-text dark:bg-dark-pine-blue">Join</div>
</div> </div>
</button> </a>

View file

@ -37,11 +37,12 @@ export const parseSuggestion = async function(sugg: any): Promise<Suggestion> {
} }
const RoomSchema = z.object({ const RoomSchema = z.object({
id: z.string(), id: z.number(),
name: z.string(), name: z.string(),
private: z.boolean(), private: z.boolean(),
coords: z.tuple([z.number(), z.number()]), coords: z.object({ latitude: z.number(), longitude: z.number() }),
range: z.number().int() range: z.number().int(),
distance: z.number()
}) })
export type Room = z.infer<typeof RoomSchema> export type Room = z.infer<typeof RoomSchema>

View file

@ -1,16 +1,21 @@
import { get_coords } from "./gps"
import { parseSong, parseSuggestion, type FetchError, type Song, type Suggestion } from "./types" import { parseSong, parseSuggestion, type FetchError, type Song, type Suggestion } from "./types"
export const joinRoom = async function (roomId: string): Promise<[FetchError | null, string]> { export const joinRoom = async function(roomId: string): Promise<[FetchError | null, string]> {
let resp = await fetch("/api/join?room=" + roomId) 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 [{ code: 400, message: "Cannot join the room" }, ""]
} }
return [null, "test"] 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) let resp = await fetch("/api/room/suggestions?room=" + roomId)
if (resp.status != 200) { if (resp.status != 200) {
@ -31,7 +36,7 @@ export const getSuggestions = async function (roomId: string): Promise<[FetchErr
return [null, suggestions] 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) let resp = await fetch("/api/queue?room=" + roomId)
if (resp.status != 200) { if (resp.status != 200) {
return [{ code: 400, message: "Failed to load queue songs" }, [], 0] 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] 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" }) let resp = await fetch("/api/queue/next?room=" + roomId, { method: "POST" })
if (resp.status != 200) { if (resp.status != 200) {
@ -66,7 +71,7 @@ export const triggerPlayNext = async function (roomId: string): Promise<[FetchEr
return [null, songs, json["index"]] 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 resp = await fetch("/api/song/audio?song=" + uuid)
let json = await resp.json() let json = await resp.json()

View file

@ -1,9 +1,21 @@
<script lang="ts"> <script lang="ts">
import RoomComponent from "$lib/components/RoomComponent.svelte" import RoomComponent from "$lib/components/RoomComponent.svelte"
import { get_coords } from "$lib/gps"
import { parseRoom, type Room } from "$lib/types" import { parseRoom, type Room } from "$lib/types"
import { Plus } from "@lucide/svelte" import { Plus } from "@lucide/svelte"
import { onMount } from "svelte"
let room: Room = { id: "asd", coords: [0.123, 0.456], name: "scatolame party", private: true, range: 124 } let rooms: Room[] = $state([])
onMount(async () => {
let { coords, error } = await get_coords()
if (error != null) return console.log(error)
if (coords == null) return
let res = await fetch(`/api/room?lat=${coords.latitude}&lon=${coords.longitude}`)
let json = await res.json()
for (let room of json) rooms.push(await parseRoom(room))
})
</script> </script>
<div class="flex flex-col items-center gap-2"> <div class="flex flex-col items-center gap-2">
@ -21,37 +33,8 @@
</button> </button>
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<RoomComponent {room}></RoomComponent> {#each rooms as room}
<RoomComponent {room}></RoomComponent> <RoomComponent {room} />
<RoomComponent {room}></RoomComponent> {/each}
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
<RoomComponent {room}></RoomComponent>
</div> </div>
</div> </div>
<!-- <div class="h-full w-full flex-row justify-center p-4"> -->
<!-- <div class="relative min-h-screen justify-center justify-items-center"> -->
<!-- <h1>Scan your nearby rooms</h1> -->
<!-- <img src="/smerdoradar.gif" alt="radar" class="h-64 w-64" /> -->
<!-- <div class="max-h-50 w-full overflow-y-auto"> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- <RoomComponent {room} /> -->
<!-- </div> -->
<!-- <div class="fixed right-0 bottom-0"> -->
<!-- <button class="mt-4 justify-end rounded bg-blue-500 px-6 py-2 text-white transition-colors hover:bg-blue-600 active:bg-blue-700"> + </button> -->
<!-- </div> -->
<!-- </div> -->
<!-- </div> -->