module.exports = (RED) => { |
module.exports = (RED) => { |
"use strict"; |
"use strict"; |
let onvif = require("node-onvif"); |
let onvif = require("node-onvif"); |
const sharp = require('sharp'); |
let isSharpAvailable = false; |
|
try { |
|
const sharp = require('sharp'); |
|
isSharpAvailable = true; |
|
} |
|
catch (ex) {} |
|
|
function snapshot(config) { |
function snapshot(config) { |
RED.nodes.createNode(this, config); |
RED.nodes.createNode(this, config); |
let node = this; |
let node = this; |
|
|
node.on("input", function (msg) { |
node.on("input", function (msg) { |
|
|
config.name = msg.payload.name || config.name; |
config.name = msg.payload.name || config.name; |
config.url = msg.payload.url || config.url; |
config.url = msg.payload.url || config.url; |
config.username = msg.payload.username || config.username; |
config.username = msg.payload.username || config.username; |
config.password = msg.payload.password || config.password; |
config.password = msg.payload.password || config.password; |
config.resize = msg.payload.resize || config.resize; |
config.resize = msg.payload.resize || config.resize; |
|
|
if(msg.hasOwnProperty("payload")) { |
if(msg.hasOwnProperty("payload")) { |
msg._payload = msg.payload; |
msg._payload = msg.payload; |
} |
} |
msg.node = this.type; |
msg.node = this.type; |
|
|
if (!config.url) { |
if (!config.url) { |
node.warn("No URL is specified. Please specify in node configuration."); |
node.warn("No URL is specified. Please specify in node configuration."); |
return; |
return; |
} |
} |
|
|
run(msg, node, config); |
run(msg, node, config); |
}); |
}); |
} |
} |
RED.nodes.registerType("ONVIF Snapshot", snapshot); |
RED.nodes.registerType("ONVIF Snapshot", snapshot); |
|
|
function run(msg, node, config) { |
function run(msg, node, config) { |
let onvifInstance = new onvif.OnvifDevice({ |
let onvifInstance = new onvif.OnvifDevice({ |
xaddr: config.url, |
xaddr: config.url, |
user : config.username, |
user : config.username, |
pass : config.password |
pass : config.password |
}); |
}); |
|
|
onvifInstance.init().then((info) => { |
onvifInstance.init().then((info) => { |
node.log('Fetching snapshot from ' + config.url); |
node.log('Fetching snapshot from ' + config.url); |
return onvifInstance.fetchSnapshot(); |
return onvifInstance.fetchSnapshot(); |
}).then((res) => { |
}).then((res) => { |
let prefix = 'data:' + res.headers['content-type'] + ';base64,'; |
let prefix = 'data:' + res.headers['content-type'] + ';base64,'; |
|
|
if (config.resize) { |
if (config.resize && isSharpAvailable) { |
sharp(Buffer.from(res.body, 'binary')) |
sharp(Buffer.from(res.body, 'binary')) |
.resize(config.resize) |
.resize(config.resize) |
.toFormat('png') |
.toFormat('png') |
.toBuffer() |
.toBuffer() |
.then( data => { |
.then( data => { |
msg.payload = { |
msg.payload = { |
config: config, |
config: config, |
image: { |
image: { |
base64: (prefix + data.toString('base64')), |
base64: (prefix + data.toString('base64')), |
binary: res.body |
binary: res.body |
} |
} |
}; |
}; |
node.send(msg); |
node.send(msg); |
}).catch( err => { |
}).catch( err => { |
|
|
}); |
}); |
} else { |
} else { |
let base64Image = Buffer.from(res.body, 'binary').toString('base64'); |
let base64Image = Buffer.from(res.body, 'binary').toString('base64'); |
msg.payload = { |
msg.payload = { |
config: config, |
config: config, |
image: { |
image: { |
base64: (prefix + base64Image), |
base64: (prefix + base64Image), |
binary: res.body |
binary: res.body |
} |
} |
}; |
}; |
node.send(msg); |
node.send(msg); |
} |
} |
}).catch((error) => { |
}).catch((error) => { |
msg.payload = null; |
msg.payload = null; |
msg.error = error; |
msg.error = error; |
node.send(msg); |
node.send(msg); |
}); |
}); |
} |
} |
} |
} |
|
|