GuilhermeC18 / node-onvif

The node-onvif is a Node.js module which allows you to communicate with the network camera which supports the ONVIF specifications.
MIT License
443 stars 198 forks source link

[Question] Exception catching #52

Open popovicidinu opened 5 years ago

popovicidinu commented 5 years ago

Hi. I'm building a nodeJS application using node-onvif to track some cameras. I have a situation where I'm looping over a couple of times over device.fetchSnapshot() in order to parse the results for my scenario.

From time to time, I'm getting an error that I cannot track down.

ERROR Error: Uncaught (in promise): 11552024 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

My code is written Promise style:

this.device.fetchSnapshot() .then(async (res) => { // my code });

I tried adding a catch statement, but the scenario is the same. I tried using try {} catch, but it seems to just hang if this error is thrown.

Unfortunately it doesn't happen all the time (I reckon from time to time, due to the looping over fetchSnapshot() the camera doesn't return a response or something breaks, thus the error.)

Any ideas ?

Paul-digitalquill commented 5 years ago

I am having the same problem, I am looping through several cameras.

I am calling the function in the server.js and the console.log output is changing fine with the actual ip's. But it uses the same data first time around and randomly jumps between cameras.

Is this a cache issue?

console.log('Current camera ip = ' + params.address);
var device = devices[params.address];

So I get the output in the terminal.

Current camera ip = 192.168.1.37
Current camera ip = 192.168.1.48
Current camera ip = 192.168.1.56
Paul-digitalquill commented 5 years ago

UPDATE.

I have changed the following and it now works but there is a major speed decrees with the init device.init().then(() Is there something wrong in the loop?

var device = devices[params.address];
device.fetchSnapshot((error, result) => {

to

var device = devices[params.address];
device.init().then(() => {
device.fetchSnapshot((error, result) => {
Paul-digitalquill commented 5 years ago

Okay anybody else having issue with multiple cameras can use this.

function saveSnapshot(conn, params) {

    console.log('saveSnapshot server.js = ' + params.address);
    var device = '';
    var device = devices[params.address];
    if(!device) {
        var res = {'id': 'fetchSnapshot', 'error': 'The specified device is not found: ' + params.address};
        conn.send(JSON.stringify(res));
        return;
    }

    // Initialize the OnvifDevice object
    device.fetchSnapshot().then(() => {
        // Get the data of the snapshot
        console.log('fetching the data of the snapshot...');
        return device.fetchSnapshot();
    }).then((res) => {
        // Save the data to a file
        fs.writeFileSync(__dirname + '/public/camera-public/' + Date.now() + '-' + params.address.split('.').join('') + '-snapshot.jpg', res.body, {encoding: 'binary'});
        console.log('Done!');
    }).catch((error) => {
        console.error(error);
    });

}