64 lines
7.7 KiB
TypeScript
64 lines
7.7 KiB
TypeScript
import {useCallback,useEffect,useRef,useState} from 'react';
|
||
import {LoadingRegion,Button,Icon,IconButton,ResourceList,ResourceRow,SettingsCard,StatusBadge} from '@nodedc/ui-react';
|
||
import type {SensorDetailProps} from '../../../../packages/sensor-ui/src/extensions';
|
||
import {perform} from '../../../../packages/sensor-ui/src/contracts';
|
||
import {LiveViewport} from '../../../../packages/sensor-ui/src/LiveViewport';
|
||
import {CameraSettings} from './CameraSettings';
|
||
import {RecoverySettings} from './RecoverySettings';
|
||
import {cameraStatus,fileName,type CameraFiles,type CameraSettings as Settings} from './model';
|
||
|
||
export function X4Detail({device,transport,enabled,back,refresh,failure}:SensorDetailProps){
|
||
const status=cameraStatus(device);
|
||
const [settings,setSettings]=useState<Settings|null>(null);
|
||
const [files,setFiles]=useState<CameraFiles|null>(null);
|
||
const [pending,setPending]=useState<string|null>(null);const busy=pending!==null;
|
||
const [settingsLoading,setSettingsLoading]=useState(false);const running=useRef(false);const mounted=useRef(true);
|
||
const available=enabled&&device.online&&status.connected;
|
||
useEffect(()=>{mounted.current=true;return()=>{mounted.current=false;};},[]);
|
||
const load=useCallback(async()=>{
|
||
if(status.function_mode===null)return;
|
||
if(mounted.current)setSettingsLoading(true);
|
||
try{const value=await perform<Settings>(transport,device,'settings.read',{mode:status.function_mode});if(mounted.current)setSettings(value);}
|
||
finally{if(mounted.current)setSettingsLoading(false);}
|
||
},[transport,device.id,device.snapshot.context.session_id,status.function_mode]);
|
||
useEffect(()=>{if(available)void load().catch(error=>{if(mounted.current)failure(error);});},[available,load]);
|
||
async function action(name:string,parameters:Record<string,unknown>={},key=name){
|
||
if(running.current||!enabled||(!available&&!['refresh','recovery.configure','preview.stop'].includes(name)))return;
|
||
running.current=true;setPending(key);failure(null);
|
||
try{
|
||
const result=name==='refresh'?(available?await load():undefined):await perform<unknown>(transport,device,name,parameters);
|
||
if(!mounted.current)return;
|
||
if(name==='settings.apply')setSettings(result as Settings);
|
||
if(name==='files.list')setFiles(result as CameraFiles);
|
||
if(name==='photo.capture'||name==='record.stop')setFiles(null);
|
||
await refresh();
|
||
}catch(error){if(mounted.current)failure(error);}
|
||
finally{running.current=false;if(mounted.current)setPending(null);}
|
||
}
|
||
const mode=settings?.mode??status.function_mode;
|
||
const recordLabel=status.recording===1?'Идёт запись на карту':status.recording===0?'Запись остановлена':'Состояние записи не подтверждено';
|
||
const viewDevice=available?device:{...device,snapshot:{...device.snapshot,acquisition:'idle'}};
|
||
return <div className="sensor-content"><div><Button onClick={back}>К устройствам</Button></div>
|
||
<SettingsCard title={device.name} description={`${device.model}${device.firmware?' · '+device.firmware:''}`} actions={<IconButton label="Обновить состояние камеры" loading={pending==='refresh'} disabled={!enabled||busy} onClick={()=>void action('refresh')}><Icon name="refresh"/></IconButton>}>
|
||
<dl className="sensor-facts"><div><dt>Серийный номер</dt><dd>{status.serial||'Не получен'}</dd></div></dl>
|
||
{!available&&<p>Нет свежей связи с камерой.</p>}
|
||
<div className="sensor-actions"><Button disabled={!available||busy||status.preview!==0} loading={pending==='preview.start'} onClick={()=>void action('preview.start')}>Начать просмотр</Button><Button disabled={!enabled||busy||(!available&&!status.recovery?.preview_wanted)||(available&&status.preview===0&&!status.recovery?.preview_wanted)} loading={pending==='preview.stop'} onClick={()=>void action('preview.stop')}>Остановить просмотр</Button><Button disabled={!available||busy||status.recording!==0} loading={pending==='verify'} onClick={()=>void action('verify')}>Проверить изображение</Button></div>
|
||
{status.alerts?.temperature_high&&<p>Камера сообщает о перегреве.</p>}
|
||
{status.alerts?.storage_full&&<p>На карте камеры закончилось место.</p>}
|
||
{status.alerts?.battery_low&&<p>Камера сообщает о низком заряде.</p>}
|
||
</SettingsCard>
|
||
{status.recovery?.supported&&<RecoverySettings value={status.recovery} online={available} enabled={enabled&&!busy} pending={pending==='recovery.configure'} change={value=>void action('recovery.configure',{enabled:value,wakeup_confirmed:value})}/>}
|
||
<LiveViewport device={viewDevice} layer="preview" title="Изображение X4" note="Запись сохраняется на карте памяти камеры." inactiveMessage="Нажмите «Начать просмотр», чтобы получить изображение." transport={transport} failure={failure}/>
|
||
<SettingsCard title="Съёмка" description="Фото и видеозапись сохраняются на карте памяти X4." actions={<StatusBadge tone={available&&status.recording===1?'success':status.recording===0?'neutral':'warning'}>{available?recordLabel:'Состояние недоступно'}</StatusBadge>}>
|
||
<div className="sensor-actions"><Button disabled={!available||busy||status.recording!==0||mode!==7} loading={pending==='record.start'} onClick={()=>void action('record.start')}>Начать запись</Button><Button disabled={!available||busy||status.recording===0} loading={pending==='record.stop'} onClick={()=>void action('record.stop')}>Остановить запись</Button><Button disabled={!available||busy||status.recording!==0||mode!==6} loading={pending==='photo.capture'} onClick={()=>void action('photo.capture')}>Сделать фото</Button></div>
|
||
{available&&status.recording===1&&<p>Запись продолжится после закрытия просмотра.</p>}
|
||
</SettingsCard>
|
||
<LoadingRegion loading={available&&((settingsLoading&&!settings)||pending==='settings.apply')} label={pending==='settings.apply'?'Применение настройки':'Получение настроек камеры'}>
|
||
{settings?<CameraSettings value={settings} disabled={!available||busy||status.recording!==0||status.preview!==0} apply={(key,value)=>void action('settings.apply',{mode:settings.mode,key,value})}/>:available&&!settingsLoading?<p>Настройки недоступны. Обновите состояние камеры.</p>:null}
|
||
</LoadingRegion>
|
||
<SettingsCard title="Файлы на камере" actions={<Button disabled={!available||busy} loading={pending==='files.refresh'} onClick={()=>void action('files.list',{offset:0},'files.refresh')}>Обновить список</Button>}>
|
||
{files?files.items.length?<><ResourceList aria-label="Файлы на карте X4">{files.items.map((item,index)=><li key={`${files.offset+index}:${item}`}><ResourceRow title={fileName(item)} icon={<Icon name="file"/>}/></li>)}</ResourceList><div className="sensor-actions"><Button disabled={!available||busy||files.offset===0} loading={pending==='files.previous'} onClick={()=>void action('files.list',{offset:Math.max(0,files.offset-32)},'files.previous')}>Назад</Button><span>{files.offset+1}–{files.offset+files.items.length} из {files.total}</span><Button disabled={!available||busy||files.offset+files.items.length>=files.total} loading={pending==='files.next'} onClick={()=>void action('files.list',{offset:files.offset+files.items.length},'files.next')}>Далее</Button></div></>:<p>Файлов нет.</p>:<p>Обновите список, чтобы увидеть записи на карте камеры.</p>}
|
||
</SettingsCard>
|
||
</div>;
|
||
}
|