server+ui: auto-equip on card insertion; reset card views on session change

- _apply_equipped_card() centralizes the post-equip refresh + TERMINAL
  PROFILE (shared by the /api/command equip branch and auto-equip)
- server tracks card_session (bumped on equip and disconnect) and
  equipping; /api/status exposes connected, card_present, card_session,
  equipping, auto_equip and is exempt from _CARD_LOCK (pure cached state)
- auto-equip is on by default (--no-auto-equip; off with --no-card-init):
  the presence observer spawns a one-shot worker after insertion, which
  runs equip under _CARD_LOCK and applies the same refresh; the monitor
  starts after the startup init so pyscard's initial 'already present'
  event does not re-equip a fresh session
- UI: /api/status polls every 2s (other views stay at 5s); when
  card_session changes it runs pysimResetCardData() (STK overlay, file
  tree, events, proactive log, PLI, status) — the same reset as a manual
  Equip; messages: initializing / press Equip / no card

Tests for the observer and auto-equip rules, session bumps,
_apply_equipped_card, and the UI state machine. SW cache v91 -> v92.
This commit is contained in:
2026-09-12 13:34:40 +03:00
parent 57eb412b6d
commit fe10603aea
8 changed files with 272 additions and 51 deletions
+38 -11
View File
@@ -4455,6 +4455,7 @@ async function pysimConnect() {
function pysimDisconnect() {
if (_pysimPollTimer) { clearInterval(_pysimPollTimer); _pysimPollTimer = null; }
if (_pysimStatusTimer) { clearInterval(_pysimStatusTimer); _pysimStatusTimer = null; }
pysimShowConnected(false);
document.getElementById('pysim-status').textContent = '';
pysimSetConnected(false);
@@ -6400,7 +6401,9 @@ async function pysimStatusPoll() {
}
let _pysimPollTimer = null;
let _pysimLastConnected = null;
let _pysimStatusTimer = null;
let _pysimCardStateKey = null;
let _pysimCardSession = null;
async function pysimPollToggle() {
const btn = document.getElementById('pli-pause-btn');
@@ -6436,41 +6439,64 @@ async function pysimPollStatusInit() {
} catch (e) { /* ignore */ }
}
async function pysimResetCardData(refreshStatus) {
stkMenuClose();
stkMenuStack = [];
pysimFsRefresh();
stkCheckMenu();
pysimEventsRender();
pysimProactiveLogRender();
pysimPliRender();
if (refreshStatus) await pysimRefresh();
}
function pysimCardStateUpdate(status) {
if (!status || typeof status.connected !== 'boolean') return;
const statusEl = document.getElementById('pysim-status');
if (status.connected === _pysimLastConnected) return;
_pysimLastConnected = status.connected;
const key = [status.connected, !!status.card_present, !!status.equipping, !!status.auto_equip, status.card_session].join('|');
if (key === _pysimCardStateKey) return;
_pysimCardStateKey = key;
const sessionChanged = _pysimCardSession !== null
&& status.card_session !== undefined && status.card_session !== _pysimCardSession;
if (status.card_session !== undefined) _pysimCardSession = status.card_session;
if (status.connected) {
pysimSetConnected(true);
pysimRefresh();
pysimResetCardData(true);
return;
}
pysimSetConnected(false);
const statusEl = document.getElementById('pysim-status');
if (statusEl && statusEl.textContent.trim() !== '') {
if (status.card_present) {
if (status.equipping || status.auto_equip) {
statusEl.innerHTML = '<span class="text-gray-500">' + esc(t('Card inserted — initializing...')) + '</span>';
} else if (status.card_present) {
statusEl.innerHTML = '<span class="text-amber-600">' + esc(t('Card inserted — press Equip')) + '</span>';
} else {
statusEl.innerHTML = '<span class="text-red-500">' + esc(t('No card detected. Insert card and click Equip card button')) + '</span>';
}
}
if (sessionChanged) pysimResetCardData(false);
}
function pysimStartBackendPoll() {
if (_pysimPollTimer) clearInterval(_pysimPollTimer);
_pysimLastConnected = null;
if (_pysimStatusTimer) clearInterval(_pysimStatusTimer);
_pysimCardStateKey = null;
_pysimCardSession = null;
_pysimStatusTimer = setInterval(async () => {
try {
pysimCardStateUpdate(await pysimFetch('/api/status'));
} catch (e) { /* ignore */ }
}, 2000);
_pysimPollTimer = setInterval(async () => {
try {
const [log, stk, ps, status] = await Promise.all([
const [log, stk, ps] = await Promise.all([
pysimFetch('/api/proactive-log'),
pysimFetch('/api/stk-status'),
pysimFetch('/api/poll-status'),
pysimFetch('/api/status'),
]);
pysimUpdatePollUI(ps.enabled, ps.interval);
pysimCardStateUpdate(status);
} catch (e) { /* ignore */ }
}, 2000);
}, 5000);
}
// ===== proactive command log =====
@@ -8762,6 +8788,7 @@ const LANG_RU = {
'pysim-unreachable-hint': 'Не удалось подключиться к серверу карт. Проверьте, что pysim-otaman-server запущен и адрес указан верно ({url}). Если эта страница открыта по HTTPS, также разрешите доступ к локальной сети в браузере (Chrome/Edge/Vivaldi: Настройки сайта → Доступ к локальной сети → Разрешить).',
'No card detected. Insert card and click Equip card button': 'Карта не обнаружена. Вставьте карту и нажмите Подключить карту',
'Card inserted — press Equip': 'Карта вставлена — нажмите «Подключить карту»',
'Card inserted — initializing...': 'Карта вставлена — инициализация...',
'Checking...': 'Проверка...',
'Resetting...': 'Сброс...',
'Equipping...': 'Подключение...',
+1 -1
View File
@@ -1,4 +1,4 @@
const CACHE = 'otaman-v91';
const CACHE = 'otaman-v92';
const URLS = [
'index.html',
'help.html',
+40 -17
View File
@@ -21,7 +21,7 @@ function extractFunc(src, name) {
return src.slice(m.index, i + 1);
}
let code = 'var _pysimLastConnected = null;\n';
let code = 'var _pysimCardStateKey = null;\nvar _pysimCardSession = null;\n';
code += extractFunc(html, 'pysimCardStateUpdate') + '\n';
code += '\nglobalThis.esc = s => s;\n';
code += 'globalThis.t = s => s;\n';
@@ -29,47 +29,70 @@ eval(code);
function setup() {
const el = { textContent: 'status line', innerHTML: '' };
const calls = { connected: [], refresh: 0 };
const calls = { connected: [], resets: [], refreshStatus: [] };
_pysimCardStateKey = null;
_pysimCardSession = null;
globalThis.document = { getElementById: () => el };
globalThis.pysimSetConnected = v => calls.connected.push(v);
globalThis.pysimRefresh = () => { calls.refresh++; };
globalThis.pysimResetCardData = refresh => calls.resets.push(refresh);
return { el, calls };
}
function status(extra) {
return Object.assign({ connected: false, card_present: false, equipping: false, auto_equip: false, card_session: 1 }, extra);
}
test('disconnect without card shows the no-card message', () => {
_pysimLastConnected = true;
const { el, calls } = setup();
pysimCardStateUpdate({ connected: false, card_present: false });
pysimCardStateUpdate(status({}));
assert.deepStrictEqual(calls.connected, [false]);
assert.ok(el.innerHTML.includes('No card detected'), el.innerHTML);
});
test('disconnect with card present shows the Equip hint', () => {
_pysimLastConnected = true;
test('disconnect with card present shows the Equip hint when auto-equip is off', () => {
const { el } = setup();
pysimCardStateUpdate({ connected: false, card_present: true });
assert.ok(el.innerHTML.includes('Card inserted'), el.innerHTML);
pysimCardStateUpdate(status({ card_present: true }));
assert.ok(el.innerHTML.includes('Card inserted — press Equip'), el.innerHTML);
});
test('unchanged state does not touch the UI again', () => {
_pysimLastConnected = false;
test('disconnect with auto-equip shows the initializing message', () => {
const { el } = setup();
pysimCardStateUpdate(status({ card_present: true, auto_equip: true }));
assert.ok(el.innerHTML.includes('initializing'), el.innerHTML);
});
test('unchanged state key does not touch the UI again', () => {
const { el, calls } = setup();
pysimCardStateUpdate(status({ card_session: 7 }));
el.innerHTML = 'unchanged';
pysimCardStateUpdate({ connected: false, card_present: false });
calls.connected.length = 0;
pysimCardStateUpdate(status({ card_session: 7 }));
assert.deepStrictEqual(calls.connected, []);
assert.strictEqual(el.innerHTML, 'unchanged');
});
test('reconnect restores the connected UI and refreshes', () => {
_pysimLastConnected = false;
test('connected restores the UI and reloads card data', () => {
const { calls } = setup();
pysimCardStateUpdate({ connected: true, card_present: true });
pysimCardStateUpdate(status({ connected: true, card_present: true, card_session: 2 }));
assert.deepStrictEqual(calls.connected, [true]);
assert.strictEqual(calls.refresh, 1);
assert.deepStrictEqual(calls.resets, [true]);
});
test('card session change triggers a data reset', () => {
const { calls } = setup();
pysimCardStateUpdate(status({ card_session: 3 }));
calls.resets.length = 0;
pysimCardStateUpdate(status({ card_session: 4 }));
assert.deepStrictEqual(calls.resets, [false]);
});
test('first observation does not trigger a reset on its own', () => {
const { calls } = setup();
pysimCardStateUpdate(status({ card_session: 9 }));
assert.deepStrictEqual(calls.resets, []);
});
test('payload without connected flag is ignored', () => {
_pysimLastConnected = null;
const { calls } = setup();
pysimCardStateUpdate({ reader: 'x' });
pysimCardStateUpdate(null);