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

165 lines
5.0 KiB
Python

#!/usr/bin/env python3
import json
import uuid
# Read flows.json
with open('/home/mdares/.node-red/flows.json', 'r') as f:
flows = json.load(f)
# Read KPI calculation function
with open('/home/mdares/.node-red/kpi_calculation_function.js', 'r') as f:
kpi_code = f.read()
# Find main tab and Home template
main_tab_id = None
home_template_id = None
for node in flows:
if node.get('type') == 'tab' and node.get('label') == 'Flow 1':
main_tab_id = node['id']
if node.get('id') == '1821c4842945ecd8':
home_template_id = node['id']
print(f"Main tab: {main_tab_id}")
print(f"Home template: {home_template_id}")
# Create UUIDs for new nodes
kpi_timer_id = str(uuid.uuid4()).replace('-', '')[:16]
kpi_function_id = str(uuid.uuid4()).replace('-', '')[:16]
# Create timer node (triggers every 5 seconds)
kpi_timer = {
"id": kpi_timer_id,
"type": "inject",
"z": main_tab_id,
"name": "KPI Timer (5s)",
"props": [],
"repeat": "5",
"crontab": "",
"once": True,
"onceDelay": "2",
"topic": "",
"x": 150,
"y": 600,
"wires": [[kpi_function_id]]
}
# Create KPI calculation function
kpi_function = {
"id": kpi_function_id,
"type": "function",
"z": main_tab_id,
"name": "KPI Calculation",
"func": kpi_code,
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 600,
"wires": [[home_template_id]] if home_template_id else [[]]
}
# Add nodes to flows
flows.extend([kpi_timer, kpi_function])
print(f"\n✅ Added KPI calculation:")
print(f" - Timer node (triggers every 5s)")
print(f" - KPI calculation function")
print(f" - Wired to Home template")
# ============================================================================
# FIX START/STOP in Work Order buttons function
# ============================================================================
# The issue is our enhanced version is too complex for START/STOP
# Let's simplify it to just set trackingEnabled like the original
simplified_start_stop = '''
case "start": {
// START/RESUME button clicked from Home dashboard
// Enable tracking of cycles for the active work order
const now = Date.now();
const activeOrder = global.get("activeWorkOrder");
// If no productionStartTime, initialize it
if (!global.get("productionStartTime")) {
global.set("productionStartTime", now);
global.set("operatingTime", 0);
global.set("downtime", 0);
}
global.set("trackingEnabled", true);
global.set("lastUpdateTime", now);
node.warn("[START] Production tracking enabled");
return [null, null, null, null, null];
}
case "stop": {
// Manual STOP button clicked from Home dashboard
// Disable tracking but keep work order active
global.set("trackingEnabled", false);
node.warn("[STOP] Production tracking disabled");
return [null, null, null, null, null];
}'''
# Find and update Work Order buttons function
for node in flows:
if node.get('id') == '9bbd4fade968036d':
func_code = node.get('func', '')
# Find the start/stop cases and replace them
# Look for the pattern from "case \"start\":" to the closing brace before next case
import re
# Replace start case
start_pattern = r'case "start":\s*\{[^}]*?\n\s*return \[null, null, null, null, null\];\s*\}'
func_code = re.sub(start_pattern, '''case "start": {
// START/RESUME button clicked from Home dashboard
const now = Date.now();
if (!global.get("productionStartTime")) {
global.set("productionStartTime", now);
global.set("operatingTime", 0);
global.set("downtime", 0);
}
global.set("trackingEnabled", true);
global.set("lastUpdateTime", now);
node.warn("[START] Production tracking enabled");
return [null, null, null, null, null];
}''', func_code, flags=re.DOTALL)
# Replace stop case
stop_pattern = r'case "stop":\s*\{[^}]*?\n\s*return \[null, null, null, null, null\];\s*\}'
func_code = re.sub(stop_pattern, '''case "stop": {
// Manual STOP button clicked from Home dashboard
global.set("trackingEnabled", false);
node.warn("[STOP] Production tracking disabled");
return [null, null, null, null, null];
}''', func_code, flags=re.DOTALL)
node['func'] = func_code
print(f"\n✅ Simplified START/STOP in Work Order buttons")
print(" - Removed complex session logic from START/STOP")
print(" - Now just enables/disables tracking")
# Write updated flows
with open('/home/mdares/.node-red/flows.json', 'w') as f:
json.dump(flows, f, indent=4)
print("\n✅ All fixes applied!")
print("\n📝 Changes:")
print(" 1. Added KPI calculation (timer + function)")
print(" 2. Fixed START/STOP buttons (simplified)")
print(" 3. KPI updates sent to Home every 5 seconds")
print("\nRestart Node-RED to apply changes.")