2021-01-09 18:26:46 +01:00
|
|
|
import { h } from 'preact';
|
2021-02-07 22:46:05 +01:00
|
|
|
import AutoUpdatingCameraImage from '../components/AutoUpdatingCameraImage';
|
|
|
|
import Button from '../components/Button';
|
|
|
|
import Card from '../components/Card';
|
|
|
|
import Heading from '../components/Heading';
|
|
|
|
import Link from '../components/Link';
|
|
|
|
import SettingsIcon from '../icons/Settings';
|
|
|
|
import Switch from '../components/Switch';
|
2021-01-09 18:26:46 +01:00
|
|
|
import { route } from 'preact-router';
|
2021-02-07 22:46:05 +01:00
|
|
|
import { usePersistence } from '../context';
|
2021-02-05 00:19:47 +01:00
|
|
|
import { useCallback, useContext, useMemo, useState } from 'preact/hooks';
|
2021-02-07 22:46:05 +01:00
|
|
|
import { useApiHost, useConfig } from '../api';
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
export default function Camera({ camera }) {
|
2021-01-26 16:04:03 +01:00
|
|
|
const { data: config } = useConfig();
|
|
|
|
const apiHost = useApiHost();
|
2021-02-05 00:19:47 +01:00
|
|
|
const [showSettings, setShowSettings] = useState(false);
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-01-26 16:04:03 +01:00
|
|
|
if (!config) {
|
2021-01-09 18:26:46 +01:00
|
|
|
return <div>{`No camera named ${camera}`}</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cameraConfig = config.cameras[camera];
|
2021-02-05 00:19:47 +01:00
|
|
|
const [options, setOptions, optionsLoaded] = usePersistence(`${camera}-feed`, Object.freeze({}));
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
const objectCount = useMemo(() => cameraConfig.objects.track.length, [cameraConfig]);
|
2021-01-09 18:26:46 +01:00
|
|
|
|
|
|
|
const handleSetOption = useCallback(
|
|
|
|
(id, value) => {
|
2021-02-05 00:19:47 +01:00
|
|
|
const newOptions = { ...options, [id]: value };
|
|
|
|
setOptions(newOptions);
|
2021-01-09 18:26:46 +01:00
|
|
|
},
|
2021-02-05 00:19:47 +01:00
|
|
|
[options]
|
2021-01-09 18:26:46 +01:00
|
|
|
);
|
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
const searchParams = useMemo(
|
|
|
|
() =>
|
|
|
|
new URLSearchParams(
|
|
|
|
Object.keys(options).reduce((memo, key) => {
|
|
|
|
memo.push([key, options[key] === true ? '1' : '0']);
|
|
|
|
return memo;
|
|
|
|
}, [])
|
|
|
|
),
|
|
|
|
[camera, options]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleToggleSettings = useCallback(() => {
|
|
|
|
setShowSettings(!showSettings);
|
|
|
|
}, [showSettings, setShowSettings]);
|
|
|
|
|
|
|
|
const optionContent = showSettings ? (
|
|
|
|
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['bbox']} id="bbox" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Bounding box</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['timestamp']} id="timestamp" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Timestamp</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['zones']} id="zones" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Zones</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['mask']} id="mask" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Masks</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['motion']} id="motion" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Motion boxes</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex space-x-3">
|
|
|
|
<Switch checked={options['regions']} id="regions" onChange={handleSetOption} />
|
|
|
|
<span class="inline-flex">Regions</span>
|
|
|
|
</div>
|
|
|
|
<Link href={`/cameras/${camera}/editor`}>Mask & Zone creator</Link>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2021-01-09 18:26:46 +01:00
|
|
|
|
|
|
|
return (
|
2021-01-19 17:44:18 +01:00
|
|
|
<div className="space-y-4">
|
2021-01-09 18:26:46 +01:00
|
|
|
<Heading size="2xl">{camera}</Heading>
|
2021-02-05 00:19:47 +01:00
|
|
|
{optionsLoaded ? (
|
|
|
|
<div>
|
|
|
|
<AutoUpdatingCameraImage camera={camera} searchParams={searchParams} />
|
2021-02-02 05:28:25 +01:00
|
|
|
</div>
|
2021-02-05 00:19:47 +01:00
|
|
|
) : null}
|
|
|
|
|
|
|
|
<Button onClick={handleToggleSettings} type="text">
|
|
|
|
<span class="w-5 h-5">
|
|
|
|
<SettingsIcon />
|
|
|
|
</span>{' '}
|
|
|
|
<span>{showSettings ? 'Hide' : 'Show'} Options</span>
|
|
|
|
</Button>
|
|
|
|
{showSettings ? <Card header="Options" elevated={false} content={optionContent} /> : null}
|
2021-01-19 17:44:18 +01:00
|
|
|
|
|
|
|
<div className="space-y-4">
|
2021-01-09 18:26:46 +01:00
|
|
|
<Heading size="sm">Tracked objects</Heading>
|
2021-02-02 05:28:25 +01:00
|
|
|
<div className="flex flex-wrap justify-start">
|
|
|
|
{cameraConfig.objects.track.map((objectType) => (
|
|
|
|
<Card
|
|
|
|
className="mb-4 mr-4"
|
|
|
|
key={objectType}
|
|
|
|
header={objectType}
|
|
|
|
href={`/events?camera=${camera}&label=${objectType}`}
|
|
|
|
media={<img src={`${apiHost}/api/${camera}/${objectType}/best.jpg?crop=1&h=150`} />}
|
|
|
|
/>
|
|
|
|
))}
|
2021-01-19 17:44:18 +01:00
|
|
|
</div>
|
2021-01-09 18:26:46 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|