Implement fetcher scaffolding
This commit is contained in:
33
src/fetcher/index.ts
Normal file
33
src/fetcher/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import dayjs, { Dayjs } from "dayjs";
|
||||
import { type Tauron } from "../tauron";
|
||||
import { Measurement } from "./types";
|
||||
|
||||
export class Fetcher {
|
||||
#service: Tauron;
|
||||
|
||||
constructor(service: Tauron) {
|
||||
this.#service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the measurements for a specific date.
|
||||
* @param date - the measurement date (note, that 'todays' date may not be available at the time being when request is made)
|
||||
* @returns the measurement data
|
||||
*/
|
||||
async fetch(date: Dayjs): Promise<Measurement> {
|
||||
const monthEnd = date.endOf('month');
|
||||
const endDay = monthEnd.isBefore(dayjs(), 'day') ? monthEnd : date;
|
||||
|
||||
const energyForDay = await this.#service.getEnergyForDay(date);
|
||||
const energyForMonth = await this.#service.getEnergyForRange(date.startOf('month'), endDay);
|
||||
const energyForYear = await this.#service.getEnergyForYear(date.format('YYYY'));
|
||||
const reading = await this.#service.getReadingForRange(date, date);
|
||||
|
||||
return {
|
||||
energyForDay,
|
||||
energyForMonth,
|
||||
energyForYear,
|
||||
reading: reading?.[0],
|
||||
};
|
||||
}
|
||||
};
|
||||
26
src/fetcher/types.ts
Normal file
26
src/fetcher/types.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { EnergyDTO, PowerDTO, ReadingDTO } from "../tauron";
|
||||
|
||||
/**
|
||||
* Measurement for a specific point of time (minute, hour, max day).
|
||||
*/
|
||||
export type Measurement = {
|
||||
/**
|
||||
* Energy reading data.
|
||||
*/
|
||||
reading: ReadingDTO;
|
||||
|
||||
/**
|
||||
* Energy measurements data for day range.
|
||||
*/
|
||||
energyForDay: EnergyDTO;
|
||||
|
||||
/**
|
||||
* Energy measurements data for month range.
|
||||
*/
|
||||
energyForMonth: EnergyDTO;
|
||||
|
||||
/**
|
||||
* Energy measurements data for year range.
|
||||
*/
|
||||
energyForYear: EnergyDTO;
|
||||
};
|
||||
Reference in New Issue
Block a user