#!/usr/bin/env python3 import json import re # Read flows.json with open('/home/mdares/.node-red/flows.json', 'r') as f: flows = json.load(f) # Find Work Order buttons node work_order_buttons_node = None for node in flows: if node.get('id') == '9bbd4fade968036d': work_order_buttons_node = node break if not work_order_buttons_node: print("❌ ERROR: Could not find Work Order buttons node") exit(1) func_code = work_order_buttons_node.get('func', '') print("Found Work Order buttons function") print(f"Current code length: {len(func_code)} characters") # ============================================================================ # Simplify START case - just enable tracking # ============================================================================ # The current START case is very complex with session management # We need to replace it with a simple version that ONLY sets trackingEnabled simple_start = ''' case "start": { // START button clicked from Home dashboard // Simply enable tracking - that's it! const now = Date.now(); // Initialize timing if needed if (!global.get("productionStartTime")) { global.set("productionStartTime", now); global.set("operatingTime", 0); global.set("downtime", 0); } // Enable tracking global.set("trackingEnabled", true); global.set("lastUpdateTime", now); node.warn("[START] Tracking enabled - cycles will now count"); return [null, null, null, null, null]; }''' # ============================================================================ # Simplify STOP case - just disable tracking and show prompt # ============================================================================ simple_stop = ''' case "stop": { // STOP button clicked from Home dashboard // Disable tracking and show stop reason prompt // First, disable tracking so cycles stop counting global.set("trackingEnabled", false); node.warn("[STOP] Tracking disabled - showing stop reason prompt"); // Now show the prompt msg._mode = "stop-prompt"; msg.stopPrompt = { timestamp: Date.now(), workOrderId: (global.get("activeWorkOrder") || {}).id || null }; return [null, msg, null, null, null]; }''' # ============================================================================ # Replace the START and STOP cases # ============================================================================ # Find START case - it's very long in the enhanced version # Look for 'case "start":' and find its matching closing brace before the next case # Find the start position start_case_match = re.search(r'case "start":\s*\{', func_code) if not start_case_match: print("❌ ERROR: Could not find START case") exit(1) start_pos = start_case_match.start() # Find the end - look for the return statement and closing brace # The pattern is: return [null, null, null, null, null]; followed by } # But there might be multiple returns in the complex version # Find the next 'case' after start to know where to stop next_case_after_start = re.search(r'\n\s+case "', func_code[start_pos + 20:]) if next_case_after_start: end_pos = start_pos + 20 + next_case_after_start.start() else: # No next case found, might be at the end end_pos = len(func_code) # Extract everything before START case before_start = func_code[:start_pos] # Extract everything after START case (which should start with next case or end of switch) after_start = func_code[end_pos:] # Now find and replace STOP case in after_start stop_case_match = re.search(r'case "stop":\s*\{', after_start) if not stop_case_match: print("❌ ERROR: Could not find STOP case") exit(1) stop_pos = stop_case_match.start() # Find the next case after stop next_case_after_stop = re.search(r'\n\s+case "', after_start[stop_pos + 20:]) if next_case_after_stop: stop_end_pos = stop_pos + 20 + next_case_after_stop.start() else: # Look for the end of switch or default case stop_end_pos = len(after_start) # Extract parts before_stop = after_start[:stop_pos] after_stop = after_start[stop_end_pos:] # Reconstruct the function code new_func_code = before_start + simple_start + "\n\n" + before_stop + simple_stop + "\n\n" + after_stop # Update the node work_order_buttons_node['func'] = new_func_code print("\n✅ Simplified START/STOP cases:") print(" - START: Only sets trackingEnabled = true") print(" - STOP: Sets trackingEnabled = false, then shows prompt") print(" - Removed all complex session management from these cases") print(" - Session management remains in start-work-order and complete-work-order") # Write updated flows with open('/home/mdares/.node-red/flows.json', 'w') as f: json.dump(flows, f, indent=4) print("\n" + "="*60) print("✅ START/STOP LOGIC FIXED") print("="*60) print("\n📋 What changed:") print(" 1. START button now simply enables trackingEnabled") print(" 2. STOP button disables trackingEnabled then shows prompt") print(" 3. Removed 100+ lines of complex session management code") print(" 4. Back to simple, reliable operation") print("\n🧪 To test:") print(" 1. Restart Node-RED") print(" 2. Start a work order") print(" 3. Click START - cycles should start counting") print(" 4. Click STOP - cycles should STOP counting AND prompt should show") print(" 5. Verify trackingEnabled changes in global context") print("\n💡 Expected behavior:") print(" - START: trackingEnabled = true, cycles count") print(" - STOP: trackingEnabled = false, cycles stop, prompt appears")