Implement some additional functions to Tauron class
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import axios, { Axios, AxiosResponse } from "axios";
|
||||
import { wrapper } from 'axios-cookiejar-support';
|
||||
import { CookieJar, MemoryCookieStore } from 'tough-cookie';
|
||||
import { CookieJar } from 'tough-cookie';
|
||||
import { FileCookieStore } from 'tough-cookie-file-store';
|
||||
import { EnergyDTO, Payload } from "./types";
|
||||
import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types";
|
||||
|
||||
export * from './types';
|
||||
|
||||
const LOGIN_API = `https://logowanie.tauron-dystrybucja.pl/login`;
|
||||
const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl';
|
||||
const ENERGY_API = '/energia/api';
|
||||
const POWER_API = '/moc/api';
|
||||
const READING_API = '/odczyty/api';
|
||||
|
||||
/**
|
||||
* Utility class for Tauron API.
|
||||
@@ -71,7 +74,7 @@ export const Tauron = class {
|
||||
* Generic function which performs the HTTP request.
|
||||
* If the request fails, the login attempt will be performed and the function will be invoked again
|
||||
* with new session cookies.
|
||||
* @param callback function which performs the request on Axios instance being passed as argument
|
||||
* @param callback - function which performs the request on Axios instance being passed as argument
|
||||
* @returns response from callback function
|
||||
*/
|
||||
async query<T>(callback: (http: Axios) => Promise<AxiosResponse<T>>): Promise<AxiosResponse<T>> {
|
||||
@@ -85,22 +88,115 @@ export const Tauron = class {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the energy data per specific day.
|
||||
* @param day measurement day
|
||||
* @returns the energy data from Tauron API
|
||||
* Utility function to handle Tauron API shape (success field) automatically.
|
||||
* @param path - HTTP API path
|
||||
* @param data - the HTTP payload
|
||||
* @returns original response
|
||||
*/
|
||||
async getEnergyForDay(day: string): Promise<EnergyDTO> {
|
||||
const response = await this.query(h => h.postForm<Payload<EnergyDTO>>('/energia/api', {
|
||||
from: day,
|
||||
to: day,
|
||||
type: "consum",
|
||||
profile: "full time"
|
||||
}));
|
||||
async #queryForm<T>(path: string, data: any): Promise<T> {
|
||||
const response = await this.query(h => h.postForm<Payload<T>>(path, data));
|
||||
|
||||
if (!response.data.success) {
|
||||
throw new Error(`Invalid energy data request: ${JSON.stringify(response.data)}`);
|
||||
throw new Error(`Invalid data request (success == false): ${JSON.stringify(response.data)}`);
|
||||
}
|
||||
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the energy data for specific request payload.
|
||||
* @param data - the payload supported by Tauron API
|
||||
* @returns the energy data from Tauron API
|
||||
*/
|
||||
async getEnergy(data: EnergyRequestDTO): Promise<EnergyDTO> {
|
||||
return this.#queryForm<EnergyDTO>(ENERGY_API, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the energy data per specific day.
|
||||
* @param day - measurement day
|
||||
* @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> {
|
||||
return await this.getEnergy({
|
||||
type: 'consum',
|
||||
profile: "full time",
|
||||
from: day,
|
||||
to: day,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the energy data per specific dates range.
|
||||
* @param from - the measurement starting date
|
||||
* @param to - the measurement ending date
|
||||
* @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> {
|
||||
return await this.getEnergy({
|
||||
type: 'consum',
|
||||
from,
|
||||
to,
|
||||
profile: "range"
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the energy data per specific year.
|
||||
* @param year measurement year
|
||||
* @param type - type of measurement, can be 'consum' (regular one) or 'average'
|
||||
* @returns the energy data from Tauron API
|
||||
*/
|
||||
async getEnergyForYear(year: string): Promise<EnergyDTO> {
|
||||
return await this.getEnergy({
|
||||
type: 'consum',
|
||||
profile: "year",
|
||||
from: `01.01.${year}`,
|
||||
to: `31.12.${year}`,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the power data for specific request payload.
|
||||
* @param data - the payload supported by Tauron API
|
||||
* @returns the power data from Tauron API
|
||||
*/
|
||||
async getPower(data: PowerRequestDTO): Promise<PowerDTO[]> {
|
||||
return this.#queryForm<PowerDTO[]>(POWER_API, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the power data per specific dates range.
|
||||
* @param from - the measurement starting date
|
||||
* @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 });
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reading data for specific request payload.
|
||||
* @param data - the payload supported by Tauron API
|
||||
* @returns the reading data from Tauron API
|
||||
*/
|
||||
async getReading(data: ReadingRequestDTO): Promise<ReadingDTO[]> {
|
||||
return this.#queryForm<ReadingDTO[]>(READING_API, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the readings per specific dates range.
|
||||
* @param from - the measurement starting date
|
||||
* @param to - the measurement ending date
|
||||
* @returns the readings from Tauron API
|
||||
*/
|
||||
async getReadingForRange(from: string, to: string): Promise<ReadingDTO[]> {
|
||||
return await this.getReading({
|
||||
from,
|
||||
to,
|
||||
type: 'energia-pobrana',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,15 +1,61 @@
|
||||
export type Payload<T> = {
|
||||
success: boolean;
|
||||
data: T;
|
||||
}
|
||||
};
|
||||
|
||||
export type EnergyDTO = {
|
||||
values: number[];
|
||||
zones: Record<string, number>;
|
||||
zones: Record<string, number|Zone>;
|
||||
sum: number;
|
||||
chartZones: Record<string, boolean[]>;
|
||||
labels: number[];
|
||||
tooltipLabels: string[];
|
||||
zonesName: Record<string, string>;
|
||||
tariff: string;
|
||||
}
|
||||
labelsRotate?: boolean;
|
||||
aCnt?: Record<string, number>;
|
||||
aCnt_sum?: number
|
||||
average?: number
|
||||
};
|
||||
|
||||
export type Zone = {
|
||||
EC: number;
|
||||
Name: string;
|
||||
ZONE: number;
|
||||
};
|
||||
|
||||
export type PowerDTO = {
|
||||
DATA: string;
|
||||
CZAS: string;
|
||||
MOC_MAX: string;
|
||||
MOC_UMOWNA: string;
|
||||
MOC_SREDNIA: string;
|
||||
};
|
||||
|
||||
export type ReadingDTO = {
|
||||
S1: string;
|
||||
Date: string;
|
||||
S2: string;
|
||||
S3: string;
|
||||
C: number;
|
||||
JM: string;
|
||||
};
|
||||
|
||||
export type EnergyRequestDTO = {
|
||||
from: string;
|
||||
to: string;
|
||||
type: 'consum' | 'average' | 'temperature';
|
||||
profile: 'full time' | 'range' | 'year';
|
||||
value?: 'G11' | 'G12' | 'G12as' | 'G12w' | 'G13';
|
||||
};
|
||||
|
||||
export type PowerRequestDTO = {
|
||||
from: string;
|
||||
to: string;
|
||||
};
|
||||
|
||||
export type ReadingRequestDTO = {
|
||||
from: string;
|
||||
to: string;
|
||||
type: 'energia-pobrana';
|
||||
};
|
||||
Reference in New Issue
Block a user