@andrei-tatar/node-red-contrib-nibetcp
Version:
Node-red integration for Nibe S series heatpumps using modbus over TCP
48 lines (47 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const nibe_1 = require("./communication/nibe");
const IGNORE = [42176, 33224, 33304, 33344, 33624];
nibe_1.Nibe.createTcp("tcp://NIBE-06545022180002.local:502", "./registers.csv")
.pipe((0, operators_1.concatMap)((n) => n.registers$.pipe((0, operators_1.tap)(() => console.log(`scan start`)), (0, operators_1.concatMap)((regs) => {
const start = new Date().getTime();
return readAllRegisters(n, regs).pipe((0, operators_1.map)((values) => {
return {
values,
seconds: Math.round((new Date().getTime() - start) / 100) / 10,
};
}));
}), (0, operators_1.tap)(({ values, seconds }) => console.log(`scan done, read ${values.length} values in ${seconds} sec`)), (0, operators_1.map)(({ values }) => values), (0, operators_1.repeat)({ delay: 5000 }))), keepOnlyChangedValues(), (0, operators_1.tap)((changedValues) => {
for (const value of changedValues) {
console.log(`'${value.label}' changed to ${value.formatted}`);
}
}))
.subscribe();
function readAllRegisters(nibe, regs) {
return (0, rxjs_1.from)(regs).pipe((0, operators_1.filter)((reg) => !IGNORE.includes(reg.address)), (0, operators_1.mergeMap)((reg) => nibe.readRegister(reg.address, 1000).pipe((0, operators_1.catchError)((err) => {
console.warn(`failed reading ${reg.address}: ${err}`);
return rxjs_1.EMPTY;
})), 20), (0, operators_1.toArray)());
}
function keepOnlyChangedValues() {
return (source) => source.pipe((0, operators_1.scan)((ctx, currentValues) => {
ctx.changes = [];
for (const current of currentValues) {
const lastIndex = ctx.lastValues.findIndex((v) => v.label === current.label);
if (lastIndex >= 0) {
const last = ctx.lastValues[lastIndex];
if (last.value !== current.value) {
ctx.changes.push(current);
}
ctx.lastValues.splice(lastIndex, 1);
}
}
ctx.lastValues.push(...currentValues);
return ctx;
}, {
lastValues: [],
changes: [],
}), (0, operators_1.skip)(1), (0, operators_1.map)((v) => v.changes));
}