Search in sources :

Example 1 with ContactItem

use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.

the class SappGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    logger.debug("processing binding configuration for context {}", context);
    super.processBindingConfiguration(context, item, bindingConfig);
    if (bindingConfig != null) {
        if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
            SappBindingConfigSwitchItem sappBindingConfigSwitchItem = new SappBindingConfigSwitchItem(item, bindingConfig);
            addBindingConfig(item, sappBindingConfigSwitchItem);
        } else if (item instanceof ContactItem) {
            SappBindingConfigContactItem sappBindingConfigContactItem = new SappBindingConfigContactItem(item, bindingConfig);
            addBindingConfig(item, sappBindingConfigContactItem);
        } else if (item instanceof NumberItem) {
            SappBindingConfigNumberItem sappBindingConfigNumberItem = new SappBindingConfigNumberItem(item, bindingConfig);
            addBindingConfig(item, sappBindingConfigNumberItem);
        } else if (item instanceof RollershutterItem) {
            SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = new SappBindingConfigRollershutterItem(item, bindingConfig);
            addBindingConfig(item, sappBindingConfigRollershutterItem);
        } else if (item instanceof DimmerItem) {
            SappBindingConfigDimmerItem sappBindingConfigDimmerItem = new SappBindingConfigDimmerItem(item, bindingConfig);
            addBindingConfig(item, sappBindingConfigDimmerItem);
        } else {
            throw new BindingConfigParseException("item '" + item.getName() + "' is of type '" + item.getClass().getSimpleName() + " - not yet implemented, please check your *.items configuration");
        }
    } else {
        logger.warn("bindingConfig is NULL (item={}) -> processing bindingConfig aborted!", item);
    }
}
Also used : SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem)

Example 2 with ContactItem

use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.

the class JpaHistoricItem method fromPersistedItem.

/**
     * Converts the string value of the persisted item to the state of a HistoricItem.
     * 
     * @param pItem the persisted JpaPersistentItem
     * @param item the source reference Item
     * @return historic item
     */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof CallItem) {
        state = new CallType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 3 with ContactItem

use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Example 4 with ContactItem

use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.

the class ContactItemIntegrationTest method storeData.

@BeforeClass
public static void storeData() throws InterruptedException {
    ContactItem item = (ContactItem) items.get(name);
    item.setState(state1);
    beforeStore = new Date();
    Thread.sleep(10);
    service.store(item);
    afterStore1 = new Date();
    Thread.sleep(10);
    item.setState(state2);
    service.store(item);
    Thread.sleep(10);
    afterStore2 = new Date();
    logger.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore), AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
}
Also used : ContactItem(org.openhab.core.library.items.ContactItem) Date(java.util.Date) BeforeClass(org.junit.BeforeClass)

Example 5 with ContactItem

use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.

the class PLCLogoBinding method execute.

@Override
protected void execute() {
    if (!bindingsExist()) {
        logger.debug("There is no existing plclogo binding configuration => refresh cycle aborted!");
        return;
    }
    Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
    while (entries.hasNext()) {
        Entry<String, PLCLogoConfig> thisEntry = entries.next();
        String controller = thisEntry.getKey();
        PLCLogoConfig logoConfig = thisEntry.getValue();
        S7Client LogoS7Client = logoConfig.getS7Client();
        if (LogoS7Client == null) {
            logger.debug("No S7client for {} found", controller);
        } else {
            lock.lock();
            int result = ReadLogoDBArea(LogoS7Client, logoConfig.getMemorySize());
            lock.unlock();
            if (result != 0) {
                logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
                ReconnectLogo(LogoS7Client);
                return;
            }
        // Now have the LOGO! memory (note: not suitable for S7) - more efficient than multiple reads (test
        // shows <14mS to read all)
        // iterate through bindings to see what has changed - this approach assumes a small number (< 100)of
        // bindings
        // otherwise might see what has changed in memory and map to binding
        }
        for (PLCLogoBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
                if (config.getController().equals(controller)) {
                    // it is for our currently selected controller
                    PLCLogoMemoryConfig rd = config.getRD();
                    int address = -1;
                    try {
                        address = rd.getAddress(logoConfig.getModel());
                    } catch (BindingConfigParseException exception) {
                        logger.error("Invalid address for block {} on {}", rd.getBlockName(), controller);
                        continue;
                    }
                    int currentValue;
                    if (rd.isDigital()) {
                        int bit = -1;
                        try {
                            bit = rd.getBit(logoConfig.getModel());
                        } catch (BindingConfigParseException exception) {
                            logger.error("Invalid bit for block {} on {}", rd.getBlockName(), controller);
                            continue;
                        }
                        currentValue = S7.GetBitAt(data, address, bit) ? 1 : 0;
                    } else {
                        /*
                             * After the data transfer from a LOGO! Base Module to LOGO!Soft Comfort,
                             * you can view only analog values within the range of -32768 to 32767 on LOGO!Soft Comfort.
                             * If an analog value exceeds the value range,
                             * then only the nearest upper limit (32767) or lower limit (-32768) can be displayed.
                             */
                        currentValue = S7.GetShortAt(data, address);
                    }
                    if (config.isSet()) {
                        if (currentValue == config.getLastValue()) {
                            continue;
                        }
                        int delta = Math.abs(config.getLastValue() - currentValue);
                        if (!rd.isDigital() && (delta < config.getAnalogDelta())) {
                            continue;
                        }
                    }
                    boolean isValid = false;
                    Item item = provider.getItem(itemName);
                    switch(rd.getKind()) {
                        case I:
                        case NI:
                            {
                                isValid = item instanceof ContactItem;
                                break;
                            }
                        case Q:
                        case NQ:
                            {
                                isValid = item instanceof SwitchItem;
                                break;
                            }
                        case M:
                        case VB:
                        case VW:
                            {
                                isValid = item instanceof ContactItem || item instanceof SwitchItem;
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                    if (item instanceof NumberItem || isValid) {
                        eventPublisher.postUpdate(itemName, createState(item, currentValue));
                        config.setLastValue(currentValue);
                    } else {
                        String block = rd.getBlockName();
                        logger.warn("Block {} is incompatible with item {} on {}", block, item.getName(), controller);
                    }
                }
            }
        }
    }
}
Also used : ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) Entry(java.util.Map.Entry) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) PLCLogoBindingConfig(org.openhab.binding.plclogo.PLCLogoBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) S7Client(Moka7.S7Client)

Aggregations

ContactItem (org.openhab.core.library.items.ContactItem)19 NumberItem (org.openhab.core.library.items.NumberItem)15 SwitchItem (org.openhab.core.library.items.SwitchItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)11 DecimalType (org.openhab.core.library.types.DecimalType)11 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 StringType (org.openhab.core.library.types.StringType)9 Item (org.openhab.core.items.Item)8 Calendar (java.util.Calendar)7 ColorItem (org.openhab.core.library.items.ColorItem)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 DateTimeType (org.openhab.core.library.types.DateTimeType)7 PercentType (org.openhab.core.library.types.PercentType)7 State (org.openhab.core.types.State)7 StringItem (org.openhab.core.library.items.StringItem)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5 BigDecimal (java.math.BigDecimal)3 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)3 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)3