Search in sources :

Example 11 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ChaserThingHandler method dispose.

@Override
public void dispose() {
    if (channels.size() != 0) {
        Bridge bridge = getBridge();
        if (bridge != null) {
            DmxBridgeHandler bridgeHandler = (DmxBridgeHandler) bridge.getHandler();
            if (bridgeHandler != null) {
                bridgeHandler.unregisterDmxChannels(this.thing);
                logger.debug("removing {} channels from {}", channels.size(), this.thing.getUID());
            }
            ChannelUID switchChannelUID = new ChannelUID(this.thing.getUID(), CHANNEL_SWITCH);
            for (DmxChannel channel : channels) {
                channel.removeListener(switchChannelUID);
            }
        }
        channels.clear();
    }
}
Also used : BaseDmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel) DmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) DmxBridgeHandler(org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler) Bridge(org.eclipse.smarthome.core.thing.Bridge)

Example 12 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ColorThingHandler method initialize.

@Override
public void initialize() {
    Configuration configuration = getConfig();
    Bridge bridge = getBridge();
    DmxBridgeHandler bridgeHandler;
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "no bridge assigned");
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    } else {
        bridgeHandler = (DmxBridgeHandler) bridge.getHandler();
        if (bridgeHandler == null) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "no bridge handler available");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    if (configuration.get(CONFIG_DMX_ID) == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "DMX channel configuration missing");
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    }
    try {
        List<BaseDmxChannel> configChannels = BaseDmxChannel.fromString((String) configuration.get(CONFIG_DMX_ID), bridgeHandler.getUniverseId());
        logger.trace("found {} channels in {}", configChannels.size(), this.thing.getUID());
        for (BaseDmxChannel channel : configChannels) {
            channels.add(bridgeHandler.getDmxChannel(channel, this.thing));
        }
    } catch (IllegalArgumentException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    }
    currentValues.add(DmxChannel.MIN_VALUE);
    currentValues.add(DmxChannel.MIN_VALUE);
    currentValues.add(DmxChannel.MIN_VALUE);
    if (configuration.get(CONFIG_DIMMER_FADE_TIME) != null) {
        fadeTime = ((BigDecimal) configuration.get(CONFIG_DIMMER_FADE_TIME)).intValue();
        logger.debug("setting fadeTime to {} ms in {}", fadeTime, this.thing.getUID());
    }
    if (configuration.get(CONFIG_DIMMER_DIM_TIME) != null) {
        dimTime = ((BigDecimal) configuration.get(CONFIG_DIMMER_DIM_TIME)).intValue();
        logger.trace("setting dimTime to {} ms in {}", fadeTime, this.thing.getUID());
    }
    if (configuration.get(CONFIG_DIMMER_TURNONVALUE) != null) {
        String turnOnValueString = String.valueOf(fadeTime) + ":" + ((String) configuration.get(CONFIG_DIMMER_TURNONVALUE)) + ":-1";
        ValueSet turnOnValue = ValueSet.fromString(turnOnValueString);
        if (turnOnValue.size() == 3) {
            this.turnOnValue = turnOnValue;
            logger.trace("set turnonvalue to {} in {}", turnOnValue, this.thing.getUID());
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "turn-on value malformed");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    this.turnOnValue.setFadeTime(fadeTime);
    if (configuration.get(CONFIG_DIMMER_TURNOFFVALUE) != null) {
        String turnOffValueString = String.valueOf(fadeTime) + ":" + ((String) configuration.get(CONFIG_DIMMER_TURNOFFVALUE)) + ":-1";
        ValueSet turnOffValue = ValueSet.fromString(turnOffValueString);
        if (turnOffValue.size() == 3) {
            this.turnOffValue = turnOffValue;
            logger.trace("set turnoffvalue to {} in {}", turnOffValue, this.thing.getUID());
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "turn-off value malformed");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    this.turnOffValue.setFadeTime(fadeTime);
    // register feedback listeners
    channels.get(0).addListener(new ChannelUID(this.thing.getUID(), CHANNEL_BRIGHTNESS_R), this, ListenerType.VALUE);
    channels.get(1).addListener(new ChannelUID(this.thing.getUID(), CHANNEL_BRIGHTNESS_G), this, ListenerType.VALUE);
    channels.get(2).addListener(new ChannelUID(this.thing.getUID(), CHANNEL_BRIGHTNESS_B), this, ListenerType.VALUE);
    if (bridge.getStatus().equals(ThingStatus.ONLINE)) {
        updateStatus(ThingStatus.ONLINE);
        dmxHandlerStatus = ThingStatusDetail.NONE;
    } else {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
    }
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseDmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) DmxBridgeHandler(org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler) ValueSet(org.eclipse.smarthome.binding.dmx.internal.ValueSet) Bridge(org.eclipse.smarthome.core.thing.Bridge)

Example 13 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class TunableWhiteThingHandler method initialize.

@Override
public void initialize() {
    Configuration configuration = getConfig();
    Bridge bridge = getBridge();
    DmxBridgeHandler bridgeHandler;
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "no bridge assigned");
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    } else {
        bridgeHandler = (DmxBridgeHandler) bridge.getHandler();
        if (bridgeHandler == null) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "no bridge handler available");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    if (configuration.get(CONFIG_DMX_ID) == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "DMX channel configuration missing");
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    }
    try {
        List<BaseDmxChannel> configChannels = BaseDmxChannel.fromString((String) configuration.get(CONFIG_DMX_ID), bridgeHandler.getUniverseId());
        logger.trace("found {} channels in {}", configChannels.size(), this.thing.getUID());
        for (BaseDmxChannel channel : configChannels) {
            channels.add(bridgeHandler.getDmxChannel(channel, this.thing));
        }
    } catch (IllegalArgumentException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    }
    if (channels.size() % 2 != 0) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Tunable white dimmers require an even number of channels");
        dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
        return;
    }
    currentValues.add(DmxChannel.MIN_VALUE);
    currentValues.add(DmxChannel.MIN_VALUE);
    if (configuration.get(CONFIG_DIMMER_FADE_TIME) != null) {
        fadeTime = ((BigDecimal) configuration.get(CONFIG_DIMMER_FADE_TIME)).intValue();
        logger.debug("setting fadeTime to {} ms in {}", fadeTime, this.thing.getUID());
    }
    if (configuration.get(CONFIG_DIMMER_DIM_TIME) != null) {
        dimTime = ((BigDecimal) configuration.get(CONFIG_DIMMER_DIM_TIME)).intValue();
        logger.trace("setting dimTime to {} ms in {}", fadeTime, this.thing.getUID());
    }
    if (configuration.get(CONFIG_DIMMER_TURNONVALUE) != null) {
        String turnOnValueString = String.valueOf(fadeTime) + ":" + ((String) configuration.get(CONFIG_DIMMER_TURNONVALUE)) + ":-1";
        ValueSet turnOnValue = ValueSet.fromString(turnOnValueString);
        if (turnOnValue.size() % 2 == 0) {
            this.turnOnValue = turnOnValue;
            logger.trace("set turnonvalue to {} in {}", turnOnValue, this.thing.getUID());
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "turn-on value malformed");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    this.turnOnValue.setFadeTime(fadeTime);
    if (configuration.get(CONFIG_DIMMER_TURNOFFVALUE) != null) {
        String turnOffValueString = String.valueOf(fadeTime) + ":" + ((String) configuration.get(CONFIG_DIMMER_TURNOFFVALUE)) + ":-1";
        ValueSet turnOffValue = ValueSet.fromString(turnOffValueString);
        if (turnOffValue.size() % 2 == 0) {
            this.turnOffValue = turnOffValue;
            logger.trace("set turnoffvalue to {} in {}", turnOffValue, this.thing.getUID());
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "turn-off value malformed");
            dmxHandlerStatus = ThingStatusDetail.CONFIGURATION_ERROR;
            return;
        }
    }
    this.turnOffValue.setFadeTime(fadeTime);
    // register feedback listeners
    channels.get(0).addListener(new ChannelUID(this.thing.getUID(), CHANNEL_BRIGHTNESS_CW), this, ListenerType.VALUE);
    channels.get(1).addListener(new ChannelUID(this.thing.getUID(), CHANNEL_BRIGHTNESS_WW), this, ListenerType.VALUE);
    if (bridge.getStatus().equals(ThingStatus.ONLINE)) {
        updateStatus(ThingStatus.ONLINE);
        dmxHandlerStatus = ThingStatusDetail.NONE;
    } else {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
    }
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseDmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) DmxBridgeHandler(org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler) ValueSet(org.eclipse.smarthome.binding.dmx.internal.ValueSet) Bridge(org.eclipse.smarthome.core.thing.Bridge)

Example 14 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_modifiedUninitializedThing.

@Test
public void testChildHandlerInitialized_modifiedUninitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            try {
                childHandlerInitializedSemaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            if (getBridge() == null) {
                throw new RuntimeException("Fail because of missing bridge");
            }
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            try {
                thingUpdatedSemapthore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    thing.setBridgeUID(bridge.getUID());
    managedThingProvider.update(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 15 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedUnitializedThing.

@Test
public void testChildHandlerInitialized_replacedUnitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            try {
                childHandlerInitializedSemaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            if (getBridge() == null) {
                throw new RuntimeException("Fail because of missing bridge");
            }
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            try {
                thingUpdatedSemapthore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
    managedThingProvider.update(thing2);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing2.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Bridge (org.eclipse.smarthome.core.thing.Bridge)26 Configuration (org.eclipse.smarthome.config.core.Configuration)9 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)9 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)9 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)9 DmxBridgeHandler (org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 Thing (org.eclipse.smarthome.core.thing.Thing)6 BaseDmxChannel (org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel)5 Semaphore (java.util.concurrent.Semaphore)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 BaseBridgeHandler (org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler)3 Command (org.eclipse.smarthome.core.types.Command)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Before (org.junit.Before)3 Test (org.junit.Test)3 HueBridge (org.eclipse.smarthome.binding.hue.internal.HueBridge)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1