Search in sources :

Example 31 with OnOffType

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

the class PowerMaxBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    // the code being executed when a command was sent on the openHAB
    // event bus goes here. This method is only called if one of the
    // BindingProviders provide a binding for the given 'itemName'.
    logger.debug("internalReceiveCommand({},{}) is called", itemName, command);
    if (!isProperlyConfigured() || !connected) {
        logger.warn("PowerMax alarm binding is not properly configured. Command is ignored.");
        return;
    }
    String armMode = null;
    String actionPGMX10 = null;
    Byte devicePGMX10 = null;
    Boolean bypass = null;
    byte zoneNr = 0;
    String commandStr = null;
    PowerMaxPanelSettings settings = PowerMaxPanelSettings.getThePanelSettings();
    PowerMaxCommDriver comm = PowerMaxCommDriver.getTheCommDriver();
    for (PowerMaxBindingProvider provider : providers) {
        PowerMaxBindingConfig config = provider.getConfig(itemName);
        if (config == null) {
            continue;
        }
        switch(config.getSelectorType()) {
            case PARTITION_ARM_MODE:
                if (command instanceof StringType) {
                    armMode = command.toString();
                }
                break;
            case PARTITION_ARMED:
                if (command instanceof OnOffType) {
                    armMode = command.equals(OnOffType.ON) ? "Armed" : "Disarmed";
                }
                break;
            case PGM_STATUS:
                if (command instanceof OnOffType) {
                    actionPGMX10 = command.toString();
                }
                break;
            case X10_STATUS:
                if (command instanceof OnOffType || command instanceof StringType) {
                    actionPGMX10 = command.toString();
                }
                try {
                    devicePGMX10 = Byte.parseByte(config.getSelectorParam());
                } catch (NumberFormatException e) {
                    logger.warn("PowerMax alarm binding: invalid X10 device id: {}", config.getSelectorParam());
                    actionPGMX10 = null;
                }
                break;
            case ZONE_BYPASSED:
                if (command instanceof OnOffType) {
                    bypass = command.equals(OnOffType.ON) ? Boolean.TRUE : Boolean.FALSE;
                }
                try {
                    zoneNr = Byte.parseByte(config.getSelectorParam());
                } catch (NumberFormatException e) {
                    logger.warn("PowerMax alarm binding: invalid zone number: {}", config.getSelectorParam());
                    bypass = null;
                }
                break;
            case COMMAND:
                if (command instanceof StringType) {
                    commandStr = command.toString();
                    eventPublisher.postUpdate(itemName, new StringType(commandStr));
                }
                break;
            default:
                break;
        }
        if (armMode != null) {
            HashMap<String, Boolean> allowedModes = new HashMap<String, Boolean>();
            allowedModes.put("Disarmed", allowDisarming);
            allowedModes.put("Stay", allowArming);
            allowedModes.put("Armed", allowArming);
            allowedModes.put("StayInstant", allowArming);
            allowedModes.put("ArmedInstant", allowArming);
            allowedModes.put("Night", allowArming);
            allowedModes.put("NightInstant", allowArming);
            Boolean allowed = allowedModes.get(armMode);
            if ((allowed == null) || !allowed) {
                logger.warn("PowerMax alarm binding: rejected command {}", armMode);
            } else {
                comm.requestArmMode(armMode, currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
            }
            break;
        } else if (actionPGMX10 != null) {
            comm.sendPGMX10(actionPGMX10, devicePGMX10);
            break;
        } else if (bypass != null) {
            if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
                logger.warn("PowerMax alarm binding: Bypass option only supported in Powerlink mode");
            } else if (!PowerMaxPanelSettings.getThePanelSettings().isBypassEnabled()) {
                logger.warn("PowerMax alarm binding: Bypass option not enabled in panel settings");
            } else {
                comm.sendZoneBypass(bypass.booleanValue(), zoneNr, PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode());
            }
            break;
        } else if (commandStr != null) {
            if (commandStr.equalsIgnoreCase("get_event_log")) {
                comm.requestEventLog(currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
            } else if (commandStr.equalsIgnoreCase("download_setup")) {
                if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
                    logger.warn("PowerMax alarm binding: download setup only supported in Powerlink mode");
                } else {
                    comm.startDownload();
                    if (currentState.getLastKeepAlive() != null) {
                        currentState.setLastKeepAlive(System.currentTimeMillis());
                    }
                }
            } else if (commandStr.equalsIgnoreCase("log_setup")) {
                settings.log();
            } else if (commandStr.equalsIgnoreCase("help_items")) {
                settings.helpItems();
            } else {
                logger.warn("PowerMax alarm binding: rejected command {}", commandStr);
            }
            break;
        }
    }
}
Also used : PowerMaxBindingConfig(org.openhab.binding.powermax.PowerMaxBindingConfig) StringType(org.openhab.core.library.types.StringType) HashMap(java.util.HashMap) PowerMaxCommDriver(org.openhab.binding.powermax.internal.message.PowerMaxCommDriver) OnOffType(org.openhab.core.library.types.OnOffType) PowerMaxPanelSettings(org.openhab.binding.powermax.internal.state.PowerMaxPanelSettings) PowerMaxBindingProvider(org.openhab.binding.powermax.PowerMaxBindingProvider)

Example 32 with OnOffType

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

the class RFXComLighting3Message method convertFromState.

@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
    this.subType = ((SubType) subType);
    seqNbr = seqNumber;
    switch(valueSelector) {
        case COMMAND:
            if (type instanceof OnOffType) {
                command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
                dimmingLevel = 0;
            } else if (type instanceof DecimalType) {
                command = Commands.fromByte(((DecimalType) type).intValue());
                dimmingLevel = 0;
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;
        case DIMMING_LEVEL:
            if (type instanceof OnOffType) {
                command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
                dimmingLevel = 0;
            } else if (type instanceof PercentType) {
                command = Commands.DIM;
                dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
                if (dimmingLevel == 0) {
                    command = Commands.OFF;
                }
            } else if (type instanceof IncreaseDecreaseType) {
                command = Commands.DIM;
                // Evert: I do not know how to get previous object state...
                dimmingLevel = 5;
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;
        default:
            throw new RFXComException("Can't convert " + type + " to " + valueSelector);
    }
}
Also used : RFXComException(org.openhab.binding.rfxcom.internal.RFXComException) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) PercentType(org.openhab.core.library.types.PercentType)

Example 33 with OnOffType

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

the class RFXComThermostat2Message method convertFromState.

@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
    this.subType = ((SubType) subType);
    seqNbr = seqNumber;
    String[] ids = id.split("\\.");
    unitcode = Byte.parseByte(ids[0]);
    switch(valueSelector) {
        case COMMAND:
            if (type instanceof OnOffType) {
                command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
            } else if (type instanceof OpenClosedType) {
                command = (type == OpenClosedType.CLOSED ? Commands.ON : Commands.OFF);
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;
        default:
            throw new RFXComException("Can't convert " + type + " to " + valueSelector);
    }
}
Also used : RFXComException(org.openhab.binding.rfxcom.internal.RFXComException) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType)

Example 34 with OnOffType

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

the class RFXComThermostat3Message method convertFromState.

@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
    this.subType = ((SubType) subType);
    seqNbr = seqNumber;
    String[] ids = id.split("\\.");
    unitcode = Byte.parseByte(ids[0]);
    switch(valueSelector) {
        case COMMAND:
            if (type instanceof OnOffType) {
                command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
            } else if (type instanceof OpenClosedType) {
                command = (type == OpenClosedType.CLOSED ? Commands.ON : Commands.OFF);
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;
        default:
            throw new RFXComException("Can't convert " + type + " to " + valueSelector);
    }
}
Also used : RFXComException(org.openhab.binding.rfxcom.internal.RFXComException) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType)

Example 35 with OnOffType

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

the class IhcDataConverter method convertCommandToResourceValue.

/**
     * Convert openHAB data type to IHC data type.
     *
     * @param type
     *            openHAB data type
     * @param value
     *
     * @param enumValues
     *
     * @return IHC data type
     */
public static WSResourceValue convertCommandToResourceValue(Type type, WSResourceValue value, ArrayList<IhcEnumValue> enumValues) {
    if (type instanceof DecimalType) {
        if (value instanceof WSFloatingPointValue) {
            double newVal = ((DecimalType) type).doubleValue();
            double max = ((WSFloatingPointValue) value).getMaximumValue();
            double min = ((WSFloatingPointValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSFloatingPointValue) value).setFloatingPointValue(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(((DecimalType) type).intValue() > 0 ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = ((DecimalType) type).intValue();
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else if (value instanceof WSTimerValue) {
            ((WSTimerValue) value).setMilliseconds(((DecimalType) type).longValue());
        } else if (value instanceof WSWeekdayValue) {
            ((WSWeekdayValue) value).setWeekdayNumber(((DecimalType) type).intValue());
        } else {
            throw new NumberFormatException("Can't convert DecimalType to " + value.getClass());
        }
    } else if (type instanceof OnOffType) {
        if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(type == OnOffType.ON ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = type == OnOffType.ON ? 100 : 0;
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert OnOffType to " + value.getClass());
        }
    } else if (type instanceof OpenClosedType) {
        ((WSBooleanValue) value).setValue(type == OpenClosedType.OPEN ? true : false);
    } else if (type instanceof DateTimeType) {
        if (value instanceof WSDateValue) {
            Calendar c = ((DateTimeType) type).getCalendar();
            short year = (short) c.get(Calendar.YEAR);
            byte month = (byte) (c.get(Calendar.MONTH) + 1);
            byte day = (byte) c.get(Calendar.DAY_OF_MONTH);
            ((WSDateValue) value).setYear(year);
            ((WSDateValue) value).setMonth(month);
            ((WSDateValue) value).setDay(day);
        } else if (value instanceof WSTimeValue) {
            Calendar c = ((DateTimeType) type).getCalendar();
            int hours = c.get(Calendar.HOUR_OF_DAY);
            int minutes = c.get(Calendar.MINUTE);
            int seconds = c.get(Calendar.SECOND);
            ((WSTimeValue) value).setHours(hours);
            ((WSTimeValue) value).setMinutes(minutes);
            ((WSTimeValue) value).setSeconds(seconds);
        } else {
            throw new NumberFormatException("Can't convert DateTimeItem to " + value.getClass());
        }
    } else if (type instanceof StringType) {
        if (value instanceof WSEnumValue) {
            boolean found = false;
            for (IhcEnumValue item : enumValues) {
                if (item.name.equals(type.toString())) {
                    ((WSEnumValue) value).setEnumValueID(item.id);
                    ((WSEnumValue) value).setEnumName(type.toString());
                    found = true;
                    break;
                }
            }
            if (found == false) {
                throw new NumberFormatException("Can't find enum value for string " + type.toString());
            }
        } else {
            throw new NumberFormatException("Can't convert StringType to " + value.getClass());
        }
    } else if (type instanceof PercentType) {
        if (value instanceof WSIntegerValue) {
            int newVal = ((DecimalType) type).intValue();
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert PercentType to " + value.getClass());
        }
    } else if (type instanceof UpDownType) {
        if (value instanceof WSBooleanValue) {
            ((WSBooleanValue) value).setValue(type == UpDownType.DOWN ? true : false);
        } else if (value instanceof WSIntegerValue) {
            int newVal = type == UpDownType.DOWN ? 100 : 0;
            int max = ((WSIntegerValue) value).getMaximumValue();
            int min = ((WSIntegerValue) value).getMinimumValue();
            if (newVal >= min && newVal <= max) {
                ((WSIntegerValue) value).setInteger(newVal);
            } else {
                throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
            }
        } else {
            throw new NumberFormatException("Can't convert UpDownType to " + value.getClass());
        }
    } else {
        throw new NumberFormatException("Can't convert " + type.getClass().toString());
    }
    return value;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) DateTimeType(org.openhab.core.library.types.DateTimeType) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) WSDateValue(org.openhab.binding.ihc.ws.datatypes.WSDateValue) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Aggregations

OnOffType (org.openhab.core.library.types.OnOffType)50 DecimalType (org.openhab.core.library.types.DecimalType)22 PercentType (org.openhab.core.library.types.PercentType)15 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)12 OpenClosedType (org.openhab.core.library.types.OpenClosedType)11 StringType (org.openhab.core.library.types.StringType)11 UpDownType (org.openhab.core.library.types.UpDownType)9 IOException (java.io.IOException)8 State (org.openhab.core.types.State)7 StopMoveType (org.openhab.core.library.types.StopMoveType)6 DateTimeType (org.openhab.core.library.types.DateTimeType)5 HSBType (org.openhab.core.library.types.HSBType)5 UnknownHostException (java.net.UnknownHostException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 RFXComException (org.openhab.binding.rfxcom.internal.RFXComException)3 Item (org.openhab.core.items.Item)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 Color (java.awt.Color)2 DatagramPacket (java.net.DatagramPacket)2 SocketTimeoutException (java.net.SocketTimeoutException)2