-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflows.json
More file actions
223 lines (223 loc) · 11.4 KB
/
flows.json
File metadata and controls
223 lines (223 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
[
{
"id": "b9aab144d65ba32b",
"type": "tab",
"label": "EEG",
"disabled": false,
"info": "",
"env": []
},
{
"id": "db00ab545ae2309d",
"type": "mqtt in",
"z": "b9aab144d65ba32b",
"name": "Listen EEG Data",
"topic": "eeg/data",
"qos": "2",
"datatype": "json",
"broker": "mqtt_broker_config",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 100,
"y": 260,
"wires": [
[
"027fe2f488247204",
"d71593e95f41b114"
]
]
},
{
"id": "027fe2f488247204",
"type": "debug",
"z": "b9aab144d65ba32b",
"name": "Raw MQTT",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 290,
"y": 220,
"wires": []
},
{
"id": "d71593e95f41b114",
"type": "change",
"z": "b9aab144d65ba32b",
"name": "Store Original Segment",
"rules": [
{
"t": "move",
"p": "payload",
"pt": "msg",
"to": "original_data",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 330,
"y": 260,
"wires": [
[
"ae43351bb942bbce"
]
]
},
{
"id": "ae43351bb942bbce",
"type": "function",
"z": "b9aab144d65ba32b",
"name": "Format For API",
"func": "/**\n * Node-RED Helper Function - Format EEG Data for Prediction API\n */\n\n// Basic validation only - minimize processing\nif (!msg || !msg.original_data) {\n node.error(\"Missing original data\");\n node.status({fill:\"red\", shape:\"dot\", text:\"No data\"});\n return null;\n}\n\ntry {\n // Log the original data structure for debugging\n if (msg.original_data) {\n node.warn(`Original data keys: ${Object.keys(msg.original_data).join(', ')}`);\n }\n \n // Get the EEG data array from the original_data\n let inputData = msg.original_data.data;\n let processedData = [];\n const MAX_POINTS = 256;\n \n // Debug the data type and structure\n node.warn(`Data type: ${typeof inputData}, IsArray: ${Array.isArray(inputData)}, Length: ${inputData ? inputData.length : 0}`);\n \n // Check if inputData is a valid array\n if (inputData && Array.isArray(inputData)) {\n // Convert every value to a number to ensure proper format for API\n processedData = inputData.map(val => {\n if (typeof val === 'string') {\n return parseFloat(val) || 0; // Convert string to number or 0 if NaN\n } else if (typeof val === 'number') {\n return val; // Already a number\n } else {\n return 0; // Default for other types\n }\n });\n \n // Limit to MAX_POINTS if needed\n if (processedData.length > MAX_POINTS) {\n processedData = processedData.slice(0, MAX_POINTS);\n }\n \n // Quick sanity check on first few values\n node.warn(`After conversion: First 3 values: [${processedData.slice(0, 3).join(', ')}], types: [${processedData.slice(0, 3).map(x => typeof x).join(', ')}]`);\n } else {\n node.warn(\"Input data is not a valid array or is empty\");\n processedData = [];\n }\n\n // Final validation - ensure all elements are numbers\n if (!processedData.every(item => typeof item === 'number')) {\n node.error(\"Not all data items could be converted to numbers\");\n // Force conversion of any non-numeric values\n processedData = processedData.map(val => typeof val === 'number' ? val : 0);\n }\n\n // Create payload for prediction API\n msg.payload = {\n \"eeg_data\": processedData,\n \"segment_id\": msg.original_data.segment_id || 0\n };\n\n // Set headers\n msg.headers = {\"Content-Type\": \"application/json\"};\n\n // Keep the increased timeout for larger data sets\n msg.requestTimeout = 10000; // 10 seconds\n\n node.status({fill:\"green\", shape:\"dot\", text:`Data: ${processedData.length} points`});\n\n} catch (e) {\n // Provide detailed error handling\n node.error(`Error formatting data: ${e.message}`);\n node.warn(`Error stack: ${e.stack}`);\n msg.payload = {\"eeg_data\": [], \"segment_id\": 0};\n msg.headers = {\"Content-Type\": \"application/json\"};\n node.status({fill:\"red\", shape:\"ring\", text:\"Format Error\"});\n}\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 480,
"y": 360,
"wires": [
[
"0cb76704767887c7"
]
]
},
{
"id": "0cb76704767887c7",
"type": "http request",
"z": "b9aab144d65ba32b",
"name": "Call Prediction API",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "http://localhost:5000/predict",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": true,
"headers": [],
"x": 610,
"y": 440,
"wires": [
[
"a2e77cfc7c09df2a"
]
]
},
{
"id": "a2e77cfc7c09df2a",
"type": "function",
"z": "b9aab144d65ba32b",
"name": "Handle HTTP Error",
"func": "/**\n * Node-RED Function - Handle HTTP Errors\n * Place this after the HTTP request node\n */\n\n// Check if the response indicates an error\nif (msg.statusCode >= 400) {\n let errorPayload = msg.payload;\n // Try to parse if it's a string (like HTML error page)\n if (typeof errorPayload === 'string') {\n node.warn(`Received non-JSON error response (Status ${msg.statusCode}): ${errorPayload.substring(0, 100)}...`);\n // Create a standard error object\n msg.payload = { error: `HTTP Error ${msg.statusCode}`, details: 'Non-JSON response received' };\n } else {\n node.error(`HTTP error: ${msg.statusCode} - ${JSON.stringify(errorPayload)}`);\n }\n node.status({fill:\"red\", shape:\"ring\", text:`Error ${msg.statusCode}`});\n msg.error = `HTTP Error ${msg.statusCode}`;\n} \n// Check for connection errors (like timeout or DNS issues)\nelse if (msg.error) {\n node.error(`Request error: ${msg.error}`);\n node.status({fill:\"red\", shape:\"ring\", text:\"Request failed\"});\n // Ensure payload reflects the error for downstream nodes\n msg.payload = { error: msg.error.message || 'Request failed' };\n} \n// Handle 'no response from server' specifically\nelse if (typeof msg === 'string' && msg.includes('no response')) {\n node.error(`Request failed: ${msg}`);\n node.status({fill:\"red\", shape:\"ring\", text:\"Timeout/No Response\"});\n // Create a structured error message\n msg = { \n statusCode: 504, // Gateway Timeout\n error: msg,\n payload: { error: msg },\n original_data: msg.original_data // Preserve original data if possible\n };\n} \n// Success case\nelse {\n node.status({fill:\"green\", shape:\"dot\", text:\"Request OK\"});\n}\n\n// Always pass through the message\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 770,
"y": 520,
"wires": [
[
"64562a588f62fe04"
]
]
},
{
"id": "64562a588f62fe04",
"type": "function",
"z": "b9aab144d65ba32b",
"name": "Combine Results",
"func": "/**\n * Node-RED Function - Combine Results from Prediction API\n */\n\n// Initial debugging info\nnode.warn(`Received payload: ${JSON.stringify(msg.payload || {})}`);\n\n// Initialize with defaults\nlet result = {\n segment_id: 0,\n data: [],\n prediction: 0.5,\n status: \"NORMAL\"\n};\n\n// Recover original data if available\nif (msg.original_data) {\n if (msg.original_data.segment_id !== undefined) {\n result.segment_id = msg.original_data.segment_id;\n }\n if (Array.isArray(msg.original_data.data)) {\n result.data = msg.original_data.data;\n }\n}\n\n// Only process payload if it exists and has no errors\nif (msg.payload && !msg.error) {\n // Check for numeric prediction value\n if (typeof msg.payload.prediction === 'number') {\n result.prediction = msg.payload.prediction;\n } \n // Handle string representation of prediction value\n else if (msg.payload.prediction === \"1\" || msg.payload.prediction === \"0\") {\n result.prediction = parseInt(msg.payload.prediction, 10);\n }\n \n // Add status based on prediction value\n if (typeof msg.payload.status === 'string') {\n result.status = msg.payload.status;\n } else {\n result.status = result.prediction > 0.5 ? \"CRISE\" : \"NORMAL\";\n }\n}\n\n// Set result as payload\nmsg.payload = result;\n\n// Log the final status\nnode.warn(`Sending final result: segment_id=${result.segment_id}, prediction=${result.prediction}, status=${result.status}`);\n\n// Debug status information\nnode.status({fill:\"green\", shape:\"dot\", text:`Prediction: ${result.prediction}, Status: ${result.status}`});\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 890,
"y": 600,
"wires": [
[
"0f0b295929382fbe",
"33acb0a07572caa5"
]
]
},
{
"id": "0f0b295929382fbe",
"type": "debug",
"z": "b9aab144d65ba32b",
"name": "Final Output",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1090,
"y": 580,
"wires": []
},
{
"id": "33acb0a07572caa5",
"type": "websocket out",
"z": "b9aab144d65ba32b",
"name": "Send to Web App",
"server": "websocket_listener_config",
"client": "",
"x": 1110,
"y": 620,
"wires": []
},
{
"id": "mqtt_broker_config",
"type": "mqtt-broker",
"name": "Local Mosquitto",
"broker": "localhost",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "websocket_listener_config",
"type": "websocket-listener",
"path": "/ws/eeg",
"wholemsg": "false"
}
]