Spaces:
Running
Running
merasabkuch
commited on
Commit
•
5981742
1
Parent(s):
95fcf2f
Update index.js
Browse files
index.js
CHANGED
@@ -27,54 +27,174 @@ app.use(cors());
|
|
27 |
var initialData = {};
|
28 |
let currentDeviceStates = {};
|
29 |
|
30 |
-
const fetchInitialData = async () => {
|
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 |
-
fetchInitialData();
|
78 |
|
79 |
|
80 |
|
|
|
27 |
var initialData = {};
|
28 |
let currentDeviceStates = {};
|
29 |
|
30 |
+
// const fetchInitialData = async () => {
|
31 |
+
// const devices = [];
|
32 |
+
// const rooms = [];
|
33 |
+
// const users = [];
|
34 |
|
35 |
+
// try {
|
36 |
+
// const deviceSnapshot = await db.collection('Devices').get();
|
37 |
+
// deviceSnapshot.forEach(doc => devices.push({ id: doc.id, ...doc.data() }));
|
38 |
|
39 |
+
// const roomsSnapshot = await db.collectionGroup('Rooms').get(); // Get all rooms across hostels
|
40 |
+
// roomsSnapshot.forEach(doc => rooms.push({ id: doc.id, ...doc.data(), hostelId: doc.ref.parent.parent.id })); // Include hostelId
|
41 |
|
42 |
+
// const usersSnapshot = await db.collection('Users').get();
|
43 |
+
// usersSnapshot.forEach(doc => users.push({ id: doc.id, ...doc.data() }));
|
44 |
|
45 |
+
// initialData = { devices, rooms, users };
|
46 |
|
47 |
+
// // console.log("Initial data fetched:", initialData);
|
48 |
+
// //for each hostel id mantain a list of rooms with devices pertaining to that hostel
|
49 |
|
50 |
+
// //setall to off
|
51 |
|
52 |
+
// initialData.devices.forEach(device => {
|
53 |
+
// device.state = false;
|
54 |
+
// } );
|
55 |
|
56 |
+
// initialData.rooms.forEach(room => {
|
57 |
+
// if (!currentDeviceStates[room.id]) {
|
58 |
+
// currentDeviceStates[room.id] = [];
|
59 |
+
// }
|
60 |
|
61 |
+
// const roomDevices = initialData.devices.filter(device => device.hostelId === room.hostelId);
|
62 |
+
// currentDeviceStates[room.id] = { ...room, devices: roomDevices };
|
63 |
+
// });
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
// } catch (error) {
|
69 |
+
// console.error("Error fetching initial data:", error);
|
70 |
+
// // return { devices: [], rooms: [], users: [] };
|
71 |
+
// initialData = { devices, rooms, users };
|
72 |
+
// }
|
73 |
+
// };
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
const fetchInitialData = async () => {
|
78 |
+
try {
|
79 |
+
const [deviceSnapshot, roomsSnapshot, usersSnapshot] = await Promise.all([
|
80 |
+
db.collection('Devices').get(),
|
81 |
+
db.collectionGroup('Rooms').get(),
|
82 |
+
db.collection('Users').get()
|
83 |
+
]);
|
84 |
+
|
85 |
+
const devices = deviceSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
|
86 |
+
const rooms = roomsSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data(), hostelId: doc.ref.parent.parent.id }));
|
87 |
+
const users = usersSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
|
88 |
+
|
89 |
+
initialData = { devices, rooms, users };
|
90 |
+
|
91 |
+
initialData.devices.forEach(device => {
|
92 |
+
device.state = false;
|
93 |
+
});
|
94 |
+
|
95 |
+
initialData.rooms.forEach(room => {
|
96 |
+
const roomDevices = initialData.devices.filter(device => device.hostelId === room.hostelId);
|
97 |
+
currentDeviceStates[room.id] = { ...room, devices: roomDevices.map(device => ({...device})) }; // Deep copy to avoid modifying initialData
|
98 |
+
});
|
99 |
+
|
100 |
+
console.log("Initial data fetched and processed.");
|
101 |
+
|
102 |
+
|
103 |
+
} catch (error) {
|
104 |
+
console.error("Error fetching initial data:", error);
|
105 |
+
initialData = { devices: [], rooms: [], users: [] }; // Provide default empty data on error
|
106 |
+
currentDeviceStates = {};
|
107 |
+
}
|
108 |
+
};
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
fetchInitialData().then(() => {
|
113 |
+
setupFirestoreListeners();
|
114 |
+
});
|
115 |
+
|
116 |
+
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
+
const updateInitialData = (collectionName, snapshot) => {
|
122 |
+
const changes = snapshot.docChanges();
|
123 |
+
changes.forEach(change => {
|
124 |
+
const docData = { id: change.doc.id, ...change.doc.data() };
|
125 |
+
|
126 |
+
switch (change.type) {
|
127 |
+
case 'added':
|
128 |
+
initialData[collectionName].push(docData);
|
129 |
+
break;
|
130 |
+
case 'modified':
|
131 |
+
const index = initialData[collectionName].findIndex(item => item.id === change.doc.id);
|
132 |
+
if (index !== -1) {
|
133 |
+
initialData[collectionName][index] = docData;
|
134 |
+
|
135 |
+
// Handle specific updates for device states in currentDeviceStates
|
136 |
+
if (collectionName === 'devices') {
|
137 |
+
|
138 |
+
for (const roomId in currentDeviceStates) {
|
139 |
+
const roomDevices = currentDeviceStates[roomId].devices;
|
140 |
+
const deviceIndex = roomDevices.findIndex(dev => dev.id === change.doc.id);
|
141 |
+
if (deviceIndex !== -1) {
|
142 |
+
currentDeviceStates[roomId].devices[deviceIndex] = {...docData, state: roomDevices[deviceIndex].state}
|
143 |
+
}
|
144 |
+
}
|
145 |
+
|
146 |
+
}
|
147 |
+
|
148 |
+
}
|
149 |
+
|
150 |
+
break;
|
151 |
+
case 'removed':
|
152 |
+
initialData[collectionName] = initialData[collectionName].filter(item => item.id !== change.doc.id);
|
153 |
+
break;
|
154 |
+
}
|
155 |
+
});
|
156 |
+
|
157 |
+
console.log(`Updated ${collectionName} in initialData.`);
|
158 |
+
|
159 |
+
|
160 |
+
if (collectionName === 'devices' || collectionName === 'rooms') {
|
161 |
+
rebuildCurrentDeviceStates();
|
162 |
+
}
|
163 |
};
|
164 |
|
165 |
|
166 |
+
const setupFirestoreListeners = () => {
|
167 |
+
|
168 |
+
db.collection('Devices').onSnapshot(snapshot => {
|
169 |
+
updateInitialData('devices', snapshot);
|
170 |
+
});
|
171 |
+
db.collectionGroup('Rooms').onSnapshot(snapshot => {
|
172 |
+
updateInitialData('rooms', snapshot);
|
173 |
+
|
174 |
+
});
|
175 |
+
db.collection('Users').onSnapshot(snapshot => {
|
176 |
+
updateInitialData('users', snapshot);
|
177 |
+
|
178 |
+
});
|
179 |
+
}
|
180 |
+
|
181 |
+
|
182 |
+
const rebuildCurrentDeviceStates = () => {
|
183 |
+
currentDeviceStates = {};
|
184 |
+
initialData.rooms.forEach(room => {
|
185 |
+
const roomDevices = initialData.devices.filter(device => device.hostelId === room.hostelId);
|
186 |
+
currentDeviceStates[room.id] = { ...room, devices: roomDevices.map(device => ({ ...device})) };
|
187 |
+
});
|
188 |
+
console.log("currentDeviceStates rebuilt.");
|
189 |
+
}
|
190 |
+
|
191 |
+
fetchInitialData().then(() => {
|
192 |
+
setupFirestoreListeners();
|
193 |
+
});
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
|
|
|
198 |
|
199 |
|
200 |
|