UNPKG

smartmeter2mqtt

Version:

Publish data from your Smartmeter with a P1 interface to your MQTT server.

123 lines 4.86 kB
"use strict"; 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 http_1 = __importDefault(require("http")); var ws_1 = __importDefault(require("ws")); var path_1 = __importDefault(require("path")); var output_1 = __importDefault(require("./output")); var p1_reader_events_1 = __importDefault(require("../p1-reader-events")); var express = require("express"); var WebServer = /** @class */ (function (_super) { __extends(WebServer, _super); function WebServer(port, startServer) { if (startServer === void 0) { startServer = true; } var _this = _super.call(this) || this; _this.port = port; _this.startServer = startServer; return _this; } WebServer.prototype.start = function (p1Reader) { var _this = this; if (p1Reader === undefined) throw new Error('p1Reader is undefined!'); p1Reader.on(p1_reader_events_1.default.ParsedResult, function (data) { _this.setReading(data); }); p1Reader.on(p1_reader_events_1.default.SolarResult, function (data) { _this.lastSolarReading = data; }); if (this.startServer === true) { this.startWebserver(); } }; WebServer.prototype.startWebserver = function (startSockets) { var _this = this; if (startSockets === void 0) { startSockets = true; } var app = express(); this.server = http_1.default.createServer(app); if (startSockets) { this.wsServer = new ws_1.default.Server({ server: this.server }); this.wsServer.on('connection', function (ws) { // ws.isAlive = true // ws.on('pong', () => { // ws.isAlive = true // }) if (_this.lastReading) { ws.send(JSON.stringify(_this.lastReading)); } else { ws.send('{"err":"No reading just yet"}'); } }); } app.get('/api/reading', function (req, res) { return _this.getReading(req, res); }); app.get('/api/solar', function (req, res) { return _this.getSolarReading(req, res); }); app.use(express.static(path_1.default.join(__dirname, 'wwwroot'), { index: 'index.html' })); this.server.listen(this.port); this.checkTimeout = setInterval(function () { _this.checkSockets(); }, 10000); }; WebServer.prototype.close = function () { var _this = this; return new Promise(function (resolve) { var _a; if (_this.checkTimeout) clearInterval(_this.checkTimeout); (_a = _this.server) === null || _a === void 0 ? void 0 : _a.close(function () { resolve(); }); }); }; WebServer.prototype.getReading = function (req, res) { if (this.lastReading) { res.json(this.lastReading); } else { res.status(400).json({ err: 'No reading just yet!' }); } }; WebServer.prototype.getSolarReading = function (req, res) { if (this.lastSolarReading) { res.json(this.lastSolarReading); } else { res.status(400).json({ err: 'No reading just yet!' }); } }; WebServer.prototype.setReading = function (newReading) { this.lastReading = newReading; this.broadcastMessage(newReading); }; WebServer.prototype.checkSockets = function () { // this._sockets.clients.forEach(client => { // if (!client.isAlive) return client.terminate() // client.isAlive = false // client.ping(null, false, true) // }) }; WebServer.prototype.broadcastMessage = function (msg) { if (this.wsServer) { var readingString_1 = JSON.stringify(msg); this.wsServer.clients.forEach(function (client) { client.send(readingString_1); }); } }; return WebServer; }(output_1.default)); exports.default = WebServer; //# sourceMappingURL=web-server.js.map