/**
* @file plc_debug.js
* @brief PLC 调试页 — 三列布局:左(遥信输入·按钮/遥测输入·设值) | 中(信号表) | 右(遥信输出·LED/遥测输出·展示)
* @details 所有条目从中间信号表选择添加,增量刷新
*/
var PlcDebugStIn = []; // 遥信输入(按钮)
var PlcDebugMxIn = []; // 遥测输入(设值)
var PlcDebugStOut = []; // 遥信输出(LED)
var PlcDebugMxOut = []; // 遥测输出(展示)
Router.register('#plc_debug', function(c) { PlcDebugPage.render(c); });
var PlcDebugPage = (function() {
var outSignals = [];
var stInChk = {}, mxInChk = {}, stOutChk = {}, mxOutChk = {}, sigChk = {};
function getOut() {
var out = SignalStore.get('out');
return (Array.isArray(out) && out.length) ? out : outSignals;
}
function findSig(sa) {
for (var i = 0; i < outSignals.length; i++)
if (outSignals[i].saddr === sa) return outSignals[i];
return null;
}
function refreshData() {
var o = getOut(); if (Array.isArray(o)) outSignals = o;
PlcDebugStIn.forEach(function(l) { var f = findSig(l.saddr); l.val = f ? f.val : 0; });
PlcDebugMxIn.forEach(function(m) { var f = findSig(m.saddr); m.val = f ? f.val : 0; });
PlcDebugStOut.forEach(function(b) { var f = findSig(b.saddr); b.val = f ? f.val : 0; });
PlcDebugMxOut.forEach(function(m) { var f = findSig(m.saddr); m.val = f ? f.val : 0; });
}
function incrRefresh() {
refreshData();
PlcDebugStIn.forEach(function(b, i) {
var on = parseFloat(b.val) > 0;
var ve = document.getElementById('sti-val-' + i);
if (ve) { ve.textContent = '(' + fmtVal(b.val) + ')'; ve.style.color = on ? 'var(--accent)' : 'var(--text-muted)'; }
});
PlcDebugMxIn.forEach(function(m, i) {
var ve = document.getElementById('mxi-cur-' + i);
if (ve) ve.textContent = '(' + fmtVal(m.val) + ')';
});
PlcDebugStOut.forEach(function(l, i) {
var on = parseFloat(l.val) > 0;
var ve = document.getElementById('sto-val-' + i), ie = document.getElementById('sto-icon-' + i);
if (ve) { ve.textContent = '(' + fmtVal(l.val) + ')'; ve.style.color = on ? 'var(--accent)' : 'var(--text-muted)'; }
if (ie) ie.className = 'plc-led' + (on ? ' plc-led-on' : '');
});
PlcDebugMxOut.forEach(function(m, i) {
var ve = document.getElementById('mxo-val-' + i);
if (ve) ve.textContent = fmtVal(m.val);
});
}
function countChecked(obj) { var n = 0; for (var k in obj) if (obj.hasOwnProperty(k)) n++; return n; }
function delChecked() {
var todel = [];
for (var k in stInChk) if (stInChk.hasOwnProperty(k)) todel.push({ t: 'sti', idx: parseInt(k) });
for (var k in mxInChk) if (mxInChk.hasOwnProperty(k)) todel.push({ t: 'mxi', idx: parseInt(k) });
for (var k in stOutChk) if (stOutChk.hasOwnProperty(k)) todel.push({ t: 'sto', idx: parseInt(k) });
for (var k in mxOutChk) if (mxOutChk.hasOwnProperty(k)) todel.push({ t: 'mxo', idx: parseInt(k) });
if (!todel.length) { toast(I18n.t('toast.no_selected'), 'error'); return; }
if (!confirm(I18n.t('plc_debug.confirm_del') + ' ' + todel.length + ' ?')) return;
todel.sort(function(a, b) { return b.idx - a.idx; }).forEach(function(t) {
if (t.t === 'sti') PlcDebugStIn.splice(t.idx, 1);
else if (t.t === 'mxi') PlcDebugMxIn.splice(t.idx, 1);
else if (t.t === 'sto') PlcDebugStOut.splice(t.idx, 1);
else PlcDebugMxOut.splice(t.idx, 1);
});
stInChk = {}; mxInChk = {}; stOutChk = {}; mxOutChk = {};
renderAll();
}
function makeBlock(title, icon, arr, chkObj, idPrefix, blockType) {
var html = '';
if (!arr.length) {
html = '
暂无
';
} else {
arr.forEach(function(item, i) {
var on = parseFloat(item.val) > 0, chk = !!chkObj[i];
html += '
';
html += '';
if (blockType === 'btn') {
html += '';
} else if (blockType === 'led') {
html += '';
}
html += '
';
if (item.saddr) {
html += '' + esc(item.saddr) + '';
if (item.desc) html += '' + esc(item.desc) + '';
} else {
html += '未绑定';
}
html += '
';
if (blockType === 'btn' || blockType === 'led') {
html += '(' + fmtVal(item.val) + ')';
} else if (blockType === 'mxi') {
html += '(' + fmtVal(item.val) + ')';
html += '';
html += '';
} else if (blockType === 'mxo') {
html += '' + fmtVal(item.val) + '';
}
html += '
';
});
}
return '
' + icon + ' ' + title + ''
+ '' + arr.length + '
'
+ '
' + html + '
';
}
function renderAll() {
var c = document.getElementById('plc-content');
if (!c) return;
refreshData();
/* 信号表 (中间列) */
var o = getOut();
var sigHtml = '';
if (!o.length) {
sigHtml = '