Update reading secrets from config file
This commit is contained in:
10
module.nix
10
module.nix
@@ -23,8 +23,8 @@ in {
|
|||||||
timezone = "Europe/Warsaw";
|
timezone = "Europe/Warsaw";
|
||||||
|
|
||||||
tauron = {
|
tauron = {
|
||||||
usernamePath = "/run/tauron.username.key";
|
username = "$_file:/run/tauron.username.key";
|
||||||
passwordPath = "/run/tauron.password.key";
|
password = "$_file:/run/tauron.password.key";
|
||||||
point = "123456";
|
point = "123456";
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ in {
|
|||||||
influxdb = {
|
influxdb = {
|
||||||
enable = true;
|
enable = true;
|
||||||
databaseURL = "https://influxdb.lan";
|
databaseURL = "https://influxdb.lan";
|
||||||
apiToken = "/run/mqtt/influxdb.token.key";
|
apiToken = "$__file:/run/mqtt/influxdb.token.key";
|
||||||
organization = "home";
|
organization = "home";
|
||||||
bucket = "tauron";
|
bucket = "tauron";
|
||||||
};
|
};
|
||||||
@@ -40,8 +40,8 @@ in {
|
|||||||
mqtt = {
|
mqtt = {
|
||||||
enable = true;
|
enable = true;
|
||||||
brokerURL = "https://mqtt.lan";
|
brokerURL = "https://mqtt.lan";
|
||||||
usernamePath = "/run/mqtt.username.key";
|
username = "$__file:/run/mqtt.username.key";
|
||||||
passwordPath = "/run/mqtt.password.key";
|
password = "$__file:/run/mqtt.password.key";
|
||||||
clientId = "tauron-scrapper";
|
clientId = "tauron-scrapper";
|
||||||
prefix = "tauron";
|
prefix = "tauron";
|
||||||
publishOptions = {
|
publishOptions = {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { InfluxDB, Point } from "@influxdata/influxdb-client";
|
|||||||
import { InfluxDBConfig, Measurement } from "@types";
|
import { InfluxDBConfig, Measurement } from "@types";
|
||||||
import { Consumer } from "./abstract";
|
import { Consumer } from "./abstract";
|
||||||
import { Dayjs } from "dayjs";
|
import { Dayjs } from "dayjs";
|
||||||
|
import { enhancedStringConfig } from "../utils";
|
||||||
|
|
||||||
export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
||||||
public name = 'influxdb';
|
public name = 'influxdb';
|
||||||
@@ -17,7 +18,7 @@ export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
|||||||
protected async publish({ databaseURL, apiToken, organization, bucket }: InfluxDBConfig, date: Dayjs, measurement: Measurement) {
|
protected async publish({ databaseURL, apiToken, organization, bucket }: InfluxDBConfig, date: Dayjs, measurement: Measurement) {
|
||||||
const db = new InfluxDB({
|
const db = new InfluxDB({
|
||||||
url: databaseURL,
|
url: databaseURL,
|
||||||
token: readFileSync(apiToken, 'utf8'),
|
token: enhancedStringConfig(apiToken),
|
||||||
});
|
});
|
||||||
|
|
||||||
const api = db.getWriteApi(organization, bucket);
|
const api = db.getWriteApi(organization, bucket);
|
||||||
|
|||||||
@@ -4,17 +4,18 @@ import { Measurement } from "@types";
|
|||||||
import { Dayjs } from 'dayjs';
|
import { Dayjs } from 'dayjs';
|
||||||
import { MQTTConfig } from '../types/mqtt';
|
import { MQTTConfig } from '../types/mqtt';
|
||||||
import { Consumer } from './abstract';
|
import { Consumer } from './abstract';
|
||||||
|
import { enhancedStringConfig } from '../utils';
|
||||||
|
|
||||||
export * from '../types/mqtt';
|
export * from '../types/mqtt';
|
||||||
|
|
||||||
export class MQTTConsumer extends Consumer<MQTTConfig> {
|
export class MQTTConsumer extends Consumer<MQTTConfig> {
|
||||||
public name = "mqtt";
|
public name = "mqtt";
|
||||||
protected requiredFields = ['brokerURL', 'usernamePath', 'passwordPath'] as const;
|
protected requiredFields = ['brokerURL', 'username', 'password'] as const;
|
||||||
|
|
||||||
protected publish({ brokerURL, usernamePath, passwordPath, clientId, prefix, publishOptions }: MQTTConfig, date: Dayjs, measurement: Measurement) {
|
protected publish({ brokerURL, username, password, clientId, prefix, publishOptions }: MQTTConfig, date: Dayjs, measurement: Measurement) {
|
||||||
const client = mqtt.connect(brokerURL, {
|
const client = mqtt.connect(brokerURL, {
|
||||||
username: fs.readFileSync(usernamePath, 'utf8'),
|
username: enhancedStringConfig(username),
|
||||||
password: fs.readFileSync(passwordPath, 'utf8'),
|
password: enhancedStringConfig(password),
|
||||||
clientId: clientId || "tauron-scrapper"
|
clientId: clientId || "tauron-scrapper"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, Readin
|
|||||||
import { TauronConfig } from '../config';
|
import { TauronConfig } from '../config';
|
||||||
import { Dayjs } from 'dayjs';
|
import { Dayjs } from 'dayjs';
|
||||||
import { withCookies } from './cookie';
|
import { withCookies } from './cookie';
|
||||||
|
import { enhancedStringConfig } from '../utils';
|
||||||
|
|
||||||
export * from '../types/tauron';
|
export * from '../types/tauron';
|
||||||
|
|
||||||
@@ -55,8 +56,8 @@ export class Tauron {
|
|||||||
await this.#http.get(LOGIN_API);
|
await this.#http.get(LOGIN_API);
|
||||||
|
|
||||||
await this.#http.postForm(LOGIN_API, {
|
await this.#http.postForm(LOGIN_API, {
|
||||||
username: fs.readFileSync(this.#config.usernamePath, 'utf8'),
|
username: enhancedStringConfig(this.#config.username),
|
||||||
password: fs.readFileSync(this.#config.passwordPath, 'utf8'),
|
password: enhancedStringConfig(this.#config.password),
|
||||||
service: BASE_URL
|
service: BASE_URL
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ export type MQTTConfig = {
|
|||||||
/**
|
/**
|
||||||
* Path to file containing a username of MQTT user.
|
* Path to file containing a username of MQTT user.
|
||||||
*/
|
*/
|
||||||
usernamePath: string;
|
username: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to file containing a password of MQTT user.
|
* Path to file containing a password of MQTT user.
|
||||||
*/
|
*/
|
||||||
passwordPath: string;
|
password: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional client ID used to connect to MQTT (visible in MQTT broker logs).
|
* Optional client ID used to connect to MQTT (visible in MQTT broker logs).
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ export type TauronConfig = {
|
|||||||
/**
|
/**
|
||||||
* Path to file containing a username of Tauron account
|
* Path to file containing a username of Tauron account
|
||||||
*/
|
*/
|
||||||
usernamePath: string;
|
username: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to file containing a password of Tauron account
|
* Path to file containing a password of Tauron account
|
||||||
*/
|
*/
|
||||||
passwordPath: string;
|
password: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The measure point name - should be retrieved with browser dev-tools on website
|
* The measure point name - should be retrieved with browser dev-tools on website
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { readFileSync } from "fs";
|
||||||
|
|
||||||
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
export const gaussianRandom = (mean = 0, stddev = 1) => {
|
export const gaussianRandom = (mean = 0, stddev = 1) => {
|
||||||
@@ -6,3 +8,19 @@ export const gaussianRandom = (mean = 0, stddev = 1) => {
|
|||||||
let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2);
|
let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2);
|
||||||
return z0 * stddev + mean;
|
return z0 * stddev + mean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const specialOptions: Record<string, (text: string) => string> = {
|
||||||
|
$__file: (arg: string) => readFileSync(arg, 'utf8').trim()
|
||||||
|
};
|
||||||
|
|
||||||
|
export const enhancedStringConfig = (value: string) => {
|
||||||
|
const trimmed = value.trim();
|
||||||
|
|
||||||
|
for(const opt of Object.keys(specialOptions)) {
|
||||||
|
if(trimmed.startsWith(`${opt}:`) && opt in specialOptions) {
|
||||||
|
return specialOptions[opt](trimmed.slice(opt.length + 1).trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return trimmed;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user