Search in sources :

Example 1 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DiyOnXBeeBinding method parseRGBState.

private State parseRGBState(final String value) {
    if (!value.startsWith("RGB") || value.length() != 12)
        return null;
    try {
        final int red = Integer.valueOf(value.substring(3, 6));
        final int green = Integer.valueOf(value.substring(6, 9));
        final int blue = Integer.valueOf(value.substring(9, 12));
        return new HSBType(new Color(red, green, blue));
    } catch (NumberFormatException e) {
        logger.warn("cannot parse color from {}", value, e);
    }
    return null;
}
Also used : Color(java.awt.Color) HSBType(org.openhab.core.library.types.HSBType)

Example 2 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DmxColorItemTest method decreasesWhenDecreaseCommandReceived.

@Test
@Override
public void decreasesWhenDecreaseCommandReceived() throws BindingConfigParseException {
    DmxItem item = getValidInstance();
    DmxService service = Mockito.mock(DmxService.class);
    HSBType hsb = new HSBType(new DecimalType(150), new PercentType(50), new PercentType(50));
    item.processCommand(service, hsb);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(3, 65);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(4, 129);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(5, 97);
    item.processCommand(service, IncreaseDecreaseType.DECREASE);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(3, 57);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(4, 116);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(5, 87);
}
Also used : DmxService(org.openhab.binding.dmx.DmxService) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType) Test(org.junit.Test)

Example 3 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DmxColorItemTest method increasesWhenIncreaseCommandReceived.

@Test
@Override
public void increasesWhenIncreaseCommandReceived() throws BindingConfigParseException {
    DmxItem item = getValidInstance();
    DmxService service = Mockito.mock(DmxService.class);
    HSBType hsb = new HSBType(new DecimalType(150), new PercentType(50), new PercentType(50));
    item.processCommand(service, hsb);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(3, 65);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(4, 129);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(5, 97);
    item.processCommand(service, IncreaseDecreaseType.INCREASE);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(3, 70);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(4, 140);
    Mockito.verify(service, Mockito.times(1)).setChannelValue(5, 106);
}
Also used : DmxService(org.openhab.binding.dmx.DmxService) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType) Test(org.junit.Test)

Example 4 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DmxColorItem method processStatusUpdate.

/**
     * {@inheritDoc}
     */
@Override
public void processStatusUpdate(int[] channelValues) {
    if (channelValues.length != 3 && channelValues.length != 4) {
        return;
    }
    HSBType cmd = new HSBType(new Color(channelValues[0], channelValues[1], channelValues[2]));
    publishState(cmd);
}
Also used : Color(java.awt.Color) HSBType(org.openhab.core.library.types.HSBType)

Example 5 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class HueBinding method execute.

/**
     * Get current hue settings of the bulbs and update the items that are connected with the bulb.
     * The refreshinterval determines the polling frequency.
     */
@Override
public void execute() {
    if (activeBridge != null) {
        // Get settings and update the bulbs
        // Observation : If the power of a hue lamp is removed, the status is not updated in hue hub.
        // The heartbeat functionality should fix this, but
        logger.debug("Start Hue data refresh");
        HueSettings settings = activeBridge.getSettings();
        if (settings == null) {
            logger.warn("Hue settings were null, maybe misconfigured bridge IP.");
            return;
        } else if (!settings.isAuthorized()) {
            logger.warn("openHAB not authorized to access Hue bridge");
            return;
        }
        Set<String> keys = settings.getKeys();
        for (String key : keys) {
            try {
                HueBulb bulb = bulbCache.get(key);
                if (bulb == null) {
                    bulb = new HueBulb(activeBridge, key, settings);
                    bulbCache.put(key, bulb);
                }
                bulb.getStatus(settings);
            } catch (NumberFormatException e) {
                logger.warn("lights index {} is not a number", key);
            }
        }
        // Multiple items of different types can be linked to one bulb.
        for (HueBindingProvider provider : this.providers) {
            for (String hueItemName : provider.getInBindingItemNames()) {
                HueBindingConfig deviceConfig = getConfigForItemName(hueItemName);
                if (deviceConfig != null) {
                    HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
                    if (bulb != null) {
                        //
                        if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                            if ((deviceConfig.itemStateOnOffType == null) || (deviceConfig.itemStateOnOffType.equals(OnOffType.ON) == false)) {
                                eventPublisher.postUpdate(hueItemName, OnOffType.ON);
                                deviceConfig.itemStateOnOffType = OnOffType.ON;
                            }
                        } else {
                            if ((deviceConfig.itemStateOnOffType == null) || (deviceConfig.itemStateOnOffType.equals(OnOffType.OFF) == false)) {
                                eventPublisher.postUpdate(hueItemName, OnOffType.OFF);
                                deviceConfig.itemStateOnOffType = OnOffType.OFF;
                            }
                        }
                        if (deviceConfig.getType().equals(BindingType.brightness)) {
                            if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                                // Only postUpdate when bulb is on, otherwise dimmer item is not retaining state and
                                // shows to max brightness value
                                PercentType newPercent = new PercentType((int) Math.round((bulb.getBrightness() * (double) 100) / HueBulb.MAX_BRIGHTNESS));
                                if ((deviceConfig.itemStatePercentType == null) || (deviceConfig.itemStatePercentType.equals(newPercent) == false)) {
                                    eventPublisher.postUpdate(hueItemName, newPercent);
                                    deviceConfig.itemStatePercentType = newPercent;
                                }
                            }
                        } else if (deviceConfig.getType().equals(BindingType.rgb)) {
                            if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                                // Only postUpdate when bulb is on, otherwise color item is not retaining state and
                                // shows to max brightness value
                                DecimalType decimalHue = new DecimalType(bulb.getHue() / (double) 182);
                                PercentType percentBrightness = new PercentType((int) Math.round((bulb.getBrightness() * (double) 100) / HueBulb.MAX_BRIGHTNESS));
                                PercentType percentSaturation = new PercentType((int) Math.round((bulb.getSaturation() * (double) 100) / HueBulb.MAX_SATURATION));
                                HSBType newHsb = new HSBType(decimalHue, percentSaturation, percentBrightness);
                                if ((deviceConfig.itemStateHSBType == null) || (deviceConfig.itemStateHSBType.equals(newHsb) == false)) {
                                    eventPublisher.postUpdate(hueItemName, newHsb);
                                    deviceConfig.itemStateHSBType = newHsb;
                                }
                            }
                        }
                    }
                }
            }
        }
        logger.debug("Done Hue data refresh.");
    }
}
Also used : HueSettings(org.openhab.binding.hue.internal.data.HueSettings) HueBulb(org.openhab.binding.hue.internal.hardware.HueBulb) DecimalType(org.openhab.core.library.types.DecimalType) HueBindingProvider(org.openhab.binding.hue.HueBindingProvider) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Aggregations

HSBType (org.openhab.core.library.types.HSBType)30 DecimalType (org.openhab.core.library.types.DecimalType)20 PercentType (org.openhab.core.library.types.PercentType)19 Color (java.awt.Color)8 StringType (org.openhab.core.library.types.StringType)8 DateTimeType (org.openhab.core.library.types.DateTimeType)7 Calendar (java.util.Calendar)6 ColorItem (org.openhab.core.library.items.ColorItem)6 ContactItem (org.openhab.core.library.items.ContactItem)5 DateTimeItem (org.openhab.core.library.items.DateTimeItem)5 DimmerItem (org.openhab.core.library.items.DimmerItem)5 NumberItem (org.openhab.core.library.items.NumberItem)5 RollershutterItem (org.openhab.core.library.items.RollershutterItem)5 SwitchItem (org.openhab.core.library.items.SwitchItem)5 OnOffType (org.openhab.core.library.types.OnOffType)5 Test (org.junit.Test)4 State (org.openhab.core.types.State)4 DmxService (org.openhab.binding.dmx.DmxService)3 Item (org.openhab.core.items.Item)3 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)3