'use strict';

const tools = require('../tools.js');
const dgram = require('dgram');
const adapterName = 'kecontact';
var txSocket = null;
var rxSocket = null;
const DEFAULT_UDP_PORT = 7090;
const BROADCAST_UDP_PORT = 7092;
const DETECT_MESSAGE = new Buffer("i");
const KEBA_TIMEOUT = 500;
var timeout = null;

function addInstance(ip, device, options) {
    let instance = tools.findInstance (options, adapterName, obj => obj.native.host === ip);
    if (instance) {
        options.log.info('Keba KeContact adapter already present for IP ' + ip);
    } else {
        instance = {
            _id: tools.getNextInstanceID(adapterName, options),
            common: {
                name: adapterName,
                title: 'Keba KeContact P30 (' + ip + ')'
            },
            native: {
                host: ip
            },
            comment: {
                add: ["control you Keba KeContact P30 charging station"]
            }
        };
        options.newInstances.push(instance);
        return true;
    } 
    return false;
}

function detect(ip, device, options, callback) {
    options.log.info('Detecting Keba KeContact wallbox on ' + ip + '...');

    timeout = setTimeout(() => {
        timeout = null;
        cleanup(ip, callback, false);
        callback = null;
    }, KEBA_TIMEOUT);

    rxSocket = dgram.createSocket('udp4');
    rxSocket.on('error', (err) => {
        if (err.message.includes('EADDRINUSE')) {
            options.log.info('Keba adapter already running');
        } else {
            options.log.error(`Keba rxSocket error:${err.stack}`);
        }
        cleanup(ip, callback, false);
        callback = null;
    });

    rxSocket.on('listening', function () {
        var address = rxSocket.address();
        options.log.debug('Keba UDP server listening on ' + address.address + ":" + address.port);
        rxSocket.setBroadcast(true);
        rxSocket.send(DETECT_MESSAGE, 0, DETECT_MESSAGE.length, DEFAULT_UDP_PORT, ip, (err, bytes) => err && options.log.warn('Error from KeContact: ' + err));
    });

    rxSocket.on('message', (message, remote) => {
        options.log.debug('UDP datagram from ' + remote.address + ':' + remote.port + ': "' + message + '"');
        if (message == DETECT_MESSAGE.toSring()) {
            options.log.debug('broadcast message received by myself ...');
            return; 
        }

        if (timeout) {
            clearTimeout(timeout);
            timeout = null;
        }
        if (message.length > 0) {
            // no defined response needed regarding Keba docs
            cleanup(ip, callback, addInstance(remote.address, device, options));
            callback = null;
        } else {
            cleanup(ip, callback, false);
            callback = null;
        }
    
    });
    rxSocket.bind(DEFAULT_UDP_PORT);
}

function cleanup(ip, callback, callbackResult) {
    if (txSocket) {
        txSocket.close();
        txSocket = null;
    }
    if (rxSocket) { 
        rxSocket.close(() => {
            if (callback) {
                callback(null, callbackResult, ip);
            }
        });
        rxSocket = null;
    }
}

exports.detect = detect;
exports.type = ['udp'];
exports.timeout = KEBA_TIMEOUT;
@@ -1196,32 +1196,83 @@
 Timeout(() =%3E %7B%0A
+        options.log.debug('Keba timeout reached');%0A
         timeout 
@@ -2295,12 +2295,16 @@
 sage
- == 
+.equals(
 DETE
@@ -2317,17 +2317,8 @@
 SAGE
-.toSring(
 )) %7B

change compare Buffer String