Search in sources :

Example 91 with PercentType

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

the class YamahaReceiverBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    YamahaReceiverBindingConfig config = getConfigForItemName(itemName);
    if (config == null) {
        logger.error("Received command for unknown item '" + itemName + "'");
        return;
    }
    YamahaReceiverProxy proxy = proxies.get(config.getDeviceUid());
    if (proxy == null) {
        logger.error("Received command for unknown device uid '" + config.getDeviceUid() + "'");
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
    }
    try {
        Zone zone = config.getZone();
        BindingType type = config.getBindingType();
        if (type == BindingType.power) {
            if (command instanceof OnOffType) {
                proxy.setPower(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.volumePercent || type == BindingType.volumeDb) {
            if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
                // increase/decrease dB by .5 dB
                float db = proxy.getState(zone).getVolume();
                float adjAmt;
                if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
                    adjAmt = .5f;
                } else {
                    adjAmt = -.5f;
                }
                float newDb = db + adjAmt;
                proxy.setVolume(zone, newDb);
                // send new value as update
                State newState = new DecimalType(newDb);
                eventPublisher.postUpdate(itemName, newState);
            } else if (command instanceof PercentType) {
                // set dB from percent
                byte percent = ((PercentType) command).byteValue();
                int db = percentToDB(percent);
                proxy.setVolume(zone, db);
            } else {
                // set dB from value
                float db = Float.parseFloat(command.toString());
                proxy.setVolume(zone, db);
            }
            // Volume updates multiple values => send update now
            sendUpdates(proxy, config.getDeviceUid());
        } else if (type == BindingType.mute) {
            if (command instanceof OnOffType) {
                proxy.setMute(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.input) {
            proxy.setInput(zone, parseString(command.toString()));
        } else if (type == BindingType.surroundProgram) {
            proxy.setSurroundProgram(zone, parseString(command.toString()));
        } else if (type == BindingType.netRadio) {
            if (command instanceof DecimalType) {
                proxy.setNetRadio(((DecimalType) command).intValue());
            }
        }
    } catch (IOException e) {
        logger.warn("Cannot communicate with " + proxy.getHost() + " (uid: " + config.getDeviceUid() + ")");
    } catch (Throwable t) {
        logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
    }
}
Also used : Zone(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) OnOffType(org.openhab.core.library.types.OnOffType) BindingType(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.BindingType) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) YamahaReceiverProxy(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverProxy)

Example 92 with PercentType

use of org.openhab.core.library.types.PercentType 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 93 with PercentType

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

the class JdbcBaseDAO method getState.

/*****************
     * H E L P E R S *
     *****************/
protected State getState(Item item, Object v) {
    String clazz = v.getClass().getSimpleName();
    logger.debug("JDBC::ItemResultHandler::handleResult getState value = '{}', getClass = '{}', clazz = '{}'", v.toString(), v.getClass(), clazz);
    if (item instanceof NumberItem) {
        String it = getSqlTypes().get("NUMBERITEM");
        if (it.toUpperCase().contains("DOUBLE")) {
            return new DecimalType(((Number) v).doubleValue());
        } else if (it.toUpperCase().contains("DECIMAL") || it.toUpperCase().contains("NUMERIC")) {
            return new DecimalType((BigDecimal) v);
        } else if (it.toUpperCase().contains("INT")) {
            return new DecimalType(((Integer) v).intValue());
        }
        return DecimalType.valueOf(((String) v).toString());
    } else if (item instanceof ColorItem) {
        return HSBType.valueOf(((String) v).toString());
    } else if (item instanceof DimmerItem) {
        return new PercentType(objectAsInteger(v));
    } else if (item instanceof SwitchItem) {
        return OnOffType.valueOf(((String) v).toString().trim());
    } else if (item instanceof ContactItem) {
        return OpenClosedType.valueOf(((String) v).toString().trim());
    } else if (item instanceof RollershutterItem) {
        return new PercentType(objectAsInteger(v));
    } else if (item instanceof DateTimeItem) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(objectAsLong(v));
        return new DateTimeType(calendar);
    } else if (item instanceof StringItem) {
        return StringType.valueOf(((String) v).toString());
    } else {
        // Call, Location, String
        return StringType.valueOf(((String) v).toString());
    }
}
Also used : 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) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) 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) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 94 with PercentType

use of org.openhab.core.library.types.PercentType 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)

Example 95 with PercentType

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

the class AbstractDynamoDBItemSerializationTest method testUpDownTypeWithRollershutterItem.

@Test
public void testUpDownTypeWithRollershutterItem() throws IOException {
    // note: comes back as PercentType instead of the original UpDownType
    DynamoDBItem<?> dbItemDown = testStateGeneric(UpDownType.DOWN, BigDecimal.ZERO);
    testAsHistoricGeneric(dbItemDown, new RollershutterItem("foo"), new PercentType(BigDecimal.ZERO));
    DynamoDBItem<?> dbItemUp = testStateGeneric(UpDownType.UP, BigDecimal.ONE);
    testAsHistoricGeneric(dbItemUp, new RollershutterItem("foo"), new PercentType(BigDecimal.ONE));
}
Also used : RollershutterItem(org.openhab.core.library.items.RollershutterItem) PercentType(org.openhab.core.library.types.PercentType) Test(org.junit.Test)

Aggregations

PercentType (org.openhab.core.library.types.PercentType)105 DecimalType (org.openhab.core.library.types.DecimalType)43 State (org.openhab.core.types.State)29 StringType (org.openhab.core.library.types.StringType)27 DimmerItem (org.openhab.core.library.items.DimmerItem)22 Test (org.junit.Test)21 HSBType (org.openhab.core.library.types.HSBType)19 OnOffType (org.openhab.core.library.types.OnOffType)19 RollershutterItem (org.openhab.core.library.items.RollershutterItem)17 NumberItem (org.openhab.core.library.items.NumberItem)16 BigDecimal (java.math.BigDecimal)14 SwitchItem (org.openhab.core.library.items.SwitchItem)13 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)13 ColorItem (org.openhab.core.library.items.ColorItem)11 DateTimeType (org.openhab.core.library.types.DateTimeType)11 Calendar (java.util.Calendar)9 ContactItem (org.openhab.core.library.items.ContactItem)9 UpDownType (org.openhab.core.library.types.UpDownType)9 StopMoveType (org.openhab.core.library.types.StopMoveType)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6