150 lines
3.5 KiB
TypeScript
150 lines
3.5 KiB
TypeScript
import { Prop } from "vue";
|
|
import { Serie } from "./Serie";
|
|
import { HttpClient } from "../Services/HttpClient";
|
|
import { TEMPERATURE, HUMIDITY, TYPE, VALUE } from "../const";
|
|
import { ref } from "vue";
|
|
|
|
export class TimeSeriesManager {
|
|
private client: HttpClient;
|
|
|
|
selected_user = {
|
|
user: "Rémi",
|
|
tag: "remi",
|
|
};
|
|
selected_room = {
|
|
room: "Terrasse",
|
|
tag: "Terrasse",
|
|
};
|
|
selected_device = {
|
|
device: "Shed",
|
|
tag: "Shed",
|
|
};
|
|
|
|
user_options = [
|
|
{
|
|
user: "Rémi",
|
|
tag: "remi",
|
|
},
|
|
{
|
|
user: "Sylvan",
|
|
tag: "sylvan",
|
|
},
|
|
];
|
|
|
|
room_options = [
|
|
{
|
|
room: "23N309",
|
|
tag: "23N309",
|
|
},
|
|
{
|
|
room: "Bedroom",
|
|
tag: "Bedroom",
|
|
},
|
|
{
|
|
room: "Terrasse",
|
|
tag: "Terrasse",
|
|
},
|
|
];
|
|
|
|
device_options = [
|
|
{
|
|
device: "Door sensor",
|
|
tag: "DoorSensor",
|
|
},
|
|
{
|
|
device: "Shed",
|
|
tag: "Shed",
|
|
},
|
|
];
|
|
|
|
error = ref(false);
|
|
loading = ref("");
|
|
|
|
series = ref<Serie[]>([]);
|
|
|
|
constructor(client: HttpClient) {
|
|
this.client = client;
|
|
}
|
|
|
|
async getTimeSeriesData() {
|
|
this.client
|
|
.getValues(
|
|
this.selected_user.tag,
|
|
this.selected_room.tag,
|
|
this.selected_device.tag
|
|
)
|
|
.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,
|
|
this.selected_user.user,
|
|
this.selected_room.room,
|
|
this.selected_device.device
|
|
);
|
|
|
|
const humiditySerie = new Serie(
|
|
HUMIDITY,
|
|
humidityRecordProcessed,
|
|
this.selected_user.user,
|
|
this.selected_room.room,
|
|
this.selected_device.device
|
|
);
|
|
|
|
this.series.value = [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() {
|
|
this.client
|
|
.newValue(
|
|
this.selected_user.user,
|
|
this.selected_room.room,
|
|
this.selected_device.device
|
|
)
|
|
.catch((error) => {
|
|
console.error("Error asking new values:", this.selected_device.device);
|
|
throw error;
|
|
});
|
|
}
|
|
}
|