// ===== START FRESH ===== // This function is called when user clicks "Start Fresh" button // msg.payload should contain the old session_id to deactivate const oldSessionId = msg.payload; if (!oldSessionId) { node.error("No session ID provided to deactivate"); return [null, null]; } // Deactivate old session in database const deactivateMsg = { topic: ` UPDATE session_state SET is_active = 0, updated_at = ${Date.now()} WHERE session_id = '${oldSessionId}'; `, _mode: "deactivate-old-session" }; // Clear all global context variables global.set("currentSessionId", null); global.set("productionStartTime", null); global.set("operatingTime", 0); global.set("trackingEnabled", false); global.set("cycleCount", 0); global.set("machineOnline", true); global.set("productionStarted", false); global.set("lastCycleTime", null); global.set("activeWorkOrder", null); flow.set("lastMachineState", 0); node.warn("Starting fresh - all session data cleared"); msg._recoveryStatus = "fresh-start"; msg.payload = { success: true, type: "success", message: "Previous session discarded. System ready for new work orders." }; return [deactivateMsg, msg]; // Output 1: DB update, Output 2: success message