Search in sources :

Example 1 with SappAddressOpenClosedStatus

use of org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus in project openhab1-addons by openhab.

the class SappBinding method updateState.

/**
     * updates item repository for a single item
     */
private void updateState(String pnmasId, SappAddressType sappAddressType, int addressToUpdate, int newState, SappBindingProvider provider) {
    logger.debug("Updating {} {} with new value {}", sappAddressType, addressToUpdate, newState);
    for (String itemName : provider.getItemNames()) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
                SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
                if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
                    SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
                    if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                        logger.debug("found binding to update {}", sappBindingConfigSwitchItem);
                        int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                        State stateToSet = result == statusAddress.getOnValue() ? OnOffType.ON : OnOffType.OFF;
                        if (!stateToSet.equals(item.getState())) {
                            eventPublisher.postUpdate(itemName, stateToSet);
                        }
                    }
                }
            } else if (item instanceof ContactItem) {
                SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
                SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigContactItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                    State stateToSet = result == statusAddress.getOpenValue() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof NumberItem) {
                SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
                SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
                if (address.getAddressType() == sappAddressType && address.getPnmasId().equals(pnmasId) && addressToUpdate == address.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigNumberItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(address.getSubAddress(), newState);
                    State stateToSet = new DecimalType(address.scaledValue(result, address.getSubAddress()));
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof RollershutterItem) {
                SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
                SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigRollershutterItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                    State stateToSet = result == statusAddress.getOpenValue() ? PercentType.HUNDRED : (result == statusAddress.getClosedValue() ? PercentType.ZERO : PercentType.valueOf("50"));
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof DimmerItem) {
                SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
                SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigDimmerItem);
                    int result = statusAddress.scaledValue(SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState), statusAddress.getSubAddress()).round(new MathContext(0, RoundingMode.HALF_EVEN)).intValue();
                    State stateToSet;
                    if (result <= PercentType.ZERO.intValue()) {
                        stateToSet = PercentType.ZERO;
                    } else if (result >= PercentType.HUNDRED.intValue()) {
                        stateToSet = PercentType.HUNDRED;
                    } else {
                        stateToSet = PercentType.valueOf(String.valueOf(result));
                    }
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else {
                logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
            }
        } catch (ItemNotFoundException e) {
            logger.error("Item {} not found", itemName);
        }
    }
}
Also used : SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) MathContext(java.math.MathContext) SappAddressOpenClosedStatus(org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SappAddressOnOffStatus(org.openhab.binding.sapp.internal.model.SappAddressOnOffStatus) SappAddressDimmer(org.openhab.binding.sapp.internal.model.SappAddressDimmer) SappAddressDecimal(org.openhab.binding.sapp.internal.model.SappAddressDecimal) State(org.openhab.core.types.State) SappAddressRollershutterStatus(org.openhab.binding.sapp.internal.model.SappAddressRollershutterStatus) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 2 with SappAddressOpenClosedStatus

use of org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus in project openhab1-addons by openhab.

the class SappBinding method queryAndSendActualState.

/**
     * reads state from device and updates item repository
     */
private void queryAndSendActualState(SappBindingProvider provider, String itemName) {
    logger.debug("querying and sending item {}", itemName);
    try {
        Item item = itemRegistry.getItem(itemName);
        if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
            SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
            SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
            if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
                updateOnOffItem(provider, statusAddress, itemName, item);
            }
        } else if (item instanceof ContactItem) {
            SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
            SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
            updateOpenClosedItem(provider, statusAddress, itemName, item);
        } else if (item instanceof NumberItem) {
            SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
            SappAddressDecimal statusAddress = sappBindingConfigNumberItem.getStatus();
            updateDecimalItem(provider, statusAddress, itemName, item);
        } else if (item instanceof RollershutterItem) {
            SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
            SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
            updateRollershutterItem(provider, statusAddress, itemName, item);
        } else if (item instanceof DimmerItem) {
            SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
            SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
            updateDimmerItem(provider, statusAddress, itemName, item);
        } else {
            logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
        }
    } catch (ItemNotFoundException e) {
        logger.error("Item {} not found", itemName);
    }
}
Also used : SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) SappAddressOpenClosedStatus(org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SappAddressOnOffStatus(org.openhab.binding.sapp.internal.model.SappAddressOnOffStatus) SappAddressDimmer(org.openhab.binding.sapp.internal.model.SappAddressDimmer) SappAddressDecimal(org.openhab.binding.sapp.internal.model.SappAddressDecimal) SappAddressRollershutterStatus(org.openhab.binding.sapp.internal.model.SappAddressRollershutterStatus) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 3 with SappAddressOpenClosedStatus

use of org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus in project openhab1-addons by openhab.

the class SappBindingConfigContactItem method parseSappAddressStatus.

private SappAddressOpenClosedStatus parseSappAddressStatus(String bindingStringAddress) throws BindingConfigParseException {
    String pnmasId;
    SappAddressType addressType;
    int address;
    String subAddress;
    int openValue = 1;
    String[] bindingAddress = bindingStringAddress.split(":");
    if (bindingAddress.length != 4 && bindingAddress.length != 5) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // pnmasId
    pnmasId = bindingAddress[0];
    if (pnmasId.length() == 0) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // addressType
    addressType = SappAddressType.fromString(bindingAddress[1].toUpperCase());
    if (!validAddresses.keySet().contains(addressType)) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // address
    try {
        address = Integer.parseInt(bindingAddress[2]);
        if (address < validAddresses.get(addressType).getLoRange() || address > validAddresses.get(addressType).getHiRange()) {
            throw new BindingConfigParseException(errorMessage(bindingStringAddress));
        }
    } catch (NumberFormatException e) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // subaddress
    subAddress = bindingAddress[3].toUpperCase();
    if (!ArrayUtils.contains(validSubAddresses, subAddress)) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // openvalue
    if (bindingAddress.length == 5) {
        try {
            openValue = Integer.parseInt(bindingAddress[4]);
        } catch (NumberFormatException e) {
            throw new BindingConfigParseException(errorMessage(bindingStringAddress));
        }
    }
    return new SappAddressOpenClosedStatus(pnmasId, addressType, address, subAddress, openValue);
}
Also used : SappAddressOpenClosedStatus(org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SappAddressType(org.openhab.binding.sapp.internal.model.SappAddressType)

Aggregations

SappAddressOpenClosedStatus (org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus)3 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)2 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)2 SappBindingConfigNumberItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem)2 SappBindingConfigRollershutterItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem)2 SappBindingConfigSwitchItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem)2 SappAddressDecimal (org.openhab.binding.sapp.internal.model.SappAddressDecimal)2 SappAddressDimmer (org.openhab.binding.sapp.internal.model.SappAddressDimmer)2 SappAddressOnOffStatus (org.openhab.binding.sapp.internal.model.SappAddressOnOffStatus)2 SappAddressRollershutterStatus (org.openhab.binding.sapp.internal.model.SappAddressRollershutterStatus)2 Item (org.openhab.core.items.Item)2 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 MathContext (java.math.MathContext)1 SappAddressType (org.openhab.binding.sapp.internal.model.SappAddressType)1 DecimalType (org.openhab.core.library.types.DecimalType)1