smartmeter2mqtt
Version:
Publish data from your Smartmeter with a P1 interface to your MQTT server.
78 lines • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var crc_1 = require("crc");
var p1_map_1 = __importDefault(require("./p1-map"));
var P1Parser = /** @class */ (function () {
function P1Parser() {
this.currentMessage = '';
this.partialData = {
crc: false,
};
}
/**
* Add the next line to the parser, returns true if complete message is ready
*
* @param {string} line
* @returns {boolean}
* @memberof P1Parser
*/
P1Parser.prototype.addLine = function (line) {
var isEnd = P1Parser.isEnd(line);
if (line.length > 0) {
if (!isEnd) {
this.currentMessage += line + "\r\n"; // Append line to compute CRC
}
if (P1Parser.isStart(line)) {
this.partialData.header = line.substr(1);
this.currentMessage += '\r\n';
}
else if (isEnd) {
// Always to crc check, it can fail, we still want te result.
var calculatedCrc = crc_1.crc16(this.currentMessage + "!").toString(16).toUpperCase();
// console.log('Calculated CRC %s line: %s', calculatedCrc, line)
this.partialData.crc = calculatedCrc === line.substr(1);
this.currentMessage += line + "\r\n";
return true;
}
else {
var parsed = p1_map_1.default.parseLine(line);
if (parsed && parsed.name) {
if (parsed.value !== undefined) {
this.partialData[parsed.name] = parsed.value;
}
else if (parsed.rawValues !== undefined) {
this.partialData[parsed.name] = parsed.rawValues;
}
}
}
}
return false;
};
Object.defineProperty(P1Parser.prototype, "data", {
get: function () {
return this.partialData;
},
enumerable: true,
configurable: true
});
Object.defineProperty(P1Parser.prototype, "message", {
get: function () {
return this.currentMessage;
},
enumerable: true,
configurable: true
});
// Statics
P1Parser.isStart = function (line) {
return line.length > 0 && line.startsWith('/');
};
P1Parser.isEnd = function (line) {
return line.length > 0 && line.startsWith('!');
};
return P1Parser;
}());
exports.default = P1Parser;
//# sourceMappingURL=p1-parser.js.map