Search in sources :

Example 21 with HSBType

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

the class HueBinding method computeCommandForItemOnBridge.

/**
     * Checks whether the command is for one of the configured Hue bulbs. If
     * this is the case, the command is translated to the corresponding action
     * which is then sent to the given bulb.
     *
     * @param command
     *            The command from the openHAB bus.
     * @param itemName
     *            The name of the targeted item.
     * @param bridge
     *            The Hue bridge the Hue bulb is connected to
     */
private void computeCommandForItemOnBridge(Command command, String itemName, HueBridge bridge) {
    HueBindingConfig deviceConfig = getConfigForItemName(itemName);
    if (deviceConfig == null) {
        return;
    }
    HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
    if (bulb == null) {
        bulb = new HueBulb(bridge, deviceConfig.getDeviceId());
        bulbCache.put(deviceConfig.getDeviceId(), bulb);
    }
    if (command instanceof OnOffType) {
        bulb.switchOn(OnOffType.ON.equals(command));
    }
    if (command instanceof HSBType) {
        HSBType hsbCommand = (HSBType) command;
        DecimalType hue = hsbCommand.getHue();
        PercentType sat = hsbCommand.getSaturation();
        PercentType bri = hsbCommand.getBrightness();
        bulb.colorizeByHSB(hue.doubleValue() / 360, sat.doubleValue() / 100, bri.doubleValue() / 100);
    }
    if (deviceConfig.getType().equals(BindingType.brightness) || deviceConfig.getType().equals(BindingType.rgb)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            int resultingValue = bulb.increaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            int resultingValue = bulb.decreaseBrightness(deviceConfig.getStepSize());
            eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
        } else if ((command instanceof PercentType) && !(command instanceof HSBType)) {
            bulb.setBrightness((int) Math.round((double) HueBulb.MAX_BRIGHTNESS / (double) 100 * ((PercentType) command).intValue()));
        }
    }
    if (deviceConfig.getType().equals(BindingType.colorTemperature)) {
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            bulb.increaseColorTemperature(deviceConfig.getStepSize());
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            bulb.decreaseColorTemperature(deviceConfig.getStepSize());
        } else if (command instanceof PercentType) {
            bulb.setColorTemperature((int) Math.round((((double) 346 / (double) 100) * ((PercentType) command).intValue()) + 154));
        }
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) HueBulb(org.openhab.binding.hue.internal.hardware.HueBulb) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 22 with HSBType

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

the class TinkerforgeBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     *
     * Searches the item with the given {@code itemName} in the {@link TinkerforgeBindingProvider}
     * collection and gets the uid and subid of the device. The appropriate device is searched in the
     * ecosystem and the command is executed on the device.
     *
     * {@code OnOffType} commands are executed on {@link MInSwitchActor} objects. {@code StringType}
     * commands are executed on {@link MTextActor} objects.
     *
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.debug("received command {} for item {}", command, itemName);
    for (TinkerforgeBindingProvider provider : providers) {
        for (String itemNameP : provider.getItemNames()) {
            if (itemNameP.equals(itemName)) {
                String deviceUid = provider.getUid(itemName);
                String deviceSubId = provider.getSubId(itemName);
                String deviceName = provider.getName(itemName);
                if (deviceName != null) {
                    String[] ids = getDeviceIdsForDeviceName(deviceName);
                    deviceUid = ids[0];
                    deviceSubId = ids[1];
                }
                logger.trace("{} found item for command: uid: {}, subid: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
                MBaseDevice mDevice = tinkerforgeEcosystem.getDevice(deviceUid, deviceSubId);
                if (mDevice != null && mDevice.getEnabledA().get()) {
                    if (command instanceof OnOffType) {
                        logger.trace("{} found onoff command", LoggerConstants.COMMAND);
                        OnOffType cmd = (OnOffType) command;
                        if (mDevice instanceof MSwitchActor) {
                            OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
                            ((MSwitchActor) mDevice).turnSwitch(state);
                        } else if (mDevice instanceof DigitalActor) {
                            HighLowValue state = cmd == OnOffType.OFF ? HighLowValue.LOW : HighLowValue.HIGH;
                            ((DigitalActor) mDevice).turnDigital(state);
                        } else if (mDevice instanceof ProgrammableSwitchActor) {
                            OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
                            ((ProgrammableSwitchActor) mDevice).turnSwitch(state, provider.getDeviceOptions(itemName));
                        } else {
                            logger.error("{} received OnOff command for non-SwitchActor", LoggerConstants.COMMAND);
                        }
                    } else if (command instanceof StringType) {
                        logger.trace("{} found string command", LoggerConstants.COMMAND);
                        if (mDevice instanceof MTextActor) {
                            ((MTextActor) mDevice).write(command.toString());
                        }
                    } else if (command instanceof DecimalType) {
                        logger.debug("{} found number command", LoggerConstants.COMMAND);
                        if (command instanceof HSBType) {
                            logger.debug("{} found HSBType command", LoggerConstants.COMMAND);
                            if (mDevice instanceof ProgrammableColorActor) {
                                logger.debug("{} found ProgrammableColorActor {}", itemName);
                                ((ProgrammableColorActor) mDevice).setSelectedColor((HSBType) command, provider.getDeviceOptions(itemName));
                            } else if (mDevice instanceof SimpleColorActor) {
                                logger.debug("{} found SimpleColorActor {}", itemName);
                                ((SimpleColorActor) mDevice).setSelectedColor((HSBType) command);
                            }
                        } else if (command instanceof PercentType) {
                            if (mDevice instanceof SetPointActor) {
                                ((SetPointActor<?>) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
                                logger.debug("found SetpointActor");
                            } else if (mDevice instanceof PercentTypeActor) {
                                ((PercentTypeActor) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
                                logger.debug("found PercentType actor");
                            } else {
                                logger.error("found no percenttype actor");
                            }
                        } else {
                            if (mDevice instanceof NumberActor) {
                                ((NumberActor) mDevice).setNumber(((DecimalType) command).toBigDecimal());
                            } else if (mDevice instanceof SetPointActor) {
                                ((SetPointActor<?>) mDevice).setValue(((DecimalType) command).toBigDecimal(), provider.getDeviceOptions(itemName));
                            } else {
                                logger.error("found no number actor");
                            }
                        }
                    } else if (command instanceof UpDownType) {
                        UpDownType cmd = (UpDownType) command;
                        logger.debug("{} UpDownType command {}", itemName, cmd);
                        if (mDevice instanceof MoveActor) {
                            ((MoveActor) mDevice).move((UpDownType) command, provider.getDeviceOptions(itemName));
                        }
                    } else if (command instanceof StopMoveType) {
                        StopMoveType cmd = (StopMoveType) command;
                        if (mDevice instanceof MoveActor) {
                            if (cmd == StopMoveType.STOP) {
                                ((MoveActor) mDevice).stop();
                            } else {
                                ((MoveActor) mDevice).moveon(provider.getDeviceOptions(itemName));
                            }
                        }
                        logger.debug("{} StopMoveType command {}", itemName, cmd);
                    } else if (command instanceof IncreaseDecreaseType) {
                        IncreaseDecreaseType cmd = (IncreaseDecreaseType) command;
                        if (mDevice instanceof DimmableActor) {
                            ((DimmableActor<?>) mDevice).dimm((IncreaseDecreaseType) command, provider.getDeviceOptions(itemName));
                        }
                        logger.debug("{} IncreaseDecreaseType command {}", itemName, cmd);
                    } else {
                        logger.error("{} got unknown command type: {}", LoggerConstants.COMMAND, command.toString());
                    }
                } else {
                    logger.error("{} no tinkerforge device found for command for item uid: {} subId: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
                }
            }
        }
    }
}
Also used : MSwitchActor(org.openhab.binding.tinkerforge.internal.model.MSwitchActor) StringType(org.openhab.core.library.types.StringType) PercentTypeActor(org.openhab.binding.tinkerforge.internal.model.PercentTypeActor) MBaseDevice(org.openhab.binding.tinkerforge.internal.model.MBaseDevice) ProgrammableColorActor(org.openhab.binding.tinkerforge.internal.model.ProgrammableColorActor) StopMoveType(org.openhab.core.library.types.StopMoveType) TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider) HighLowValue(org.openhab.binding.tinkerforge.internal.types.HighLowValue) HSBType(org.openhab.core.library.types.HSBType) ProgrammableSwitchActor(org.openhab.binding.tinkerforge.internal.model.ProgrammableSwitchActor) OnOffValue(org.openhab.binding.tinkerforge.internal.types.OnOffValue) SetPointActor(org.openhab.binding.tinkerforge.internal.model.SetPointActor) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) DigitalActor(org.openhab.binding.tinkerforge.internal.model.DigitalActor) OnOffType(org.openhab.core.library.types.OnOffType) MoveActor(org.openhab.binding.tinkerforge.internal.model.MoveActor) DimmableActor(org.openhab.binding.tinkerforge.internal.model.DimmableActor) MTextActor(org.openhab.binding.tinkerforge.internal.model.MTextActor) DecimalType(org.openhab.core.library.types.DecimalType) NumberActor(org.openhab.binding.tinkerforge.internal.model.NumberActor) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) SimpleColorActor(org.openhab.binding.tinkerforge.internal.model.SimpleColorActor)

Example 23 with HSBType

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

the class ColorColorImpl method fetchSensorValue.

/**
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * 
     * @generated NOT
     */
@Override
public void fetchSensorValue() {
    try {
        com.tinkerforge.BrickletColor.Color color = tinkerforgeDevice.getColor();
        setSensorValue(new HSBValue(new HSBType(new Color(getRGBValue(color.r), getRGBValue(color.g), getRGBValue(color.b)))));
    } catch (TimeoutException e) {
        TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_TIMEOUT_EXCEPTION, e);
    } catch (NotConnectedException e) {
        TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_NOT_CONNECTION_EXCEPTION, e);
    }
}
Also used : NotConnectedException(com.tinkerforge.NotConnectedException) MBrickletColor(org.openhab.binding.tinkerforge.internal.model.MBrickletColor) BrickletColor(com.tinkerforge.BrickletColor) Color(java.awt.Color) ColorColor(org.openhab.binding.tinkerforge.internal.model.ColorColor) MBrickletColor(org.openhab.binding.tinkerforge.internal.model.MBrickletColor) BrickletColor(com.tinkerforge.BrickletColor) HSBValue(org.openhab.binding.tinkerforge.internal.types.HSBValue) HSBType(org.openhab.core.library.types.HSBType) TimeoutException(com.tinkerforge.TimeoutException)

Example 24 with HSBType

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

the class InfluxDBPersistenceService method objectToState.

/**
   * Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
   * needed for querying a {@link HistoricState}.
   * 
   * @param value to be converted to a {@link State}
   * @param itemName name of the {@link Item} to get the {@link State} for
   * @return the state of the item represented by the itemName parameter, 
   *         else the string value of the Object parameter
   */
private State objectToState(Object value, String itemName) {
    String valueStr = String.valueOf(value);
    if (itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof NumberItem) {
                return new DecimalType(valueStr);
            } else if (item instanceof ColorItem) {
                return new HSBType(valueStr);
            } else if (item instanceof DimmerItem) {
                return new PercentType(valueStr);
            } else if (item instanceof SwitchItem) {
                return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON;
            } else if (item instanceof ContactItem) {
                return (string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF)) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (item instanceof RollershutterItem) {
                return new PercentType(valueStr);
            } else if (item instanceof DateTimeItem) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(new BigDecimal(valueStr).longValue());
                return new DateTimeType(calendar);
            } else {
                return new StringType(valueStr);
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Could not find item '{}' in registry", itemName);
        }
    }
    // just return a StringType as a fallback
    return new StringType(valueStr);
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) HistoricItem(org.openhab.core.persistence.HistoricItem) NumberItem(org.openhab.core.library.items.NumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 25 with HSBType

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

the class AbstractDynamoDBItemSerializationTest method testHSBTypeWithColorItem.

@Test
public void testHSBTypeWithColorItem() throws IOException {
    HSBType hsb = new HSBType(new DecimalType(1.5), new PercentType(new BigDecimal(2.5)), new PercentType(new BigDecimal(3.5)));
    DynamoDBItem<?> dbitem = testStateGeneric(hsb, "1.5,2.5,3.5");
    testAsHistoricGeneric(dbitem, new ColorItem("foo"), hsb);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

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