109 lines
3.9 KiB
Python
109 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
|
|
with open('/home/mdares/.node-red/flows.json', 'r') as f:
|
|
flows = json.load(f)
|
|
|
|
# Update restore-query handler in Back to UI
|
|
for node in flows:
|
|
if node.get('name') == 'Back to UI':
|
|
func = node['func']
|
|
|
|
# Find and replace the restore-query handler
|
|
old_restore = '''if (mode === "restore-query") {
|
|
const rows = Array.isArray(msg.payload) ? msg.payload : [];
|
|
|
|
if (rows.length > 0) {
|
|
const row = rows[0];
|
|
const restoredOrder = {
|
|
id: row.work_order_id || row.id || "",
|
|
sku: row.sku || "",
|
|
target: Number(row.target_qty || row.target || 0),
|
|
good: Number(row.good_parts || row.good || 0),
|
|
scrap: Number(row.scrap_parts || row.scrap || 0),
|
|
progressPercent: Number(row.progress_percent || 0),
|
|
cycleTime: Number(row.cycle_time || 0),
|
|
lastUpdateIso: row.updated_at || null
|
|
};
|
|
|
|
// Restore global state
|
|
global.set("activeWorkOrder", restoredOrder);
|
|
global.set("cycleCount", Number(row.cycle_count) || 0);
|
|
// Don't auto-start tracking - user must click START
|
|
global.set("trackingEnabled", false);
|
|
global.set("productionStarted", false);
|
|
|
|
node.warn('[RESTORE] Restored work order: ' + restoredOrder.id + ' with ' + global.get("cycleCount") + ' cycles');
|
|
|
|
const homeMsg = {
|
|
topic: "activeWorkOrder",
|
|
payload: restoredOrder
|
|
};
|
|
return [null, homeMsg, null, null];
|
|
} else {
|
|
node.warn('[RESTORE] No running work order found');
|
|
}
|
|
return [null, null, null, null];
|
|
}'''
|
|
|
|
new_restore = '''if (mode === "restore-query") {
|
|
const rows = Array.isArray(msg.payload) ? msg.payload : [];
|
|
|
|
if (rows.length > 0) {
|
|
const row = rows[0];
|
|
const restoredOrder = {
|
|
id: row.work_order_id || row.id || "",
|
|
sku: row.sku || "",
|
|
target: Number(row.target_qty || row.target || 0),
|
|
good: Number(row.good_parts || row.good || 0),
|
|
scrap: Number(row.scrap_parts || row.scrap || 0),
|
|
progressPercent: Number(row.progress_percent || 0),
|
|
cycleTime: Number(row.cycle_time || 0),
|
|
lastUpdateIso: row.updated_at || null
|
|
};
|
|
|
|
// Restore global state
|
|
global.set("activeWorkOrder", restoredOrder);
|
|
global.set("cycleCount", Number(row.cycle_count) || 0);
|
|
// Don't auto-start tracking - user must click START
|
|
global.set("trackingEnabled", false);
|
|
global.set("productionStarted", false);
|
|
|
|
node.warn('[RESTORE] Restored work order: ' + restoredOrder.id + ' with ' + global.get("cycleCount") + ' cycles');
|
|
|
|
// Set status back to RUNNING in database (if not already DONE)
|
|
// This prevents user from having to "Load" the work order again
|
|
const dbMsg = {
|
|
topic: "UPDATE work_orders SET status = 'RUNNING', updated_at = NOW() WHERE work_order_id = ? AND status != 'DONE'",
|
|
payload: [restoredOrder.id]
|
|
};
|
|
|
|
const homeMsg = {
|
|
topic: "activeWorkOrder",
|
|
payload: restoredOrder
|
|
};
|
|
|
|
// Output 1: workOrderMsg (to refresh WO table)
|
|
// Output 2: homeMsg (to update UI)
|
|
// Output 3: dbMsg (to update DB status)
|
|
return [dbMsg, homeMsg, null, null];
|
|
} else {
|
|
node.warn('[RESTORE] No running work order found');
|
|
}
|
|
return [null, null, null, null];
|
|
}'''
|
|
|
|
if old_restore in func:
|
|
func = func.replace(old_restore, new_restore)
|
|
node['func'] = func
|
|
print("✓ Updated restore-query handler to set status to RUNNING")
|
|
else:
|
|
print("✗ Could not find exact restore-query handler - may have been modified")
|
|
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")
|