domotz-remote-pawn
Version:
Domotz Agent
86 lines (80 loc) • 3.41 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 <iacopo@domotz.com> on 20/03/2018.
*/
var utils = require('./utils');
var request = require('request');
var onvifSnapshot = function (message, resourceLocator) {
var onvif = resourceLocator.onvif;
var baseConsole = resourceLocator.log.decorateLogs();
return {
execute: function (callback) {
var username = message.payload.username;
var password = message.payload.password;
var port = message.payload.port || utils.defaultPort;
var hostname = message.payload.host;
var myConsole = baseConsole.decorate(+hostname + ':' + String(port));
new onvif.Cam(
{
username: username,
password: password,
hostname: hostname,
port: port,
},
function (err) {
if (err) {
callback(null, err);
return;
}
this.getSnapshotUri(function (err, data) {
if (err) {
callback(null, err);
} else {
myConsole.debug('Getting image from uri: %s', data.uri);
request.get(
{
uri: data.uri,
auth: {
user: username,
pass: password,
sendImmediately: false,
},
encoding: 'binary',
},
function (err, data) {
if (data.statusCode === 200) {
callback(data.body, null, {
asBinary: true,
'content-type': data.headers['content-type'],
});
} else {
callback(null, err);
}
}
);
}
});
}
);
},
describe: function () {
return message.correlationId + ' - Get ONVIF Snapshot from host: ' + message.payload.host + ':' + message.payload.port;
},
};
};
module.exports.command = onvifSnapshot;