Skip to content

Commit

Permalink
Allow to configure plugins used from website
Browse files Browse the repository at this point in the history
  • Loading branch information
pirquessa committed Mar 14, 2020
1 parent ae4d7d6 commit 2ab0f1b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
13 changes: 4 additions & 9 deletions clientrobotpi.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ const RL = require("readline");
const GPIO = require("pigpio").Gpio;
const I2C = require("i2c-bus");
const PCA9685 = require("pca9685");
const PLUGINS = new (require("./plugins"))([
"./Safety.js",
"./VideoDiffusion.js",
// "./AudioDiffusion.js",
// "./SerialSlave.js",
// "./TextToSpeech.js",
"./Gentank.js"
]);
const PLUGINS = new (require("./plugins"))();

const VERSION = Math.trunc(FS.statSync(__filename).mtimeMs);
const PROCESSTIME = Date.now();
Expand Down Expand Up @@ -243,6 +236,8 @@ CONF.SERVEURS.forEach(function(serveur, index) {
conf = data.conf;
hard = data.hard;

PLUGINS.onNewConfig(hard, conf);

tx = new TRAME.Tx(conf.TX);
rx = new TRAME.Rx(conf.TX, conf.RX);

Expand Down Expand Up @@ -420,7 +415,7 @@ CONF.SERVEURS.forEach(function(serveur, index) {
}
});

PLUGINS.apply('registerNewSocket', [sockets[serveur]]);
PLUGINS.onReady(PLUGINS.apply.bind(PLUGINS, 'registerNewSocket', [sockets[serveur]]));
});

PLUGINS.on('dataToServer', (eventName, data) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vigiclient",
"version": "0.5.0",
"version": "0.5.1",
"description": "Raspberry PI robot client for Vigibot.com",
"main": "clientrobotpi.js",
"dependencies": {
Expand Down
77 changes: 61 additions & 16 deletions plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,86 @@
const LOGGER = require("../utils/Logger.js").getLogger();

class PluginManager {
constructor(pluginPathes) {
this.plugins = [];
constructor() {
this.plugins = null;
this.callbackStack = [];
}

init(pluginConfigs) {
LOGGER.local('PLUGIN | DEBUG | Will instanciate all plugins:');
pluginPathes.forEach(pluginPath => {
LOGGER.local('PLUGIN | DEBUG | Instanciate plugin: ' + pluginPath);

var plugins = [];
pluginConfigs.forEach(pluginConfig => {
LOGGER.local('PLUGIN | DEBUG | Instanciate plugin: ' + pluginConfig.name);
try {
this.plugins.push(new (require(pluginPath))());
plugins.push(new (require(pluginConfig.path))());
}
catch(e) {
LOGGER.local('PLUGIN | ERROR | Fail to instanciate plugin ' + pluginPath + ': ' + e);
LOGGER.local('PLUGIN | ERROR | Fail to instanciate plugin ' + pluginPath.name + ': ' + e);
}
});
this.plugins = plugins;
this.callbackStack.forEach(cb => {
cb();
});
}

onNewConfig(hardwareConfig, remoteConfig) {
if (hardwareConfig.PLUGINS !== undefined) {
if (this.plugins === null) {
var pluginPathes = hardwareConfig.PLUGINS.filter(pluginName => {
return /^\w+$/g.test(pluginName);
}).map((pluginName => {
return {
name: pluginName,
path: './' + pluginName + '.js'
};
}));

this.init(pluginPathes);
}
}
else {
LOGGER.local('PLUGIN | ERROR | You need to define the plugins to load !');
}
}

onReady(callback) {
if (this.plugins === null) {
this.callbackStack.push(callback);
}
else {
callback();
}
}

apply(funName, args) {
//LOGGER.local('Apply ' + funName);

let promises = [];

this.plugins.forEach(plugin => {
try {
promises.push(plugin[funName].apply(plugin, args));
}
catch(e) {
LOGGER.local('PLUGIN | ERROR | Fail to apply ' + funName + ' on plugin ' + plugin.name + ': ' + e);
}
});
if (this.plugins !== null) {
this.plugins.forEach(plugin => {
try {
promises.push(plugin[funName].apply(plugin, args));
}
catch(e) {
LOGGER.local('PLUGIN | ERROR | Fail to apply ' + funName + ' on plugin ' + plugin.name + ': ' + e);
}
});
}
else {
LOGGER.local('PLUGIN | ERROR | Can\'t apply method "' + funName + '" because plugins are not initialized');
}

return Promise.all(promises);
}

on(eventName, listener) {
this.plugins.forEach(plugin => {
plugin.on(eventName, listener);
this.onReady(() => {
this.plugins.forEach(plugin => {
plugin.on(eventName, listener);
});
});
}
}
Expand Down

0 comments on commit 2ab0f1b

Please sign in to comment.