u
This commit is contained in:
@@ -48,12 +48,16 @@ export function updateAgentPrinters(companyId, agentId, agentName, rawPrinters)
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const printerList = Array.isArray(rawPrinters) ? rawPrinters : [];
|
||||
const normalized = printerList.map((p) => ({
|
||||
...p,
|
||||
agentId: aId,
|
||||
agentName: agentName || null,
|
||||
updatedAt: now,
|
||||
}));
|
||||
const normalized = printerList.map((p) => {
|
||||
const liveStatus = extractLiveStatus(p);
|
||||
return {
|
||||
...p,
|
||||
agentId: aId,
|
||||
agentName: agentName || null,
|
||||
updatedAt: now,
|
||||
...liveStatus,
|
||||
};
|
||||
});
|
||||
|
||||
companyAgents.get(cId).set(aId, normalized);
|
||||
|
||||
@@ -155,6 +159,106 @@ export function broadcastCompanyPrinters(companyId, printers) {
|
||||
emit('agentprinter', { companyId: cId, data: payload });
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts normalized boolean live status indicators and telemetry from live printer telemetry.
|
||||
*
|
||||
* @param {object|null|undefined} live
|
||||
* @returns {{ open: boolean, pause: boolean, error: boolean, online: boolean, labelLength: number|null }}
|
||||
*/
|
||||
export function extractLiveStatus(live) {
|
||||
if (!live) {
|
||||
return {
|
||||
open: false,
|
||||
pause: false,
|
||||
error: false,
|
||||
online: false,
|
||||
labelLength: null,
|
||||
};
|
||||
}
|
||||
|
||||
const errors = live.errors || {};
|
||||
const isOffline = live.status === 'offline' || live.unreachable === true || errors.unreachable === true;
|
||||
const online = !isOffline;
|
||||
|
||||
const open = Boolean(live.open ?? live.headOpen ?? errors.headOpen ?? false);
|
||||
const pause = Boolean(live.pause ?? live.paused ?? errors.pause ?? false);
|
||||
|
||||
const hasOtherError = Boolean(
|
||||
live.error ||
|
||||
errors.other ||
|
||||
errors.error ||
|
||||
Object.entries(errors).some(([key, val]) => !['headOpen', 'pause', 'unreachable'].includes(key) && Boolean(val))
|
||||
);
|
||||
|
||||
const rawLabelLength = live.labelLength ?? live.labellength;
|
||||
const labelLength = rawLabelLength != null && Number.isFinite(Number(rawLabelLength))
|
||||
? Number(rawLabelLength)
|
||||
: null;
|
||||
|
||||
return {
|
||||
open,
|
||||
pause,
|
||||
error: hasOtherError,
|
||||
online,
|
||||
labelLength,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a matching live printer from memory for a given database agentprinter record.
|
||||
*
|
||||
* @param {number} companyId
|
||||
* @param {object} agentprinter
|
||||
* @returns {LivePrinter|null}
|
||||
*/
|
||||
export function getLivePrinterForAgentPrinter(companyId, agentprinter) {
|
||||
const printers = getCompanyPrinters(companyId);
|
||||
if (!agentprinter || printers.length === 0) return null;
|
||||
|
||||
if (agentprinter.id != null) {
|
||||
const match = printers.find((p) => p.agentprinterId === agentprinter.id);
|
||||
if (match) return match;
|
||||
}
|
||||
|
||||
if (agentprinter.printeragentId && agentprinter.serial) {
|
||||
const match = printers.find((p) =>
|
||||
Number(p.agentId) === Number(agentprinter.printeragentId) &&
|
||||
p.serial &&
|
||||
String(p.serial).trim().toLowerCase() === String(agentprinter.serial).trim().toLowerCase()
|
||||
);
|
||||
if (match) return match;
|
||||
}
|
||||
|
||||
if (agentprinter.printeragentId && agentprinter.model) {
|
||||
const match = printers.find((p) =>
|
||||
Number(p.agentId) === Number(agentprinter.printeragentId) &&
|
||||
p.model &&
|
||||
String(p.model).trim().toLowerCase() === String(agentprinter.model).trim().toLowerCase() &&
|
||||
(!agentprinter.type || !p.type || String(p.type).trim().toLowerCase() === String(agentprinter.type).trim().toLowerCase())
|
||||
);
|
||||
if (match) return match;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enriches an agentprinter database record with live operational status (open, pause, error, online).
|
||||
*
|
||||
* @param {number} companyId
|
||||
* @param {object} agentprinter
|
||||
* @returns {object}
|
||||
*/
|
||||
export function enrichAgentPrinter(companyId, agentprinter) {
|
||||
if (!agentprinter) return agentprinter;
|
||||
const live = getLivePrinterForAgentPrinter(companyId, agentprinter);
|
||||
const status = extractLiveStatus(live);
|
||||
return {
|
||||
...agentprinter,
|
||||
...status,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all live printers for a company (e.g. on company deletion).
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user