序
新能源这组先从光伏说起。光伏场景有个特点,拉远看很整齐,拉近看也很整齐,一排一排铺过去,做总览挺省心。真要找某一块组件的时候,这份整齐就开始给人添麻烦了:左边第三排,还是从检修通道那头数第三排?模型转了半圈,方向也跟着换了。
如果只是做展示,点一下方阵、弹个总功率,也能讲过去。但准备接组串和逆变器以后,编号关系就得落细。一个逆变器挂了哪些组串,一个组串对应哪几块组件,缺一份绑定表,画面上那一大片板子就只能一起变色。看着很热闹,想找问题还得回表格。
还有一种情况更容易糊弄过去:数据没来,也给个零。零功率、缺测、设备离线就这样被塞进同一个格子,图上倒是挺干净。可天气、运行状态和通信状态并不是一回事,这个零有时候比一串空值更让人误会。
这篇先做一个小方阵,六行组件,两台逆变器。从编号映射到实例拾取,再到数据质量和关联定位,一条线走下来。先保证 M032 真的是 M032,点它以后能找到对应组串和逆变器。至于整站发电分析,等底下这些关系接稳了再聊。
一、组件编号不要从屏幕位置临时拼
业务编号由配置提供,实例序号只负责当前绘制。配置可以从设备台账转换,导入时检查重复编号、组串和逆变器引用。缺失关系单独返回,不能悄悄归到第一台逆变器。
javascript
import * as THREE from 'three';
function buildAssetIndex({ modules, strings, inverters }) {
function index(rows, label) {
const map = new Map();
for (const row of rows) {
if (!row.id || map.has(row.id)) throw new Error(`${label}编号为空或重复`);
map.set(row.id, row);
}
return map;
}
const moduleMap = index(modules, '组件');
const stringMap = index(strings, '组串');
const inverterMap = index(inverters, '逆变器');
const issues = [], members = new Map();
for (const item of modules) {
const string = stringMap.get(item.stringId);
if (!string || !inverterMap.has(string.inverterId)) {
issues.push({ id: item.id, reason: '关联链缺失' }); continue;
}
const rows = members.get(item.stringId) ?? [];
rows.push(item.id); members.set(item.stringId, rows);
}
return { moduleMap, stringMap, inverterMap, members, issues };
}
function relatedAssets(index, moduleId) {
const module = index.moduleMap.get(moduleId);
const string = module && index.stringMap.get(module.stringId);
if (!module || !string || !index.inverterMap.has(string.inverterId)) return null;
return { moduleId, stringId: string.id, inverterId: string.inverterId,
moduleIds: index.members.get(string.id) ?? [] };
}
二、实例拾取要有正反两份索引
从画面点到组件,使用 instanceId → moduleId;从列表搜编号,使用反向映射。下面传入组件共享几何、材质和配置,几何采用居中原点,位置和角度来自配置。倾角单位为度。
javascript
function buildModuleMesh(geometry, material, rows) {
const mesh = new THREE.InstancedMesh(geometry, material, rows.length);
const ids = [], instanceById = new Map();
const temporary = new THREE.Object3D();
rows.forEach((row, i) => {
if (instanceById.has(row.id) || !row.position.every(Number.isFinite)) {
throw new Error('组件配置无效');
}
temporary.position.set(...row.position);
temporary.rotation.set(THREE.MathUtils.degToRad(row.tilt ?? 20), 0, 0);
temporary.scale.set(1, 1, 1); temporary.updateMatrix();
mesh.setMatrixAt(i, temporary.matrix);
mesh.setColorAt(i, new THREE.Color('#315c79'));
ids.push(row.id); instanceById.set(row.id, i);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.computeBoundingSphere();
return { mesh, ids, instanceById,
resolve(hit) { return hit.object === mesh ? ids[hit.instanceId] ?? null : null; },
worldPosition(id) {
const i = instanceById.get(id);
if (i === undefined) return null;
const matrix = new THREE.Matrix4(); mesh.getMatrixAt(i, matrix);
const point = new THREE.Vector3().setFromMatrixPosition(matrix);
mesh.updateWorldMatrix(true, false);
return mesh.localToWorld(point);
}
};
}
三、缺测、过期和业务异常分开
读取数据先看质量,再看采样时间,最后才比较数值。null 不转成 0;有些采集端会上传空字符串,也明确拒绝。本文不根据单个功率值推断设备故障,业务异常由接口告警标志表达。
javascript
function classifyReading(reading, now, maxAge = 60000) {
if (!reading) return { state: 'missing', value: null, label: '未收到数据' };
if (reading.quality !== 'good' || typeof reading.value !== 'number' ||
!Number.isFinite(reading.value) || !Number.isFinite(reading.at)) {
return { state: 'invalid', value: null, label: '采样无效' };
}
if (reading.at > now + 5000) return { state: 'invalid', value: null, label: '时间异常' };
if (now - reading.at > maxAge) return { state: 'stale', value: reading.value, label: '数据过期' };
return { state: reading.alarm ? 'alarm' : 'normal', value: reading.value,
label: reading.alarm ? '业务告警' : '有效采样' };
}
function createTelemetry() {
const latest = new Map();
return {
receive(packet) {
if (!packet.id || !Number.isSafeInteger(packet.version) || !Number.isFinite(packet.at)) return false;
const previous = latest.get(packet.id);
if (previous && packet.version <= previous.version) return false;
latest.set(packet.id, { ...packet }); return true;
},
get(id, now) { return classifyReading(latest.get(id), now); },
raw(id) { return latest.get(id) ?? null; }
};
}
四、选中组件不要把原状态涂没了
状态色是一层,选中标记是另一层。这里用 Box3Helper 表示选中,实例颜色仍按数据质量更新。右侧展示当前组件和关联对象,别把组串值复制成每块组件的独立测量值。
javascript
const stateColors = { missing: '#78838d', invalid: '#95839e', stale: '#bda56c',
alarm: '#cd735d', normal: '#315c79' };
function updateModuleColors(view, telemetry, now) {
const color = new THREE.Color();
view.ids.forEach((id, i) => {
color.set(stateColors[telemetry.get(id, now).state]);
view.mesh.setColorAt(i, color);
});
if (view.mesh.instanceColor) view.mesh.instanceColor.needsUpdate = true;
}
function makeSelection(scene, view, assetIndex, telemetry, renderPanel) {
const helper = new THREE.Box3Helper(new THREE.Box3(), 0xe5b551);
helper.visible = false; scene.add(helper);
const local = new THREE.Matrix4(), world = new THREE.Matrix4();
let selectedId = null;
return {
select(id) {
const instance = view.instanceById.get(id), related = relatedAssets(assetIndex, id);
if (instance === undefined || !related) return false;
selectedId = id;
view.mesh.geometry.computeBoundingBox();
view.mesh.getMatrixAt(instance, local); view.mesh.updateWorldMatrix(true, false);
world.multiplyMatrices(view.mesh.matrixWorld, local);
helper.box.copy(view.mesh.geometry.boundingBox).applyMatrix4(world);
helper.visible = true;
renderPanel({ ...related, reading: telemetry.get(id, Date.now()),
source: telemetry.raw(id)?.source ?? '组件独立采样未提供' });
return true;
},
refresh() { if (selectedId) this.select(selectedId); },
dispose() { helper.removeFromParent(); helper.geometry.dispose(); helper.material.dispose(); }
};
}
五、同一批更新合并后再刷新画面
接口可能连续推很多条,没必要每条都更新所有实例颜色。先收集最新数据,每个渲染周期最多刷一次;数据安静下来以后,仍定时检查是否过期。离开页面时取消订阅和定时器。
javascript
function connectSolarView({ subscribe, view, telemetry, selection }) {
let dirty = true, lastQualityCheck = 0, disposed = false;
const unsubscribe = subscribe(packet => {
if (!disposed && telemetry.receive(packet)) dirty = true;
});
return {
tick(now = Date.now()) {
if (disposed) return;
if (now - lastQualityCheck >= 1000) { dirty = true; lastQualityCheck = now; }
if (!dirty) return;
updateModuleColors(view, telemetry, now); selection.refresh(); dirty = false;
},
dispose() {
disposed = true; unsubscribe(); selection.dispose();
view.mesh.removeFromParent(); view.mesh.dispose();
}
};
}
共享几何和材质的释放由创建它们的资源管理器负责,这里只释放实例对象,避免误伤另外一个方阵。组件很多时进一步记录脏实例编号,局部改色;采样过期检查也可以按到期时间排队,不必一直遍历全站。
这一篇可以用三条消息检查:值为 0 但质量有效,值为空且质量无效,旧版本晚到。它们应该分别显示有效零值、无效采样、忽略旧消息。再点一块组件,看看组串和逆变器是否能顺着关系找到。
光伏先写到这里。下一篇去看风机,设备会转,工单也会变,但展示转速、遥测状态和检修进度仍然得各管各的。