-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
201 lines (165 loc) · 7.26 KB
/
App.js
File metadata and controls
201 lines (165 loc) · 7.26 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
import React, { useEffect, useRef, useState } from 'react';
import { WebView } from 'react-native-webview';
import { BackHandler, StyleSheet } from 'react-native';
import { accelerometer, gyroscope } from 'react-native-sensors';
const WebViewScreen = () => {
const webViewRef = useRef(null);
const [currentUrl, setCurrentUrl] = useState('');
const [speed, setSpeed] = useState(0);
const [cornering, setCornering] = useState(0);
const [braking, setBraking] = useState(0);
const [calibration, setCalibration] = useState({ x: 0, y: 0, z: 0 });
const NOISE_THRESHOLD = 1.0; // Ignore small fluctuations
const MOVING_AVERAGE_COUNT = 20;
let accBuffer = [];
let gyroBuffer = [];
// Handle Back Button
const handleBackPress = () => {
if (currentUrl.endsWith('owner_profile.php' || 'driver_profile.php')) {
BackHandler.exitApp();
return true;
} else if (webViewRef.current) {
webViewRef.current.goBack();
return true;
}
return false;
};
useEffect(() => {
const backHandler = BackHandler.addEventListener('hardwareBackPress', handleBackPress);
return () => backHandler.remove();
}, [currentUrl]);
// Calibration - Get average of 100 readings
useEffect(() => {
let calibrationReadings = [];
const calibrationSubscription = accelerometer.subscribe(({ x, y, z }) => {
calibrationReadings.push({ x, y, z });
if (calibrationReadings.length >= 100) {
const avgX = calibrationReadings.reduce((sum, val) => sum + val.x, 0) / calibrationReadings.length;
const avgY = calibrationReadings.reduce((sum, val) => sum + val.y, 0) / calibrationReadings.length;
const avgZ = calibrationReadings.reduce((sum, val) => sum + val.z, 0) / calibrationReadings.length;
setCalibration({ x: avgX, y: avgY, z: avgZ });
calibrationSubscription.unsubscribe();
}
});
return () => calibrationSubscription.unsubscribe();
}, []);
// Moving Average Filter Function
const movingAverage = (buffer, newValue) => {
buffer.push(newValue);
if (buffer.length > MOVING_AVERAGE_COUNT) buffer.shift();
return buffer.reduce((sum, val) => sum + val, 0) / buffer.length;
};
useEffect(() => {
const accSubscription = accelerometer.subscribe(({ x, y, z }) => {
const adjustedX = (x - calibration.x)*4;
const adjustedY = (y - calibration.y)*4;
const adjustedZ = (z - calibration.z)*4;
const filteredX = movingAverage(accBuffer, adjustedX);
const filteredY = movingAverage(accBuffer, adjustedY);
const filteredZ = movingAverage(accBuffer, adjustedZ);
const finalX = Math.abs(filteredX) > NOISE_THRESHOLD ? filteredX : 0;
const finalY = Math.abs(filteredY) > NOISE_THRESHOLD ? filteredY : 0;
const finalZ = Math.abs(filteredZ) > NOISE_THRESHOLD ? filteredZ : 0;
setSpeed(Math.sqrt(finalX ** 2 + finalY ** 2 + finalZ ** 2).toFixed(2));
setBraking(Math.abs(finalZ).toFixed(2));
});
const gyroSubscription = gyroscope.subscribe(({ x, y, z }) => {
const smoothGyro = movingAverage(gyroBuffer, Math.abs(x) + Math.abs(y) + Math.abs(z));
setCornering(smoothGyro.toFixed(2));
});
return () => {
accSubscription.unsubscribe();
gyroSubscription.unsubscribe();
};
}, [calibration]);
const calculateScore = () => {
const w1 = 10; // Weight for Speed
const w2 = 10; // Weight for Cornering
const w3 = 10; // Weight for Braking
return Math.max(0, Math.min(100, Math.round(100 - (w1 * speed) - (w2 * cornering) - (w3 * braking))));
};
useEffect(() => {
if (currentUrl.includes('dashboard.html') && webViewRef.current) {
// Function to update score every 10 seconds
const interval = setInterval(() => {
const score = calculateScore(); // Calculate the score every 10 sec
const script = `
(function updateDashboard() {
const score = ${score};
document.getElementById('speedText').innerText = score + '%';
let color;
if (score < 25) color = 'red';
else if (score < 50) color = 'yello';
else if (score < 75) color = 'orange';
else color = 'green';
document.getElementById('scoreBox').style.background =
'conic-gradient(' + color + ' ' + score + '%, #26374D ' + score + '%)';
})();
`;
webViewRef.current.injectJavaScript(script);
}, 5000); // Runs every 5 seconds
return () => clearInterval(interval); // Cleanup interval on unmount
}
}, [currentUrl]); // Run only when the page changes
// Effect to update speed, cornering, and braking continuously
useEffect(() => {
if (currentUrl.includes('dashboard.html') && webViewRef.current) {
const script = `
(function updateRealTimeData() {
const speed = ${speed};
const cornering = ${cornering};
const braking = ${braking};
document.getElementById('speedBox').innerText = + speed + '%';
document.getElementById('corneringBox').innerText = 'Cornering: ' + cornering + '%';
document.getElementById('brakingBox').innerText = 'Braking: ' + braking + '%';
const updateBoxColor = (boxId, value) => {
let color;
if (value < 25) color = 'green';
else if (value < 50) color = 'orange';
else if (value < 75) color = 'yellow';
else color = 'red';
document.getElementById(boxId).style.background =
'linear-gradient(to top, ' + color + ' ' + value + '%, #26374D ' + value + '%)';
};
updateBoxColor('speedBox', speed);
updateBoxColor('corneringBox', cornering);
updateBoxColor('brakingBox', braking);
// Updating the Dashboard Page
document.getElementById('speedBox').innerText = 'Speed: ' + speed + '%';
document.getElementById('corneringBox').innerText = 'Cornering: ' + cornering + '%';
document.getElementById('brakingBox').innerText = 'Braking: ' + braking + '%';
// Storing values for the report page
localStorage.setItem('speed', speed);
localStorage.setItem('cornering', cornering);
localStorage.setItem('braking', braking);
})();
`;
webViewRef.current.injectJavaScript(script);
}
}, [speed, cornering, braking, currentUrl]);
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', handleBackPress);
return () => {
BackHandler.removeEventListener('hardwareBackPress', handleBackPress);
};
}, []);
return (
<WebView
ref={webViewRef}
originWhitelist={['*']}
source={{ uri: 'http://192.168.43.14/anim.html' }} // Replace with your server URL
style={styles.webview}
javaScriptEnabled={true}
geolocationEnabled={true}
domStorageEnabled={true}
mixedContentMode="always"
onNavigationStateChange={(navState) => setCurrentUrl(navState.url)}
/>
);
};
const styles = StyleSheet.create({
webview: {
flex: 1,
},
});
export default WebViewScreen;