#!/usr/bin/env python3 import json with open('/home/mdares/.node-red/flows.json', 'r') as f: flows = json.load(f) print("RESTORING WORKING FUNCTIONALITY + CLEAN STOP PROMPT") print("="*60) # ============================================================================ # FIX 1: Restore toggleStartStop to working version # ============================================================================ for node in flows: if node.get('id') == '1821c4842945ecd8': template = node.get('format', '') # Find and replace toggleStartStop with the ORIGINAL working version # that also shows the stop prompt toggle_start = template.find('scope.toggleStartStop = function()') if toggle_start > 0: toggle_end = template.find('};', toggle_start) + 2 # Replace with working version that shows stop prompt working_toggle = '''scope.toggleStartStop = function() { if (scope.isProductionRunning) { // STOP clicked - show prompt console.log('[STOP] Showing stop reason prompt'); document.getElementById('stopReasonModal').style.display = 'flex'; // Reset selection state window._stopCategory = null; window._stopReason = null; document.querySelectorAll('.stop-reason-option').forEach(btn => { btn.classList.remove('selected'); }); if (document.getElementById('submitStopReason')) { document.getElementById('submitStopReason').disabled = true; } if (document.getElementById('stopReasonNotes')) { document.getElementById('stopReasonNotes').value = ''; } } else { // START/RESUME production scope.send({ action: "start" }); scope.isProductionRunning = true; } };''' template = template[:toggle_start] + working_toggle + template[toggle_end:] print("✅ Restored toggleStartStop function") # Make sure submitStopReason properly disables tracking if 'window.submitStopReason' in template: submit_start = template.find('window.submitStopReason') submit_end = template.find('window.hideStopPrompt', submit_start) correct_submit = '''window.submitStopReason = function() { const category = window._stopCategory; const reason = window._stopReason; if (!category || !reason) { alert('Please select a stop reason'); return; } const notes = document.getElementById('stopReasonNotes').value; console.log('[STOP SUBMIT] Category:', category, 'Reason:', reason); // Send stop-reason action to backend scope.send({ action: 'stop-reason', payload: { category: category, reason: reason, notes: notes } }); // Update UI state - production stopped scope.isProductionRunning = false; scope.$apply(); // Close the modal hideStopPrompt(); }; window.hideStopPrompt = function() { document.getElementById('stopReasonModal').style.display = 'none'; };''' template = template[:submit_start] + correct_submit + template[submit_end:] print("✅ Fixed submitStopReason function") node['format'] = template break # ============================================================================ # FIX 2: Ensure stop-reason case in Work Order buttons disables tracking # ============================================================================ for node in flows: if node.get('id') == '9bbd4fade968036d': func = node.get('func', '') # Find stop-reason case if 'case "stop-reason"' in func: # Check if it has trackingEnabled case_start = func.find('case "stop-reason"') case_end = func.find('\n case "', case_start + 10) if case_end < 0: case_end = func.find('\n}', case_start + 500) stop_reason_case = func[case_start:case_end] if 'global.set("trackingEnabled", false)' in stop_reason_case: print("✅ stop-reason case already disables tracking") else: # Find the opening brace of the case brace_pos = func.find('{', case_start) # Insert tracking disable func = func[:brace_pos+1] + '\n global.set("trackingEnabled", false);\n node.warn("[STOP REASON] Tracking disabled");' + func[brace_pos+1:] node['func'] = func print("✅ Added trackingEnabled = false to stop-reason case") else: print("⚠️ No stop-reason case found in Work Order buttons") break with open('/home/mdares/.node-red/flows.json', 'w') as f: json.dump(flows, f, indent=4) print("\n" + "="*60) print("✅ RESTORATION COMPLETE") print("="*60) print("\nWhat was fixed:") print(" 1. toggleStartStop() restored to working version") print(" 2. START sends action + sets isProductionRunning = true") print(" 3. STOP shows prompt (doesn't send action yet)") print(" 4. submitStopReason() sends stop-reason + updates UI") print(" 5. Work Order buttons disables tracking on stop-reason") print("\nExpected behavior:") print(" - Select WO + mold → START button enables") print(" - Click START → Production starts, cycles count") print(" - Click STOP → Prompt appears, cycles KEEP counting") print(" - Select reason + Submit → Tracking stops, cycles stop") print("\nRESTART NODE-RED AND TEST!") EOF