'use strict';

const tools = require('../tools.js');
const http = require('http');

function addInstance(ip, device, options, native, callback) {
    let instance = tools.findInstance(options, 'doorbird', obj => obj.native.birdip === ip);
    if (!instance) {
        instance = {
            _id: tools.getNextInstanceID('doorbird', options),
            common: {
                name: 'doorbird',
                title: 'DoorBird (' + ip + ')'
            },
            native: {
                birdip: ip
            },
            comment: {
                add: [ip]
            }
        };
        options.newInstances.push(instance);
        callback(null, true, ip);
    } else {
        callback(null, false, ip);
    }
}

function detect(ip, device, options, callback) {
    http.get('http://' + ip + '/bha-api/info.cgi', res => {
        if (res.headers['www-authenticate'] && res.headers['www-authenticate'].indexOf('DoorBird') !== -1) {
            addInstance(ip, device, options, {ip}, callback);
        } else {
            callback && callback(null, false, ip);
        }
    }).on('error', e => {
        callback && callback(null, false, ip);
    });
}

exports.detect = detect;
exports.type = ['ip'];
exports.timeout = 1500;
@@ -853,16 +853,23 @@
  if (res
+ && res
 .headers
@@ -950,24 +950,24 @@
 ) !== -1) %7B%0A
-
             
@@ -1025,32 +1025,58 @@
        %7D else %7B%0A
+            res.resume();%0A
             call
@@ -1106,24 +1106,53 @@
 false, ip);%0A
+            callback = null;%0A
         %7D%0A  
@@ -1218,24 +1218,49 @@
 false, ip);%0A
+        callback = null;%0A
     %7D);%0A%7D%0A%0Ae
@@ -1306,16 +1306,16 @@
 %5B'ip'%5D;%0A
-
 exports.
@@ -1329,8 +1329,9 @@
  = 1500;
+%0A

Update doorbird.js