@chart-sg/node-red-ros2-manager
Version:
Shared ROS2 context manager for Node-RED plugins. Eliminates conflicts and enables multi-plugin compatibility.
60 lines (50 loc) • 2.26 kB
JavaScript
module.exports = function(RED) {
function ROS2ConfigNode(config) {
RED.nodes.createNode(this, config);
// Store configuration
this.domain = parseInt(config.domain) || 0;
this.namespace = config.namespace || '';
// Validate domain range
if (this.domain < 0 || this.domain > 232) {
this.error(`Invalid domain ID: ${this.domain}. Must be between 0-232.`);
return;
}
// Configure SharedManager with these settings
try {
const { getROS2Manager } = require('../index');
const sharedManager = getROS2Manager();
// Check if ROS2 is available
if (!sharedManager.available) {
this.status({fill:"red", shape:"ring", text:"ROS2 setup required"});
this.warn(`ROS2 functionality not available. Please install rclnodejs with proper ROS2 environment.`);
return;
}
// Update SharedManager configuration
sharedManager.configure({
domain: this.domain,
namespace: this.namespace
});
this.status({fill:"green", shape:"dot", text:`domain: ${this.domain}`});
this.log(`ROS2 Config initialized: domain=${this.domain}, namespace="${this.namespace}"`);
} catch (error) {
this.status({fill:"red", shape:"ring", text:"config error"});
this.error(`Failed to configure SharedManager: ${error.message}`);
}
// Cleanup on node removal
this.on('close', function(removed, done) {
if (removed) {
// Node is being deleted
try {
const { getROS2Manager } = require('../index');
const sharedManager = getROS2Manager();
sharedManager.shutdown();
this.log('ROS2 Config shutdown completed');
} catch (error) {
this.warn(`Shutdown warning: ${error.message}`);
}
}
done();
});
}
RED.nodes.registerType("ros2-config", ROS2ConfigNode);
};