Make Tauron service supports Dayjs

This commit is contained in:
2024-11-12 22:46:17 +01:00
parent 59e0829f8a
commit 33e9644e1a
3 changed files with 27 additions and 12 deletions

6
package-lock.json generated
View File

@@ -11,6 +11,7 @@
"dependencies": {
"axios": "^1.7.7",
"axios-cookiejar-support": "^5.0.3",
"dayjs": "^1.11.13",
"tough-cookie": "^5.0.0",
"tough-cookie-file-store": "^2.0.3",
"yaml": "^2.6.0"
@@ -469,6 +470,11 @@
"node": ">= 0.8"
}
},
"node_modules/dayjs": {
"version": "1.11.13",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.13.tgz",
"integrity": "sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg=="
},
"node_modules/debug": {
"version": "4.3.7",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",

View File

@@ -16,6 +16,7 @@
"dependencies": {
"axios": "^1.7.7",
"axios-cookiejar-support": "^5.0.3",
"dayjs": "^1.11.13",
"tough-cookie": "^5.0.0",
"tough-cookie-file-store": "^2.0.3",
"yaml": "^2.6.0"

View File

@@ -5,6 +5,7 @@ import { CookieJar } from 'tough-cookie';
import { FileCookieStore } from 'tough-cookie-file-store';
import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types";
import { TauronConfig } from '../config';
import { Dayjs } from 'dayjs';
export * from './types';
@@ -13,12 +14,14 @@ const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl';
const ENERGY_API = '/energia/api';
const POWER_API = '/moc/api';
const READING_API = '/odczyty/api';
const DATE_FORMAT = 'DD.MM.YYYY';
/**
* Utility class for Tauron API.
* @param config - configuration of service
*/
export const Tauron = class {
export class Tauron {
#http: Axios;
#config: TauronConfig;
@@ -113,12 +116,14 @@ export const Tauron = class {
* @param type - type of measurement, can be 'consum' (regular one) or 'average'
* @returns the energy data from Tauron API
*/
async getEnergyForDay(day: string): Promise<EnergyDTO> {
async getEnergyForDay(day: Dayjs): Promise<EnergyDTO> {
const date = day.format(DATE_FORMAT);
return await this.getEnergy({
type: 'consum',
profile: "full time",
from: day,
to: day,
from: date,
to: date,
});
}
@@ -129,11 +134,11 @@ export const Tauron = class {
* @param type - type of measurement, can be 'consum' (regular one) or 'average'
* @returns the energy data from Tauron API
*/
async getEnergyForRange(from: string, to: string): Promise<EnergyDTO> {
async getEnergyForRange(from: Dayjs, to: Dayjs): Promise<EnergyDTO> {
return await this.getEnergy({
type: 'consum',
from,
to,
from: from.format(DATE_FORMAT),
to: to.format(DATE_FORMAT),
profile: "range"
});
}
@@ -168,8 +173,11 @@ export const Tauron = class {
* @param to - the measurement ending date
* @returns the power data from Tauron API
*/
async getPowerForRange(from: string, to: string): Promise<PowerDTO[]> {
return await this.getPower({ from, to });
async getPowerForRange(from: Dayjs, to: Dayjs): Promise<PowerDTO[]> {
return await this.getPower({
from: from.format(DATE_FORMAT),
to: to.format(DATE_FORMAT)
});
}
/**
@@ -187,10 +195,10 @@ export const Tauron = class {
* @param to - the measurement ending date
* @returns the readings from Tauron API
*/
async getReadingForRange(from: string, to: string): Promise<ReadingDTO[]> {
async getReadingForRange(from: Dayjs, to: Dayjs): Promise<ReadingDTO[]> {
return await this.getReading({
from,
to,
from: from.format(DATE_FORMAT),
to: to.format(DATE_FORMAT),
type: 'energia-pobrana',
});
}