domotz-remote-pawn
Version:
Domotz Agent
141 lines (113 loc) • 4.62 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
*
* Created by Iacopo Papalini <ipapalini@domotz.com> on 08/09/15.
*/
module.exports.post = function (resourceLocator, onException, onSuccess) {
var configurator = resourceLocator.configurator;
var serviceReStarter = resourceLocator.utils.proc.restart;
var domotzAgent = resourceLocator.entities.agent;
return function (req, res) {
console.info('API - (%s): %s', req.method, req.url);
var body = req.body;
var userId = req.params.user_id;
var displayName = body.display_name;
var geoLocation = body.location;
var context = body.context;
configurator.setGeographicalCoordinates(geoLocation);
domotzAgent.create(userId, displayName, context)
.then(configurator.setConfigurationUrl)
.then(configurator.sendGeographicalCoordinates)
.then(configurator.getConfiguration)
.then(configurator.writeConfiguration)
.then(serviceReStarter)
.then(function () {
onSuccess(res);
})
.catch(function (err) {
onException(res, err);
});
};
};
module.exports.get = function(resourceLocator, onException, onSuccess) {
return function (req, res) {
var domotzAgent = resourceLocator.entities.agent;
var data = domotzAgent.get();
onSuccess(res, data);
};
};
module.exports.put = function (resourceLocator, onException, onSuccess) {
var configurator = resourceLocator.configurator;
var serviceReStarter = resourceLocator.utils.proc.restart;
return function (req, res) {
var domotzAgent = resourceLocator.entities.agent;
console.info('API - (%s): %s', req.method, req.url);
var params = req.params;
var userId = params.user_id;
var agentId = params.agent_id;
var agentName = req.query.agent_name;
var geoLocation = req.body;
configurator.setGeographicalCoordinates(geoLocation);
domotzAgent.update(userId, agentId, agentName)
.then(configurator.setConfigurationUrl)
.then(configurator.sendGeographicalCoordinates)
.then(configurator.getConfiguration)
.then(configurator.writeConfiguration)
.then(serviceReStarter)
.then(function () {
onSuccess(res);
})
.catch(function (err) {
onException(res, err);
});
};
};
module.exports.delete = function (resourceLocator, onException, onSuccess) {
var read = resourceLocator.utils.file.readFile;
var kill = resourceLocator.utils.proc.kill;
var serviceReStarter = resourceLocator.utils.proc.restart;
var configDeleter = resourceLocator.configurator.deleteConfiguration;
var userId;
var licenceId = resourceLocator.configuration.licence.id;
return function(req, res) {
var domotzAgent = resourceLocator.entities.agent;
var domotzUser = resourceLocator.entities.user;
console.info('API - (%s): %s', req.method, req.url);
var username = req.query.username;
var password = req.headers['x-forward-password'];
domotzUser.get(username, password)
.then(function (userData) {
if (userData.type === 'DEPUTY'){
userId = userData.partner_id;
} else {
userId = userData.id;
}
})
.then(function () {
return domotzAgent.delete(userId, licenceId);
})
.then(function(engineResponseBody) {
onSuccess(res, engineResponseBody);
})
.then(configDeleter)
.then(serviceReStarter)
.catch(function (err) {
onException(res, err);
});
};
};