Merge branch 'master' of git.reim.ar:ReiMerc/temperature-alarm

This commit is contained in:
Jeas0001 2025-03-31 10:39:35 +02:00
commit 7a3d5b4f71
10 changed files with 145 additions and 133 deletions

View File

@ -4,34 +4,37 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Temperature-Alarm-Web</title> <title>Temperature-Alarm-Web</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script> <script defer src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<link rel="stylesheet" href="/styles/home.css" />
<script defer type="module" src="/scripts/home.js"></script> <script defer type="module" src="/scripts/home.js"></script>
<link rel="stylesheet" href="/styles/common.css">
<link rel="stylesheet" href="/styles/home.css" />
</head> </head>
<body> <body>
<div id="container"> <div class="topnav">
<div class="topnav"> <a class="active" href="/home/index.html">Home</a>
<a class="active" href="/home/index.html">Home</a> <div style="display: flex; justify-content: flex-end;">
<div style="display: flex; justify-content: flex-end;"> <a class="" href="/home/index.html">Devices</a>
<a class="" href="/home/index.html">Devices</a> <a class="" href="/profile/index.html">Profile</a>
<a class="" href="/profile/index.html">Profile</a>
</div>
</div> </div>
<div class="chartContainer"> </div>
<div id="container">
<div class="chart-container">
<canvas id="myChart" style="width: 49%; height: 49%;"></canvas> <canvas id="myChart" style="width: 49%; height: 49%;"></canvas>
</div> </div>
<table> <div id="table-wrapper">
<tr> <table>
<th>Name</th> <tr>
<th>Temperature</th> <th>Temperature</th>
<th>Date</th> <th>Date</th>
<th>TempHigh</th> <th>Time</th>
<th>TempLow</th> <th>Limits</th>
</tr> </tr>
<tbody id="TemperatureTable"></tbody> <tbody id="TemperatureTable"></tbody>
</table> </table>
</div>
</div> </div>
<div id="error" class="error"></div>
</body> </body>
</html> </html>

View File

@ -4,6 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>Login - Temperature alarm</title> <title>Login - Temperature alarm</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/styles/common.css">
<link rel="stylesheet" href="/styles/auth.css"> <link rel="stylesheet" href="/styles/auth.css">
<script defer type="module" src="/scripts/login.js"></script> <script defer type="module" src="/scripts/login.js"></script>
</head> </head>
@ -28,7 +29,7 @@
</span> </span>
</div> </div>
<div id="form-error"></div> <div id="form-error" class="error"></div>
</div> </div>
</form> </form>
</body> </body>

View File

@ -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 },
]

View File

@ -4,6 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>Register - Temperature Alarm</title> <title>Register - Temperature Alarm</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/styles/common.css">
<link rel="stylesheet" href="/styles/auth.css"> <link rel="stylesheet" href="/styles/auth.css">
<script defer type="module" src="/scripts/register.js"></script> <script defer type="module" src="/scripts/register.js"></script>
</head> </head>
@ -33,7 +34,7 @@
</label> </label>
</div> </div>
<div id="form-error"></div> <div id="form-error" class="error"></div>
</div> </div>
</form> </form>
</body> </body>

View File

@ -1,20 +1,20 @@
import { mockTemperatureLogs } from "../mockdata/temperature-logs.mockdata.js"; // Import data
import { getLogsOnDeviceId } from "./services/devices.service.js"; import { getLogsOnDeviceId } from "./services/devices.service.js";
async function buildChart() { async function buildChart(data) {
const data = await getLogsOnDeviceId(1); data.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
const xValues = mockTemperatureLogs.map((log) => const xValues = data.map((log) =>
new Date(log.date).toLocaleString() new Date(log.date).toLocaleString()
); // Full Date labels ); // Full Date labels
const yValues = mockTemperatureLogs.map((log) => log.temperature); // Temperature values const yValues = data.map((log) => log.temperature); // Temperature values
buildTable(mockTemperatureLogs); buildTable(data);
new Chart("myChart", { new Chart("myChart", {
type: "line", type: "line",
data: { data: {
labels: xValues, labels: xValues,
datasets: [ datasets: [
{ {
label: "Temperature",
fill: false, fill: false,
lineTension: 0.4, lineTension: 0.4,
backgroundColor: "rgba(0,0,255,1.0)", backgroundColor: "rgba(0,0,255,1.0)",
@ -43,29 +43,41 @@ function buildTable(data) {
data.forEach((log) => { data.forEach((log) => {
var averageTemp = (log.tempHigh + log.tempLow) / 2.0; var averageTemp = (log.tempHigh + log.tempLow) / 2.0;
var color; var color;
if (log.temperature > log.tempHigh) { if (log.temperature >= log.tempHigh) {
color = "tempHigh"; color = "tempHigh";
} else if ( } else if (
log.temperature < log.tempHigh && log.temperature < log.tempHigh &&
log.temperature > averageTemp log.temperature > averageTemp
) { ) {
color = "tempMidHigh"; color = "tempMidHigh";
} else if (log.temperature < log.tempLow) { } else if (log.temperature <= log.tempLow) {
color = "tempLow"; color = "tempLow";
} else if (log.temperature > log.tempLow && log.temperature < averageTemp) { } else if (log.temperature > log.tempLow && log.temperature < averageTemp) {
color = "tempMidLow"; color = "tempMidLow";
} else { } else {
color = "tempNormal"; color = "tempNormal";
} }
var row = ` <tr>
<td>Name</td> const date = new Date(log.date).toLocaleDateString();
<td class="${color}">${log.temperature}</td> const time = new Date(log.date).toLocaleTimeString();
<td>${log.date}</td>
<td class="tempHigh">${log.tempHigh}</td> table.innerHTML += `
<td class="tempLow">${log.tempLow}</td> <tr>
</tr>`; <td class="temperature ${color}">${log.temperature}&deg;C</td>
table.innerHTML += row; <td>${date}</td>
<td width="50%">${time}</td>
<td width="50%">Min: <b class="low-limit">${log.tempLow}&deg;C</b>, Max: <b class="high-limit">${log.tempHigh}&deg;C</b></td>
</tr>
`;
}); });
} }
buildChart(); // TODO change device id
getLogsOnDeviceId(1)
.then(buildChart)
.catch(err => {
document.getElementById("error").innerText = err;
document.getElementById("error").style.display = "block";
document.getElementById("container").style.display = "none";
});

View File

@ -1,4 +1,5 @@
import { address } from "../../shared/constants.js"; import { address } from "../../shared/constants.js";
import { request } from "../../shared/utils.js";
export function getDevicesOnUserId(id) { export function getDevicesOnUserId(id) {
fetch(`${address}/get-on-user-id`, { fetch(`${address}/get-on-user-id`, {
@ -27,13 +28,5 @@ export function update(ids) {
} }
export function getLogsOnDeviceId(id) { export function getLogsOnDeviceId(id) {
fetch(`${address}/device/logs/${id}`, { return request("GET", `/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));
} }

View File

@ -13,17 +13,19 @@ export async function request(method, path, body = null) {
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,
}) })
.then(async response => { .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]); if (json.errors) return reject(Object.values(json.errors)[0][0]);
} finally {
reject("Request failed with HTTP code " + response.status); reject("Request failed with HTTP code " + response.status);
}
}) })
.catch(err => reject(err.message)); .catch(err => reject(err.message));
}); });

View File

@ -39,16 +39,6 @@ button:hover {
margin-top: 0.5rem; margin-top: 0.5rem;
} }
#form-error {
display: none;
background-color: #FFCDD2;
color: #C62828;
border: 1px solid #C62828;
border-radius: 4px;
padding: 1rem 2rem;
margin-top: 1rem;
}
button{ button{
border-radius: 20px; border-radius: 20px;
} }

View File

@ -1,3 +1,10 @@
.error { .error {
background-color: #EF9A9A; display: none;
background-color: #FFCDD2;
color: #C62828;
border: 1px solid #C62828;
border-radius: 4px;
padding: 1rem 2rem;
margin-top: 1rem;
} }

View File

@ -1,79 +1,110 @@
body { body {
margin: 0; margin: 0;
font-family: Arial, Helvetica, sans-serif; font-family: Arial, Helvetica, sans-serif;
background-color: #F9F9F9;
} }
#container { #container {
background-color: white; margin: 0 2rem;
opacity: 100%;
} }
.topnav { .topnav {
overflow: hidden; overflow: hidden;
background-color: #333; background-color: #333;
} }
.topnav a { .topnav a {
float: left; float: left;
color: #f2f2f2; color: #f2f2f2;
text-align: center; text-align: center;
padding: 14px 16px; padding: 14px 16px;
text-decoration: none; text-decoration: none;
font-size: 17px; font-size: 17px;
} }
.topnav a:hover { .topnav a:hover {
background-color: #ddd; background-color: #ddd;
color: black; color: black;
} }
.topnav a.active { .topnav a.active {
background-color: #04aa6d; background-color: #04aa6d;
color: white; color: white;
}
#table-wrapper {
overflow: hidden;
border-radius: 8px;
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.1);
border: 1px solid #DDD;
} }
table { table {
font-family: arial, sans-serif; font-family: arial, sans-serif;
border-collapse: collapse; border-collapse: collapse;
width: 100%; width: 100%;
background-color: white;
color: #616161;
} }
td, td,
th { th {
border: 1px solid #dddddd; border-right: 1px solid #DDD;
text-align: left; border-bottom: 1px solid #DDD;
padding: 8px; text-align: left;
padding: 8px;
}
th {
border-bottom: 2px solid #DDD;
} }
tr:nth-child(even) { tr:nth-child(even) {
background-color: #dddddd; background-color: #F5F5F5;
}
table .temperature {
text-align: center;
color: white;
font-weight: bold;
width: 20px;
}
table tr:not(:last-child) .temperature {
border-bottom-color: white;
} }
.tempHigh { .tempHigh {
color: #ff0000; background-color: #ff0000;
width: 20px;
} }
.tempMidHigh { .tempMidHigh {
color: #fffb00; background-color: #FFA000;
width: 20px;
} }
.tempNormal { .tempNormal {
color: #02ed26; background-color: #AAA;
width: 20px;
} }
.tempMidLow { .tempMidLow {
color: #16fae7; background-color: #64B5F6;
width: 20px;
} }
.tempLow { .tempLow {
color: #0004ff; background-color: #3F51B5;
width: 20px;
} }
.chartContainer{ .low-limit {
margin: 20px; color: #3F51B5;
} }
.high-limit {
color: #F00;
}
.chart-container {
margin: 2rem 0;
background-color: white;
border-radius: 8px;
border: 1px solid #DDD;
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.1);
}
#error {
margin: 2rem;
}