55 lines
4.5 KiB
TypeScript
55 lines
4.5 KiB
TypeScript
import {useRef,useState} from 'react';
|
||
import {Button,InspectorSelectField,ResourceList,ResourceRow,SettingsCard} from '@nodedc/ui-react';
|
||
import type {SensorBoardSettingsProps} from '../../../../packages/sensor-ui/src/extensions';
|
||
import {perform,type Sensor} from '../../../../packages/sensor-ui/src/contracts';
|
||
import {drivePositions,vescStatus,type DriveProfile} from './model';
|
||
import {VescLimits} from './VescLimits';
|
||
|
||
export function VescBoardSettings({inventory,transport,enabled,refresh,failure,openDevice}:SensorBoardSettingsProps){
|
||
const controllers=inventory?.items.filter(device=>device.kind==='vesc.controller')??[];
|
||
const profile=controllers.map(device=>vescStatus(device).drive_profile).filter((value):value is DriveProfile=>!!value).sort((a,b)=>b.revision-a.revision)[0];
|
||
const anchor=controllers.find(device=>device.online&&device.verified&&vescStatus(device).board_settings_supported);
|
||
const [busy,setBusy]=useState(false);const running=useRef(false);
|
||
const active=inventory?.operations?.some(value=>['queued','running'].includes(value.state)&&controllers.some(device=>device.id===value.device_id));
|
||
const blocked=!enabled||!anchor||busy||active||!profile;
|
||
const positions=drivePositions(profile?.layout??null);
|
||
async function change(device:Sensor,action:string,parameters:Record<string,unknown>){
|
||
if(blocked||running.current||!profile)return;
|
||
running.current=true;setBusy(true);failure(null);
|
||
try{await perform(transport,device,action,{revision:profile.revision,...parameters});await refresh();}
|
||
catch(error){failure(error);await refresh();}finally{running.current=false;setBusy(false);}
|
||
}
|
||
return <div className="sensor-content">
|
||
<SettingsCard title="Привод" description="Профиль аппарата и расположение его моторов.">
|
||
<InspectorSelectField label="Профиль привода" value={profile?.layout??''} disabled={blocked} options={[
|
||
{value:'',label:'Выберите профиль',disabled:true},
|
||
{value:'1x1',label:'1×1 · 2 мотора',disabled:!!profile?.bindings['left.2']||!!profile?.bindings['right.2']},
|
||
{value:'2x2',label:'2×2 · 4 мотора'},
|
||
]} onChange={layout=>{if(anchor)void change(anchor,'vesc.drive.layout',{layout});}}/>
|
||
{!profile&&<p>{inventory?'Подключите VESC, чтобы получить профиль привода с борта.':'Получение профиля привода…'}</p>}
|
||
{profile&&!anchor&&<p>Профиль показан по последним сведениям с борта. Для изменения нужна связь с VESC и актуальное бортовое приложение.</p>}
|
||
{profile?.layout&&<>
|
||
<p>Стороны — по направлению движения вперёд. Назначения сохраняются автоматически и остаются с контроллером при смене USB-порта.</p>
|
||
{Object.entries(positions).map(([slot,label])=>{
|
||
const bound=profile.bindings[slot];
|
||
const missing=bound&&!controllers.some(device=>device.id===bound.device_id);
|
||
return <InspectorSelectField key={slot} label={label} value={bound?.device_id??''} disabled={blocked} options={[
|
||
{value:'',label:'Не назначен'},
|
||
...controllers.map(device=>({value:device.id,label:device.name+(!device.online?' · нет связи':''),disabled:!device.online||!device.verified||Object.entries(profile.bindings).some(([other,binding])=>other!==slot&&binding.device_id===device.id)})),
|
||
...(missing?[{value:bound.device_id,label:`VESC ${bound.uuid.slice(0,6).toUpperCase()} · нет связи`,disabled:true}]:[]),
|
||
]} onChange={id=>{
|
||
const target=id?controllers.find(device=>device.id===id):anchor;
|
||
if(!target)return;
|
||
void change(target,id?'vesc.drive.assign':'vesc.drive.unassign',id?{layout:profile.layout,slot}:{slot});
|
||
}}/>;
|
||
})}
|
||
<ResourceList aria-label="Настройка назначенных моторов">{Object.entries(profile.bindings).map(([slot,binding])=>{
|
||
const target=controllers.find(device=>device.id===binding.device_id);
|
||
return <li key={slot}><ResourceRow title={positions[slot]??slot} description={target?.name??`VESC ${binding.uuid.slice(0,6).toUpperCase()}`} actions={<Button disabled={!target?.prepared||busy||active} onClick={()=>openDevice(binding.device_id)}>Настройка мотора</Button>}/></li>;
|
||
})}</ResourceList>
|
||
</>}
|
||
</SettingsCard>
|
||
<VescLimits controllers={controllers} transport={transport} enabled={enabled&&!busy&&!active} failure={failure}/>
|
||
</div>;
|
||
}
|