Search in sources :

Example 1 with SappBindingProvider

use of org.openhab.binding.sapp.SappBindingProvider in project openhab1-addons by openhab.

the class SappBinding method executeSappCommand.

/**
     * executes the real command on pnmas device
     */
private void executeSappCommand(String itemName, Command command) {
    SappBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    if (provider == null) {
        logger.error("cannot find a provider, skipping command");
    }
    try {
        Item item = itemRegistry.getItem(itemName);
        logger.debug("found item {}", item);
        if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
            SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
            logger.debug("found binding {}", sappBindingConfigSwitchItem);
            if (sappBindingConfigSwitchItem.isPollerSuspender()) {
                if (pollingEnabled) {
                    // turning off polling
                    pollingEnabled = false;
                    // force updates of polling switches because polling is
                    updatePollingSwitchesState(provider);
                // off
                } else {
                    // turning on polling
                    provider.getSappUpdatePendingRequests().replaceAllPendingUpdateRequests(ALL_UPDATE_REQUEST_KEY);
                    pollingEnabled = true;
                }
            } else {
                SappAddressOnOffControl controlAddress = sappBindingConfigSwitchItem.getControl();
                if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
                    logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigSwitchItem);
                    return;
                }
                try {
                    if (command instanceof OnOffType) {
                        switch(controlAddress.getAddressType()) {
                            case VIRTUAL:
                                {
                                    // mask bits on previous value
                                    int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
                                    int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), command.equals(OnOffType.ON) ? controlAddress.getOnValue() : controlAddress.getOffValue(), previousValue);
                                    // update pnmas
                                    SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
                                    SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                    sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
                                    break;
                                }
                            default:
                                logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
                                break;
                        }
                    } else {
                        logger.error("command {} not applicable", command.getClass().getSimpleName());
                    }
                } catch (SappException e) {
                    logger.error("could not run sappcommand", e);
                }
            }
        } else if (item instanceof NumberItem) {
            SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
            logger.debug("found binding {}", sappBindingConfigNumberItem);
            SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
            if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
                logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigNumberItem);
                return;
            }
            try {
                if (command instanceof DecimalType) {
                    switch(address.getAddressType()) {
                        case VIRTUAL:
                            {
                                // mask bits on previous value
                                int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
                                int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((DecimalType) command).toBigDecimal()), previousValue);
                                // update pnmas
                                SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
                                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
                                break;
                            }
                        default:
                            logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
                            break;
                    }
                } else {
                    logger.error("command {} not applicable", command.getClass().getSimpleName());
                }
            } catch (SappException e) {
                logger.error("could not run sappcommand", e);
            }
        } else if (item instanceof RollershutterItem) {
            SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
            logger.debug("found binding {}", sappBindingConfigRollershutterItem);
            SappAddressRollershutterControl controlAddress = null;
            if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.UP) {
                controlAddress = sappBindingConfigRollershutterItem.getUpControl();
            } else if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.DOWN) {
                controlAddress = sappBindingConfigRollershutterItem.getDownControl();
            } else if (command instanceof StopMoveType && ((StopMoveType) command) == StopMoveType.STOP) {
                controlAddress = sappBindingConfigRollershutterItem.getStopControl();
            }
            if (controlAddress != null) {
                if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
                    logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigRollershutterItem);
                    return;
                }
                try {
                    switch(controlAddress.getAddressType()) {
                        case VIRTUAL:
                            {
                                // mask bits on previous value
                                int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
                                int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), controlAddress.getActivateValue(), previousValue);
                                // update pnmas
                                SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
                                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
                                break;
                            }
                        default:
                            logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
                            break;
                    }
                } catch (SappException e) {
                    logger.error("could not run sappcommand", e);
                }
            } else {
                logger.error("command {} not applicable", command.getClass().getSimpleName());
            }
        } else if (item instanceof DimmerItem) {
            SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
            logger.debug("found binding {}", sappBindingConfigDimmerItem);
            SappAddressDimmer address = sappBindingConfigDimmerItem.getStatus();
            if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
                logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigDimmerItem);
                return;
            }
            try {
                if (command instanceof OnOffType) {
                    switch(address.getAddressType()) {
                        case VIRTUAL:
                            {
                                // mask bits on previous value
                                int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
                                int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((OnOffType) command) == OnOffType.ON ? address.getOriginalMaxScale() : address.getOriginalMinScale(), previousValue);
                                // update pnmas
                                SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
                                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
                                break;
                            }
                        default:
                            logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
                            break;
                    }
                } else if (command instanceof IncreaseDecreaseType) {
                    switch(address.getAddressType()) {
                        case VIRTUAL:
                            {
                                // mask bits on previous value
                                int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
                                int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE ? Math.min(previousValue + address.getIncrement(), address.getOriginalMaxScale()) : Math.max(previousValue - address.getIncrement(), address.getOriginalMinScale()), previousValue);
                                // update pnmas
                                SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
                                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
                                break;
                            }
                        default:
                            logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
                            break;
                    }
                } else if (command instanceof PercentType) {
                    switch(address.getAddressType()) {
                        case VIRTUAL:
                            {
                                // mask bits on previous value
                                int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
                                int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((PercentType) command).toBigDecimal()), previousValue);
                                // update pnmas
                                SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
                                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                                sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
                                break;
                            }
                        default:
                            logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
                            break;
                    }
                } else {
                    logger.error("command {} not applicable", command.getClass().getSimpleName());
                }
            } catch (SappException e) {
                logger.error("could not run sappcommand", e);
            }
        } else {
            logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
        }
    } catch (ItemNotFoundException e) {
        logger.error("Item {} not found", itemName);
    }
}
Also used : SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingProvider(org.openhab.binding.sapp.SappBindingProvider) StopMoveType(org.openhab.core.library.types.StopMoveType) 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) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) 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) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappPnmas(org.openhab.binding.sapp.internal.model.SappPnmas) SappAddressRollershutterControl(org.openhab.binding.sapp.internal.model.SappAddressRollershutterControl) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) SappCentralExecuter(org.openhab.binding.sapp.internal.executer.SappCentralExecuter) SappAddressOnOffControl(org.openhab.binding.sapp.internal.model.SappAddressOnOffControl) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) SappAddressDimmer(org.openhab.binding.sapp.internal.model.SappAddressDimmer) SappAddressDecimal(org.openhab.binding.sapp.internal.model.SappAddressDecimal) OnOffType(org.openhab.core.library.types.OnOffType) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) SappException(com.github.paolodenti.jsapp.core.command.base.SappException) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 2 with SappBindingProvider

use of org.openhab.binding.sapp.SappBindingProvider in project openhab1-addons by openhab.

the class SappBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    if (isProperlyConfigured()) {
        // wait until provider is properly configured and all items are loaded
        SappBindingProvider provider = getFirstSappBindingProvider();
        if (provider != null) {
            if (provider.getSappUpdatePendingRequests().areUpdatePendingRequestsPresent()) {
                // reinit
                // items
                Set<String> toBeProcessed = provider.getSappUpdatePendingRequests().getAndClearPendingUpdateRequests();
                initializeAllItemsInProvider(provider, toBeProcessed);
            } else {
                if (!pollingEnabled) {
                    return;
                }
                SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
                for (String pnmasId : provider.getPnmasMap().keySet()) {
                    // each pnmas
                    SappPnmas pnmas = provider.getPnmasMap().get(pnmasId);
                    try {
                        PollingResult pollingResult = sappCentralExecuter.executePollingSappCommands(pnmas.getIp(), pnmas.getPort());
                        if (pollingResult.changedOutputs.size() != 0) {
                            for (Byte outputAddress : pollingResult.changedOutputs.keySet()) {
                                logger.debug("Output variation {} received, new value is {}", SappUtils.byteToUnsigned(outputAddress), pollingResult.changedOutputs.get(outputAddress));
                                provider.setOutputCachedValue(SappUtils.byteToUnsigned(outputAddress), pollingResult.changedOutputs.get(outputAddress).intValue());
                                updateState(pnmasId, SappAddressType.OUTPUT, SappUtils.byteToUnsigned(outputAddress), pollingResult.changedOutputs.get(outputAddress).intValue(), provider);
                            }
                        }
                        if (pollingResult.changedInputs.size() != 0) {
                            for (Byte inputAddress : pollingResult.changedInputs.keySet()) {
                                logger.debug("Input variation {} received, new value is {}", SappUtils.byteToUnsigned(inputAddress), pollingResult.changedInputs.get(inputAddress));
                                provider.setInputCachedValue(SappUtils.byteToUnsigned(inputAddress), pollingResult.changedInputs.get(inputAddress).intValue());
                                updateState(pnmasId, SappAddressType.INPUT, SappUtils.byteToUnsigned(inputAddress), pollingResult.changedInputs.get(inputAddress).intValue(), provider);
                            }
                        }
                        if (pollingResult.changedVirtuals.size() != 0) {
                            for (Integer virtualAddress : pollingResult.changedVirtuals.keySet()) {
                                logger.debug("Virtual variation {} received, new value is {}", virtualAddress, pollingResult.changedVirtuals.get(virtualAddress));
                                provider.setVirtualCachedValue(virtualAddress, pollingResult.changedVirtuals.get(virtualAddress).intValue());
                                updateState(pnmasId, SappAddressType.VIRTUAL, virtualAddress, pollingResult.changedVirtuals.get(virtualAddress).intValue(), provider);
                            }
                        }
                    } catch (SappException e) {
                        logger.error("polling failed on pnmas {}", pnmas);
                    }
                }
            }
        }
    }
}
Also used : SappPnmas(org.openhab.binding.sapp.internal.model.SappPnmas) PollingResult(org.openhab.binding.sapp.internal.executer.SappCentralExecuter.PollingResult) SappBindingProvider(org.openhab.binding.sapp.SappBindingProvider) SappException(com.github.paolodenti.jsapp.core.command.base.SappException) SappCentralExecuter(org.openhab.binding.sapp.internal.executer.SappCentralExecuter)

Example 3 with SappBindingProvider

use of org.openhab.binding.sapp.SappBindingProvider in project openhab1-addons by openhab.

the class SappBinding method allItemsChanged.

@Override
public void allItemsChanged(Collection<String> oldItemNames) {
    SappBindingProvider provider = getFirstSappBindingProvider();
    provider.getSappUpdatePendingRequests().replaceAllPendingUpdateRequests(ALL_UPDATE_REQUEST_KEY);
}
Also used : SappBindingProvider(org.openhab.binding.sapp.SappBindingProvider)

Example 4 with SappBindingProvider

use of org.openhab.binding.sapp.SappBindingProvider in project openhab1-addons by openhab.

the class SappBinding method activate.

/**
     * Called by the SCR to activate the component with its configuration read from CAS
     *
     * @param bundleContext
     *            BundleContext of the Bundle that defines this component
     * @param configuration
     *            Configuration properties for this component obtained from the ConfigAdmin service
     */
public void activate(final BundleContext bundleContext, final Map<String, Object> configuration) {
    logger.debug("sapp activate called");
    String refreshIntervalString = (String) configuration.get(CONFIG_KEY_REFRESH);
    if (StringUtils.isNotBlank(refreshIntervalString)) {
        refreshInterval = Long.parseLong(refreshIntervalString);
        logger.debug("set refresh interval: {}", refreshInterval);
    }
    SappBindingProvider provider = getFirstSappBindingProvider();
    if (provider != null) {
        String pnmasEnabled = (String) configuration.get(CONFIG_KEY_PNMAS_ENABLED);
        if (pnmasEnabled != null) {
            String[] pnmasIds = pnmasEnabled.split(",");
            for (String pnmasId : pnmasIds) {
                logger.debug("loading info for pnmas {}", pnmasId);
                String ip = (String) configuration.get(String.format(CONFIG_KEY_PNMAS_ID, pnmasId));
                if (ip == null) {
                    logger.warn("ip not found for pnmas {}", pnmasId);
                    continue;
                }
                int port;
                String portString = (String) configuration.get(String.format(CONFIG_KEY_PNMAS_PORT, pnmasId));
                if (portString == null) {
                    logger.warn("port not found for pnmas {}", pnmasId);
                    continue;
                } else {
                    try {
                        port = Integer.parseInt(portString);
                    } catch (NumberFormatException e) {
                        logger.warn("bad port number for pnmas {}", pnmasId);
                        continue;
                    }
                }
                if (provider.getPnmasMap().containsKey(pnmasId)) {
                    logger.warn("pnmas {} duplicated, skipping", pnmasId);
                    continue;
                }
                provider.getPnmasMap().put(pnmasId, new SappPnmas(ip, port));
            }
            for (String pnmasKey : provider.getPnmasMap().keySet()) {
                logger.debug("pnmas {} : {}:", pnmasKey, provider.getPnmasMap().get(pnmasKey));
            }
        }
    }
    provider.getSappUpdatePendingRequests().replaceAllPendingUpdateRequests(ALL_UPDATE_REQUEST_KEY);
    setProperlyConfigured(true);
}
Also used : SappPnmas(org.openhab.binding.sapp.internal.model.SappPnmas) SappBindingProvider(org.openhab.binding.sapp.SappBindingProvider)

Aggregations

SappBindingProvider (org.openhab.binding.sapp.SappBindingProvider)4 SappPnmas (org.openhab.binding.sapp.internal.model.SappPnmas)3 SappException (com.github.paolodenti.jsapp.core.command.base.SappException)2 SappCentralExecuter (org.openhab.binding.sapp.internal.executer.SappCentralExecuter)2 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)1 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)1 SappBindingConfigNumberItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem)1 SappBindingConfigRollershutterItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem)1 SappBindingConfigSwitchItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem)1 PollingResult (org.openhab.binding.sapp.internal.executer.SappCentralExecuter.PollingResult)1 SappAddressDecimal (org.openhab.binding.sapp.internal.model.SappAddressDecimal)1 SappAddressDimmer (org.openhab.binding.sapp.internal.model.SappAddressDimmer)1 SappAddressOnOffControl (org.openhab.binding.sapp.internal.model.SappAddressOnOffControl)1 SappAddressRollershutterControl (org.openhab.binding.sapp.internal.model.SappAddressRollershutterControl)1 Item (org.openhab.core.items.Item)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 ContactItem (org.openhab.core.library.items.ContactItem)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 RollershutterItem (org.openhab.core.library.items.RollershutterItem)1