# Node-RED Node Configuration Reference Quick reference for configuring nodes when implementing data persistence. --- ## Modified Existing Nodes ### 1. Machine Cycles Function Node ``` Name: Machine cycles Type: function Outputs: 3 (changed from 2) Code: See modified_machine_cycles.js Wiring: Output 1 → [Scrap prompt handler] (existing) Output 2 → [Production state handler] (existing) Output 3 → [Session State DB] (NEW - MySQL node) ``` ### 2. Work Order Buttons Function Node ``` Name: Work Order buttons Type: function Outputs: 5 (changed from 4) Code: See modified_work_order_buttons.js Wiring: Output 1 → [Upload handler] (existing) Output 2 → [Select query handler] (existing) Output 3 → [Start/scrap handler] (existing) Output 4 → [Complete handler] (existing) Output 5 → [Session State DB] (NEW - MySQL node) ``` --- ## New Recovery Flow Nodes ### 3. Startup Inject Node ``` Name: Startup Trigger Type: inject Payload: timestamp Topic: (empty) Repeat: none Once after: 0.1 seconds On start: checked ✓ Wiring: Output → [Query Previous Session] ``` ### 4. Query Previous Session Function ``` Name: Query Previous Session Type: function Outputs: 1 Code: See startup_recovery_query.js Wiring: Output → [Session State DB] ``` ### 5. Process Recovery Response Function ``` Name: Process Recovery Response Type: function Outputs: 2 Code: See process_recovery_response.js Wiring: Output 1 → [Session State DB] (deactivate stale) Output 2 → [Recovery Prompt UI] ``` ### 6. Restore Session Function ``` Name: Restore Session Type: function Outputs: 2 Code: See restore_session.js Triggered by: User clicking "Restore Session" button Wiring: Output 1 → [Session State DB] (update work order status) Output 2 → [Success notification UI] ``` ### 7. Start Fresh Function ``` Name: Start Fresh Type: function Outputs: 2 Code: See start_fresh.js Triggered by: User clicking "Start Fresh" button Wiring: Output 1 → [Session State DB] (deactivate old session) Output 2 → [Success notification UI] ``` ### 8. Session State DB MySQL Node ``` Name: Session State DB Type: mysql Database: [Select your database config - same as work_orders] This node receives inputs from: - Machine cycles (output 3) - Work Order buttons (output 5) - Query Previous Session - Process Recovery Response (output 1) - Restore Session (output 1) - Start Fresh (output 1) ``` --- ## UI Notification Nodes ### Option A: Simple Toast Notifications ``` Node: notification Type: ui_toast Position: top right Duration: 5 seconds Highlight: (varies by message type) Receives from: - Process Recovery Response (output 2) - Restore Session (output 2) - Start Fresh (output 2) ``` ### Option B: Custom Dashboard Modal (Recommended) Create a custom UI template for recovery prompts: ``` Node: Recovery Prompt Modal Type: ui_template Template HTML: (see below) Receives from: - Process Recovery Response (output 2) Sends to: - Restore Session (when "Restore" clicked) - Start Fresh (when "Start Fresh" clicked) ``` **Modal Template Example:** ```html

{{msg.payload.title}}

{{msg.payload.message}}

Work Order: {{msg.payload.details.workOrderId}}

Cycles Completed: {{msg.payload.details.cyclesCompleted}}

Operating Time: {{msg.payload.details.operatingTime}}

Status: {{msg.payload.details.trackingWas}}

Last Update: {{msg.payload.details.lastUpdate}}

``` **Route Modal Actions:** ``` Node: Route Recovery Actions Type: switch Property: msg.action Rules: 1. == "restore-session" → [Restore Session function] 2. == "start-fresh" → [Start Fresh function] ``` --- ## Complete Recovery Flow Diagram ``` ┌─────────────────────┐ │ Startup Inject │ │ (0.1 sec delay) │ └──────────┬──────────┘ │ ▼ ┌─────────────────────┐ │ Query Previous │ │ Session (function) │ └──────────┬──────────┘ │ ▼ ┌─────────────────────┐ │ Session State DB │ │ (MySQL) │ └──────────┬──────────┘ │ ▼ ┌─────────────────────┐ ┌─────────────────────┐ │ Process Recovery ├─(1)─→│ Session State DB │ │ Response (function) │ │ (deactivate stale) │ └──────────┬──────────┘ └─────────────────────┘ │(2) ▼ ┌─────────────────────┐ │ Recovery Prompt UI │ │ (Modal or Toast) │ └──────────┬──────────┘ │ ┌──────┴──────┐ │ │ ▼ ▼ ┌────────┐ ┌────────┐ │Restore │ │ Start │ │Session │ │ Fresh │ └───┬─┬──┘ └──┬─┬───┘ │ │ │ │ (1) │ │(2) (1)│ │(2) │ │ │ │ │ └────┬────┘ │ │ │ │ ▼ ▼ ▼ ┌──────────┐ ┌────────────┐ │Session │ │ Success UI │ │State DB │ │ │ └──────────┘ └────────────┘ ``` --- ## Debug Nodes (Recommended) Add debug nodes to monitor the flow: ### Debug 1: Session Sync Monitor ``` Name: Debug Session Sync Type: debug Connected to: Session State DB output Output: complete msg object To: debug panel ``` ### Debug 2: Recovery Data Monitor ``` Name: Debug Recovery Data Type: debug Connected to: Process Recovery Response output 2 Output: msg.payload To: debug panel ``` ### Debug 3: Restore Status Monitor ``` Name: Debug Restore Status Type: debug Connected to: Restore Session output 2 Output: msg.payload To: debug panel ``` --- ## Testing Checklist ### Test Node Outputs 1. **Machine Cycles Output 3:** - Should fire every 5 seconds during production - Should fire immediately on state changes - Check msg._mode === "session-sync" - Check msg.topic contains INSERT query 2. **Work Order Buttons Output 5:** - Should fire on START button - Should fire on STOP button - Should fire on work order start - Should fire on work order complete - Check msg._mode === "session-sync" or "deactivate-session" 3. **Recovery Flow:** - Inject should trigger 0.1 sec after deploy - Query function should output SELECT statement - Process function should detect sessions correctly - Restore/Fresh functions should update database ### Verify Database ```sql -- Check if syncing is working SELECT COUNT(*) FROM session_state; -- Check latest update SELECT * FROM session_state ORDER BY updated_at DESC LIMIT 1; -- Check sync frequency (should be ~5 seconds apart) SELECT session_id, FROM_UNIXTIME(updated_at/1000) as update_time FROM session_state WHERE is_active = 1 ORDER BY updated_at DESC LIMIT 10; ``` --- ## Common Wiring Mistakes ❌ **Wrong:** Machine Cycles output 3 → existing handler ✅ **Correct:** Machine Cycles output 3 → NEW Session State DB node ❌ **Wrong:** Using separate MySQL nodes with different configs ✅ **Correct:** All session_state queries use SAME MySQL config ❌ **Wrong:** Recovery prompt wired directly to database ✅ **Correct:** Recovery prompt → User action → Function → Database ❌ **Wrong:** Inject node set to repeat ✅ **Correct:** Inject node fires ONCE on startup only --- ## Performance Tips 1. **Reuse MySQL Node:** Use same Session State DB node for all queries 2. **Throttle Writes:** Don't modify the 5-second throttle (already optimized) 3. **Index Check:** Ensure indexes are created (see SQL file) 4. **Connection Pool:** Configure MySQL node with connection pooling 5. **Debug Cleanup:** Remove debug nodes in production --- ## Quick Rollback If you need to revert changes: 1. Restore original function code from backups: - `flows.json.backup_phase2` - Or remove sync code and restore original output counts 2. Remove recovery flow nodes 3. Database table is harmless to leave (no overhead if not used) --- **Last Updated:** 2025-11-21 **Version:** 1.0 **Related Files:** - IMPLEMENTATION_GUIDE.md - IMPLEMENTATION_SUMMARY.md - optimization_prompt.txt