feat(web-app): change location of the timeserie manager
This commit is contained in:
87
web-app/src/Measures/TimeSeriesManager.ts
Normal file
87
web-app/src/Measures/TimeSeriesManager.ts
Normal 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user