Search in sources :

Example 71 with DecimalType

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

the class IRtransGenericBindingProvider method getQualifiedCommands.

/**
     * {@inheritDoc}
     */
@Override
public List<Command> getQualifiedCommands(String itemName, Command command) {
    List<Command> commands = new ArrayList<Command>();
    IRtransBindingConfig aConfig = (IRtransBindingConfig) bindingConfigs.get(itemName);
    for (Command aCommand : aConfig.keySet()) {
        if (aCommand == command) {
            commands.add(aCommand);
        } else {
            if (aCommand instanceof DecimalType) {
                commands.add(aCommand);
            }
        }
    }
    return commands;
}
Also used : Command(org.openhab.core.types.Command) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType)

Example 72 with DecimalType

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

the class JointSpaceBinding method updateItemState.

/**
     * Polls the TV for the values specified in @see tvCommand and posts state
     * update for @see itemName Currently only the following commands are
     * available - "ambilight[...]" returning a HSBType state for the given
     * ambilight pixel specified in [...] - "volume" returning a DecimalType -
     * "volume.mute" returning 'On' or 'Off' - "source" returning a String with
     * selected source (e.g. "hdmi1", "tv", etc)
     *
     * @param itemName
     * @param tvCommand
     */
private void updateItemState(String itemName, String tvCommand) {
    if (tvCommand.contains("ambilight")) {
        String[] layer = command2LayerString(tvCommand);
        HSBType state = new HSBType(getAmbilightColor(ip + ":" + port, layer));
        eventPublisher.postUpdate(itemName, state);
    } else if (tvCommand.contains("volume")) {
        if (tvCommand.contains("mute")) {
            eventPublisher.postUpdate(itemName, getTVVolume(ip + ":" + port).mute ? OnOffType.ON : OnOffType.OFF);
        } else {
            eventPublisher.postUpdate(itemName, new DecimalType(getTVVolume(ip + ":" + port).volume));
        }
    } else if (tvCommand.contains("source")) {
        eventPublisher.postUpdate(itemName, new StringType(getSource(ip + ":" + port)));
    } else {
        logger.error("Could not parse item state\"" + tvCommand + "\" for polling");
        return;
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType)

Example 73 with DecimalType

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

the class JointSpaceBinding method internalReceiveCommand.

/**
     * @{inheritDoc Processes the commands and maps them to jointspace commands
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    if (itemName != null && !this.providers.isEmpty()) {
        JointSpaceBindingProvider provider = this.providers.iterator().next();
        if (provider == null) {
            logger.warn("Doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
            return;
        }
        logger.debug("Received command (item='{}', state='{}', class='{}')", new Object[] { itemName, command.toString(), command.getClass().toString() });
        String tvCommandString = null;
        // first check if we can translate the command directly
        tvCommandString = provider.getTVCommand(itemName, command.toString());
        // if not try some special notations
        if (tvCommandString == null) {
            if (command instanceof HSBType) {
                tvCommandString = provider.getTVCommand(itemName, "HSB");
            } else if (command instanceof DecimalType) {
                tvCommandString = provider.getTVCommand(itemName, "DEC");
            }
            if (tvCommandString == null) {
                tvCommandString = provider.getTVCommand(itemName, "*");
            }
        }
        if (tvCommandString == null) {
            logger.warn("Unrecognized command \"{}\"", command.toString());
            return;
        }
        if (tvCommandString.contains("key")) {
            logger.debug("Found a Key command: " + tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            if (commandlist.length != 2) {
                logger.warn("wrong number of arguments for key command \"{}\". Should be key.X", tvCommandString);
                return;
            }
            String key = commandlist[1];
            sendTVCommand(key, ip + ":" + port);
        } else if (tvCommandString.contains("ambilight")) {
            logger.debug("Found an ambilight command: {}", tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            String[] layer = command2LayerString(tvCommandString);
            if (commandlist.length < 2) {
                logger.warn("wrong number of arguments for ambilight command \"{}\". Should be at least ambilight.color, ambilight.mode.X, etc...", tvCommandString);
                return;
            }
            if (commandlist[1].contains("color")) {
                setAmbilightColor(ip + ":" + port, command, layer);
            } else if (commandlist[1].contains("mode")) {
                if (commandlist.length != 3) {
                    logger.warn("wrong number of arguments for ambilight.mode command \"{}\". Should be ambilight.mode.internal, ambilight.mode.manual, ambilight.mode.expert", tvCommandString);
                    return;
                }
                setAmbilightMode(commandlist[2], ip + ":" + port);
            }
        } else if (tvCommandString.contains("volume")) {
            logger.debug("Found a Volume command: {}", tvCommandString);
            sendVolume(ip + ":" + port, command);
        } else if (tvCommandString.contains("source")) {
            logger.debug("Found a Source command: {}", tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            if (commandlist.length < 2) {
                logger.warn("wrong number of arguments for source command \"{}\". Should be at least mode.X...", tvCommandString);
                return;
            }
            sendSource(ip + ":" + port, commandlist[1]);
        } else {
            logger.warn("Unrecognized tv command \"{}\". Only key.X or ambilight[].X is supported", tvCommandString);
            return;
        }
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) JointSpaceBindingProvider(org.openhab.binding.jointspace.JointSpaceBindingProvider)

Example 74 with DecimalType

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

the class SerialDevice method serialEvent.

@Override
public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            // we get here if data has been received
            StringBuilder sb = new StringBuilder();
            byte[] readBuffer = new byte[20];
            try {
                do {
                    // read data from serial device
                    while (inputStream.available() > 0) {
                        int bytes = inputStream.read(readBuffer);
                        sb.append(new String(readBuffer, 0, bytes));
                    }
                    try {
                        // add wait states around reading the stream, so that interrupted transmissions are merged
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    // ignore interruption
                    }
                } while (inputStream.available() > 0);
                // sent data
                String result = sb.toString();
                // send data to the bus
                logger.debug("Received message '{}' on serial port {}", new String[] { result, port });
                if (eventPublisher != null) {
                    if (configMap != null && !configMap.isEmpty()) {
                        for (Entry<String, ItemType> entry : configMap.entrySet()) {
                            // use pattern
                            if (entry.getValue().pattern != null) {
                                if (transformationService == null) {
                                    logger.error("No transformation service available!");
                                } else {
                                    try {
                                        String value = transformationService.transform(entry.getValue().pattern, result);
                                        if (entry.getValue().type.equals(NumberItem.class)) {
                                            try {
                                                eventPublisher.postUpdate(entry.getKey(), new DecimalType(value));
                                            } catch (NumberFormatException e) {
                                                logger.warn("Unable to convert regex result '{}' for item {} to number", new String[] { result, entry.getKey() });
                                            }
                                        } else {
                                            eventPublisher.postUpdate(entry.getKey(), new StringType(value));
                                        }
                                    } catch (TransformationException e) {
                                        logger.error("Unable to transform!", e);
                                    }
                                }
                            } else if (entry.getValue().type == StringItem.class) {
                                if (entry.getValue().base64) {
                                    result = Base64.encodeBase64String(result.getBytes());
                                }
                                eventPublisher.postUpdate(entry.getKey(), new StringType(result));
                            } else if (entry.getValue().type == SwitchItem.class && result.trim().isEmpty()) {
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.ON);
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.OFF);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                logger.debug("Error receiving data on serial port {}: {}", new String[] { port, e.getMessage() });
            }
            break;
    }
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) StringType(org.openhab.core.library.types.StringType) IOException(java.io.IOException) StringItem(org.openhab.core.library.items.StringItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 75 with DecimalType

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

the class TestSHCMessage method testWeatherTempHumMinMax2.

/**
     * test data is: weather temperature & humidity: 0 (0x000) temperatur: 32767
     * (0x7FFF)
     */
@Test
public void testWeatherTempHumMinMax2() {
    String message = " PKT:SID=10;PC=17531;MT=8;MGID=10;MID=2;MD=001FFFC00000;785c34de";
    SHCMessage shcMessage = new SHCMessage(message, packet);
    List<Type> values = shcMessage.getData().getOpenHABTypes();
    assertEquals(0, ((DecimalType) values.get(0)).intValue());
    assertEquals(32767, ((DecimalType) values.get(1)).intValue());
}
Also used : Type(org.openhab.core.types.Type) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) SHCMessage(org.openhab.binding.smarthomatic.internal.SHCMessage) Test(org.junit.Test)

Aggregations

DecimalType (org.openhab.core.library.types.DecimalType)222 Test (org.junit.Test)70 StringType (org.openhab.core.library.types.StringType)69 OnOffType (org.openhab.core.library.types.OnOffType)63 PercentType (org.openhab.core.library.types.PercentType)43 State (org.openhab.core.types.State)40 Type (org.openhab.core.types.Type)39 NumberItem (org.openhab.core.library.items.NumberItem)38 SHCMessage (org.openhab.binding.smarthomatic.internal.SHCMessage)37 DateTimeType (org.openhab.core.library.types.DateTimeType)29 BigDecimal (java.math.BigDecimal)23 Calendar (java.util.Calendar)23 HSBType (org.openhab.core.library.types.HSBType)20 SwitchItem (org.openhab.core.library.items.SwitchItem)18 IOException (java.io.IOException)16 StringItem (org.openhab.core.library.items.StringItem)16 ConfigurationException (org.osgi.service.cm.ConfigurationException)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14