This commit is contained in:
Marcelo
2025-11-28 09:11:59 -06:00
commit b66cb97f16
34 changed files with 17756 additions and 0 deletions

86
restore_session.js Normal file
View File

@@ -0,0 +1,86 @@
// ===== RESTORE SESSION =====
// This function is called when user clicks "Restore Session" button
// msg.payload should contain the session data from the recovery prompt
const sessionData = msg.payload;
if (!sessionData || !sessionData.session_id) {
node.error("No valid session data provided for restoration");
msg.error = "Invalid session data";
return [null, msg]; // Output 2: error message
}
try {
// Restore global context variables
global.set("currentSessionId", sessionData.session_id);
global.set("productionStartTime", sessionData.production_start_time);
global.set("operatingTime", sessionData.operating_time || 0);
global.set("trackingEnabled", sessionData.tracking_enabled === 1);
global.set("cycleCount", sessionData.cycle_count || 0);
global.set("machineOnline", sessionData.machine_online === 1);
global.set("productionStarted", sessionData.production_started === 1);
global.set("lastCycleTime", sessionData.last_cycle_time);
// Restore flow context variables
flow.set("lastMachineState", sessionData.last_machine_state || 0);
// Restore active work order if exists
if (sessionData.active_work_order_data) {
try {
const workOrder = typeof sessionData.active_work_order_data === 'string'
? JSON.parse(sessionData.active_work_order_data)
: sessionData.active_work_order_data;
global.set("activeWorkOrder", workOrder);
// Also need to update work order status in database
const updateMsg = {
topic: `
UPDATE work_orders
SET status = 'RUNNING', updated_at = NOW()
WHERE work_order_id = '${workOrder.id}';
`,
_mode: "restore-work-order-status"
};
node.warn(`Session restored: ${sessionData.session_id} | Work Order: ${workOrder.id} | Cycles: ${sessionData.cycle_count}`);
msg._recoveryStatus = "restored";
msg.payload = {
success: true,
type: "success",
message: `Session restored successfully! Work Order ${workOrder.id} is now active with ${sessionData.cycle_count} cycles completed.`,
sessionId: sessionData.session_id,
workOrder: workOrder
};
return [updateMsg, msg]; // Output 1: DB update, Output 2: success message
} catch (e) {
node.error("Failed to parse work order data: " + e.message);
throw e;
}
} else {
// No active work order in session
node.warn(`Session restored: ${sessionData.session_id} | No active work order`);
msg._recoveryStatus = "restored-no-wo";
msg.payload = {
success: true,
type: "info",
message: "Session restored, but no active work order was found. You can start a new work order.",
sessionId: sessionData.session_id
};
return [null, msg]; // Output 2: info message
}
} catch (error) {
node.error("Error restoring session: " + error.message);
msg._recoveryStatus = "restore-failed";
msg.payload = {
success: false,
type: "error",
message: "Failed to restore session: " + error.message
};
return [null, msg]; // Output 2: error message
}