domotz-remote-pawn
Version:
Domotz Agent
86 lines (81 loc) • 3.31 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 Georgi Tsatsev <gtsatsev@domotz.com> on 26/03/2018.
*/
var utils = require('./utils');
var onvifSend = function (message, resourceLocator) {
var onvif = resourceLocator.onvif;
var username = message.payload.username;
var password = message.payload.password;
var port = message.payload.port || utils.defaultPort;
var hostname = message.payload.host;
var commandList = message.payload.commandList;
var resultList = [];
var myConsole = resourceLocator.log.decorateLogs();
var executeFunction = function (commandList, callback) {
function resultListCallback(error, response) {
var errorMessage = null;
if (error) {
errorMessage = error.message;
}
resultList.push({ error: errorMessage, outcome: response });
if (!commandList || resultList.length === commandList.length) {
myConsole.debug('Calling back');
callback({ outcome: resultList, error: null });
}
}
return new onvif.Cam(
{
username: username,
password: password,
hostname: hostname,
port: port,
},
function (err) {
if (err) {
myConsole.info('Cannot connect to camera');
myConsole.debug(JSON.stringify(err));
callback({ outcome: null, error: err.message });
} else {
myConsole.info('Successfully connected to camera ');
if (!commandList) {
resultListCallback(new TypeError('CommandList is empty: ' + commandList), null, null);
} else {
for (var i = 0; i < commandList.length; i++) {
var command = this[commandList[i]];
if (command) {
this[commandList[i]](resultListCallback);
} else {
resultListCallback(new TypeError('Command not recognized: ' + command), null, null);
}
}
}
}
}
);
};
return {
execute: function (callback) {
return executeFunction(commandList, callback);
},
describe: function () {
return utils.logCommand(message, 'executeOnvifCommandList');
},
};
};
module.exports.command = onvifSend;