Search in sources :

Example 1 with Iec6205621MeterBindingProvider

use of org.openhab.binding.iec6205621meter.Iec6205621MeterBindingProvider in project openhab1-addons by openhab.

the class Iec6205621MeterBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    // the frequently executed code (polling) goes here ...
    Map<String, Map<String, DataSet>> cache = new HashMap<String, Map<String, DataSet>>();
    for (Iec6205621MeterBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            for (Entry<String, Meter> entry : meterDeviceConfigurtions.entrySet()) {
                Meter reader = entry.getValue();
                String meterName = provider.getMeterName(itemName);
                if (meterName != null && meterName.equals(entry.getKey())) {
                    Map<String, DataSet> dataSets;
                    if ((dataSets = cache.get(meterName)) == null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Read meter: {}; {}", meterName, reader.getConfig().getSerialPort());
                        }
                        dataSets = reader.read();
                        cache.put(meterName, dataSets);
                    }
                    String obis = provider.getObis(itemName);
                    if (obis != null && dataSets.containsKey(obis)) {
                        DataSet dataSet = dataSets.get(obis);
                        if (logger.isDebugEnabled()) {
                            logger.debug("Updating item {} with OBIS code {} and value {}", itemName, obis, dataSet.getValue());
                        }
                        Class<? extends Item> itemType = provider.getItemType(itemName);
                        if (itemType.isAssignableFrom(NumberItem.class)) {
                            eventPublisher.postUpdate(itemName, new DecimalType(dataSet.getValue()));
                        }
                        if (itemType.isAssignableFrom(StringItem.class)) {
                            String value = dataSet.getValue();
                            eventPublisher.postUpdate(itemName, new StringType(value));
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) DataSet(org.openmuc.j62056.DataSet) StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) HashMap(java.util.HashMap) Map(java.util.Map) Iec6205621MeterBindingProvider(org.openhab.binding.iec6205621meter.Iec6205621MeterBindingProvider)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Iec6205621MeterBindingProvider (org.openhab.binding.iec6205621meter.Iec6205621MeterBindingProvider)1 DecimalType (org.openhab.core.library.types.DecimalType)1 StringType (org.openhab.core.library.types.StringType)1 DataSet (org.openmuc.j62056.DataSet)1