2021-05-28 19:13:48 +02:00
|
|
|
import { h } from 'preact';
|
2021-06-02 08:41:26 +02:00
|
|
|
import { closestTo, format, parseISO } from 'date-fns';
|
2021-05-28 19:13:48 +02:00
|
|
|
import ActivityIndicator from '../components/ActivityIndicator';
|
|
|
|
import Heading from '../components/Heading';
|
2021-06-02 08:41:26 +02:00
|
|
|
import RecordingPlaylist from '../components/RecordingPlaylist';
|
2021-05-28 19:13:48 +02:00
|
|
|
import VideoPlayer from '../components/VideoPlayer';
|
|
|
|
import { FetchStatus, useApiHost, useRecording } from '../api';
|
|
|
|
|
2021-06-02 10:27:07 +02:00
|
|
|
export default function Recording({ camera, date, hour, seconds }) {
|
2021-05-28 19:13:48 +02:00
|
|
|
const apiHost = useApiHost();
|
|
|
|
const { data, status } = useRecording(camera);
|
|
|
|
|
|
|
|
if (status !== FetchStatus.LOADED) {
|
|
|
|
return <ActivityIndicator />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const recordingDates = data.map((item) => item.date);
|
|
|
|
const selectedDate = closestTo(
|
|
|
|
date ? parseISO(date) : new Date(),
|
|
|
|
recordingDates.map((i) => parseISO(i))
|
|
|
|
);
|
|
|
|
const selectedKey = format(selectedDate, 'yyyy-MM-dd');
|
|
|
|
const [year, month, day] = selectedKey.split('-');
|
|
|
|
const playlist = [];
|
|
|
|
const hours = [];
|
|
|
|
|
|
|
|
for (const item of data) {
|
2021-05-28 19:25:49 +02:00
|
|
|
if (item.date === selectedKey) {
|
2021-05-28 19:13:48 +02:00
|
|
|
for (const recording of item.recordings) {
|
|
|
|
playlist.push({
|
|
|
|
name: `${selectedKey} ${recording.hour}:00`,
|
|
|
|
description: `${camera} recording @ ${recording.hour}:00.`,
|
|
|
|
sources: [
|
|
|
|
{
|
|
|
|
src: `${apiHost}/vod/${year}-${month}/${day}/${recording.hour}/${camera}/index.m3u8`,
|
|
|
|
type: 'application/vnd.apple.mpegurl',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
hours.push(recording.hour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedHour = hours.indexOf(hour);
|
|
|
|
|
|
|
|
if (this.player !== undefined) {
|
|
|
|
this.player.playlist([]);
|
|
|
|
this.player.playlist(playlist);
|
|
|
|
this.player.playlist.autoadvance(0);
|
|
|
|
if (selectedHour !== -1) {
|
|
|
|
this.player.playlist.currentItem(selectedHour);
|
2021-06-02 10:27:07 +02:00
|
|
|
if (seconds !== undefined) {
|
|
|
|
this.player.currentTime(seconds);
|
|
|
|
}
|
2021-05-28 19:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="space-y-4">
|
|
|
|
<Heading>{camera} Recordings</Heading>
|
|
|
|
|
|
|
|
<VideoPlayer
|
|
|
|
onReady={(player) => {
|
|
|
|
if (player.playlist) {
|
|
|
|
player.playlist(playlist);
|
|
|
|
player.playlist.autoadvance(0);
|
|
|
|
if (selectedHour !== -1) {
|
|
|
|
player.playlist.currentItem(selectedHour);
|
2021-06-02 10:27:07 +02:00
|
|
|
if (seconds !== undefined) {
|
|
|
|
player.currentTime(seconds);
|
|
|
|
}
|
2021-05-28 19:13:48 +02:00
|
|
|
}
|
|
|
|
this.player = player;
|
|
|
|
}
|
|
|
|
}}
|
2021-06-02 08:41:26 +02:00
|
|
|
>
|
|
|
|
<RecordingPlaylist camera={camera} recordings={data} selectedDate={selectedKey} />
|
|
|
|
</VideoPlayer>
|
2021-05-28 19:13:48 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|