@gravypower/node-red-franklinwh
Version:
Node-RED node to control FranklinWH gateway
97 lines (88 loc) • 4.09 kB
JavaScript
const apiHandler = require('../../../api-handler');
module.exports = function(RED) {
function FranklinWHGetSystemNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const server = RED.nodes.getNode(config.server);
if (!server) {
node.status({fill:"red", shape:"ring", text:"missing config"});
node.error("No server configuration");
return;
}
node.on("input", async function (msg, send, done) {
try {
// Show requesting status
node.status({fill:"blue", shape:"dot", text:"requesting..."});
await apiHandler.executeWithRetry(
node,
server,
async (api) => {
// Get current system data from multiple sources
const [status, currentMode, isOnline, reserve] = await Promise.all([
api.getAGateStatus(),
api.getMode(),
api.isGridOnline(),
api.getReserve()
]);
// Format status text
const statusText = `${currentMode.toUpperCase()}, Grid ${isOnline ? 'Online' : 'Offline'}, ${status.chargePercentage}% Battery`;
node.status({
fill: isOnline ? "green" : "yellow",
shape: "dot",
text: statusText
});
// Return comprehensive system information
return {
system: {
mode: currentMode,
gridStatus: isOnline ? 'online' : 'offline',
batteryReserve: reserve
},
power: {
solar: {
current: status.solarIn,
total: status.solarInKWh
},
battery: {
current: status.batteryOut,
charging: status.batteryInKWh,
discharging: status.batteryOutKWh,
percentage: status.chargePercentage
},
grid: {
current: status.gridOut,
import: status.gridInKWh,
export: status.gridOutKWh
},
load: {
current: status.loadOut,
total: status.loadOutKWh
},
generator: {
current: status.generatorIn,
total: status.generatorInKWh
}
}
};
},
msg,
send,
done
);
// Update status on success (handled by apiHandler)
node.status({fill:"green", shape:"dot", text:"success"});
} catch (err) {
// Error handling (should be caught by apiHandler, but just in case)
const errorMsg = err.message || 'Unknown error';
node.status({fill:"red", shape:"ring", text: errorMsg.substring(0, 25)});
node.error("FranklinWH Error: " + errorMsg, msg);
if (done) done(err);
}
});
node.on('close', function(done) {
node.status({});
done();
});
}
RED.nodes.registerType("franklinwh-get-system", FranklinWHGetSystemNode);
}