package de.unidue.stud.sehawagn.openhab.binding.wmbus.internal;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.osgi.framework.ServiceRegistration;

import com.google.common.collect.Sets;

import de.unidue.stud.sehawagn.openhab.binding.wmbus.WMBusBindingConstants;
import de.unidue.stud.sehawagn.openhab.binding.wmbus.handler.WMBusBridgeHandler;
import de.unidue.stud.sehawagn.openhab.binding.wmbus.handler.WMBusTechemHKVHandler;
import de.unidue.stud.sehawagn.openhab.binding.wmbus.internal.discovery.WMBusHKVDiscoveryService;

public class WMBusHandlerFactory extends BaseThingHandlerFactory {

    private final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Sets
            .union(WMBusBridgeHandler.SUPPORTED_THING_TYPES, WMBusTechemHKVHandler.SUPPORTED_THING_TYPES);

    private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

    @Override
    public boolean supportsThingType(ThingTypeUID thingTypeUID) {
        return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
    }

    @Override
    protected ThingHandler createHandler(Thing thing) {
        ThingTypeUID thingTypeUID = thing.getThingTypeUID();

        if (thingTypeUID.equals(WMBusBindingConstants.THING_TYPE_BRIDGE)) {
            if (thing instanceof Bridge) {
                WMBusBridgeHandler handler = new WMBusBridgeHandler((Bridge) thing);
                registerDiscoveryService(handler);
                return handler;
            } else {
                return null;
            }
        } else if (thingTypeUID.equals(WMBusBindingConstants.THING_TYPE_TECHEM_HKV)) {
            return new WMBusTechemHKVHandler(thing);
        } else {
            return null;
        }
    }

    private synchronized void registerDiscoveryService(WMBusBridgeHandler bridgeHandler) {
        WMBusHKVDiscoveryService discoveryService = new WMBusHKVDiscoveryService(bridgeHandler);
        discoveryService.activate();
        this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext
                .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<String, Object>()));
    }

}

@@ -601,16 +601,73 @@
 tration;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1040,16 +1040,77 @@
 rvice;%0A%0A
+/*%0A * This class is the main entry point of the binding.%0A */%0A
 public c
@@ -1441,24 +1441,248 @@
 shMap%3C%3E();%0A%0A
+    // OpenHAB logger%0A    private final Logger logger = LoggerFactory.getLogger(WMBusHandlerFactory.class);%0A%0A    public WMBusHandlerFactory() %7B%0A        logger.debug(%22WMBusHandlerFactory: wmbus binding starting up.%22);%0A    %7D%0A%0A
     @Overrid
@@ -2026,24 +2026,137 @@
 _BRIDGE)) %7B%0A
+            // create handler for WMBus bridge%0A            logger.debug(%22Creating (handler for) WMBus bridge.%22);%0A
             
@@ -2501,24 +2501,146 @@
 HEM_HKV)) %7B%0A
+            // create handler for Techem HKV device%0A            logger.debug(%22Creating (handler for) TechemHKV device.%22);%0A
             
@@ -2826,24 +2826,80 @@
 eHandler) %7B%0A
+        logger.debug(%22Registering discovery service.%22);%0A
         WMBu

Use OpenHAB logger instead of System.out WMBusHandlerFactory: More debug output