Lösung um Werte zu lesen:
Code
const DigestFetch = require('digest-fetch').default; // npm install digest-fetch
//console.log({'📦 Modul geladen': typeof DigestFetch}); // Sollte "function" sein
const username = 'xxx';
const password = 'xxx';
const ip = 'xxx.xxx.xxx.xx';
const client = new DigestFetch(username, password, { algorithm: 'SHA-256' });
//console.log({'🔐 DigestFetch client erstellt:': typeof client.fetch}); // Sollte "function" sein
(async () => {
try {
const url = `http://${ip}/rpc/BluTrv.GetRemoteStatus?id=200`;
//console.info({'➡️ URL: ': url});
const res = await client.fetch(url, { method: 'GET' });
//console.log('✅ Status: ' + res.status);
const text = await res.text();
//console.info({'📄 Text: ': text});
//console.info('📄 Text: ' + text); // Alternative Ausgabe
const data = JSON.parse(text);
TWohnzimmer = data?.status?.["trv:0"]?.["current_C"]; //status.trv:0.current_C
POSWohnzimmer = data?.status?.["trv:0"]?.["pos"]; //status.trv:0.pos
TIMEWohnzimmer = data?.["ts"]; //ts
TARGETWohnzimmer = data?.status?.["trv:0"]?.["target_C"]; //status.trv:0.target_C
} catch (err) {
//console.error({'❌ Fehler: ': err.message});
}
})();
Alles anzeigen
Lösung um Werte zu schreiben:
Code
const DigestFetch = require('digest-fetch').default;
const username = 'xxx';
const password = 'xxx';
const baseUrl = 'http://xxx.xxx.xxx.xx';
const client = new DigestFetch(username, password, { algorithm: 'SHA-256' });
const rpcBody = {
id: 1,
method: 'BluTrv.Call',
params: {
id: 200,
method: 'TRV.SetTarget',
params: {
id: 0,
target_C: TSETWohnzimmer
}
}
};
(async () => {
try {
const res = await client.fetch(`${baseUrl}/rpc/`, {
method: 'POST',
body: JSON.stringify(rpcBody),
headers: {
'Content-Type': 'application/json'
}
});
const data = await res.json();
const datastr = JSON.stringify(data); // wandelt ein JavaScript-Objekt oder einen Wert in einen JSON-formatierten String um
//const dataparse = JSON.parse(datastr); // wandelt einen JSON-formatierten String in ein JavaScript-Objekt
console.info('✅ Antwort Wohnzimmer:'+ datastr);
if ('result' in data && data.result === null) {
console.info('✅ Temperatursollwert fürs Wohnzimmer (' + TSETWohnzimmer + '°C) wurde geschrieben.');
} else if ('error' in data) {
console.warn('❌ Temperatursollwert fürs Wohnzimmer (' + TSETWohnzimmer + '°C) wurde nicht geschrieben.');
}
} catch (err) {
console.error({'❌ Fehler Wohnzimmer:': err});
}
})();
Alles anzeigen