Skip to content

Commit ce256bc

Browse files
Merge pull request #292 from Anonym-tsk/rest_api
Remote Control REST API improvements
2 parents a25ddc1 + 04cf710 commit ce256bc

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

API/api.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,17 @@ module.exports = {
7373
let modActions = getActions(Module.notificationHandler[mod.module]);
7474

7575
if (modActions.length > 0) {
76-
let pathGuess = mod.module.replace(/MMM-/g, '').replace(/-/g, '').toLowerCase();
77-
7876
// Generate formatted actions object
7977
let actionsGuess = {};
8078

8179
modActions.forEach(a => {
8280
actionsGuess[a.replace(/[-_]/g, '').toLowerCase()] = { notification: a, guessed: true };
8381
});
8482

85-
if (pathGuess in this.externalApiRoutes) {
86-
this.externalApiRoutes[pathGuess].actions = Object.assign({}, actionsGuess, this.externalApiRoutes[pathGuess].actions);
83+
if (mod.module in this.externalApiRoutes) {
84+
this.externalApiRoutes[mod.module].actions = Object.assign({}, actionsGuess, this.externalApiRoutes[mod.module].actions);
8785
} else {
88-
this.externalApiRoutes[pathGuess] = {
86+
this.externalApiRoutes[mod.module] = {
8987
module: mod.module,
9088
path: mod.module.replace(/MMM-/g, '').replace(/-/g, '').toLowerCase(),
9189
actions: actionsGuess
@@ -326,12 +324,12 @@ module.exports = {
326324
let extApiRoutes = this.externalApiRoutes;
327325
let modules = this.configData.moduleData
328326
let query = {success: true, data: []};
329-
327+
330328
modules.forEach((mod) => {
331329
if (extApiRoutes[mod.name] === undefined) {
332330
query.data.push(mod);
333331
} else {
334-
query.data.push(Object.assign({},mod, {actions: extApiRoutes[mod.name].actions}));
332+
query.data.push(Object.assign({}, mod, {actions: extApiRoutes[mod.name].actions, urlpath: extApiRoutes[mod.name].path}));
335333
}
336334
});
337335

@@ -341,7 +339,7 @@ module.exports = {
341339
answerModuleApi(req, res) {
342340
if (!this.checkInitialized(res)) { return; }
343341
let dataMerged = this.mergeData().data
344-
342+
345343
if (!req.params.moduleName) {
346344
res.json({ success: true, data: dataMerged });
347345
return;
@@ -350,7 +348,8 @@ module.exports = {
350348
let modData = [];
351349
if(req.params.moduleName !== 'all') {
352350
modData = dataMerged.filter(m => {
353-
return (req.params.moduleName.includes(m.identifier));
351+
const name = req.params.moduleName;
352+
return name.includes(m.identifier) || name.includes(m.name) || name.includes(m.urlpath);
354353
});
355354
if (!modData.length) {
356355
modData = dataMerged.filter(m => {
@@ -493,26 +492,26 @@ module.exports = {
493492
icon: "window-restore",
494493
items: []
495494
};
496-
Object.keys(this.externalApiRoutes).forEach(r => {
495+
Object.values(this.externalApiRoutes).forEach(r => {
497496
let sub = {
498-
id: "mc-" + r,
497+
id: "mc-" + r.path,
499498
type: "menu",
500499
icon: "bars",
501-
text: this.formatName(this.externalApiRoutes[r].module),
500+
text: this.formatName(r.module),
502501
items: []
503502
};
504-
Object.keys(this.externalApiRoutes[r].actions).forEach(a => {
503+
Object.keys(r.actions).forEach(a => {
505504
let item = {
506-
id: `mc-${r}-${a}`,
505+
id: `mc-${r.path}-${a}`,
507506
menu: "item",
508507
icon: "dot-circle-o",
509508
action: "NOTIFICATION",
510-
content: this.externalApiRoutes[r].actions[a]
509+
content: r.actions[a]
511510
};
512-
if ("prettyName" in this.externalApiRoutes[r].actions[a]) {
513-
item.text = this.translate(this.externalApiRoutes[r].actions[a].prettyName);
511+
if ("prettyName" in r.actions[a]) {
512+
item.text = this.translate(r.actions[a].prettyName);
514513
} else {
515-
item.text = this.translate(this.externalApiRoutes[r].actions[a].notification).toLowerCase().replace(/(^|_)(\w)/g, function($0, $1, $2) {
514+
item.text = this.translate(r.actions[a].notification).toLowerCase().replace(/(^|_)(\w)/g, function($0, $1, $2) {
516515
return ($1 && ' ') + $2.toUpperCase();
517516
});
518517
}

MMM-Remote-Control.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Module.register("MMM-Remote-Control", {
270270
let modData = Object.assign({}, module.data);
271271
modData.hidden = module.hidden;
272272
modData.lockStrings = module.lockStrings;
273+
modData.urlpath = module.name.replace(/MMM-/g, '').replace(/-/g, '').toLowerCase();
273274
modData.config = module.config;
274275
currentModuleData.push(modData);
275276
});

node_helper.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ module.exports = NodeHelper.create(Object.assign({
8383

8484
loadTimers() {
8585
let delay = 24*3600;
86-
86+
8787
let self = this;
88-
88+
8989
clearTimeout(this.delayedQueryTimers['update'])
9090
this.delayedQueryTimers['update'] = setTimeout(function () {
9191
self.updateModuleList();
@@ -716,7 +716,7 @@ module.exports = NodeHelper.create(Object.assign({
716716
}
717717
if (query.action === "COMMAND") {
718718
if (this.thisConfig.customCommand && this.thisConfig.customCommand[query.command]) {
719-
exec(this.thisConfig.customCommand[query.command], opts, (error, stdout, stderr) => {
719+
exec(this.thisConfig.customCommand[query.command], opts, (error, stdout, stderr) => {
720720
self.checkForExecError(error, stdout, stderr, res, { stdout: stdout });
721721
});
722722
} else {
@@ -859,7 +859,7 @@ module.exports = NodeHelper.create(Object.assign({
859859
return;
860860
}
861861
if (query.action === "DELAYED") {
862-
/* Expects a nested query object
862+
/* Expects a nested query object
863863
* {
864864
* action: "DELAYED",
865865
* did: "SOME_UNIQUE_ID",
@@ -959,7 +959,7 @@ module.exports = NodeHelper.create(Object.assign({
959959
}
960960
});
961961
});
962-
962+
963963
},
964964

965965
checkForExecError(error, stdout, stderr, res, data) {
@@ -1019,6 +1019,7 @@ module.exports = NodeHelper.create(Object.assign({
10191019
simpleModuleData[k].identifier = moduleData[k].identifier;
10201020
simpleModuleData[k].hidden = moduleData[k].hidden;
10211021
simpleModuleData[k].lockStrings = moduleData[k].lockStrings;
1022+
simpleModuleData[k].urlpath = moduleData[k].urlpath;
10221023
}
10231024

10241025
let text = JSON.stringify({
@@ -1153,7 +1154,7 @@ module.exports = NodeHelper.create(Object.assign({
11531154
}
11541155
let backupPath = path.resolve("config/config.js.backup" + iteration);
11551156
let req = require(backupPath)
1156-
1157+
11571158
this.answerPost({ data: "config" }, { body: req }, { isSocket: true });
11581159
}
11591160
if (notification === "NEW_CONFIG") {
@@ -1176,10 +1177,10 @@ module.exports = NodeHelper.create(Object.assign({
11761177
if (notification === "REGISTER_API") {
11771178
if ("module" in payload) {
11781179
if ("actions" in payload && payload.actions !== {}) {
1179-
this.externalApiRoutes[payload.path] = payload;
1180+
this.externalApiRoutes[payload.module] = payload;
11801181
} else {
11811182
// Blank actions means the module has requested to be removed from API
1182-
delete this.externalApiRoutes[payload.path];
1183+
delete this.externalApiRoutes[payload.module];
11831184
}
11841185
this.updateModuleApiMenu();
11851186
}

0 commit comments

Comments
 (0)