2022-01-13 08:55:18 +03:00
|
|
|
import { h, Fragment } from 'preact';
|
2022-01-13 01:53:50 +03:00
|
|
|
import AutoUpdatingCameraImage from '../components/AutoUpdatingCameraImage';
|
|
|
|
|
import JSMpegPlayer from '../components/JSMpegPlayer';
|
|
|
|
|
import Heading from '../components/Heading';
|
|
|
|
|
import Link from '../components/Link';
|
|
|
|
|
import Switch from '../components/Switch';
|
|
|
|
|
import { usePersistence } from '../context';
|
2022-01-13 08:55:18 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
|
|
|
|
|
import { useConfig } from '../api';
|
2022-01-13 01:53:50 +03:00
|
|
|
import { Tabs, TextTab } from '../components/Tabs';
|
|
|
|
|
import { LiveChip } from '../components/LiveChip';
|
2022-01-13 08:44:25 +03:00
|
|
|
import HistoryViewer from '../components/HistoryViewer';
|
2022-01-13 01:53:50 +03:00
|
|
|
|
|
|
|
|
const emptyObject = Object.freeze({});
|
|
|
|
|
|
|
|
|
|
export default function Camera({ camera }) {
|
|
|
|
|
const { data: config } = useConfig();
|
2022-01-13 08:09:39 +03:00
|
|
|
|
2022-01-13 02:10:59 +03:00
|
|
|
const [playerType, setPlayerType] = useState('live');
|
2022-01-13 01:53:50 +03:00
|
|
|
|
|
|
|
|
const cameraConfig = config?.cameras[camera];
|
2022-01-13 02:10:59 +03:00
|
|
|
const liveWidth = Math.round(cameraConfig.live.height * (cameraConfig.detect.width / cameraConfig.detect.height));
|
2022-01-13 01:53:50 +03:00
|
|
|
const [options, setOptions] = usePersistence(`${camera}-feed`, emptyObject);
|
|
|
|
|
|
|
|
|
|
const handleSetOption = useCallback(
|
|
|
|
|
(id, value) => {
|
|
|
|
|
const newOptions = { ...options, [id]: value };
|
|
|
|
|
setOptions(newOptions);
|
|
|
|
|
},
|
|
|
|
|
[options, setOptions]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const searchParams = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
new URLSearchParams(
|
|
|
|
|
Object.keys(options).reduce((memo, key) => {
|
|
|
|
|
memo.push([key, options[key] === true ? '1' : '0']);
|
|
|
|
|
return memo;
|
|
|
|
|
}, [])
|
|
|
|
|
),
|
|
|
|
|
[options]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const optionContent = (
|
2022-01-13 08:09:39 +03:00
|
|
|
<div className='grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4'>
|
2022-01-13 01:53:50 +03:00
|
|
|
<Switch
|
|
|
|
|
checked={options['bbox']}
|
2022-01-13 08:09:39 +03:00
|
|
|
id='bbox'
|
2022-01-13 01:53:50 +03:00
|
|
|
onChange={handleSetOption}
|
2022-01-13 08:09:39 +03:00
|
|
|
label='Bounding box'
|
|
|
|
|
labelPosition='after'
|
2022-01-13 01:53:50 +03:00
|
|
|
/>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={options['timestamp']}
|
2022-01-13 08:09:39 +03:00
|
|
|
id='timestamp'
|
2022-01-13 01:53:50 +03:00
|
|
|
onChange={handleSetOption}
|
2022-01-13 08:09:39 +03:00
|
|
|
label='Timestamp'
|
|
|
|
|
labelPosition='after'
|
2022-01-13 01:53:50 +03:00
|
|
|
/>
|
2022-01-13 08:09:39 +03:00
|
|
|
<Switch checked={options['zones']} id='zones' onChange={handleSetOption} label='Zones' labelPosition='after' />
|
|
|
|
|
<Switch checked={options['mask']} id='mask' onChange={handleSetOption} label='Masks' labelPosition='after' />
|
2022-01-13 01:53:50 +03:00
|
|
|
<Switch
|
|
|
|
|
checked={options['motion']}
|
2022-01-13 08:09:39 +03:00
|
|
|
id='motion'
|
2022-01-13 01:53:50 +03:00
|
|
|
onChange={handleSetOption}
|
2022-01-13 08:09:39 +03:00
|
|
|
label='Motion boxes'
|
|
|
|
|
labelPosition='after'
|
2022-01-13 01:53:50 +03:00
|
|
|
/>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={options['regions']}
|
2022-01-13 08:09:39 +03:00
|
|
|
id='regions'
|
2022-01-13 01:53:50 +03:00
|
|
|
onChange={handleSetOption}
|
2022-01-13 08:09:39 +03:00
|
|
|
label='Regions'
|
|
|
|
|
labelPosition='after'
|
2022-01-13 01:53:50 +03:00
|
|
|
/>
|
|
|
|
|
<Link href={`/cameras/${camera}/editor`}>Mask & Zone creator</Link>
|
|
|
|
|
</div>
|
2022-01-13 02:10:59 +03:00
|
|
|
);
|
2022-01-13 01:53:50 +03:00
|
|
|
|
2022-01-13 08:09:39 +03:00
|
|
|
let renderPlayer;
|
|
|
|
|
|
|
|
|
|
switch (playerType) {
|
|
|
|
|
case 'live':
|
|
|
|
|
renderPlayer = (
|
|
|
|
|
<Fragment>
|
|
|
|
|
<div>
|
|
|
|
|
<JSMpegPlayer camera={camera} width={liveWidth} height={cameraConfig.live.height} />
|
|
|
|
|
</div>
|
|
|
|
|
</Fragment>
|
2022-01-13 01:53:50 +03:00
|
|
|
);
|
2022-01-13 08:09:39 +03:00
|
|
|
break;
|
|
|
|
|
case 'history':
|
2022-01-13 08:44:25 +03:00
|
|
|
renderPlayer = <HistoryViewer camera={camera} />;
|
2022-01-13 08:09:39 +03:00
|
|
|
break;
|
|
|
|
|
case 'debug':
|
|
|
|
|
renderPlayer = (
|
|
|
|
|
<Fragment>
|
2022-01-13 08:44:25 +03:00
|
|
|
<AutoUpdatingCameraImage camera={camera} searchParams={searchParams} />
|
2022-01-13 08:09:39 +03:00
|
|
|
{optionContent}
|
|
|
|
|
</Fragment>
|
2022-01-13 02:10:59 +03:00
|
|
|
);
|
2022-01-13 08:09:39 +03:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 01:53:50 +03:00
|
|
|
const handleTabChange = (index) => {
|
|
|
|
|
if (index === 0) {
|
2022-01-13 02:10:59 +03:00
|
|
|
setPlayerType('history');
|
|
|
|
|
} else if (index === 1) {
|
|
|
|
|
setPlayerType('live');
|
2022-01-13 01:53:50 +03:00
|
|
|
} else if (index === 2) {
|
2022-01-13 02:10:59 +03:00
|
|
|
setPlayerType('debug');
|
2022-01-13 01:53:50 +03:00
|
|
|
}
|
2022-01-13 02:10:59 +03:00
|
|
|
};
|
2022-01-13 01:53:50 +03:00
|
|
|
|
|
|
|
|
return (
|
2022-01-13 08:09:39 +03:00
|
|
|
<div className='flex bg-gray-900 w-full h-full justify-center'>
|
|
|
|
|
<div className='relative max-w-screen-md flex-grow w-full'>
|
2022-01-13 08:12:07 +03:00
|
|
|
<div className='absolute top-0 text-white w-full'>
|
|
|
|
|
<div className='flex pt-4 pl-4 items-center w-full h-16 z10'>
|
2022-01-13 02:10:59 +03:00
|
|
|
{(playerType === 'live' || playerType === 'debug') && (
|
2022-01-13 01:53:50 +03:00
|
|
|
<Fragment>
|
2022-01-13 08:09:39 +03:00
|
|
|
<Heading size='xl' className='mr-2'>
|
2022-01-13 02:10:59 +03:00
|
|
|
{camera}
|
|
|
|
|
</Heading>
|
2022-01-13 01:53:50 +03:00
|
|
|
<LiveChip />
|
|
|
|
|
</Fragment>
|
2022-01-13 02:10:59 +03:00
|
|
|
)}
|
2022-01-13 01:53:50 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2022-01-13 08:44:25 +03:00
|
|
|
<div className='flex flex-col justify-center h-full'>{renderPlayer}</div>
|
2022-01-13 01:53:50 +03:00
|
|
|
|
2022-01-13 08:09:39 +03:00
|
|
|
<div className='absolute flex justify-center bottom-8 w-full'>
|
|
|
|
|
<Tabs selectedIndex={1} onChange={handleTabChange} className='justify'>
|
|
|
|
|
<TextTab text='History' />
|
|
|
|
|
<TextTab text='Live' />
|
|
|
|
|
<TextTab text='Debug' />
|
2022-01-13 01:53:50 +03:00
|
|
|
</Tabs>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|