feat(bms): discover JK batteries and integrate read-only telemetry scenes

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 00:43:00 +03:00
parent 2f0a066dde
commit f3d0c25ccd
46 changed files with 2887 additions and 10 deletions
@@ -0,0 +1,14 @@
import {defaultGlassMaterial,type GlassMaterialSettings} from '@nodedc/ui-core';
export type CellMarkingAppearance={color:string;size:number};
export type BatteryPanelAppearance={height:number;radius:number;labelSize:number;valueSize:number;textColor:string;glass:GlassMaterialSettings};
export type BatteryAppearance={schema:'missioncore.battery-ui/v1';markings:CellMarkingAppearance;panels:BatteryPanelAppearance};
export const batteryAppearanceKey=(id:string)=>`missioncore.battery-ui/v1:${id}`;
export function defaultBatteryAppearance():BatteryAppearance{return {schema:'missioncore.battery-ui/v1',markings:{color:'#ffffff',size:100},panels:{height:76,radius:24,labelSize:12,valueSize:18,textColor:'#f1f1ef',glass:{...defaultGlassMaterial,tintOpacity:52,blur:18}}};}
const object=(v:unknown):Record<string,unknown>=>v!==null&&typeof v==='object'?v as Record<string,unknown>:{};
const number=(v:unknown,fallback:number,min:number,max:number)=>typeof v==='number'&&Number.isFinite(v)?Math.min(max,Math.max(min,v)):fallback;
const color=(v:unknown,fallback:string)=>typeof v==='string'&&/^#[0-9a-f]{6}$/i.test(v)?v.toLowerCase():fallback;
export function parseBatteryAppearance(raw:unknown):BatteryAppearance{
const source=object(raw);if(source.schema!=='missioncore.battery-ui/v1')throw new Error('Это не профиль интерфейса батареи.');
const d=defaultBatteryAppearance(),m=object(source.markings),p=object(source.panels),g=object(p.glass);
return {schema:d.schema,markings:{color:color(m.color,d.markings.color),size:number(m.size,100,50,160)},panels:{height:number(p.height,76,56,160),radius:number(p.radius,24,0,48),labelSize:number(p.labelSize,12,10,20),valueSize:number(p.valueSize,18,12,30),textColor:color(p.textColor,d.panels.textColor),glass:{...d.panels.glass,tintHex:color(g.tintHex,d.panels.glass.tintHex),tintOpacity:number(g.tintOpacity,52,0,100),blur:number(g.blur,18,0,60)}}};
}
@@ -0,0 +1,12 @@
import type {CellMarkingAppearance} from './appearance';
export function cellDisplayIndex(name:string){const match=/^bms\/cell-display\/C(0[1-9]|1[0-3])$/.exec(name);return match?Number(match[1])-1:null;}
export function cellDisplayText(index:number,value:number|undefined){return {number:String(index+1).padStart(2,'0'),voltage:Number.isFinite(value)?`${value!.toLocaleString('ru-RU',{minimumFractionDigits:3,maximumFractionDigits:3})} В`:'— В'};}
/** One fixed composition on each existing 27 × 29 mm label face. No camera projection. */
export function drawCellDisplay(ctx:CanvasRenderingContext2D,index:number,value:number|undefined,appearance:CellMarkingAppearance){
const text=cellDisplayText(index,value),size=appearance.size/100;
ctx.clearRect(0,0,256,256);ctx.save();ctx.translate(128,128);ctx.scale(size,size);
ctx.fillStyle=appearance.color;ctx.textAlign='center';ctx.textBaseline='middle';
ctx.font='600 76px sans-serif';ctx.fillText(text.number,0,-36);
ctx.font='500 40px sans-serif';ctx.fillText(text.voltage,0,46);
ctx.restore();
}
@@ -0,0 +1,34 @@
import {defaultChargeGradient} from '../roverScene/chargeGradient';
import r04 from './r04.json';
import {defaultScene,parseScene,type SceneDocument} from '../roverScene/sceneDocument';
import {readScene} from '../roverScene/sceneStorage';
import type {SceneDocumentProfile} from '../roverScene/useSceneDocument';
export function defaultBatteryScene():SceneDocument {
return {...defaultScene(),schema:'missioncore.battery-scene/v1',model:'catl-r04/v1',
materials:{...structuredClone(r04.materials),'bms/cells':{...structuredClone(r04.materials['bms/cells']),chargeGradient:defaultChargeGradient()}},assignments:{},objects:{...r04.objects}};
}
function database():Promise<IDBDatabase>{return new Promise((resolve,reject)=>{
const request=indexedDB.open('missioncore-battery-scenes',1);
request.onupgradeneeded=()=>request.result.createObjectStore('scenes');
request.onsuccess=()=>resolve(request.result);request.onerror=()=>reject(request.error);
request.onblocked=()=>reject(new Error('Хранилище батареи занято другим окном.'));
});}
async function save(id:string,document:SceneDocument){const db=await database();try{await new Promise<void>((resolve,reject)=>{
const tx=db.transaction('scenes','readwrite');tx.objectStore('scenes').put(document,id);
tx.oncomplete=()=>resolve();tx.onerror=()=>reject(tx.error);tx.onabort=()=>reject(tx.error);
});}finally{db.close();}}
export function batterySceneProfile(seedKey?:string):SceneDocumentProfile {
return {defaults:defaultBatteryScene,write:save,filename:'battery-r04-scene.json',read:async id=>{
const db=await database();let raw:unknown;try{raw=await new Promise((resolve,reject)=>{
const request=db.transaction('scenes').objectStore('scenes').get(id);
request.onsuccess=()=>resolve(request.result);request.onerror=()=>reject(request.error);
});}finally{db.close();}
if(raw)return parseScene(raw,defaultBatteryScene);
const seed=seedKey?await readScene(seedKey):null;
const doc=defaultBatteryScene();
if(seed){doc.postFx=structuredClone(seed.postFx);doc.cameraAutoRotate=seed.cameraAutoRotate;doc.cameraZoomStrength=seed.cameraZoomStrength;}
// Copy once. Subsequent rover edits and battery edits have separate owners.
await save(id,doc);return doc;
}};
}
@@ -0,0 +1,501 @@
{
"materials": {
"bms/Cable / red silicone": {
"name": "Cable / red silicone",
"color": "#ac1922",
"metalness": 0,
"roughness": 0.47999998927116394,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Cable / graphite silicone": {
"name": "Cable / graphite silicone",
"color": "#22252a",
"metalness": 0.05000000074505806,
"roughness": 0.3700000047683716,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Mount / G10 dielectric": {
"name": "Mount / G10 dielectric",
"color": "#3e8572",
"metalness": 0,
"roughness": 0.5199999809265137,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Nickel plated copper / busbars": {
"name": "Nickel plated copper / busbars",
"color": "#c4ccd2",
"metalness": 0.8299999833106995,
"roughness": 0.27000001072883606,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Sense / warm yellow": {
"name": "Sense / warm yellow",
"color": "#f6c629",
"metalness": 0.05000000074505806,
"roughness": 0.3199999928474426,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/labels": {
"name": "Надписи",
"color": "#e5eee8",
"metalness": 0,
"roughness": 0.9,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#ffffff",
"emissiveIntensity": 0.08,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Connector / ivory polymer": {
"name": "Connector / ivory polymer",
"color": "#e4e2d2",
"metalness": 0.05000000074505806,
"roughness": 0.4000000059604645,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/BMS / green PCB": {
"name": "BMS / green PCB",
"color": "#266c50",
"metalness": 0.15000000596046448,
"roughness": 0.4000000059604645,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Aluminium / satin": {
"name": "Aluminium / satin",
"color": "#cdd6dd",
"metalness": 0.800000011920929,
"roughness": 0.3100000023841858,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Lid / insulating black": {
"name": "Lid / insulating black",
"color": "#15191e",
"metalness": 0,
"roughness": 0.6499999761581421,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Mount / adhesive film": {
"name": "Mount / adhesive film",
"color": "#6f756f",
"metalness": 0,
"roughness": 0.6200000047683716,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Steel / fasteners": {
"name": "Steel / fasteners",
"color": "#bdc4ca",
"metalness": 0.8799999952316284,
"roughness": 0.23000000417232513,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/cells": {
"name": "Ячейки · общий SOC",
"color": "#487b80",
"metalness": 0,
"roughness": 0.68,
"opacity": 0.82,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#ffffff",
"emissiveIntensity": 0.65,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Cell / wrapped lower edge": {
"name": "Cell / wrapped lower edge",
"color": "#223894",
"metalness": 0.20000000298023224,
"roughness": 0.3100000023841858,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/CASE | bead-blasted graphite": {
"name": "CASE | bead-blasted graphite",
"color": "#464a4f",
"metalness": 0.3199999928474426,
"roughness": 0.5199999809265137,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/glass": {
"name": "Прозрачная часть корпуса",
"color": "#7898a1",
"metalness": 0.05,
"roughness": 0.42,
"opacity": 0.12,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/R04 | elastomer seal": {
"name": "R04 | elastomer seal",
"color": "#121517",
"metalness": 0,
"roughness": 0.699999988079071,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/R04 | graphite moulded polymer": {
"name": "R04 | graphite moulded polymer",
"color": "#222930",
"metalness": 0,
"roughness": 0.36000001430511475,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/R04 | smoked polycarbonate": {
"name": "R04 | smoked polycarbonate",
"color": "#98aab5",
"metalness": 0,
"roughness": 0.15000000596046448,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/R04 | display protective polycarbonate": {
"name": "R04 | display protective polycarbonate",
"color": "#eef7fa",
"metalness": 0,
"roughness": 0.11999999731779099,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/DISPLAY | black glass": {
"name": "DISPLAY | black glass",
"color": "#121f23",
"metalness": 0.18000000715255737,
"roughness": 0.2199999988079071,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/CASE | recessed black": {
"name": "CASE | recessed black",
"color": "#1e2227",
"metalness": 0.11999999731779099,
"roughness": 0.5,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/CASE | darker frame": {
"name": "CASE | darker frame",
"color": "#41464c",
"metalness": 0.6499999761581421,
"roughness": 0.3199999928474426,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/CASE | dielectric liner": {
"name": "CASE | dielectric liner",
"color": "#313437",
"metalness": 0,
"roughness": 0.6499999761581421,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/Connector / gold plated contacts": {
"name": "Connector / gold plated contacts",
"color": "#d6a44a",
"metalness": 0.8500000238418579,
"roughness": 0.25,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/XT90-S / anti spark green insert": {
"name": "XT90-S / anti spark green insert",
"color": "#30a455",
"metalness": 0,
"roughness": 0.4300000071525574,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
},
"bms/XT90-S / yellow PA housing": {
"name": "XT90-S / yellow PA housing",
"color": "#f9bd22",
"metalness": 0,
"roughness": 0.3799999952316284,
"opacity": 1,
"reflectivity": 1,
"clearCoat": 0,
"clearCoatRoughness": 0.3,
"emissive": "#000000",
"emissiveIntensity": 0,
"normalStrength": 1,
"doubleSided": true,
"tiling": 1,
"textures": {}
}
},
"objects": {
"bms/Cable / red silicone": true,
"bms/Cable / graphite silicone": true,
"bms/Mount / G10 dielectric": true,
"bms/Nickel plated copper / busbars": true,
"bms/Sense / warm yellow": true,
"bms/labels/electronics": true,
"bms/Connector / ivory polymer": true,
"bms/BMS / green PCB": true,
"bms/Aluminium / satin": true,
"bms/Lid / insulating black": true,
"bms/Mount / adhesive film": true,
"bms/Steel / fasteners": true,
"bms/cells/C01": true,
"bms/Cell / wrapped lower edge": true,
"bms/labels/cells": true,
"bms/cell-display/C01": true,
"bms/cells/C02": true,
"bms/cell-display/C02": true,
"bms/cells/C03": true,
"bms/cell-display/C03": true,
"bms/cells/C04": true,
"bms/cell-display/C04": true,
"bms/cells/C05": true,
"bms/cell-display/C05": true,
"bms/cells/C06": true,
"bms/cell-display/C06": true,
"bms/cells/C07": true,
"bms/cell-display/C07": true,
"bms/cells/C08": true,
"bms/cell-display/C08": true,
"bms/cells/C09": true,
"bms/cell-display/C09": true,
"bms/cells/C10": true,
"bms/cell-display/C10": true,
"bms/cells/C11": true,
"bms/cell-display/C11": true,
"bms/cells/C12": true,
"bms/cell-display/C12": true,
"bms/cells/C13": true,
"bms/cell-display/C13": true,
"bms/CASE | bead-blasted graphite/CASE | base tray": true,
"bms/CASE | bead-blasted graphite/CASE | front endplate": true,
"bms/CASE | bead-blasted graphite/CASE | rear endplate": true,
"bms/glass/CASE | removable solid top": true,
"bms/glass/CASE | slotted side -1": true,
"bms/glass/CASE | slotted side 1": true,
"bms/R04 | elastomer seal": true,
"bms/R04 | graphite moulded polymer": true,
"bms/labels/enclosure": true,
"bms/R04 | smoked polycarbonate": true,
"bms/R04 | display protective polycarbonate": true,
"bms/DISPLAY | black glass": true,
"bms/CASE | recessed black": true,
"bms/CASE | darker frame": true,
"bms/CASE | bead-blasted graphite/HANDLE | folded aperture return": true,
"bms/CASE | bead-blasted graphite/HANDLE | folded aperture return.001": true,
"bms/CASE | bead-blasted graphite/HANDLE | integral web underside": true,
"bms/CASE | dielectric liner": true,
"bms/CASE | dielectric liner/left": true,
"bms/CASE | dielectric liner/right": true,
"bms/Connector / gold plated contacts": true,
"bms/XT90-S / anti spark green insert": true,
"bms/XT90-S / yellow PA housing": true
}
}
@@ -0,0 +1,21 @@
import {useCallback,useEffect,useState} from 'react';
import {SceneHistory,historyCommand} from '../roverScene/sceneHistory';
import {batteryAppearanceKey,defaultBatteryAppearance,parseBatteryAppearance,type BatteryAppearance} from './appearance';
export function useBatteryAppearance(id:string,active:boolean){
const [error,setError]=useState('');
const [history]=useState(()=>{let profile=defaultBatteryAppearance();try{const raw=localStorage.getItem(batteryAppearanceKey(id));if(raw)profile=parseBatteryAppearance(JSON.parse(raw));}catch{/* Keep the usable default if the local profile is unavailable. */}return new SceneHistory(profile);});
const [value,setValue]=useState(history.present);
const publish=useCallback(()=>{setValue(history.present);try{localStorage.setItem(batteryAppearanceKey(id),JSON.stringify(history.present));setError('');}catch{setError('Не удалось сохранить настройки батареи на этом компьютере.');}},[history,id]);
const update=useCallback((next:BatteryAppearance)=>{if(JSON.stringify(next)!==JSON.stringify(history.present)){history.update(next);publish();}},[history,publish]);
const undo=useCallback(()=>{if(history.undo())publish();},[history,publish]);
const redo=useCallback(()=>{if(history.redo())publish();},[history,publish]);
useEffect(()=>{
if(!active)return;
const begin=()=>history.begin(),end=()=>history.end();
const key=(e:KeyboardEvent)=>{const command=historyCommand(e);if(command){e.preventDefault();e.stopImmediatePropagation();if(command==='undo')undo();else redo();}else if(!e.repeat&&e.key.startsWith('Arrow')&&(e.target as HTMLElement)?.closest?.('[role=slider],input[type=range]'))begin();};
const up=(e:KeyboardEvent)=>{if(e.key.startsWith('Arrow'))end();};
window.addEventListener('keydown',key,true);window.addEventListener('keyup',up,true);window.addEventListener('pointerdown',begin,true);window.addEventListener('pointerup',end);window.addEventListener('pointercancel',end);window.addEventListener('blur',end);
return()=>{end();window.removeEventListener('keydown',key,true);window.removeEventListener('keyup',up,true);window.removeEventListener('pointerdown',begin,true);window.removeEventListener('pointerup',end);window.removeEventListener('pointercancel',end);window.removeEventListener('blur',end);};
},[active,history,undo,redo]);
return {value,update,undo,redo,canUndo:history.canUndo,canRedo:history.canRedo,error,setError};
}
@@ -0,0 +1,7 @@
/** Local view geometry is independent of imported scene/material JSON. */
export const minBatteryViewHeight=360,maxBatteryViewHeight=2160;
export const batteryViewHeightKey=(id:string)=>`missioncore.battery-view/v1:${encodeURIComponent(id)}`;
export function batteryViewHeight(raw:string|null,viewportHeight:number):number{
const fallback=Math.max(minBatteryViewHeight,Math.min(900,Math.round(viewportHeight-300)));
try{const value=JSON.parse(raw??'null');return typeof value?.height==='number'&&Number.isFinite(value.height)?Math.max(minBatteryViewHeight,Math.min(maxBatteryViewHeight,Math.round(value.height))):fallback;}catch{return fallback;}
}