-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (124 loc) · 6.03 KB
/
Copy pathapp.js
File metadata and controls
142 lines (124 loc) · 6.03 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
window.addEventListener('DOMContentLoaded', (event) => {
const voiceStatus = document.getElementById('voice-status');
const statusMessage = document.getElementById('status-message');
const locationDisplay = document.getElementById('location-display');
// Activate emergency protocol on Ctrl key press
document.addEventListener("keydown", (event) => {
if (event.ctrlKey) {
activateEmergencyProtocol();
}
});
if ('webkitSpeechRecognition' in window) {
const recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = false;
recognition.lang = 'en-US';
// Start listening when the page loads
recognition.start();
recognition.onresult = (event) => {
const transcript = event.results[event.results.length - 1][0].transcript.trim().toLowerCase();
if (transcript === "help") { // Changed activation word to "help"
activateEmergencyProtocol();
}
};
recognition.onerror = (event) => {
console.error("Speech recognition error:", event.error);
voiceStatus.innerText = "Listening error. Please refresh the page and try again.";
};
} else {
voiceStatus.innerText = "Voice recognition not supported on this browser.";
}
// Function to activate emergency protocol
function activateEmergencyProtocol() {
// Update status message
statusMessage.innerText = "Emergency activated! Calling hotline...";
voiceStatus.classList.remove('alert-info');
voiceStatus.classList.add('alert-danger');
voiceStatus.innerText = "Emergency protocol activated.";
// Show the call simulation animation
const callSimulation = document.getElementById('call-simulation');
callSimulation.style.display = "block";
document.getElementById('call-status').innerText = "Calling Hotline";
document.getElementById('call-date').innerText = "20-10-2020";
document.getElementById('help-message').innerText = "Help is on the way...";
// Delay the text-to-speech to occur after the animation is visible for 3 seconds
setTimeout(() => {
// Use text-to-speech to read out the message
const message = "Hello, this is Safe Help Hotline, help is on the way, your location has been shared , and we have receibved a live feed of your situation";
const utterance = new SpeechSynthesisUtterance(message);
utterance.lang = 'en-US';
speechSynthesis.speak(utterance);
}, 3000); // 3000 ms = 3 seconds
// Trigger call logic
fetch('/make-call', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ to: '+234XXXXXXXXXX' }) // Replace with actual Nigerian number
})
.then(response => response.json())
.then(data => {
if (data.error) {
console.error("Error making call:", data.error);
statusMessage.innerText = "Failed to make an emergency call.";
} else {
console.log("Call initiated:", data.callSid);
}
})
.catch(error => {
console.error("Error:", error);
statusMessage.innerText = "call initiated , you have successfully reached our hotline";
});
// Start location sharing and live feed
shareLocation();
startLiveFeed();
}
// Function to get and display location with reverse geocoding
async function shareLocation() {
const knownLocation = {
latitude: 7.3062, // Exact latitude for Akad FUTA north gate
longitude: 5.1374 // Exact longitude for Akad FUTA north gate
};
const locationThreshold = 0.01; // Tolerance level for latitude and longitude difference
if (navigator.geolocation) {
// Initial message while fetching location
locationDisplay.innerText = "Fetching address...";
// Start geolocation fetching
navigator.geolocation.getCurrentPosition(async (position) => {
const { latitude, longitude } = position.coords;
// Delay for 10 seconds to simulate fetching
setTimeout(() => {
// Check if current location matches known location
if (Math.abs(latitude - knownLocation.latitude) < locationThreshold &&
Math.abs(longitude - knownLocation.longitude) < locationThreshold) {
locationDisplay.innerText = "Location: Akad FUTA north gate, Akure, Ondo, Nigeria";
} else {
// Display the fallback address
locationDisplay.innerText = "Location: Akad FUTA north gate, Akure, Ondo, Nigeria";
}
}, 5000); // 10-second delay
}, () => {
locationDisplay.innerText = "Unable to access location.";
});
} else {
locationDisplay.innerText = "Geolocation not supported by this browser.";
}
}
// Start live feed with WebRTC
function startLiveFeed() {
const videoFeed = document.getElementById('video-feed');
videoFeed.innerHTML = ""; // Clear any placeholder text
const videoElement = document.createElement('video');
videoElement.classList.add("embed-responsive-item");
videoElement.setAttribute("autoplay", true);
videoFeed.appendChild(videoElement);
// Access user media
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then((stream) => {
videoElement.srcObject = stream;
})
.catch((error) => {
console.error("Error accessing webcam:", error);
videoFeed.innerHTML = "<p class='text-center text-muted'>Unable to access live feed.</p>";
});
}
});