Files
2025-12-02 16:27:21 +00:00

71 lines
2.9 KiB
Python

#!/usr/bin/env python3
import json
with open('/home/mdares/.node-red/flows.json', 'r') as f:
flows = json.load(f)
# Fix 1: Update Progress Check Handler to query scrap_parts
for node in flows:
if node.get('name') == 'Progress Check Handler':
func = node['func']
# Update to get scrap_parts from DB
func = func.replace(
'const cycleCount = dbRow ? (Number(dbRow.cycle_count) || 0) : 0;\n const goodParts = dbRow ? (Number(dbRow.good_parts) || 0) : 0;',
'const cycleCount = dbRow ? (Number(dbRow.cycle_count) || 0) : 0;\n const goodParts = dbRow ? (Number(dbRow.good_parts) || 0) : 0;\n const scrapParts = dbRow ? (Number(dbRow.scrap_parts) || 0) : 0;'
)
# Update the order object to include scrap_parts
func = func.replace(
'order: {...order, cycle_count: cycleCount, good_parts: goodParts}',
'order: {...order, cycle_count: cycleCount, good_parts: goodParts, scrap: scrapParts}'
)
node['func'] = func
print("✓ Updated Progress Check Handler to include scrap_parts")
break
# Fix 2: Update start-work-order to query scrap_parts
for node in flows:
if node.get('name') == 'Work Order buttons':
func = node['func']
# Update the SELECT query to include scrap_parts
func = func.replace(
'msg.topic = "SELECT cycle_count, good_parts, progress_percent, target_qty FROM work_orders WHERE work_order_id = ? LIMIT 1";',
'msg.topic = "SELECT cycle_count, good_parts, scrap_parts, progress_percent, target_qty FROM work_orders WHERE work_order_id = ? LIMIT 1";'
)
# Update resume-work-order to set scrap
old_resume = ''' // Load existing values into global state (will be set from DB query result)
global.set("activeWorkOrder", order);
global.set("cycleCount", Number(order.cycle_count) || 0);
flow.set("lastMachineState", 0);
global.set("scrapPromptIssuedFor", null);
node.warn(`[RESUME-WO] Set cycleCount to ${order.cycle_count}`);'''
new_resume = ''' // Load existing values into global state
// IMPORTANT: Also set scrap so good_parts calculation is correct
order.scrap = Number(order.scrap) || 0;
order.good = Number(order.good_parts) || 0;
global.set("activeWorkOrder", order);
global.set("cycleCount", Number(order.cycle_count) || 0);
flow.set("lastMachineState", 0);
global.set("scrapPromptIssuedFor", null);
node.warn(`[RESUME-WO] Set cycleCount=${order.cycle_count}, scrap=${order.scrap}, good=${order.good}`);'''
func = func.replace(old_resume, new_resume)
node['func'] = func
print("✓ Updated Work Order buttons to load scrap_parts")
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")