70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
/**
|
||
* 按型号目录回填马达 / 扬声器的尺寸、体积、功率。
|
||
*
|
||
* 示例:
|
||
* pnpm tsx scripts/apply-hardware-catalog.mjs # 只统计
|
||
* pnpm tsx scripts/apply-hardware-catalog.mjs --write # 写库
|
||
*/
|
||
import { enrichHardwareSpecs, lookupMotorModel, lookupSpeakerModel } from '../src/lib/hardware-catalog';
|
||
import { listDevices, saveDevice } from '../src/lib/repo';
|
||
|
||
const write = process.argv.includes('--write');
|
||
|
||
const stats = {
|
||
devices: 0,
|
||
changed: 0,
|
||
motors: 0,
|
||
motorSizes: 0,
|
||
motorVolumes: 0,
|
||
units: 0,
|
||
unitFilled: 0,
|
||
};
|
||
|
||
const changedSamples = [];
|
||
|
||
for (const device of listDevices()) {
|
||
stats.devices += 1;
|
||
const before = JSON.stringify(device.specs);
|
||
const afterSpecs = enrichHardwareSpecs(device.specs);
|
||
const after = JSON.stringify(afterSpecs);
|
||
if (before === after) continue;
|
||
|
||
stats.changed += 1;
|
||
const motor = device.specs.vibration;
|
||
const nextMotor = afterSpecs.vibration;
|
||
if (motor?.model && lookupMotorModel(motor.model)) {
|
||
stats.motors += 1;
|
||
if (!motor.size && nextMotor?.size) stats.motorSizes += 1;
|
||
if (!motor.volume && nextMotor?.volume) stats.motorVolumes += 1;
|
||
}
|
||
const units = device.specs.speakers?.units ?? [];
|
||
const nextUnits = afterSpecs.speakers?.units ?? [];
|
||
units.forEach((unit, index) => {
|
||
if (!unit.model || !lookupSpeakerModel(unit.model)) return;
|
||
stats.units += 1;
|
||
if ((!unit.size && nextUnits[index]?.size) || (!unit.power && nextUnits[index]?.power)) {
|
||
stats.unitFilled += 1;
|
||
}
|
||
});
|
||
|
||
if (changedSamples.length < 8) {
|
||
changedSamples.push({
|
||
slug: device.slug,
|
||
motor: afterSpecs.vibration,
|
||
units: afterSpecs.speakers?.units,
|
||
});
|
||
}
|
||
|
||
if (write) saveDevice({ ...device, specs: afterSpecs });
|
||
}
|
||
|
||
console.log(`扫描 ${stats.devices} 款,需回填 ${stats.changed} 款(${write ? '已写入' : '未写入,加 --write 生效'})`);
|
||
console.log(
|
||
`马达:可识别 ${stats.motors} 款,补尺寸 ${stats.motorSizes},补体积 ${stats.motorVolumes}`,
|
||
);
|
||
console.log(`扬声器:可识别单元 ${stats.units},补尺寸/功率 ${stats.unitFilled}`);
|
||
console.log('示例:');
|
||
for (const sample of changedSamples) {
|
||
console.log(JSON.stringify(sample));
|
||
}
|