Refactor code structure
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import fs from 'fs';
|
||||
import yaml from 'yaml';
|
||||
import { Config } from './types';
|
||||
import { Config } from "@types";
|
||||
|
||||
export * from './types';
|
||||
export * from '@types';
|
||||
|
||||
export const parseConfig = (path: string): Config => {
|
||||
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 { Measurement, ConsumerConfig } from "@types";
|
||||
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;
|
||||
protected abstract requiredFields: readonly (keyof C)[];
|
||||
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) {
|
||||
if (config[field] === undefined) {
|
||||
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 {
|
||||
const cfg = config.consumers?.[this.name] as Partial<C>;
|
||||
const cfg = config.consumers?.[this.name] as Partial<C & ConsumerConfig>;
|
||||
|
||||
if (cfg?.enable !== true) {
|
||||
return
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Dayjs } from "dayjs";
|
||||
import { Config } from "../config";
|
||||
import { Config, Measurement } from "@types";
|
||||
import { MQTTConsumer } from "./mqtt";
|
||||
import { Measurement } from "../fetcher/types";
|
||||
import { InfluxDBConsumer } from "./influxdb";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { readFileSync } from "fs";
|
||||
import { InfluxDB, Point } from "@influxdata/influxdb-client";
|
||||
import { Consumer } from "../abstract";
|
||||
import { InfluxDBConfig } from "./types";
|
||||
import { InfluxDBConfig, Measurement } from "@types";
|
||||
import { Consumer } from "./abstract";
|
||||
import { Dayjs } from "dayjs";
|
||||
import { Measurement } from "../../fetcher/types";
|
||||
|
||||
export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
||||
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 mqtt from "mqtt";
|
||||
import { Measurement } from "../../fetcher/types";
|
||||
import { Measurement } from "@types";
|
||||
import { Dayjs } from 'dayjs';
|
||||
import { MQTTConfig } from './types';
|
||||
import { Consumer } from '../abstract';
|
||||
import { MQTTConfig } from '../types/mqtt';
|
||||
import { Consumer } from './abstract';
|
||||
|
||||
export * from './types';
|
||||
export * from '../types/mqtt';
|
||||
|
||||
export class MQTTConsumer extends Consumer<MQTTConfig> {
|
||||
public name = "mqtt";
|
||||
@@ -1,6 +1,6 @@
|
||||
import dayjs, { Dayjs } from "dayjs";
|
||||
import { Measurement } from "@types";
|
||||
import { type Tauron } from "../tauron";
|
||||
import { Measurement } from "./types";
|
||||
import { consume } from "../consumers";
|
||||
import { Config } from "../config";
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import fs from 'fs';
|
||||
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 { Dayjs } from 'dayjs';
|
||||
import { withCookies } from './cookie';
|
||||
|
||||
export * from './types';
|
||||
export * from '../types/tauron';
|
||||
|
||||
const LOGIN_API = `https://logowanie.tauron-dystrybucja.pl/login`;
|
||||
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 { ConsumerConfig } from "../types";
|
||||
|
||||
export type MQTTConfig = ConsumerConfig & {
|
||||
export type MQTTConfig = {
|
||||
|
||||
/**
|
||||
* 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> = {
|
||||
success: boolean;
|
||||
data: T;
|
||||
@@ -9,7 +9,10 @@
|
||||
"typeRoots": [
|
||||
"./src/types",
|
||||
"./node_modules/@types"
|
||||
]
|
||||
],
|
||||
"paths": {
|
||||
"@types": ["./src/types"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": [
|
||||
|
||||
Reference in New Issue
Block a user