-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (148 loc) · 6.49 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Digital Twin Data App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Digital Twin - Accelerometer Data</h1>
<div id="accelerometerData">
<p>X: <span id="xValue">-</span></p>
<p>Y: <span id="yValue">-</span></p>
<p>Z: <span id="zValue">-</span></p>
</div>
<button id="toggleFetching" class="fetch-button">Start Fetching Data</button>
<canvas id="accelerometerChart" width="400" height="100"></canvas>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>let isFetchingStarted = false;
let x, y, z;
let timestampCounter = 0;
let isThresholdExceeded = false; // New variable to track threshold exceeding
// Create an empty array to store accelerometer data for chart
let accelerometerChartData = [];
// Create the chart context
const ctx = document.getElementById('accelerometerChart').getContext('2d');
const accelerometerChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: 'X',
borderColor: 'rgb(255, 99, 132)',
data: [],
cubicInterpolationMode: 'monotone',
},
{
label: 'Y',
borderColor: 'rgb(75, 192, 192)',
data: [],
cubicInterpolationMode: 'monotone',
},
{
label: 'Z',
borderColor: 'rgb(54, 162, 235)',
data: [],
cubicInterpolationMode: 'monotone',
},
],
},
options: {
animation: {
duration: 0, // Turn off animation
},
scales: {
x: {
type: 'linear',
position: 'bottom',
title: {
display: true,
text: 'Time Interval', // Label for X-axis
color: 'black', // Optional: Customize color
font: {
size: 14, // Optional: Customize font size
weight: 'bold' // Optional: Customize font weight
}
},
ticks: {
stepSize: 1, // Adjust the step size based on your preference
},
},
y: {
type: 'linear',
position: 'left',
title: {
display: true,
text: 'Accelerometer Position', // Label for Y-axis
color: 'black', // Optional: Customize color
font: {
size: 14, // Optional: Customize font size
weight: 'bold' // Optional: Customize font weight
}
},
},
},
},
});
// Function to update the chart with new data
const updateChart = () => {
accelerometerChart.data.datasets[0].data = accelerometerChartData.map(entry => ({ x: entry.timestamp, y: entry.x }));
accelerometerChart.data.datasets[1].data = accelerometerChartData.map(entry => ({ x: entry.timestamp, y: entry.y }));
accelerometerChart.data.datasets[2].data = accelerometerChartData.map(entry => ({ x: entry.timestamp, y: entry.z }));
// Limit the number of data points to keep the chart clean
const maxDataPoints = 20; // Adjust the maximum data points based on your preference
if (accelerometerChartData.length > maxDataPoints) {
accelerometerChartData.shift();
}
// Update the chart
accelerometerChart.update();
// Log chart data for debugging
console.log('Chart Data:', accelerometerChart.data);
// Update every 2s (2000) or 0.2 (200)
setTimeout(updateAccelerometerData, 200);
};
const updateAccelerometerData = () => {
if (isFetchingStarted) {
$.ajax({
url: '/api/accelerometer',
method: 'GET',
dataType: 'json',
success: (data) => {
// Update the values
x = data.x;
y = data.y;
z = data.z;
// Log values for debugging
console.log('X:', x, 'Y:', y, 'Z:', z);
// Update the displayed values
$('#xValue').text(x);
$('#yValue').text(y);
$('#zValue').text(z);
// Check if any value is below -2 or above 2
if (x < -2 || x > 2 || y < -2 || y > 2 || z < -2 || z > 2) {
isThresholdExceeded = true; // Set threshold exceeded flag
alert('Warning: Accelerometer threshold exceeded!');
}
// Update the chart with new data
accelerometerChartData.push({ timestamp: timestampCounter++, x, y, z });
updateChart();
},
error: (xhr, status, error) => {
console.error('Error fetching accelerometer data:', xhr, status, error);
setTimeout(updateAccelerometerData, 200);
}
});
}
};
$('#toggleFetching').click(() => {
isFetchingStarted = !isFetchingStarted;
const buttonLabel = isFetchingStarted ? 'Stop Fetching Data' : 'Start Fetching Data';
$('#toggleFetching').text(buttonLabel);
if (isFetchingStarted) {
updateAccelerometerData();
}
});</script>
</body>
</html>