Files
Projects-plastic/NODE_CONFIGURATION.md
Marcelo b66cb97f16 MVP
2025-11-28 09:11:59 -06:00

388 lines
9.8 KiB
Markdown

# 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
<div ng-show="msg.payload.type === 'recovery-prompt'"
style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background: white; padding: 30px; border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1); z-index: 1000;">
<h2>{{msg.payload.title}}</h2>
<p>{{msg.payload.message}}</p>
<div style="margin: 20px 0;">
<p><strong>Work Order:</strong> {{msg.payload.details.workOrderId}}</p>
<p><strong>Cycles Completed:</strong> {{msg.payload.details.cyclesCompleted}}</p>
<p><strong>Operating Time:</strong> {{msg.payload.details.operatingTime}}</p>
<p><strong>Status:</strong> {{msg.payload.details.trackingWas}}</p>
<p><strong>Last Update:</strong> {{msg.payload.details.lastUpdate}}</p>
</div>
<button ng-click="send({action: 'restore-session', payload: msg.sessionData})"
style="padding: 10px 20px; margin-right: 10px; background: #4CAF50;
color: white; border: none; border-radius: 4px; cursor: pointer;">
Restore Session
</button>
<button ng-click="send({action: 'start-fresh', payload: msg.sessionData.session_id})"
style="padding: 10px 20px; background: #f44336; color: white;
border: none; border-radius: 4px; cursor: pointer;">
Start Fresh
</button>
</div>
<div ng-show="msg.payload.type === 'recovery-prompt'"
style="position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.5); z-index: 999;">
</div>
```
**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