feat(web-app): change location of the timeserie manager

This commit is contained in:
fastium
2025-05-14 21:27:34 +02:00
parent 63429dafa2
commit a511e6e18e
2 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,87 @@
import { Prop } from "vue";
import { Serie } from "./Serie";
import { HttpClient } from "../Services/HttpClient";
import { TEMPERATURE, HUMIDITY, TYPE, VALUE } from "../const";
export class TimeSeriesManager {
private client: HttpClient;
constructor(client: HttpClient) {
this.client = client;
}
async getTimeSeriesData(
user: string,
room: string,
device: string
): Promise<Serie[]> {
return this.client
.getValues(user, room, device)
.then((response) => {
// Filter temperature records
let temperatureRecords = response.data.filter(
(x: any) => x[TYPE] === TEMPERATURE
);
// Create a new array with type field removed and time converted to timestamp
let temperatureRecordsProcessed = temperatureRecords.map((x: any) => {
// Create a new object without the TYPE field
const { [TYPE]: removed, ...rest } = x;
// Convert the ISO time string to a timestamp (if it's a string)
if (typeof rest.time === "string") {
rest.time = new Date(rest.time).getTime();
}
return rest;
});
//filter humidity records
let humidityRecord = response.data.filter(
(x: any) => x[TYPE] === HUMIDITY
);
// Create a new array with type field removed and time converted to timestamp
let humidityRecordProcessed = humidityRecord.map((x: any) => {
// Create a new object without the TYPE field
const { [TYPE]: removed, ...rest } = x;
// Convert the ISO time string to a timestamp (if it's a string)
if (typeof rest.time === "string") {
rest.time = new Date(rest.time).getTime();
}
return rest;
});
// Create actual Serie instances
const temperatureSerie = new Serie(
TEMPERATURE,
temperatureRecordsProcessed,
user,
room,
device
);
const humiditySerie = new Serie(
HUMIDITY,
humidityRecordProcessed,
user,
room,
device
);
return [temperatureSerie, humiditySerie];
})
.catch((error) => {
console.error("Error fetching time series data:", error);
throw error; // Re-throw to allow calling code to handle it
});
}
async getNewValue(user: string, room: string, device: string) {
this.client.newValue(user, room, device).catch((error) => {
console.error("Error asking new values:", error);
throw error;
});
}
}