Search in sources :

Example 1 with UrtsiBindingProvider

use of org.openhab.binding.urtsi.UrtsiBindingProvider in project openhab1-addons by openhab.

the class UrtsiBinding method sendToUrtsi.

/**
     * The method determines the appropriate
     * {@link org.openhab.core.binding.BindingProvider} and uses it to get the
     * corresponding URTSI device and channel. Bases on the given type a command
     * is send to the device.
     *
     * @param itemName
     *            name of the item
     * @param type
     *            Type of the command or status update
     * @return Returns true, if the command has been executed successfully.
     *         Returns false otherwise.
     * @throws BindingConfigParseException
     */
private boolean sendToUrtsi(String itemName, Type type) {
    UrtsiBindingProvider provider = null;
    if (!providers.isEmpty()) {
        provider = providers.iterator().next();
    }
    if (provider == null) {
        logger.error("doesn't find matching binding provider [itemName={}, type={}]", itemName, type);
        return false;
    }
    String urtsiDeviceId = provider.getDeviceId(itemName);
    UrtsiDevice urtsiDevice = idToDeviceMap.get(urtsiDeviceId);
    if (urtsiDevice == null) {
        logger.error("No serial port has been configured for urtsi device id '{}'", urtsiDeviceId);
        return false;
    }
    int channel = provider.getChannel(itemName);
    int address = provider.getAddress(itemName);
    logger.debug("Send to URTSI for item: {}; Type: {}", itemName, type);
    String actionKey = null;
    if (type instanceof UpDownType) {
        switch((UpDownType) type) {
            case UP:
                actionKey = COMMAND_UP;
                break;
            case DOWN:
                actionKey = COMMAND_DOWN;
                break;
        }
    } else if (type instanceof StopMoveType) {
        switch((StopMoveType) type) {
            case STOP:
                actionKey = COMMAND_STOP;
                break;
            default:
                break;
        }
    }
    logger.debug("Action key: {}", actionKey);
    if (actionKey != null) {
        String channelString = String.format("%02d", channel);
        String addressString = String.format("%02d", address);
        String command = addressString + channelString + actionKey;
        boolean executedSuccessfully = urtsiDevice.writeString(command);
        if (!executedSuccessfully) {
            logger.warn("Command has not been processed [itemName={}, command={}]", itemName, command);
        }
        return executedSuccessfully;
    }
    return false;
}
Also used : UpDownType(org.openhab.core.library.types.UpDownType) StopMoveType(org.openhab.core.library.types.StopMoveType) UrtsiBindingProvider(org.openhab.binding.urtsi.UrtsiBindingProvider)

Aggregations

UrtsiBindingProvider (org.openhab.binding.urtsi.UrtsiBindingProvider)1 StopMoveType (org.openhab.core.library.types.StopMoveType)1 UpDownType (org.openhab.core.library.types.UpDownType)1