#!/usr/bin/env python3 import json with open('/home/mdares/.node-red/flows.json', 'r') as f: flows = json.load(f) # Update Progress Check Handler to use DB values even when 0 for node in flows: if node.get('name') == 'Progress Check Handler': func = node['func'] # Replace the "no progress" path to use DB values old_no_progress = ''' } else { // No existing progress - proceed with normal start node.warn(`[PROGRESS-CHECK] No existing progress - proceeding with normal start`); // Simulate the original start-work-order behavior const startMsg = { _mode: "start", startOrder: order, topic: "UPDATE work_orders SET status = CASE WHEN work_order_id = ? THEN 'RUNNING' ELSE 'PENDING' END, updated_at = CASE WHEN work_order_id = ? THEN NOW() ELSE updated_at END WHERE status <> 'DONE'", payload: [order.id, order.id] }; global.set("activeWorkOrder", order); global.set("cycleCount", 0); flow.set("lastMachineState", 0); global.set("scrapPromptIssuedFor", null); return [startMsg, null]; }''' new_no_progress = ''' } else { // No existing progress - proceed with normal start // But still use DB values (even if 0) to ensure DB is source of truth node.warn(`[PROGRESS-CHECK] No existing progress - proceeding with normal start`); // Update order object with DB values (makes DB the source of truth) order.cycle_count = cycleCount; // Will be 0 from DB order.good_parts = goodParts; // Will be 0 from DB order.scrap = scrapParts; // Will be 0 from DB order.good = goodParts; // For consistency order.target = targetQty; // From DB const startMsg = { _mode: "start", startOrder: order, topic: "UPDATE work_orders SET status = CASE WHEN work_order_id = ? THEN 'RUNNING' ELSE 'PENDING' END, updated_at = CASE WHEN work_order_id = ? THEN NOW() ELSE updated_at END WHERE status <> 'DONE'", payload: [order.id, order.id] }; // Initialize global state with DB values (even if 0) global.set("activeWorkOrder", order); global.set("cycleCount", cycleCount); // Use DB value instead of hardcoded 0 flow.set("lastMachineState", 0); global.set("scrapPromptIssuedFor", null); node.warn(`[PROGRESS-CHECK] Initialized from DB: cycles=${cycleCount}, good=${goodParts}, scrap=${scrapParts}`); return [startMsg, null]; }''' if old_no_progress in func: func = func.replace(old_no_progress, new_no_progress) node['func'] = func print("✓ Updated Progress Check Handler to use DB values as source of truth") else: print("✗ Could not find exact no-progress section") break # Write back with open('/home/mdares/.node-red/flows.json', 'w') as f: json.dump(flows, f, indent=4) print("✓ flows.json updated successfully")