domotz-remote-pawn
Version:
Domotz Agent
96 lines (88 loc) • 3.96 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/>.
**/
const driverTypes = require('../../sandbox/constants').driverTypes;
var sandboxRun = function (message, resourceLocator) {
return {
execute: function (callback) {
var cid = message.correlationId;
var myConsole = resourceLocator.log.decorateLogs().decorate('(' + cid + ')');
myConsole.debug('Executing command echo: %s', JSON.stringify(message.payload));
resourceLocator.sandboxRunner(
message.payload,
cid,
createFailure(message, resourceLocator, myConsole, callback),
createSuccess(message, resourceLocator, myConsole, callback)
);
},
describe: function () {
return message.correlationId + ' - Run in Sandbox, device MAC: ' + message.payload.device.hw_address;
},
};
};
function createSuccess(message, resourceLocator, myConsole, callback) {
return function success(result) {
myConsole.info('Success: Dry Run - %s', message.payload.dry_run);
if (result && !message.payload.dry_run) {
var domotzApi = resourceLocator.engineRequest;
var deviceId = message.payload.device.id;
var driverId = message.payload.driver_id;
var driverType = message.payload.driver_type || driverTypes.GENERIC;
var requestBody;
if (driverType === driverTypes.GENERIC && (result.variables || result.tables)) {
var table = null;
if (result.tables) {
table = result.tables[0];
}
requestBody = {
variables: result.variables || null,
table: table,
};
domotzApi
.sendWithPromise('/device/' + deviceId + '/driver/' + driverId + '/variable', 'PUT', requestBody, undefined, undefined, {
toSendCompressed: true,
})
.then(function () {
myConsole.info('Driver Variables sent to cloud');
});
} else if (driverType === driverTypes.CONFIGURATION_MANAGEMENT && result.configurationData) {
data = result.configurationData;
requestBody = {
label: data.label,
running: data.running,
startup: data.startup,
ignored_lines: data.ignoredLines,
};
domotzApi
.sendWithPromise('/device/' + deviceId + '/driver/' + driverId + '/configuration', 'POST', requestBody, undefined, undefined, {
toSendCompressed: true,
})
.then(function () {
myConsole.info('Configuration Backups sent to cloud');
});
}
}
return callback(result);
};
}
function createFailure(_, __, myConsole, callback) {
return function failure(result) {
myConsole.warn('Failure: %s', JSON.stringify(result));
return callback(result);
};
}
module.exports.command = sandboxRun;
module.exports.createSuccess = createSuccess;