Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
piznel committed Nov 11, 2018
1 parent 8c892e5 commit a591f50
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 33 deletions.
21 changes: 20 additions & 1 deletion api/controllers/ModuleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ module.exports = {
.catch(next);
},

/**
* @api {post} /module/getMethods Get Methods
* @apiName modulegetMethods
* @apiGroup module
* @apiPermission authenticated
*
* @apiUse param
*
* @apiSuccess {array} All module's methods tested
*/
getMethods: function(req, res, next) {
gladys.module
.getMethods(req.body)
.then(function(result) {
return res.json(result);
})
.catch(next);
},

upgrade: function(req, res, next) {
gladys.module.upgrade({ id: req.params.id, version: req.body.version });
return res.json({ message: 'Upgrade started with success' });
}
};
};
3 changes: 2 additions & 1 deletion api/core/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ module.exports.getById = require('./module.getById.js');
module.exports.uninstall = require('./module.uninstall.js');
module.exports.update = require('./module.update.js');
module.exports.upgrade = require('./module.upgrade.js');
module.exports.checkUpdate = require('./module.checkUpdate.js');
module.exports.checkUpdate = require('./module.checkUpdate.js');
module.exports.getMethods = require('./module.getMethods.js');
59 changes: 59 additions & 0 deletions api/core/module/module.getMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @public
* @description this function returns a list of methods passed in parameters with a true or false value if they are exposed by a module.
* @name gladys.module.getMethods
* @param {string} module The module slug
* @param {string} service The name of the gladys api service, like 'television', 'music', ... (option)
* @param {array} methods the module's method(s) you want to test
* @returns {array} data
* @example
*
* var params = {
'module' :"livebox",
'service':"television",
'methods' :["getState", "setChannel", "getChannel", "setMuted", "getMuted"]
};
*
* gladys.module.getMethods(params)
* .then((data) => {
* var availableMethods = data.data
*
* })
* .catch((err) => {
* // something bad happened ! :/
* });
*
* The structure of availableMethods is then as follows:
* {
* getState: true,
* setChannel: true,
* getChannel: false,
* setMuted: true,
* getMuted: false
* }
*/


module.exports = function getMethods(params) {
var availableMethods = {};
if(params.hasOwnProperty('service')) {
params.methods.forEach(function(method) {
if (!gladys.modules[params.module] || !gladys.modules[params.module][params.service] || typeof gladys.modules[params.module][params.service][method] != 'function') {
availableMethods[method] = false;
} else {
availableMethods[method] = true;
}
});
} else {
params.methods.forEach(function(method) {
if (!gladys.modules[params.module] || typeof gladys.modules[params.module][method] != 'function') {
availableMethods[method] = false;
} else {
availableMethods[method] = true;
}
});
}


return Promise.resolve(availableMethods);
};
67 changes: 36 additions & 31 deletions assets/js/app/module/module.service.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@

(function () {
'use strict';
'use strict';

angular
.module('gladys')
.factory('moduleService', moduleService);
angular
.module('gladys')
.factory('moduleService', moduleService);

moduleService.$inject = ['$http'];
moduleService.$inject = ['$http'];

function moduleService($http) {
function moduleService($http) {

var service = {
get: get,
install: install,
config: config,
uninstall: uninstall,
upgrade: upgrade
};

return service;

function get() {
return $http({method: 'GET', url: '/module'});
}
var service = {
get: get,
install: install,
config: config,
uninstall: uninstall,
upgrade: upgrade,
getMethods: getMethods
};

return service;

function get() {
return $http({method: 'GET', url: '/module'});
}

function install(module){
return $http({method: 'POST', url: '/module/install', data: module});
}
function install(module){
return $http({method: 'POST', url: '/module/install', data: module});
}

function config(slug){
return $http({method: 'POST', url: '/module/' + slug + '/config'});
}
function config(slug){
return $http({method: 'POST', url: '/module/' + slug + '/config'});
}

function uninstall(id){
return $http({method: 'DELETE', url: '/module/' + id });
}
function uninstall(id){
return $http({method: 'DELETE', url: '/module/' + id });
}

function upgrade(id, version){
return $http({method: 'POST', url: '/module/' + id + '/upgrade', data: {version: version}});
}

function upgrade(id, version){
return $http({method: 'POST', url: '/module/' + id + '/upgrade', data: {version: version}});
}
function getMethods(params) {
return $http({method: 'POST', url: '/module/getMethods', data: params});
}
}
})();
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ module.exports.routes = {
'post /module/:slug/config': 'ModuleController.config',
'delete /module/:id': 'ModuleController.uninstall',
'post /module/:id/upgrade': 'ModuleController.upgrade',
'post /module/getMethods': 'ModuleController.getMethods',

// Music
'post /music/flushqueue': 'MusicController.flushQueue',
Expand Down

0 comments on commit a591f50

Please sign in to comment.