Skip to content

Commit

Permalink
feat: Enabled support for TCP meter device
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed Feb 23, 2021
1 parent 0e6c6aa commit f135572
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module.exports = {
"class-methods-use-this":"off",
"max-len":"off",
"no-console": "off",
"linebreak-style": "off"
}
};
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,17 @@ For a **direct connection** you'll need a Smartmeter cable like [this one at sos
### TCP socket
You can also connect to a **TCP socket**, this way you don't need the device running this program to be on a device near your meter. You can also check out this [ESP8266 P1 reader](http://www.esp8266thingies.nl/wp/), it creates a TCP socket for your meter.
You can also connect to a **TCP socket**, this way you don't need the device running this program to be on a device near your meter.
Tested with:
| Device | Meter Type | User | Date |
|:-------|:-----------|:-----|:-----|
|[Slimme lezer](https://www.zuidwijk.com/slimme-lezer-smart-reader/?referal=svrooij) by Marcel Zuidwijk | DSMR 4.2 | [@svrooij](https://svrooij.io) | `2021-02-23` |
I'm tempted to recommend the device above since it doesn't require your pi to be near your smartmeter. The latest version of the **Slimme lezer** in combination with a DSMR 5 meter, doesn't even require an external power source.
This app can also [output a raw socket](#output---raw-tcp-socket) so you can use one instance running all the time and have a second instance connected to the socket for debugging.
### Solar panel inverter (optional)
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@
"smartmeter2mqtt": "./dist/index.js"
},
"files": [
"LICENSE",
"README.md",
"package.json",
"dist"
"dist/**/*.js",
"dist/output/wwwroot"
]
}
2 changes: 1 addition & 1 deletion src/output/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export default abstract class Output extends EventEmitter {
/**
* close is where you would close your output, it needed.
*/
abstract async close(): Promise<void>;
abstract close(): Promise<void>;
}
26 changes: 20 additions & 6 deletions src/p1-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default class P1Reader extends EventEmitter {

private parser?: P1Parser;

private dataBuffer = '';

private bufferInterval?: NodeJS.Timeout;

// Inverter stuff
private solarInput?: SolarInput;

Expand Down Expand Up @@ -62,10 +66,17 @@ export default class P1Reader extends EventEmitter {
this.socket.connect(port, host);
this.socket.setEncoding('ascii');
this.socket.on('data', (data) => {
const lines = data.toString().trim().split('\n');
lines.forEach((line) => {
this.emit(P1ReaderEvents.Line, line);
});
if (this.bufferInterval) {
clearTimeout(this.bufferInterval);
}
this.dataBuffer += data.toString();
this.bufferInterval = setTimeout(() => {
const lines = this.dataBuffer.trim().split('\r\n');
lines.forEach((line) => {
this.emit(P1ReaderEvents.Line, line);
});
this.dataBuffer = '';
}, 100);
});

this.socket.on('close', () => {
Expand Down Expand Up @@ -174,10 +185,13 @@ export default class P1Reader extends EventEmitter {
if (this.solarInput) {
this.solarInput = undefined;
}
return new Promise((resolve) => {
return new Promise<void>((resolve, reject) => {
this.reading = false;
if (this.serialPort) {
this.serialPort.close(resolve);
this.serialPort.close((err) => {
if (err) reject(err);
else resolve();
});
} else if (this.socket) {
this.socket.destroy();
resolve();
Expand Down
12 changes: 6 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
Expand All @@ -24,7 +24,7 @@

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
Expand All @@ -35,7 +35,7 @@
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
Expand Down

0 comments on commit f135572

Please sign in to comment.