diff --git a/UserInterface/api/api.js b/UserInterface/api/api.js index 9f2220d..117cafb 100644 --- a/UserInterface/api/api.js +++ b/UserInterface/api/api.js @@ -47,6 +47,7 @@ export class GreenhouseProxy { const options = { method: 'POST', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(user) @@ -92,8 +93,14 @@ export class GreenhouseProxy { // Get all users async getUsers() { + const options = { + headers: { + 'Authorization': token + } + } + try { - let response = await fetch(`/users`); + let response = await fetch(`/users`, options); if (response.ok) { let data = await response.json(); @@ -116,6 +123,7 @@ export class GreenhouseProxy { const options = { method: 'PATCH', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(userData) @@ -136,7 +144,10 @@ export class GreenhouseProxy { // Delete user by id async deleteUser(userID) { const options = { - method: 'DELETE' + method: 'DELETE', + headers: { + 'Authorization': token + } } try { @@ -153,8 +164,14 @@ export class GreenhouseProxy { // ------------------ROOM------------------ // // Get all rooms async getRooms() { + const options = { + headers: { + 'Authorization': token + } + } + try { - let response = await fetch(`/rooms`); + let response = await fetch(`/rooms`, options); return await response.json(); } catch (error) { console.log(error); @@ -167,6 +184,7 @@ export class GreenhouseProxy { const options = { method: 'POST', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(room) @@ -184,8 +202,14 @@ export class GreenhouseProxy { // Get room by id async getRoomByID(roomID) { + const options = { + headers: { + 'Authorization': token + } + } + try { - let response = await fetch(`/rooms/${roomID}`); + let response = await fetch(`/rooms/${roomID}`, options); if (response.ok) { let data = await response.json(); return data; @@ -201,6 +225,7 @@ export class GreenhouseProxy { const options = { method: 'PATCH', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(name) @@ -242,6 +267,7 @@ export class GreenhouseProxy { const options = { method: 'PUT', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(dateObj) @@ -263,6 +289,7 @@ export class GreenhouseProxy { const options = { method: 'POST', headers: { + 'Authorization': token, 'Content-Type': 'application/json', 'Key': 'TRD1IU4G5E45RZU8UOXNNMR7WID34Q7SM0EFHV6FHZS9PQBNYXINTTS1BSR8' // Agent Key for Room 1 }, @@ -284,6 +311,7 @@ export class GreenhouseProxy { getAllMessages() { const options = { headers: { + 'Authorization': token, 'Content-Type': 'application/json' } } @@ -297,6 +325,7 @@ export class GreenhouseProxy { async getAllMessagesByRoom(roomID) { const options = { headers: { + 'Authorization': token, 'Content-Type': 'application/json' } } @@ -322,6 +351,7 @@ export class GreenhouseProxy { const options = { method: 'POST', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(_body) @@ -344,8 +374,14 @@ export class GreenhouseProxy { // ------------------EXPERIMENT------------------ // Get all experiments async getExperiments() { + const options = { + headers: { + 'Authorization': token, + } + } + try { - let response = await fetch(`/experiments`); + let response = await fetch(`/experiments`, options); if (response.ok) { let data = await response.json(); return data; @@ -360,6 +396,7 @@ export class GreenhouseProxy { const options = { method: 'POST', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(experimentObj) @@ -377,6 +414,12 @@ export class GreenhouseProxy { // Get experiment by id async getExperimentByID(experimentID) { + const options = { + headers: { + 'Authorization': token + } + } + try { let response = await fetch(`/experiments/${experimentID}`, options); if (response.ok) { @@ -393,6 +436,7 @@ export class GreenhouseProxy { const options = { method: 'PATCH', headers: { + 'Authorization': token, 'Content-Type': 'application/json' }, body: JSON.stringify(experimentObj) @@ -459,19 +503,3 @@ export class GreenhouseProxy { // Delete an agent by id } - -// const proxy = new GreenhouseProxy(); -// proxy.registerUser(userRegister2); -// proxy.registerUser(userRegister3); -// proxy.login(); -// proxy.getUsers(); -// proxy.createRoom("Room 1"); -// proxy.getRooms(); -// proxy.deleteRoom(1); -// proxy.createMeasurement(1, measurement); -// proxy.getAllMessages(); -// proxy.getAllMessagesByRoom(1); -// proxy.getAllMessagesByRoom(2); -// proxy.getAllMessagesByRoom(3); -// proxy.getAllMessagesByRoom(4); -// proxy.createRoomMessage(4, 1, "That sounds like a great idea. Let's do it!"); diff --git a/UserInterface/js/chart.js b/UserInterface/js/chart.js index 6dd6e57..e8613d8 100644 --- a/UserInterface/js/chart.js +++ b/UserInterface/js/chart.js @@ -47,7 +47,7 @@ async function renderMeasurements() { exportDataBtn.addEventListener('click', () => { const csvData = Utils.csvMaker(measurements); // console.log(csvData); - + Utils.download(csvData['csv_data'], csvData['file_name']); }); @@ -62,9 +62,15 @@ async function renderMeasurements() { const day = date.getUTCDate(); const month = date.getUTCMonth(); const hours = date.getUTCHours(); - const minutes = date.getUTCMinutes(); - const seconds = date.getUTCSeconds(); - const timestamp = `${day} ${Utils.months[month]} ${Utils.toStandardTime(hours)}:${minutes}:${seconds}`; + let minutes = date.getUTCMinutes(); + if (minutes < 10) { + minutes = '0' + minutes; + } + let seconds = date.getUTCSeconds(); + if (seconds < 10) { + seconds = '0' + seconds; + } + const timestamp = `${day} ${Utils.months[month]} ${Utils.toStandardTime(hours)['hours']}:${minutes}:${seconds}`; dates.push(timestamp); const temperatureValue = measurement['temperature']; diff --git a/UserInterface/js/roomMessages.js b/UserInterface/js/roomMessages.js index 4f8d9dd..d32dca6 100644 --- a/UserInterface/js/roomMessages.js +++ b/UserInterface/js/roomMessages.js @@ -32,21 +32,42 @@ async function renderMessages() { if (messages.length) { messages.forEach(message => { - const user = message["user_id"]; const textContainer = document.createElement('div'); textContainer.setAttribute('class', 'd-flex align-items-baseline mb-4'); const container2 = document.createElement('div'); container2.setAttribute('class', 'pe-2'); const textCard = document.createElement('div'); textCard.setAttribute('class', 'card card-text d-inline-block p-2 px-3 m-1'); + // const info = document.createElement('div'); + // info.setAttribute('class', 'd-flex'); + const user = document.createElement('div'); + user.setAttribute('class', 'small'); + user.textContent = usernames[message['user_id']]; const date = document.createElement('div'); date.setAttribute('class', 'small'); - date.textContent = usernames[message['user_id']]; + + const dateObj = new Date(message['timestamp']); + const day = dateObj.getDate(); + const month = Utils.months[dateObj.getMonth()]; + const year = dateObj.getFullYear(); + const hoursObj = Utils.toStandardTime(dateObj.getHours()); + let minutes = dateObj.getMinutes(); + if (minutes < 10) { + minutes = '0' + minutes; + } + const hours = hoursObj['hours']; + const amPm = hoursObj['amPm']; + const dateStr = `${hours}:${minutes} ${amPm} - ${day} ${month}, ${year}`; + + date.textContent = dateStr; textCard.textContent = message["body"]; + container2.append(user); container2.append(textCard); container2.append(date); + // info.append(user); + // info.append(date); textContainer.append(container2); diff --git a/UserInterface/js/utilities.js b/UserInterface/js/utilities.js index e1e05db..5f99c17 100644 --- a/UserInterface/js/utilities.js +++ b/UserInterface/js/utilities.js @@ -49,13 +49,23 @@ export function logout() { export function toStandardTime(_hours) { let hours = _hours; + let amPm = ''; if (hours > 12) { + amPm = 'AM'; hours -= 12; } else if (hours == 0) { - timeValue= "12"; + amPm = 'AM'; + hours = "12"; + }else{ + amPm = 'PM'; } - - return hours; + + const hoursObj = { + hours: hours, + amPm: amPm + } + + return hoursObj; } export function download(data, filename) {