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

164 lines
4.6 KiB
Python

#!/usr/bin/env python3
import json
# Read the function code files
with open('/tmp/anomaly_detector_function.js', 'r') as f:
anomaly_detector_code = f.read()
with open('/tmp/event_logger_function.js', 'r') as f:
event_logger_code = f.read()
# Load flows.json
with open('/home/mdares/.node-red/flows.json', 'r') as f:
flows = json.load(f)
# Find the main tab and Machine Cycles node
tab_id = None
machine_cycles_node = None
for node in flows:
if node.get('type') == 'tab' and not tab_id:
tab_id = node['id']
if node.get('id') == '0d023d87a13bf56f':
machine_cycles_node = node
if not tab_id or not machine_cycles_node:
print("✗ Could not find required nodes")
exit(1)
# ============================================================
# 1. CREATE ANOMALY DETECTOR FUNCTION NODE
# ============================================================
anomaly_detector_node = {
"id": "anomaly_detector_node_id",
"type": "function",
"z": tab_id,
"name": "Anomaly Detector",
"func": anomaly_detector_code,
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 850,
"y": 300,
"wires": [["event_logger_node_id"]]
}
# ============================================================
# 2. CREATE EVENT LOGGER FUNCTION NODE
# ============================================================
event_logger_node = {
"id": "event_logger_node_id",
"type": "function",
"z": tab_id,
"name": "Event Logger",
"func": event_logger_code,
"outputs": 2, # Output 1: DB inserts, Output 2: UI updates
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1050,
"y": 300,
"wires": [
["anomaly_mysql_node_id"], # Output 1: to MySQL
[] # Output 2: UI updates (will wire to Home tab later)
]
}
# ============================================================
# 3. CREATE MYSQL NODE FOR ANOMALY EVENTS
# ============================================================
anomaly_mysql_node = {
"id": "anomaly_mysql_node_id",
"type": "mysql",
"z": tab_id,
"mydb": "00d8ad2b0277f906",
"name": "Anomaly Events DB",
"x": 1270,
"y": 280,
"wires": [["anomaly_db_debug_node_id"]]
}
# ============================================================
# 4. CREATE DEBUG NODE
# ============================================================
anomaly_debug_node = {
"id": "anomaly_db_debug_node_id",
"type": "debug",
"z": tab_id,
"name": "Anomaly DB Result",
"active": True,
"tosidebar": True,
"console": False,
"tostatus": False,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1490,
"y": 280,
"wires": []
}
# ============================================================
# 5. CREATE SPLIT NODE (to handle array of DB inserts)
# ============================================================
split_node = {
"id": "anomaly_split_node_id",
"type": "split",
"z": tab_id,
"name": "Split DB Inserts",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": False,
"addname": "",
"x": 1270,
"y": 240,
"wires": [["anomaly_mysql_node_id"]]
}
# Update Event Logger to send to split node instead
event_logger_node["wires"][0] = ["anomaly_split_node_id"]
# ============================================================
# 6. WIRE ANOMALY DETECTOR TO MACHINE CYCLES OUTPUT 2
# ============================================================
# Machine Cycles output 2 already goes to Calculate KPIs
# Add Anomaly Detector as an additional target
if len(machine_cycles_node["wires"]) > 1:
machine_cycles_node["wires"][1].append("anomaly_detector_node_id")
print("✓ Wired Anomaly Detector to Machine Cycles output 2")
else:
print("✗ Could not wire to Machine Cycles")
# ============================================================
# 7. ADD ALL NEW NODES TO FLOWS
# ============================================================
flows.extend([
anomaly_detector_node,
event_logger_node,
split_node,
anomaly_mysql_node,
anomaly_debug_node
])
# Save flows.json
with open('/home/mdares/.node-red/flows.json', 'w') as f:
json.dump(flows, f, indent=4)
print("✓ Added Anomaly Detection nodes to flows.json")
print(" - Anomaly Detector function")
print(" - Event Logger function")
print(" - Split node (for DB inserts)")
print(" - MySQL node (Anomaly Events DB)")
print(" - Debug node")
print("")
print("✓ Wired into Machine Cycles flow")
print(" Machine Cycles → Anomaly Detector → Event Logger → MySQL")