smartmeter2mqtt
Version:
Publish data from your Smartmeter with a P1 interface to your MQTT server.
70 lines • 3.05 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var tcp_server_1 = require("@svrooij/tcp-server");
var output_1 = __importDefault(require("./output"));
var p1_reader_events_1 = __importDefault(require("../p1-reader-events"));
var TcpOutput = /** @class */ (function (_super) {
__extends(TcpOutput, _super);
function TcpOutput(port, raw, startServer) {
if (raw === void 0) { raw = false; }
if (startServer === void 0) { startServer = true; }
var _this = _super.call(this) || this;
_this.port = port;
_this.raw = raw;
_this.startServer = startServer;
return _this;
}
TcpOutput.prototype.start = function (p1Reader) {
var _this = this;
var _a, _b, _c;
if (p1Reader === undefined) {
throw new Error('p1Reader is undefined!');
}
this.server = new tcp_server_1.TcpServer({ port: this.port, host: '0.0.0.0', maxConnections: 3 });
if (this.raw) {
p1Reader.on(p1_reader_events_1.default.Line, function (line) {
var _a;
(_a = _this.server) === null || _a === void 0 ? void 0 : _a.publish(line + "\r\n");
});
}
else {
p1Reader.on(p1_reader_events_1.default.ParsedResult, function (data) {
var _a;
(_a = _this.server) === null || _a === void 0 ? void 0 : _a.publishAsJson(data, '\n');
});
}
if (this.startServer === true) {
(_a = this.server) === null || _a === void 0 ? void 0 : _a.start();
(_b = this.server) === null || _b === void 0 ? void 0 : _b.on('onDisconnect', function (address) {
console.log("Output: Socket, connection " + address + " closed");
});
(_c = this.server) === null || _c === void 0 ? void 0 : _c.on('onConnect', function (socket) {
console.log("Output: Socket, new connection from " + socket.remoteAddress);
});
}
};
TcpOutput.prototype.close = function () {
var _a;
(_a = this.server) === null || _a === void 0 ? void 0 : _a.stop();
return Promise.resolve();
};
return TcpOutput;
}(output_1.default));
exports.default = TcpOutput;
//# sourceMappingURL=tcp-output.js.map