thermostat-pi-dht
Version:
A Node.js framework (standalone app + lib) to control a heating system with a Raspberry Pi using DHT11 or DHT22/AM2302 sensors and GPIO actuators.
77 lines • 3.48 kB
JavaScript
;
// Project: thermostat-pi-dht
// File: updateRemoteThermostatSetpoint.ts
//
// Copyright 2021 Henning Kerstan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateRemoteThermostatSetpoint = void 0;
const hmac_authenticated_payload_1 = require("@henningkerstan/hmac-authenticated-payload");
const httpPostJSON_1 = require("./httpPostJSON");
const ThermostatSetpoint_1 = require("./ThermostatSetpoint");
async function updateRemoteThermostatSetpoint(options) {
// create ThermostatSetpoint object
const thermostatSetpoint = new ThermostatSetpoint_1.ThermostatSetpoint();
thermostatSetpoint.name = options.thermostatName;
thermostatSetpoint.setpoint = options.setpoint;
// enclose the ThermostatSetpoint in HMACAuthenticatedData
let ad = hmac_authenticated_payload_1.HMACAuthenticatedPayload.create(options.hmacKey, thermostatSetpoint);
// post the stringified data and wait for the response
const result = await (0, httpPostJSON_1.httpPostJSON)({
host: options.host,
port: options.port,
path: '/' + options.endpoint,
json: JSON.stringify(ad),
});
// parse the result as JSON
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const resultObject = JSON.parse(result);
// create an HMACAuthenticatedData from the received result
ad = new hmac_authenticated_payload_1.HMACAuthenticatedPayload(resultObject.payload, resultObject.nonce, resultObject.hmac);
// validate the hmac
if (!ad.validate(options.hmacKey)) {
return Promise.reject();
}
// interpret the received data; extract data for the updated thermostat
const thermostatData = { name: options.thermostatName };
// depending on the endpoint, the received data will be different
switch (options.endpoint) {
case 'config.json': {
const configuration = ad.payload;
for (const t of configuration.thermostats) {
if (t.name === options.thermostatName) {
thermostatData.setpoint = t.setpoint;
return thermostatData;
}
}
break;
}
case 'data.json': {
const thermostats = ad.payload;
for (const t of thermostats) {
if (t.name === options.thermostatName) {
thermostatData.setpoint = t.setpoint;
thermostatData.timestamp = t.timestamp;
thermostatData.temperature = t.temperature;
thermostatData.humidity = t.humidity;
thermostatData.heatingIsOn = t.heatingIsOn;
return thermostatData;
}
}
}
}
return Promise.reject();
}
exports.updateRemoteThermostatSetpoint = updateRemoteThermostatSetpoint;
//# sourceMappingURL=updateRemoteThermostatSetpoint.js.map