const vbus = require('../resol-vbus');
const values = [{
valueId: 'Sensor_Regler_S3',
valueIdHash: 474103359,
valueIndex: null,
// changedValue: 2,
currentValue: null,
}];
console.log('Reading the temperature sensor 3.');
function hex(value, digits) {
return `0x${value.toString(16).padStart(digits, '0').toUpperCase()}`;
}
async function main() {
const conn = new vbus.TcpConnection({
host: '192.168.1.174',
password: 'vbus',
});
await conn.connect();
conn.on('datagram', dgram => {
const bytes = Array.from(dgram.toLiveBuffer()).map(b => hex(b, 2)).join(' ');
console.log(`- \`${bytes}\``);
console.log(` - destination address = \`${hex(dgram.destinationAddress, 4)}\``);
console.log(` - source address = \`${hex(dgram.sourceAddress, 4)}\``);
console.log(` - command = \`${hex(dgram.command, 4)}\``);
console.log(` - 16-bit parameter = \`${hex(dgram.valueId, 4)}\``);
console.log(` - 32-bit parameter = \`${hex(dgram.value, 8)}\``);
});
let dgram = await conn.waitForFreeBus();
if (!dgram) {
throw new Error(`No bus offer received`);
}
const peerAddress = dgram.sourceAddress;
// console.log(`PeerAddress = 0x${hex(peerAddress, 4)}`);
dgram = await conn.getValueById(peerAddress, 0X0861);
if (!dgram) {
throw new Error(`Unable to get changeset ID`);
}
//console.log(`Changeset ID = ${hex(dgram.value, 8)}`);
const valuesToLookup = values.filter(value => (value.valueIdHash != null) && (value.valueIndex == null));
if (valuesToLookup.length > 0) {
for (const value of valuesToLookup) {
dgram = await conn.getValueIdByIdHash(peerAddress, value.valueIdHash);
if (!dgram) {
throw new Error(`Unable to lookup value ID hash`);
}
console.log('THE VALUE->');
console.log(dgram);
value.valueIndex = dgram.valueId;
}
// Resynchronize between lookup and get/set
dgram = await conn.getValueById(peerAddress, valueIndex);
if (!dgram) {
throw new Error(`Unable to get changeset ID`);
}
console.log('TRY THE VALUE -> ');
console.log(dgram.getValue());
}
await conn.releaseBus(peerAddress);
await conn.disconnect();
}
main().then(null, err => {
console.error(err);
process.exit(1);
});