Agregar dropdown de eventos Frigate y corregir endpoint de creacion
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 59s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 59s
- Agregar toggle Live/Eventos en StreamViewer - Agregar dropdown para seleccionar eventos recientes (max 10) - Reproducir clips de eventos en el mismo reproductor - Crear endpoint proxy /api/frigate/events para listar eventos - Corregir URL de creacion de eventos: /api/events/:camera/:label/create - Actualizar useFrigateEvents con fetchEvents, formatEventTime, getEventClipUrl
This commit is contained in:
@@ -64,10 +64,52 @@
|
|||||||
:items="streamTypeOptions"
|
:items="streamTypeOptions"
|
||||||
value-key="value"
|
value-key="value"
|
||||||
class="w-full"
|
class="w-full"
|
||||||
|
:disabled="viewMode === 'event'"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Toggle Live/Eventos -->
|
||||||
|
<div v-if="selectedStream" class="mb-4">
|
||||||
|
<div class="flex items-center gap-2 mb-2">
|
||||||
|
<UButton
|
||||||
|
:color="viewMode === 'live' ? 'primary' : 'neutral'"
|
||||||
|
:variant="viewMode === 'live' ? 'solid' : 'ghost'"
|
||||||
|
size="sm"
|
||||||
|
icon="i-heroicons-signal"
|
||||||
|
@click="viewMode = 'live'; selectedEventId = null"
|
||||||
|
>
|
||||||
|
En Vivo
|
||||||
|
</UButton>
|
||||||
|
<UButton
|
||||||
|
:color="viewMode === 'event' ? 'primary' : 'neutral'"
|
||||||
|
:variant="viewMode === 'event' ? 'solid' : 'ghost'"
|
||||||
|
size="sm"
|
||||||
|
icon="i-heroicons-film"
|
||||||
|
:loading="isLoadingEvents"
|
||||||
|
@click="viewMode = 'event'"
|
||||||
|
>
|
||||||
|
Eventos ({{ events.length }})
|
||||||
|
</UButton>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Selector de Eventos -->
|
||||||
|
<div v-if="viewMode === 'event'" class="mt-2">
|
||||||
|
<USelect
|
||||||
|
v-model="selectedEventId"
|
||||||
|
:items="eventOptions"
|
||||||
|
placeholder="Seleccionar evento..."
|
||||||
|
:loading="isLoadingEvents"
|
||||||
|
:disabled="events.length === 0"
|
||||||
|
value-key="value"
|
||||||
|
class="w-full"
|
||||||
|
/>
|
||||||
|
<p v-if="events.length === 0 && !isLoadingEvents" class="text-sm text-gray-500 mt-1">
|
||||||
|
No hay eventos recientes para esta camara
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Banner de conexión -->
|
<!-- Banner de conexión -->
|
||||||
<div
|
<div
|
||||||
v-if="!isStreamSessionActive"
|
v-if="!isStreamSessionActive"
|
||||||
@@ -95,8 +137,10 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Reproductor (solo si hay sesión activa) -->
|
<!-- Reproductor (solo si hay sesión activa) -->
|
||||||
|
<template v-if="isStreamSessionActive">
|
||||||
|
<!-- Reproductor Live -->
|
||||||
<StreamsStreamPlayer
|
<StreamsStreamPlayer
|
||||||
v-if="isStreamSessionActive"
|
v-if="viewMode === 'live'"
|
||||||
:key="streamPlayerKey"
|
:key="streamPlayerKey"
|
||||||
:stream-url="getStreamUrl"
|
:stream-url="getStreamUrl"
|
||||||
:use-iframe="useIframe"
|
:use-iframe="useIframe"
|
||||||
@@ -105,6 +149,29 @@
|
|||||||
@error="handlePlayerError"
|
@error="handlePlayerError"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<!-- Reproductor de Evento -->
|
||||||
|
<div v-else-if="viewMode === 'event'" class="relative aspect-video bg-black rounded-lg overflow-hidden">
|
||||||
|
<video
|
||||||
|
v-if="selectedEventUrl"
|
||||||
|
:key="selectedEventId"
|
||||||
|
:src="selectedEventUrl"
|
||||||
|
controls
|
||||||
|
autoplay
|
||||||
|
class="w-full h-full object-contain"
|
||||||
|
@error="handlePlayerError('Error al cargar el clip del evento')"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
v-else
|
||||||
|
class="absolute inset-0 flex items-center justify-center text-gray-400"
|
||||||
|
>
|
||||||
|
<div class="text-center">
|
||||||
|
<UIcon name="i-heroicons-film" class="w-12 h-12 mb-2" />
|
||||||
|
<p>Selecciona un evento para reproducir</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<!-- Botones de Eventos -->
|
<!-- Botones de Eventos -->
|
||||||
<div v-if="selectedStream" class="mt-4 flex items-center gap-2">
|
<div v-if="selectedStream" class="mt-4 flex items-center gap-2">
|
||||||
<UButton
|
<UButton
|
||||||
@@ -245,7 +312,13 @@ const {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
isCreating: isCreatingEvent,
|
isCreating: isCreatingEvent,
|
||||||
|
isLoadingEvents,
|
||||||
error: eventError,
|
error: eventError,
|
||||||
|
events,
|
||||||
|
fetchEvents,
|
||||||
|
getEventClipUrl,
|
||||||
|
getEventSnapshotUrl,
|
||||||
|
formatEventTime,
|
||||||
createEvent,
|
createEvent,
|
||||||
createQuickEvent,
|
createQuickEvent,
|
||||||
clearError: clearEventError
|
clearError: clearEventError
|
||||||
@@ -256,6 +329,10 @@ const toast = useToast()
|
|||||||
// Modal state
|
// Modal state
|
||||||
const showEventModal = ref(false)
|
const showEventModal = ref(false)
|
||||||
|
|
||||||
|
// View mode: 'live' or 'event'
|
||||||
|
const viewMode = ref<'live' | 'event'>('live')
|
||||||
|
const selectedEventId = ref<string | null>(null)
|
||||||
|
|
||||||
// Estado de conexión a streams
|
// Estado de conexión a streams
|
||||||
const isConnecting = ref(false)
|
const isConnecting = ref(false)
|
||||||
const isStreamSessionActive = ref(false)
|
const isStreamSessionActive = ref(false)
|
||||||
@@ -338,6 +415,30 @@ const currentTypeDescription = computed(() => {
|
|||||||
return type?.description || ''
|
return type?.description || ''
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Opciones para el dropdown de eventos
|
||||||
|
const eventOptions = computed(() => {
|
||||||
|
return events.value.map(e => ({
|
||||||
|
value: e.id,
|
||||||
|
label: `${formatEventTime(e.start_time)} - ${e.label}${e.sub_label ? ` (${e.sub_label})` : ''}`
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
// URL del evento seleccionado
|
||||||
|
const selectedEventUrl = computed(() => {
|
||||||
|
if (!selectedEventId.value) return null
|
||||||
|
return getEventClipUrl(selectedEventId.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Cargar eventos cuando cambia la cámara
|
||||||
|
watch(() => selectedStream.value, async (newStream) => {
|
||||||
|
if (newStream) {
|
||||||
|
await fetchEvents(newStream, 10)
|
||||||
|
// Reset event selection when camera changes
|
||||||
|
selectedEventId.value = null
|
||||||
|
viewMode.value = 'live'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Cargar streams al montar el componente
|
// Cargar streams al montar el componente
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchStreams()
|
fetchStreams()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Composable para gestionar eventos de Frigate
|
* Composable para gestionar eventos de Frigate
|
||||||
* Usa proxy backend para evitar problemas de CORS/cookies entre subdominios
|
* Usa proxy backend para evitar problemas de CORS/cookies entre subdominios
|
||||||
* API Proxy: /api/frigate/event
|
* API Proxy: /api/frigate/event, /api/frigate/events
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export interface FrigateEventParams {
|
export interface FrigateEventParams {
|
||||||
@@ -24,10 +24,81 @@ export interface FrigateEventResponse {
|
|||||||
message?: string
|
message?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FrigateEvent {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
sub_label?: string
|
||||||
|
camera: string
|
||||||
|
start_time: number
|
||||||
|
end_time?: number
|
||||||
|
has_clip: boolean
|
||||||
|
has_snapshot: boolean
|
||||||
|
zones: string[]
|
||||||
|
}
|
||||||
|
|
||||||
export const useFrigateEvents = () => {
|
export const useFrigateEvents = () => {
|
||||||
|
const FRIGATE_PUBLIC_URL = 'https://camaras.nucleoriofrio.com'
|
||||||
|
|
||||||
const isCreating = useState<boolean>('frigate_creating', () => false)
|
const isCreating = useState<boolean>('frigate_creating', () => false)
|
||||||
|
const isLoadingEvents = useState<boolean>('frigate_loading_events', () => false)
|
||||||
const error = useState<string | null>('frigate_error', () => null)
|
const error = useState<string | null>('frigate_error', () => null)
|
||||||
const lastEventId = useState<string | null>('frigate_last_event', () => null)
|
const lastEventId = useState<string | null>('frigate_last_event', () => null)
|
||||||
|
const events = useState<FrigateEvent[]>('frigate_events', () => [])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtiene los eventos recientes de una camara
|
||||||
|
*/
|
||||||
|
const fetchEvents = async (camera?: string, limit: number = 10): Promise<FrigateEvent[]> => {
|
||||||
|
isLoadingEvents.value = true
|
||||||
|
error.value = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
if (camera) {
|
||||||
|
params.set('camera', camera)
|
||||||
|
}
|
||||||
|
params.set('limit', limit.toString())
|
||||||
|
|
||||||
|
const response = await $fetch<FrigateEvent[]>(`/api/frigate/events?${params.toString()}`)
|
||||||
|
|
||||||
|
events.value = response
|
||||||
|
return response
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const errorMessage = (err as Error)?.message || 'Error al obtener eventos'
|
||||||
|
error.value = errorMessage
|
||||||
|
console.error('[Frigate] Error fetching events:', err)
|
||||||
|
return []
|
||||||
|
} finally {
|
||||||
|
isLoadingEvents.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Genera la URL del clip de un evento
|
||||||
|
*/
|
||||||
|
const getEventClipUrl = (eventId: string): string => {
|
||||||
|
return `${FRIGATE_PUBLIC_URL}/api/events/${eventId}/clip.mp4`
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Genera la URL del snapshot de un evento
|
||||||
|
*/
|
||||||
|
const getEventSnapshotUrl = (eventId: string): string => {
|
||||||
|
return `${FRIGATE_PUBLIC_URL}/api/events/${eventId}/snapshot.jpg`
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formatea la fecha de un evento
|
||||||
|
*/
|
||||||
|
const formatEventTime = (timestamp: number): string => {
|
||||||
|
const date = new Date(timestamp * 1000)
|
||||||
|
return date.toLocaleString('es-ES', {
|
||||||
|
day: '2-digit',
|
||||||
|
month: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crea un evento en Frigate para una camara especifica via proxy
|
* Crea un evento en Frigate para una camara especifica via proxy
|
||||||
@@ -40,7 +111,6 @@ export const useFrigateEvents = () => {
|
|||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Usar proxy backend para evitar CORS/cookies issues
|
|
||||||
const response = await $fetch<FrigateEventResponse>('/api/frigate/event', {
|
const response = await $fetch<FrigateEventResponse>('/api/frigate/event', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: {
|
body: {
|
||||||
@@ -83,9 +153,18 @@ export const useFrigateEvents = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
// Estado
|
||||||
isCreating: readonly(isCreating),
|
isCreating: readonly(isCreating),
|
||||||
|
isLoadingEvents: readonly(isLoadingEvents),
|
||||||
error: readonly(error),
|
error: readonly(error),
|
||||||
lastEventId: readonly(lastEventId),
|
lastEventId: readonly(lastEventId),
|
||||||
|
events: readonly(events),
|
||||||
|
|
||||||
|
// Métodos
|
||||||
|
fetchEvents,
|
||||||
|
getEventClipUrl,
|
||||||
|
getEventSnapshotUrl,
|
||||||
|
formatEventTime,
|
||||||
createEvent,
|
createEvent,
|
||||||
createQuickEvent,
|
createQuickEvent,
|
||||||
clearError
|
clearError
|
||||||
|
|||||||
@@ -39,9 +39,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
const cameraName = body.camera.replace(/_main$|_sub$/, '')
|
const cameraName = body.camera.replace(/_main$|_sub$/, '')
|
||||||
|
|
||||||
// Preparar payload para Frigate
|
// Preparar payload para Frigate
|
||||||
const frigatePayload: Record<string, unknown> = {
|
const frigatePayload: Record<string, unknown> = {}
|
||||||
label: body.label
|
|
||||||
}
|
|
||||||
|
|
||||||
if (body.sub_label) {
|
if (body.sub_label) {
|
||||||
frigatePayload.sub_label = body.sub_label
|
frigatePayload.sub_label = body.sub_label
|
||||||
@@ -59,8 +57,9 @@ export default defineEventHandler(async (event) => {
|
|||||||
const FRIGATE_URL = process.env.FRIGATE_URL || 'http://192.168.87.29:5000'
|
const FRIGATE_URL = process.env.FRIGATE_URL || 'http://192.168.87.29:5000'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// API: POST /api/events/:camera_name/:label/create
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${FRIGATE_URL}/api/events/${cameraName}/create`,
|
`${FRIGATE_URL}/api/events/${cameraName}/${body.label}/create`,
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
68
nuxt4/server/api/frigate/events.get.ts
Normal file
68
nuxt4/server/api/frigate/events.get.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/**
|
||||||
|
* Proxy endpoint para obtener eventos de Frigate
|
||||||
|
* GET /api/frigate/events?camera=X&limit=10
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Verificar autenticación via headers de Authentik
|
||||||
|
const headers = getRequestHeaders(event)
|
||||||
|
const username = headers['x-authentik-username']
|
||||||
|
|
||||||
|
if (!username) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'No autenticado'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leer query params
|
||||||
|
const query = getQuery(event)
|
||||||
|
const camera = query.camera as string | undefined
|
||||||
|
const limit = query.limit ? parseInt(query.limit as string) : 10
|
||||||
|
|
||||||
|
// URL interna de Frigate
|
||||||
|
const FRIGATE_URL = process.env.FRIGATE_URL || 'http://192.168.87.29:5000'
|
||||||
|
|
||||||
|
// Construir URL con filtros
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
if (camera) {
|
||||||
|
// Extraer nombre base de la cámara (sin _main o _sub)
|
||||||
|
params.set('camera', camera.replace(/_main$|_sub$/, ''))
|
||||||
|
}
|
||||||
|
params.set('limit', limit.toString())
|
||||||
|
params.set('has_clip', 'true') // Solo eventos con clip
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`${FRIGATE_URL}/api/events?${params.toString()}`,
|
||||||
|
{
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: response.status,
|
||||||
|
message: `Error al obtener eventos: ${response.statusText}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = await response.json()
|
||||||
|
|
||||||
|
return events
|
||||||
|
} catch (error: unknown) {
|
||||||
|
console.error('[Frigate Events] Error:', error)
|
||||||
|
|
||||||
|
if ((error as any).statusCode) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: (error as Error)?.message || 'Error al obtener eventos'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user