Search in sources :

Example 46 with StringType

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

the class PulseaudioBinding method execute.

/**
     * @{inheritDoc
     */
@Override
@SuppressWarnings("incomplete-switch")
public void execute() {
    List<PulseaudioClient> updatedClients = new ArrayList<PulseaudioClient>();
    for (PulseaudioBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            String audioItemName = provider.getItemName(itemName);
            String serverId = provider.getServerId(itemName);
            Class<? extends Item> itemType = provider.getItemType(itemName);
            String command = provider.getCommand(itemName);
            PulseaudioCommandTypeMapping commandType = null;
            if (command != null && !command.isEmpty()) {
                try {
                    commandType = PulseaudioCommandTypeMapping.valueOf(command.toUpperCase());
                } catch (IllegalArgumentException e) {
                    logger.warn("unknown command specified for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
                    continue;
                }
            }
            if (itemType.isAssignableFrom(GroupItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                // directly
                continue;
            }
            PulseaudioClient client = clients.get(serverId);
            if (client == null) {
                logger.warn("connection to pulseaudio server in not available " + "for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
                continue;
            }
            if (audioItemName == null) {
                logger.warn("audio-item-name isn't configured properly " + "for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
                continue;
            }
            if (!updatedClients.contains(client)) {
                // update the clients data structure to avoid
                // inconsistencies
                client.update();
                updatedClients.add(client);
            }
            State value = UnDefType.UNDEF;
            AbstractAudioDeviceConfig audioItem = client.getGenericAudioItem(audioItemName);
            if (audioItem != null) {
                // item found
                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (commandType == null) {
                        // Check if item is unmuted and running
                        if (!audioItem.isMuted() && audioItem.getState() != null && audioItem.getState().equals(AbstractAudioDeviceConfig.State.RUNNING)) {
                            value = OnOffType.ON;
                        } else {
                            value = OnOffType.OFF;
                        }
                    } else {
                        switch(commandType) {
                            case EXISTS:
                                value = OnOffType.ON;
                                break;
                            case MUTED:
                                value = audioItem.isMuted() ? OnOffType.ON : OnOffType.OFF;
                                break;
                            case RUNNING:
                            case CORKED:
                            case SUSPENDED:
                            case IDLE:
                                try {
                                    value = audioItem.getState() != null && audioItem.getState().equals(AbstractAudioDeviceConfig.State.valueOf(commandType.name())) ? OnOffType.ON : OnOffType.OFF;
                                } catch (IllegalArgumentException e) {
                                    logger.warn("no corresponding AbstractAudioDeviceConfig.State found for " + commandType.name());
                                }
                                break;
                        }
                    }
                } else if (itemType.isAssignableFrom(DimmerItem.class)) {
                    value = new PercentType(audioItem.getVolume());
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    if (commandType == null) {
                        // when no other pulseaudioCommand specified, we use
                        // VOLUME
                        value = new DecimalType(audioItem.getVolume());
                    } else {
                        // ID command types
                        switch(commandType) {
                            case VOLUME:
                                value = new DecimalType(audioItem.getVolume());
                                break;
                            case ID:
                                value = new DecimalType(audioItem.getId());
                                break;
                            case MODULE_ID:
                                if (audioItem.getModule() != null) {
                                    value = new DecimalType(audioItem.getModule().getId());
                                }
                                break;
                        }
                    }
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    if (commandType == null) {
                        value = new StringType(audioItem.toString());
                    } else if (audioItem instanceof Sink) {
                        Sink sink = (Sink) audioItem;
                        switch(commandType) {
                            case SLAVE_SINKS:
                                if (sink.isCombinedSink()) {
                                    value = new StringType(StringUtils.join(sink.getCombinedSinkNames(), ","));
                                }
                                break;
                        }
                    }
                } else {
                    logger.debug("unhandled item type [type={}, name={}]", itemType.getClass(), audioItemName);
                }
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = OnOffType.OFF;
            }
            eventPublisher.postUpdate(itemName, value);
        }
    }
}
Also used : PulseaudioBindingProvider(org.openhab.binding.pulseaudio.PulseaudioBindingProvider) StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) Sink(org.openhab.binding.pulseaudio.internal.items.Sink) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) GroupItem(org.openhab.core.items.GroupItem) AbstractAudioDeviceConfig(org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 47 with StringType

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

the class PlugwiseBinding method createStateForType.

@SuppressWarnings("unchecked")
private State createStateForType(PlugwiseCommandType ctype, Object value) throws BindingConfigParseException {
    Class<? extends Type> typeClass = ctype.getTypeClass();
    // the logic below covers all possible command types and value types
    if (typeClass == DecimalType.class && value instanceof Float) {
        return new DecimalType((Float) value);
    } else if (typeClass == DecimalType.class && value instanceof Double) {
        return new DecimalType((Double) value);
    } else if (typeClass == OnOffType.class && value instanceof Boolean) {
        return ((Boolean) value).booleanValue() ? OnOffType.ON : OnOffType.OFF;
    } else if (typeClass == DateTimeType.class && value instanceof Calendar) {
        return new DateTimeType((Calendar) value);
    } else if (typeClass == DateTimeType.class && value instanceof DateTime) {
        return new DateTimeType(((DateTime) value).toCalendar(Locale.getDefault()));
    } else if (typeClass == StringType.class && value instanceof String) {
        return new StringType((String) value);
    }
    logger.debug("less efficient (generic) logic is applied for converting a Plugwise value " + "(command type class: {}, value class {})", typeClass.getName(), value.getClass().getName());
    List<Class<? extends State>> stateTypeList = new ArrayList<Class<? extends State>>();
    stateTypeList.add((Class<? extends State>) typeClass);
    return TypeParser.parseState(stateTypeList, value.toString());
}
Also used : StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) DateTimeType(org.openhab.core.library.types.DateTimeType) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType)

Example 48 with StringType

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

the class SonosBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    SonosBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    String commandAsString = command.toString();
    if (command != null) {
        List<Command> commands = new ArrayList<Command>();
        if (command instanceof StringType || command instanceof DecimalType) {
            commands = provider.getVariableCommands(itemName);
        } else {
            commands.add(command);
        }
        for (Command someCommand : commands) {
            String sonosID = provider.getSonosID(itemName, someCommand);
            String sonosCommand = provider.getSonosCommand(itemName, someCommand);
            SonosCommandType sonosCommandType = null;
            try {
                sonosCommandType = SonosCommandType.getCommandType(sonosCommand, Direction.OUT);
            } catch (Exception e) {
                logger.error("An exception occured while verifying command compatibility ({})", e.getMessage());
            }
            if (sonosID != null) {
                if (sonosCommandType != null) {
                    logger.debug("Executing command: item:{}, command:{}, ID:{}, CommandType:{}, commandString:{}", new Object[] { itemName, someCommand, sonosID, sonosCommandType, commandAsString });
                    executeCommand(itemName, someCommand, sonosID, sonosCommandType, commandAsString);
                } else {
                    logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
                }
            } else {
                logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
            }
        }
    }
}
Also used : SonosBindingProvider(org.openhab.binding.sonos.SonosBindingProvider) Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) SonosCommandType(org.openhab.binding.sonos.SonosCommandType) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType) ConfigurationException(org.osgi.service.cm.ConfigurationException) IllegalClassException(org.apache.commons.lang.IllegalClassException) JobExecutionException(org.quartz.JobExecutionException) SAXException(org.xml.sax.SAXException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SchedulerException(org.quartz.SchedulerException)

Example 49 with StringType

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

the class SonosGenericBindingProvider method getVariableCommands.

@Override
public List<Command> getVariableCommands(String anItem) {
    List<Command> commands = new ArrayList<Command>();
    SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(anItem);
    for (Command aCommand : aConfig.keySet()) {
        if (aCommand instanceof StringType || aCommand instanceof DecimalType) {
            commands.add(aCommand);
        }
    }
    return commands;
}
Also used : Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType)

Example 50 with StringType

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

the class DigitalSTROMBinding method deviceCall.

private void deviceCall(String itemName, Command cm) {
    Device device = deviceMap.get(itemName);
    if (device != null) {
        if (cm instanceof org.openhab.core.library.types.OnOffType) {
            if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.ON)) {
                boolean transmitted = digitalSTROM.turnDeviceOn(getSessionToken(), device.getDSID(), null);
                if (transmitted) {
                    device.setOutputValue(device.getMaxOutPutValue());
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MAXIMUM.getSceneNumber());
                }
            } else if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.OFF)) {
                boolean transmitted = digitalSTROM.turnDeviceOff(getSessionToken(), device.getDSID(), null);
                if (transmitted) {
                    device.setOutputValue(0);
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MINIMUM.getSceneNumber());
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.IncreaseDecreaseType) {
            if (!device.isDimmable()) {
                logger.warn("device is not in dimm mode: " + itemName + " outputMode: " + device.getOutputMode().getMode());
                return;
            }
            if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.INCREASE)) {
                boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.INCREMENT, false);
                if (transmitted) {
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.INCREMENT.getSceneNumber());
                    if (device.getOutputValue() == 0) {
                        initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
                    } else {
                        device.increase();
                    }
                } else {
                    logger.error("transmitting increase command FAILED " + itemName);
                }
            } else if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.DECREASE)) {
                boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.DECREMENT, false);
                if (transmitted) {
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.DECREMENT.getSceneNumber());
                    device.decrease();
                } else {
                    logger.error("transmitting decrease command FAILED " + itemName);
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.PercentType) {
            int percent = -1;
            try {
                percent = (int) Float.parseFloat(cm.toString());
            } catch (java.lang.NumberFormatException e) {
                logger.error("NumberFormatException on a PercentType with command: " + cm.toString());
            }
            if (percent != -1) {
                if (percent > -1 && percent < 101) {
                    if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                        DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                        if (confItem != null) {
                            if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                                int old = device.getSlatPosition();
                                device.setSlatPosition(fromPercentToValue(percent, device.getMaxSlatPosition()));
                                boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, fromPercentToValue(percent, device.getMaxSlatPosition()));
                                if (!transmitted) {
                                    device.setSlatPosition(old);
                                    logger.error("could NOT successfully set new value for slats ..." + cm.toString());
                                }
                            } else {
                                int old = device.getOutputValue();
                                device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
                                boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
                                if (!transmitted) {
                                    device.setOutputValue(old);
                                    logger.error("could NOT successfully set new value ..." + cm.toString());
                                }
                            }
                        }
                    } else {
                        int old = device.getOutputValue();
                        device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
                        boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
                        if (!transmitted) {
                            device.setOutputValue(old);
                            logger.error("could NOT successfully set new value ..." + cm.toString());
                        }
                    }
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.StopMoveType) {
            if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        logger.warn("stop and move command NOT possible for slats, use PercentType command or up and down please");
                    } else {
                        handleStopMoveForRollershutter(device, cm);
                    }
                }
            } else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
                handleStopMoveForRollershutter(device, cm);
            }
        } else if (cm instanceof org.openhab.core.library.types.UpDownType) {
            if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                // 255 is max open, 0 is closed
                DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.UP)) {
                            int slatPosition = device.getSlatPosition();
                            int newPosition = slatPosition + DeviceConstants.MOVE_STEP_SLAT;
                            if (newPosition > device.getMaxSlatPosition()) {
                                newPosition = device.getMaxSlatPosition();
                            }
                            boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
                            if (transmitted) {
                                device.setSlatPosition(newPosition);
                            }
                        } else if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.DOWN)) {
                            int slatPosition = device.getSlatPosition();
                            int newPosition = slatPosition - DeviceConstants.MOVE_STEP_SLAT;
                            if (newPosition < device.getMinSlatPosition()) {
                                newPosition = device.getMinSlatPosition();
                            }
                            boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
                            if (transmitted) {
                                device.setSlatPosition(newPosition);
                            }
                        }
                    } else {
                        handleUpDownForRollershutter(device, cm);
                    }
                }
            } else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
                handleUpDownForRollershutter(device, cm);
            } else {
                logger.warn("Wrong item configuration ... this hardware is not a rollershutter: " + itemName);
            }
        }
    } else {
        if (cm instanceof DecimalType) {
            DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
            if (confItem != null && confItem.context != null) {
                if (confItem.context.equals(ContextConfig.apartment)) {
                    digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(((DecimalType) cm).intValue()), false);
                } else if (confItem.context.equals(ContextConfig.zone)) {
                    digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(((DecimalType) cm).intValue()), false);
                }
            }
        } else if (cm instanceof StringType) {
            DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
            if (confItem != null && confItem.context != null) {
                int scene = -1;
                try {
                    scene = Integer.parseInt(cm.toString());
                } catch (java.lang.NumberFormatException e) {
                    logger.error("NumberFormatException by parsing " + cm.toString() + " for " + confItem.itemName);
                }
                if (scene != -1) {
                    if (confItem.context.equals(ContextConfig.apartment)) {
                        digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(scene), false);
                    } else if (confItem.context.equals(ContextConfig.zone)) {
                        digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(scene), false);
                    }
                }
            }
        } else {
            logger.warn("couldn't find digitalstrom device for " + itemName);
        }
    }
}
Also used : IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) StringType(org.openhab.core.library.types.StringType) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) OnOffType(org.openhab.core.library.types.OnOffType) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) StopMoveType(org.openhab.core.library.types.StopMoveType)

Aggregations

StringType (org.openhab.core.library.types.StringType)90 DecimalType (org.openhab.core.library.types.DecimalType)69 State (org.openhab.core.types.State)30 DateTimeType (org.openhab.core.library.types.DateTimeType)28 PercentType (org.openhab.core.library.types.PercentType)27 NumberItem (org.openhab.core.library.items.NumberItem)25 Calendar (java.util.Calendar)23 StringItem (org.openhab.core.library.items.StringItem)18 OnOffType (org.openhab.core.library.types.OnOffType)15 SwitchItem (org.openhab.core.library.items.SwitchItem)12 ContactItem (org.openhab.core.library.items.ContactItem)10 DimmerItem (org.openhab.core.library.items.DimmerItem)10 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)8 HSBType (org.openhab.core.library.types.HSBType)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7