Refactor code structure
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import yaml from 'yaml';
|
import yaml from 'yaml';
|
||||||
import { Config } from './types';
|
import { Config } from "@types";
|
||||||
|
|
||||||
export * from './types';
|
export * from '@types';
|
||||||
|
|
||||||
export const parseConfig = (path: string): Config => {
|
export const parseConfig = (path: string): Config => {
|
||||||
const text = fs.readFileSync(path, 'utf8');
|
const text = fs.readFileSync(path, 'utf8');
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
import { ConsumerConfig } from "../consumers/types";
|
|
||||||
|
|
||||||
export type Config = {
|
|
||||||
/**
|
|
||||||
* Tauron service configuration
|
|
||||||
*/
|
|
||||||
tauron: TauronConfig;
|
|
||||||
|
|
||||||
consumers: Record<string, unknown>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type TauronConfig = ConsumerConfig & {
|
|
||||||
/**
|
|
||||||
* Path to file containing a username of Tauron account
|
|
||||||
*/
|
|
||||||
usernamePath: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path to file containing a password of Tauron account
|
|
||||||
*/
|
|
||||||
passwordPath: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The measure point name - should be retrieved with browser dev-tools on website
|
|
||||||
*/
|
|
||||||
point: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The path to the json file with cookie jar
|
|
||||||
*/
|
|
||||||
cookiesJarPath?: string;
|
|
||||||
};
|
|
||||||
@@ -1,25 +1,24 @@
|
|||||||
import { Dayjs } from "dayjs";
|
import { Dayjs } from "dayjs";
|
||||||
|
import { Measurement, ConsumerConfig } from "@types";
|
||||||
import { Config } from "../config";
|
import { Config } from "../config";
|
||||||
import { Measurement } from "../fetcher/types";
|
|
||||||
import { ConsumerConfig } from "./types";
|
|
||||||
|
|
||||||
export abstract class Consumer<C extends ConsumerConfig> {
|
export abstract class Consumer<C> {
|
||||||
public abstract readonly name: string;
|
public abstract readonly name: string;
|
||||||
protected abstract requiredFields: readonly (keyof C)[];
|
protected abstract requiredFields: readonly (keyof C)[];
|
||||||
protected abstract publish(config: C, date: Dayjs, measurement: Measurement): void;
|
protected abstract publish(config: C, date: Dayjs, measurement: Measurement): void;
|
||||||
|
|
||||||
#validate(config: Partial<C>): C {
|
#validate(config: Partial<C & ConsumerConfig>): C & ConsumerConfig {
|
||||||
for (const field of this.requiredFields) {
|
for (const field of this.requiredFields) {
|
||||||
if (config[field] === undefined) {
|
if (config[field] === undefined) {
|
||||||
throw new Error(`The '${String(field)}' configuration field of'${this.name}' consumer is required`)
|
throw new Error(`The '${String(field)}' configuration field of'${this.name}' consumer is required`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config as C;
|
return config as C & ConsumerConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
consume(config: Config, date: Dayjs, measurement: Measurement): void {
|
consume(config: Config, date: Dayjs, measurement: Measurement): void {
|
||||||
const cfg = config.consumers?.[this.name] as Partial<C>;
|
const cfg = config.consumers?.[this.name] as Partial<C & ConsumerConfig>;
|
||||||
|
|
||||||
if (cfg?.enable !== true) {
|
if (cfg?.enable !== true) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Dayjs } from "dayjs";
|
import { Dayjs } from "dayjs";
|
||||||
import { Config } from "../config";
|
import { Config, Measurement } from "@types";
|
||||||
import { MQTTConsumer } from "./mqtt";
|
import { MQTTConsumer } from "./mqtt";
|
||||||
import { Measurement } from "../fetcher/types";
|
|
||||||
import { InfluxDBConsumer } from "./influxdb";
|
import { InfluxDBConsumer } from "./influxdb";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { InfluxDB, Point } from "@influxdata/influxdb-client";
|
import { InfluxDB, Point } from "@influxdata/influxdb-client";
|
||||||
import { Consumer } from "../abstract";
|
import { InfluxDBConfig, Measurement } from "@types";
|
||||||
import { InfluxDBConfig } from "./types";
|
import { Consumer } from "./abstract";
|
||||||
import { Dayjs } from "dayjs";
|
import { Dayjs } from "dayjs";
|
||||||
import { Measurement } from "../../fetcher/types";
|
|
||||||
|
|
||||||
export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
||||||
public name = 'influxdb';
|
public name = 'influxdb';
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { ConsumerConfig } from "../types";
|
|
||||||
|
|
||||||
export type InfluxDBConfig = ConsumerConfig & {
|
|
||||||
databaseURL: string;
|
|
||||||
apiToken: string;
|
|
||||||
organization: string;
|
|
||||||
bucket: string;
|
|
||||||
};
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import mqtt from "mqtt";
|
import mqtt from "mqtt";
|
||||||
import { Measurement } from "../../fetcher/types";
|
import { Measurement } from "@types";
|
||||||
import { Dayjs } from 'dayjs';
|
import { Dayjs } from 'dayjs';
|
||||||
import { MQTTConfig } from './types';
|
import { MQTTConfig } from '../types/mqtt';
|
||||||
import { Consumer } from '../abstract';
|
import { Consumer } from './abstract';
|
||||||
|
|
||||||
export * from './types';
|
export * from '../types/mqtt';
|
||||||
|
|
||||||
export class MQTTConsumer extends Consumer<MQTTConfig> {
|
export class MQTTConsumer extends Consumer<MQTTConfig> {
|
||||||
public name = "mqtt";
|
public name = "mqtt";
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import dayjs, { Dayjs } from "dayjs";
|
import dayjs, { Dayjs } from "dayjs";
|
||||||
|
import { Measurement } from "@types";
|
||||||
import { type Tauron } from "../tauron";
|
import { type Tauron } from "../tauron";
|
||||||
import { Measurement } from "./types";
|
|
||||||
import { consume } from "../consumers";
|
import { consume } from "../consumers";
|
||||||
import { Config } from "../config";
|
import { Config } from "../config";
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import axios, { Axios, AxiosResponse } from "axios";
|
import axios, { Axios, AxiosResponse } from "axios";
|
||||||
import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types";
|
import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "@types";
|
||||||
import { TauronConfig } from '../config';
|
import { TauronConfig } from '../config';
|
||||||
import { Dayjs } from 'dayjs';
|
import { Dayjs } from 'dayjs';
|
||||||
import { withCookies } from './cookie';
|
import { withCookies } from './cookie';
|
||||||
|
|
||||||
export * from './types';
|
export * from '../types/tauron';
|
||||||
|
|
||||||
const LOGIN_API = `https://logowanie.tauron-dystrybucja.pl/login`;
|
const LOGIN_API = `https://logowanie.tauron-dystrybucja.pl/login`;
|
||||||
const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl';
|
const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl';
|
||||||
|
|||||||
10
src/types/config.ts
Normal file
10
src/types/config.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { TauronConfig } from "./tauron";
|
||||||
|
|
||||||
|
export type Config = {
|
||||||
|
/**
|
||||||
|
* Tauron service configuration
|
||||||
|
*/
|
||||||
|
tauron: TauronConfig;
|
||||||
|
|
||||||
|
consumers: Record<string, unknown>;
|
||||||
|
};
|
||||||
6
src/types/index.ts
Normal file
6
src/types/index.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export * from './config';
|
||||||
|
export * from './consumer';
|
||||||
|
export * from './fetcher';
|
||||||
|
export * from './influxdb';
|
||||||
|
export * from './mqtt';
|
||||||
|
export * from './tauron';
|
||||||
6
src/types/influxdb.ts
Normal file
6
src/types/influxdb.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export type InfluxDBConfig = {
|
||||||
|
databaseURL: string;
|
||||||
|
apiToken: string;
|
||||||
|
organization: string;
|
||||||
|
bucket: string;
|
||||||
|
};
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import mqtt from "mqtt";
|
import mqtt from "mqtt";
|
||||||
import { ConsumerConfig } from "../types";
|
|
||||||
|
|
||||||
export type MQTTConfig = ConsumerConfig & {
|
export type MQTTConfig = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The URL to broker.
|
* The URL to broker.
|
||||||
@@ -1,3 +1,25 @@
|
|||||||
|
export type TauronConfig = {
|
||||||
|
/**
|
||||||
|
* Path to file containing a username of Tauron account
|
||||||
|
*/
|
||||||
|
usernamePath: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to file containing a password of Tauron account
|
||||||
|
*/
|
||||||
|
passwordPath: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The measure point name - should be retrieved with browser dev-tools on website
|
||||||
|
*/
|
||||||
|
point: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path to the json file with cookie jar
|
||||||
|
*/
|
||||||
|
cookiesJarPath?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type Payload<T> = {
|
export type Payload<T> = {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
data: T;
|
data: T;
|
||||||
@@ -9,7 +9,10 @@
|
|||||||
"typeRoots": [
|
"typeRoots": [
|
||||||
"./src/types",
|
"./src/types",
|
||||||
"./node_modules/@types"
|
"./node_modules/@types"
|
||||||
]
|
],
|
||||||
|
"paths": {
|
||||||
|
"@types": ["./src/types"]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"include": ["src/**/*"],
|
"include": ["src/**/*"],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
|||||||
Reference in New Issue
Block a user