diff --git a/src/fetcher/index.ts b/src/fetcher/index.ts new file mode 100644 index 0000000..64b06cf --- /dev/null +++ b/src/fetcher/index.ts @@ -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 { + 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], + }; + } +}; \ No newline at end of file diff --git a/src/fetcher/types.ts b/src/fetcher/types.ts new file mode 100644 index 0000000..aab3527 --- /dev/null +++ b/src/fetcher/types.ts @@ -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; +}; \ No newline at end of file