'use strict';

const tools = require('../tools.js');
const dgram = require('dgram');
const adapterName = 'kecontact';
const KEBA_TIMEOUT = 500;

function addInstance(ip, device, options, native, callback) {
    let instance = tools.findInstance (options, adapterName, obj => true);
    if (!instance) {
        instance = {
            _id: tools.getNextInstanceID(adapterName, options),
            common: {
                name: adapterName,
                title: 'Keba KeContact P30 (' + ip + ')'
            },
            native: {
                host: ip
            },
            comment: {
                add: [ip]
            }
        };
        options.newInstances.push(instance);
        return true;
    } 
    return false;
}

function detect(ip, device, options, callback) {

    const DEFAULT_UDP_PORT =  7090;
    const server = dgram.createSocket('udp4');

    let timeout = setTimeout(() => {
        timeout = null;
        if (server) {
            server.close(() => {
                if (callback) {
                    callback(null, false, ip);
                    callback = null;
                }
            });
        }
    }, KEBA_TIMEOUT);

    server.on('listening', () => {
        const address = server.address();
        options.log.debug('UDP Server listening on ' + address.address + ":" + address.port);
        // send connect request
        server.send("i", 0, 1, DEFAULT_UDP_PORT, ip, (err, bytes) => err && options.log.warn('Error from KeContact: ' + err));
    });

    server.on('message', (message, remote) => {
        options.log.debug('UDP datagram from ' + remote.address + ':' + remote.port + ': "' + message + '"');
        if (timeout) {
            clearTimeout(timeout);
            timeout = null;
        }
        if (message.length > 0) {
            // no defined response needed regarding Keba docs
            server.close(() => {
                callback(null, addInstance(ip, device, options, {ip}, callback), ip);
                callback = null;
            });
        } else {
            server.close(() => {
                if (callback) {
                    callback(null, false, ip);
                    callback = null;
                }
            });
        }
    });

    server.bind();
}

exports.detect = detect;
exports.type = ['udp'];
exports.timeout = KEBA_TIMEOUT;
@@ -121,26 +121,167 @@
 nst 
-KEBA_TIMEOUT = 500
+txSocket = dgram.createSocket('udp4');%0Aconst rxSocket = dgram.createSocket('udp4');%0Aconst DEFAULT_UDP_PORT = 7090;%0Aconst KEBA_TIMEOUT = 500;%0Avar timeout = null
 ;%0A%0Af
@@ -939,96 +939,8 @@
     
-const DEFAULT_UDP_PORT =  7090;%0A    const server = dgram.createSocket('udp4');%0A%0A    let 
 time
@@ -1000,22 +1000,24 @@
     if (
-server
+txSocket
 ) %7B%0A    
@@ -1016,38 +1016,104 @@
 ) %7B%0A            
-server
+txSocket.close()%0A        %7D%0A        if (rxSocket) %7B %0A            rxSocket
 .close(() =%3E %7B%0A 
@@ -1294,30 +1294,167 @@
 EOUT);%0A%0A    
-server
+rxSocket.on('error', (err) =%3E %7B%0A        options.log.error(%60Keba rxSocket error:%5Cn$%7Berr.stack%7D%60);%0A        rxSocket.close();%0A    %7D);%0A    rxSocket
 .on('listeni
@@ -1458,21 +1458,27 @@
 ening', 
-() =%3E
+function ()
  %7B%0A     
@@ -1480,21 +1480,19 @@
         
-const
+var
  address
@@ -1494,22 +1494,24 @@
 dress = 
-server
+rxSocket
 .address
@@ -1545,13 +1545,18 @@
 ug('
+Keba 
 UDP 
-S
+s
 erve
@@ -1653,22 +1653,24 @@
         
-server
+txSocket
 .send(%22i
@@ -1782,27 +1782,28 @@
     %7D);%0A
-%0A
     
-server
+rxSocket
 .on('mes
@@ -2138,38 +2138,40 @@
 ocs%0A            
-server
+txSocket
 .close(() =%3E %7B%0A 
@@ -2333,22 +2333,132 @@
         
-server
+if (txSocket) %7B%0A                txSocket.close()%0A            %7D%0A            if (rxSocket) %7B %0A                rxSocket
 .close((
@@ -2456,32 +2456,36 @@
 t.close(() =%3E %7B%0A
+    
                 
@@ -2512,32 +2512,36 @@
                 
+   
+ 
 callback(null, f
@@ -2563,32 +2563,36 @@
                 
+    
 callback = null;
@@ -2584,32 +2584,36 @@
 allback = null;%0A
+    
                 
@@ -2618,35 +2618,53 @@
   %7D%0A            
-%7D);
+    %7D);%0A            %7D
 %0A        %7D%0A    %7D
@@ -2658,24 +2658,29 @@
       %7D%0A
+    %0A
     %7D);%0A
 %0A    ser
@@ -2675,25 +2675,42 @@
 %7D);%0A
-%0A
     
-server.bind(
+rxSocket.bind(DEFAULT_UDP_PORT
 );%0A%7D

split socket into rx and tx