Refactor code structure

This commit is contained in:
2024-11-14 14:37:36 +01:00
parent b94940e8fd
commit c9e82fd001
17 changed files with 66 additions and 63 deletions

View File

@@ -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');

View File

@@ -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;
};

View File

@@ -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

View File

@@ -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";
/** /**

View File

@@ -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';

View File

@@ -1,8 +0,0 @@
import { ConsumerConfig } from "../types";
export type InfluxDBConfig = ConsumerConfig & {
databaseURL: string;
apiToken: string;
organization: string;
bucket: string;
};

View File

@@ -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";

View File

@@ -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";

View File

@@ -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
View 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
View 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
View File

@@ -0,0 +1,6 @@
export type InfluxDBConfig = {
databaseURL: string;
apiToken: string;
organization: string;
bucket: string;
};

View File

@@ -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.

View File

@@ -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;

View File

@@ -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": [