From dd2a3cf1508c53ce320f340d762f5a584ae4f047 Mon Sep 17 00:00:00 2001 From: Reimar Date: Mon, 31 Mar 2025 09:29:55 +0200 Subject: [PATCH] Show real temperature data in frontend --- .../mockdata/temperature-logs.mockdata.js | 28 ------------------- frontend/scripts/home.js | 8 +++--- frontend/scripts/services/devices.service.js | 11 ++------ frontend/shared/utils.js | 16 ++++++----- 4 files changed, 15 insertions(+), 48 deletions(-) delete mode 100644 frontend/mockdata/temperature-logs.mockdata.js diff --git a/frontend/mockdata/temperature-logs.mockdata.js b/frontend/mockdata/temperature-logs.mockdata.js deleted file mode 100644 index e6056ae..0000000 --- a/frontend/mockdata/temperature-logs.mockdata.js +++ /dev/null @@ -1,28 +0,0 @@ -export const mockTemperatureLogs = [ - { id: 1, temperature: 18.9, date: "2025-03-19T17:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.1, date: "2025-03-19T17:10:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.5, date: "2025-03-19T17:20:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.8, date: "2025-03-19T17:30:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.1, date: "2025-03-19T17:40:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.3, date: "2025-03-19T17:50:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.6, date: "2025-03-19T18:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.9, date: "2025-03-19T18:10:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.8, date: "2025-03-19T18:20:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.7, date: "2025-03-19T18:30:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.5, date: "2025-03-19T18:40:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.3, date: "2025-03-19T18:50:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.1, date: "2025-03-19T19:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 18.9, date: "2025-03-19T20:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.1, date: "2025-03-19T20:10:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.5, date: "2025-03-19T20:20:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 19.8, date: "2025-03-19T20:30:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.1, date: "2025-03-19T20:40:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 20.3, date: "2025-03-19T20:50:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 22.6, date: "2025-03-19T21:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 22.9, date: "2025-03-19T21:10:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 22.8, date: "2025-03-19T21:20:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 22.7, date: "2025-03-19T21:30:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 22.5, date: "2025-03-19T21:40:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 23.3, date: "2025-03-19T21:50:00Z", tempHigh: 22.0, tempLow: 18.0 }, - { id: 1, temperature: 24.1, date: "2025-03-19T22:00:00Z", tempHigh: 22.0, tempLow: 18.0 }, -] diff --git a/frontend/scripts/home.js b/frontend/scripts/home.js index 01c4bf1..6e19caa 100644 --- a/frontend/scripts/home.js +++ b/frontend/scripts/home.js @@ -1,14 +1,14 @@ -import { mockTemperatureLogs } from "../mockdata/temperature-logs.mockdata.js"; // Import data import { getLogsOnDeviceId } from "./services/devices.service.js"; async function buildChart() { + // TODO change device id const data = await getLogsOnDeviceId(1); - const xValues = mockTemperatureLogs.map((log) => + const xValues = data.map((log) => new Date(log.date).toLocaleString() ); // Full Date labels - const yValues = mockTemperatureLogs.map((log) => log.temperature); // Temperature values - buildTable(mockTemperatureLogs); + const yValues = data.map((log) => log.temperature); // Temperature values + buildTable(data); new Chart("myChart", { type: "line", data: { diff --git a/frontend/scripts/services/devices.service.js b/frontend/scripts/services/devices.service.js index 5b8830f..2d6eb82 100644 --- a/frontend/scripts/services/devices.service.js +++ b/frontend/scripts/services/devices.service.js @@ -1,4 +1,5 @@ import { address } from "../../shared/constants.js"; +import { request } from "../../shared/utils.js"; export function getDevicesOnUserId(id) { fetch(`${address}/get-on-user-id`, { @@ -27,13 +28,5 @@ export function update(ids) { } export function getLogsOnDeviceId(id) { - fetch(`${address}/device/logs/${id}`, { - method: "GET", - headers: { - "Content-Type": "application/json" - }, - }) - .then(response => response.json()) - .then(data => console.log("Success:", data)) - .catch(error => console.error("Error:", error)); + return request("GET", `/device/logs/${id}`); } diff --git a/frontend/shared/utils.js b/frontend/shared/utils.js index 30badae..08c045c 100644 --- a/frontend/shared/utils.js +++ b/frontend/shared/utils.js @@ -13,17 +13,19 @@ export async function request(method, path, body = null) { body: body ? JSON.stringify(body) : undefined, }) .then(async response => { - const json = await response.json(); + try { + const json = await response.json(); - if (response.ok) return resolve(json); + if (response.ok) return resolve(json); - if (json.error) return reject(json.error); + if (json.error) return reject(json.error); - if (json.message) return reject(json.message); + if (json.message) return reject(json.message); - if (json.errors) return reject(Object.values(response.errors)[0][0]); - - reject("Request failed with HTTP code " + response.status); + if (json.errors) return reject(Object.values(json.errors)[0][0]); + } finally { + reject("Request failed with HTTP code " + response.status); + } }) .catch(err => reject(err.message)); });