node-red-contrib-freya-nodes
Version:
Custom nodes for Freya Vivarium Control System
67 lines (66 loc) • 3.29 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const system_actuators_1 = require("./system-actuators");
const systemActuators = (RED) => {
function SystemActuatorsNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const actuator = config.actuator;
const channel = parseInt(config.channel);
const mode = config.mode;
const actuatorsDriver = new system_actuators_1.ActuatorsDriver();
actuatorsDriver.on('status', (status) => {
switch (status.level) {
case 'ok':
node.status({ fill: 'green', shape: 'dot', text: status.message });
break;
case 'warning':
node.status({ fill: 'yellow', shape: 'dot', text: status.message });
break;
default:
node.status({ fill: 'red', shape: 'dot', text: status.message });
break;
}
});
node.on('input', (msg, send, done) => __awaiter(this, void 0, void 0, function* () {
try {
if (msg.payload != null && typeof msg.payload === 'object') {
const rawValue = msg.payload[actuator];
if (typeof rawValue !== 'undefined') {
if (mode === 'digital') {
const state = (rawValue === true || rawValue === "true" || rawValue === 1 || rawValue === "1" || rawValue === 'on') ? true : false;
actuatorsDriver.setDigitalOutput(channel, state)
.then((res) => {
if (res)
node.status({ fill: 'green', shape: 'dot', text: `D${channel} turned ${state ? 'on' : 'off'}` });
else
node.status({ fill: 'yellow', shape: 'dot', text: 'Invalid request' });
})
.catch((err) => {
node.status({ fill: 'yellow', shape: 'dot', text: err });
});
}
else if (mode === 'pwm') {
}
}
}
}
catch (err) {
node.status({ fill: 'red', shape: 'dot', text: "Failed to set output" });
done(err);
return;
}
done();
}));
}
RED.nodes.registerType('system actuators', SystemActuatorsNode);
};
module.exports = systemActuators;