#!/usr/bin/env python3 import json # Read flows.json with open('/home/mdares/.node-red/flows.json', 'r') as f: flows = json.load(f) # Find Home Template for node in flows: if node.get('id') == '1821c4842945ecd8': template_code = node.get('format', '') # Find where to insert the KPI handler (after scope.$watch('msg', function(msg) {) # Look for the pattern after machineStatus handler kpi_handler = ''' if (msg.topic === 'kpiUpdate') { const kpis = msg.payload || {}; window.kpiOeePercent = Number(kpis.oee) || 0; window.kpiAvailabilityPercent = Number(kpis.availability) || 0; window.kpiPerformancePercent = Number(kpis.performance) || 0; window.kpiQualityPercent = Number(kpis.quality) || 0; scope.renderDashboard(); return; } ''' # Insert after machineStatus handler insert_point = template_code.find("if (msg.topic === 'activeWorkOrder') {") if insert_point > 0: template_code = template_code[:insert_point] + kpi_handler + template_code[insert_point:] node['format'] = template_code print("āœ… Added KPI update handler to Home Template") print(" - Listens for 'kpiUpdate' topic") print(" - Updates window.kpiOeePercent, etc.") print(" - Calls renderDashboard() to update UI") else: print("āŒ Could not find insertion point in Home Template") # Write updated flows with open('/home/mdares/.node-red/flows.json', 'w') as f: json.dump(flows, f, indent=4) print("\nāœ… Home Template updated to receive KPI updates")