-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (76 loc) · 2.84 KB
/
script.js
File metadata and controls
96 lines (76 loc) · 2.84 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
//* ================== FOOD ================== */
let foods = [];
let map, markers = [];
function addFood() {
const name = document.getElementById("foodName").value;
const qty = document.getElementById("foodQty").value;
const minutes = parseInt(document.getElementById("expiry").value);
if (!name || !qty || !minutes) {
document.getElementById("foodStatus").innerText = "Fill all fields";
return;
}
const expiryTime = Date.now() + minutes * 60000;
foods.push({ name, qty, expiryTime });
document.getElementById("foodStatus").innerText = "Food uploaded successfully";
updateFoodList();
if (!map) initMap();
}
function updateFoodList() {
const now = Date.now();
const listDiv = document.getElementById("foodList");
listDiv.innerHTML = "";
foods = foods.filter(f => f.expiryTime > now);
if (foods.length === 0) { listDiv.innerText = "No food available"; return; }
foods.forEach(f => {
const remaining = Math.ceil((f.expiryTime - now) / 60000);
listDiv.innerHTML += `🍱 ${f.name} (${f.qty}) - Fresh for ${remaining} min<br>`;
});
setTimeout(updateFoodList, 60000);
}
function initMap() {
navigator.geolocation.getCurrentPosition(pos => {
map = L.map("map").setView([pos.coords.latitude, pos.coords.longitude], 14);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
foods.forEach(f => {
const marker = L.marker([pos.coords.latitude, pos.coords.longitude]).addTo(map)
.bindPopup(`🍱 ${f.name} (${f.qty})`);
markers.push(marker);
});
});
}
/* ================== WASTE ================== */
function startCamera() {
const video = document.getElementById("camera");
const status = document.getElementById("wasteStatus");
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
video.srcObject = stream;
status.innerText = "Camera started. Select waste type manually.";
})
.catch(() => status.innerText = "Camera permission denied");
}
function markDump() {
navigator.geolocation.getCurrentPosition(pos => {
document.getElementById("dumpStatus").innerText =
`Dump location: (${pos.coords.latitude}, ${pos.coords.longitude})`;
});
}
/* ================== WATER ================== */
let user = {};
function registerUser() {
user.name = document.getElementById("owner").value;
user.city = document.getElementById("city").value;
if(!user.name || !user.city) {
regStatus.innerText = "Fill all fields";
return;
}
regStatus.innerText = `Registered: ${user.name}, ${user.city}`;
}
function predictRain() {
if(!user.city) { rainStatus.innerText="Register first"; return;}
rainStatus.innerText = `Rain predicted in ${user.city} (demo data)`;
}
function checkTank() {
if(!user.name) { tankStatus.innerText="Register first"; return;}
tankStatus.innerText = `Tank at ${user.name}'s location is optimal`;
}