Search in sources :

Example 1 with TinkerforgeBindingProvider

use of org.openhab.binding.tinkerforge.TinkerforgeBindingProvider in project openhab1-addons by openhab.

the class TinkerforgeBinding method getBindingProviders.

/**
     * Searches the provider which is bound to the device with the given uid and subid.
     *
     * @param uid The device uid as {@code String}.
     * @param subId The device subid as {@code String} or {@code null} if it is not a sub device.
     * @return The {@code TinkerforgeBindingProvider} which is bound to the device as {@code Item} or
     *         {@code null} if no item was found.
     */
private Map<String, TinkerforgeBindingProvider> getBindingProviders(String uid, String subId) {
    Map<String, TinkerforgeBindingProvider> providerMap = new HashMap<String, TinkerforgeBindingProvider>();
    for (TinkerforgeBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            String deviceUid = provider.getUid(itemName);
            String subDeviceId = provider.getSubId(itemName);
            String deviceName = provider.getName(itemName);
            if (deviceName != null) {
                OHTFDevice<?, ?> ohtfDevice = ohConfig.getConfigByOHId(deviceName);
                deviceUid = ohtfDevice.getUid();
                subDeviceId = ohtfDevice.getSubid();
                logger.trace("found deviceName {}, uid={}, subId {}", deviceName, deviceUid, subDeviceId);
            }
            if (uid.equals(deviceUid)) {
                if (subId == null && subDeviceId == null) {
                    providerMap.put(itemName, provider);
                } else if (subId != null && subId.equals(subDeviceId)) {
                    providerMap.put(itemName, provider);
                }
            }
        }
    }
    return providerMap;
}
Also used : TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider) HashMap(java.util.HashMap)

Example 2 with TinkerforgeBindingProvider

use of org.openhab.binding.tinkerforge.TinkerforgeBindingProvider in project openhab1-addons by openhab.

the class TinkerforgeBinding method searchConfiguredItemName.

/**
     * Searches the name of an item which is bound to the device with the given uid and subid.
     *
     * @param uid The device uid as {@code String}.
     * @param subId The device subid as {@code String} or {@code null} if it is not a sub device.
     * @return The name of the item which is bound to the device as {@code String} or {@code null} if
     *         no item was found.
     */
private String searchConfiguredItemName(String uid, String subId) {
    for (TinkerforgeBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            String deviceUid = provider.getUid(itemName);
            String subDeviceId = provider.getSubId(itemName);
            String deviceName = provider.getName(itemName);
            if (deviceName != null) {
                logger.trace("found item for command: name {}", deviceName);
                OHTFDevice<?, ?> ohtfDevice = ohConfig.getConfigByOHId(deviceName);
                deviceUid = ohtfDevice.getUid();
                deviceName = ohtfDevice.getSubid();
            }
            if (uid.equals(deviceUid)) {
                if (subId == null && subDeviceId == null) {
                    return itemName;
                } else if (subId != null && subId.equals(subDeviceId)) {
                    return itemName;
                }
            }
        }
    }
    return null;
}
Also used : TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider)

Example 3 with TinkerforgeBindingProvider

use of org.openhab.binding.tinkerforge.TinkerforgeBindingProvider in project openhab1-addons by openhab.

the class TinkerforgeBinding method postUpdate.

private void postUpdate(String uid, String subId, TinkerforgeValue sensorValue) {
    // TODO undef handling
    logger.trace("postUpdate called for uid {} subid {}", uid, subId);
    Map<String, TinkerforgeBindingProvider> providerMap = getBindingProviders(uid, subId);
    if (providerMap.size() == 0) {
        logger.debug("{} found no item for uid {}, subid {}", LoggerConstants.TFMODELUPDATE, uid, subId);
    }
    for (Entry<String, TinkerforgeBindingProvider> entry : providerMap.entrySet()) {
        String itemName = entry.getKey();
        TinkerforgeBindingProvider provider = entry.getValue();
        Class<? extends Item> itemType = provider.getItemType(itemName);
        State value = UnDefType.UNDEF;
        if (sensorValue instanceof DecimalValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = DecimalType.valueOf(String.valueOf(sensorValue));
                logger.trace("found item to update for DecimalValue {}", itemName);
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue.equals(DecimalValue.ZERO) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue.equals(DecimalValue.ZERO) ? OnOffType.OFF : OnOffType.ON;
            } else {
                logger.trace("no update for DecimalValue for item {}", itemName);
                continue;
            }
        } else if (sensorValue instanceof HighLowValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? OnOffType.ON : OnOffType.OFF;
            } else {
                continue;
            }
        } else if (sensorValue instanceof OnOffValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = sensorValue == OnOffValue.ON ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue == OnOffValue.ON ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue == OnOffValue.ON ? OnOffType.ON : OnOffType.OFF;
            } else {
                continue;
            }
        } else if (sensorValue instanceof PercentValue) {
            if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == 1 ? OnOffType.ON : OnOffType.OFF;
                logger.debug("switch found {}", itemName);
            } else if (itemType.isAssignableFrom(RollershutterItem.class) || itemType.isAssignableFrom(DimmerItem.class)) {
                value = new PercentType(((PercentValue) sensorValue).toBigDecimal());
                logger.debug("Rollershutter or dimmer found {} {}", itemName);
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == -1 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                logger.debug("contact found {}", itemName);
            } else {
                continue;
            }
        } else if (sensorValue instanceof DirectionValue) {
            if (itemType.isAssignableFrom(RollershutterItem.class)) {
                value = sensorValue == DirectionValue.RIGHT ? UpDownType.UP : UpDownType.DOWN;
                logger.trace("found item to update for UpDownValue {}", itemName);
            } else {
                continue;
            }
        } else if (sensorValue instanceof HSBValue) {
            if (itemType.isAssignableFrom(ColorItem.class)) {
                logger.trace("found item to update for HSBValue {}", itemName);
                value = ((HSBValue) sensorValue).getHsbValue();
            }
        } else if (sensorValue == UnDefValue.UNDEF || sensorValue == null) {
            value = UnDefType.UNDEF;
        }
        eventPublisher.postUpdate(itemName, value);
        logger.debug("{} postupdate: found sensorValue: {} for item {}", LoggerConstants.TFMODELUPDATE, sensorValue, itemName);
    }
}
Also used : OnOffValue(org.openhab.binding.tinkerforge.internal.types.OnOffValue) PercentValue(org.openhab.binding.tinkerforge.internal.types.PercentValue) DirectionValue(org.openhab.binding.tinkerforge.internal.types.DirectionValue) ContactItem(org.openhab.core.library.items.ContactItem) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider) DecimalValue(org.openhab.binding.tinkerforge.internal.types.DecimalValue) State(org.openhab.core.types.State) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DimmerItem(org.openhab.core.library.items.DimmerItem) HSBValue(org.openhab.binding.tinkerforge.internal.types.HSBValue) HighLowValue(org.openhab.binding.tinkerforge.internal.types.HighLowValue) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 4 with TinkerforgeBindingProvider

use of org.openhab.binding.tinkerforge.TinkerforgeBindingProvider 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)

Aggregations

TinkerforgeBindingProvider (org.openhab.binding.tinkerforge.TinkerforgeBindingProvider)4 HighLowValue (org.openhab.binding.tinkerforge.internal.types.HighLowValue)2 OnOffValue (org.openhab.binding.tinkerforge.internal.types.OnOffValue)2 PercentType (org.openhab.core.library.types.PercentType)2 HashMap (java.util.HashMap)1 DigitalActor (org.openhab.binding.tinkerforge.internal.model.DigitalActor)1 DimmableActor (org.openhab.binding.tinkerforge.internal.model.DimmableActor)1 MBaseDevice (org.openhab.binding.tinkerforge.internal.model.MBaseDevice)1 MSwitchActor (org.openhab.binding.tinkerforge.internal.model.MSwitchActor)1 MTextActor (org.openhab.binding.tinkerforge.internal.model.MTextActor)1 MoveActor (org.openhab.binding.tinkerforge.internal.model.MoveActor)1 NumberActor (org.openhab.binding.tinkerforge.internal.model.NumberActor)1 PercentTypeActor (org.openhab.binding.tinkerforge.internal.model.PercentTypeActor)1 ProgrammableColorActor (org.openhab.binding.tinkerforge.internal.model.ProgrammableColorActor)1 ProgrammableSwitchActor (org.openhab.binding.tinkerforge.internal.model.ProgrammableSwitchActor)1 SetPointActor (org.openhab.binding.tinkerforge.internal.model.SetPointActor)1 SimpleColorActor (org.openhab.binding.tinkerforge.internal.model.SimpleColorActor)1 DecimalValue (org.openhab.binding.tinkerforge.internal.types.DecimalValue)1 DirectionValue (org.openhab.binding.tinkerforge.internal.types.DirectionValue)1 HSBValue (org.openhab.binding.tinkerforge.internal.types.HSBValue)1