Skip to content

Commit

Permalink
feat: Add gasUsage topic (#12)
Browse files Browse the repository at this point in the history
Support for gas usage by @zegerk 

Co-authored-by: Zeger Knops <15801866+zegerk@users.noreply.github.com>
  • Loading branch information
zegerk and majinonifox1 committed Jul 31, 2020
1 parent af46a0d commit 0e6c6aa
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/output/debug-output.ts
Expand Up @@ -18,6 +18,9 @@ export default class DebugOutput extends Output {
p1Reader.on(P1ReaderEvents.UsageChanged, (result) => {
console.log(' - usageChange %s', result.message);
});
p1Reader.on(P1ReaderEvents.GasUsageChanged, (result) => {
console.log(' - gasUsageChange %s', result.message);
});
p1Reader.on(P1ReaderEvents.ErrorMessage, (message) => {
console.log(' - errorMessage %s', message);
});
Expand Down
3 changes: 3 additions & 0 deletions src/output/interval-output.ts
Expand Up @@ -28,6 +28,9 @@ export default class IntervalOutput extends Output {
p1Reader.on(P1ReaderEvents.UsageChanged, (result) => {
this.emit(P1ReaderEvents.UsageChanged, result);
});
p1Reader.on(P1ReaderEvents.GasUsageChanged, (result) => {
this.emit(P1ReaderEvents.GasUsageChanged, result);
});

this.timer = setInterval(() => {
this.publishNextEvent = true;
Expand Down
12 changes: 12 additions & 0 deletions src/output/mqtt-output.ts
Expand Up @@ -34,6 +34,10 @@ export default class MqttOutput extends Output {
this.publishUsage(data);
});

p1Reader.on(P1ReaderEvents.GasUsageChanged, (data) => {
this.publishGasUsage(data);
});

p1Reader.on(P1ReaderEvents.SolarResult, (data) => {
this.publishSolar(data);
});
Expand Down Expand Up @@ -68,6 +72,14 @@ export default class MqttOutput extends Output {
this.mqtt?.publish(this.getTopic('usage'), JSON.stringify(message), { qos: 0, retain: false });
}

private publishGasUsage(data: any): void {
const message = data;
message.val = data.currentUsage;
delete message.currentUsage;
message.tc = Date.now();
this.mqtt?.publish(this.getTopic('gasUsage'), JSON.stringify(message), { qos: 0, retain: false });
}

private publishData(data: DsmrMessage): void {
if (this.config.distinct) {
this.config.distinctFields.forEach((element) => {
Expand Down
2 changes: 2 additions & 0 deletions src/p1-reader-events.ts
Expand Up @@ -14,5 +14,7 @@ export default class P1ReaderEvents {
/** Usage change is emitted after the parsed result. It keeps the last result to compare. */
static get UsageChanged(): string { return 'usageChanged'; }

static get GasUsageChanged(): string { return 'gasUsageChanged'; }

static get SolarResult(): string { return 'solar'; }
}
48 changes: 48 additions & 0 deletions src/p1-reader.ts
Expand Up @@ -5,10 +5,17 @@ import P1Parser from './p1-parser';
import P1ReaderEvents from './p1-reader-events';
import DsmrMessage from './dsmr-message';
import SolarInput from './solar-input';
import GasValue from './gas-value';

export default class P1Reader extends EventEmitter {
private usage: number;

private gasUsage: number;

private gasReadingTimestamp: number;

private gasReading: number;

private reading: boolean;

private parsing: boolean;
Expand All @@ -31,6 +38,9 @@ export default class P1Reader extends EventEmitter {
constructor() {
super();
this.usage = 0;
this.gasUsage = 0;
this.gasReading = 0;
this.gasReadingTimestamp = 0;
this.reading = false;
this.parsing = false;
}
Expand Down Expand Up @@ -120,6 +130,44 @@ export default class P1Reader extends EventEmitter {
});
this.usage = newUsage;
}

/**
* Handle the gas value - this is a bit different from electricity usage, the meter does not
* indicate the actual gas usage in m3/hour but only submits the meter reading every XX minutes
*/
const gas = result.xGas ?? result.gas;
if (gas) {
const currentGasReadingTimestamp = (new Date(((gas as GasValue)).ts ?? 0).getTime() / 1000);
const period = currentGasReadingTimestamp - this.gasReadingTimestamp;
/**
* Report for every new timestamp
*/
if (period) {
const newGasReading = ((gas as GasValue).totalUse ?? 0);
const relative = this.gasReading ? (newGasReading - this.gasReading) : 0;
let newGasUsage = 0;

/**
* Gas usage in m3 per hour
*/
newGasUsage = relative * (3600 / period);

/**
* Gas usage is measured in thousands (0.001) - round the numbers
* accordingly
*/
this.emit(P1ReaderEvents.GasUsageChanged, {
previousUsage: parseFloat(this.gasUsage.toFixed(3)),
currentUsage: parseFloat(newGasUsage.toFixed(3)),
relative: parseFloat(relative.toFixed(3)),
message: `Reading increased +${relative} to ${newGasReading}`,
});

this.gasReadingTimestamp = currentGasReadingTimestamp;
this.gasReading = newGasReading;
this.gasUsage = newGasUsage;
}
}
}

public close(): Promise<void> {
Expand Down

0 comments on commit 0e6c6aa

Please sign in to comment.