forked from SnehalRay/NeuralLlamas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketel.js
More file actions
134 lines (124 loc) · 3.58 KB
/
socketel.js
File metadata and controls
134 lines (124 loc) · 3.58 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
// Create a new WebSocket connection
const ws = new WebSocket('ws://localhost:8765');
// EEG data buffers
const alphaData = [];
const betaData = [];
const thetaData = [];
const deltaData = [];
const gammaData = [];
const concentrationData = [];
const labels = []; // Timestamps for the x-axis
// Create a real-time Chart.js line graph
const ctx = document.getElementById('eegChart').getContext('2d');
const eegChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Alpha',
data: alphaData,
borderColor: 'red',
borderWidth: 2,
fill: false,
},
{
label: 'Beta',
data: betaData,
borderColor: 'blue',
borderWidth: 2,
fill: false,
},
{
label: 'Theta',
data: thetaData,
borderColor: 'green',
borderWidth: 2,
fill: false,
},
{
label: 'Delta',
data: deltaData,
borderColor: 'purple',
borderWidth: 2,
fill: false,
},
{
label: 'Gamma',
data: gammaData,
borderColor: 'orange',
borderWidth: 2,
fill: false,
},
{
label: 'Concentration',
data: concentrationData,
borderColor: 'black',
borderWidth: 2,
fill: false,
},
],
},
options: {
scales: {
x: {
title: {
display: true,
text: 'Time (s)',
},
},
y: {
title: {
display: true,
text: 'EEG Value',
},
beginAtZero: true,
},
},
},
});
// WebSocket event handlers
ws.onopen = () => {
console.log('Connected to WebSocket server.');
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
const alpha = data.alpha;
const beta = data.beta;
const theta = data.theta;
const delta = data.delta;
const gamma = data.gamma;
const concentration = data.concentration;
// Add data to buffers
const timestamp = new Date().toLocaleTimeString();
labels.push(timestamp);
alphaData.push(alpha);
betaData.push(beta);
thetaData.push(theta);
deltaData.push(delta);
gammaData.push(gamma);
concentrationData.push(concentration);
// Limit the number of data points to display (e.g., last 50 points)
if (labels.length > 500000000000000) {
labels.shift();
alphaData.shift();
betaData.shift();
thetaData.shift();
deltaData.shift();
gammaData.shift();
concentrationData.shift();
}
// Update the chart
eegChart.update();
console.log(`Alpha: ${alpha.toFixed(2)}, Beta: ${beta.toFixed(2)}, Theta: ${theta.toFixed(2)}, Delta: ${delta.toFixed(2)}, Gamma: ${gamma.toFixed(2)}, Concentration: ${concentration.toFixed(2)}`);
} catch (error) {
console.error('Error parsing message:', error);
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('WebSocket connection closed.');
};