Search in sources :

Example 1 with DigitalSTROMBindingProvider

use of org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider in project openhab1-addons by openhab.

the class DigitalSTROMBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    if (!serverIsFound()) {
        login();
    } else {
        if (digitalSTROM.getTime(getSessionToken()) == -1) {
            logger.warn("test method failed ... new login now");
            login();
        }
    }
    for (DigitalSTROMBindingProvider provider : providers) {
        for (DigitalSTROMBindingConfig itemConf : provider.getAllCircuitConsumptionItems()) {
            String itemName = itemConf.itemName;
            int refreshInterval = itemConf.timeinterval;
            Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
            if (lastUpdateTimeStamp == null) {
                lastUpdateTimeStamp = 0L;
            }
            long age = System.currentTimeMillis() - lastUpdateTimeStamp;
            boolean needsUpdate = age >= refreshInterval;
            if (needsUpdate) {
                logger.debug("item '{}' is about to be refreshed now", itemName);
                int consumptionValue = -1;
                if (itemConf.consumption == null || itemConf.consumption.equals(ConsumptionConfig.OUTPUT_CURRENT)) {
                    itemConf.consumption = ConsumptionConfig.ACTIVE_POWER;
                }
                switch(itemConf.consumption) {
                    case ACTIVE_POWER:
                        List<CachedMeteringValue> consumptionList = digitalSTROM.getLatest(getSessionToken(), MeteringTypeEnum.consumption, ".meters(" + itemConf.dsmid.getValue().toLowerCase() + ")", null);
                        if (consumptionList != null) {
                            consumptionValue = 0;
                            for (CachedMeteringValue value : consumptionList) {
                                consumptionValue += value.getValue();
                            }
                        }
                        break;
                    case ELECTRIC_METER:
                        List<CachedMeteringValue> energyList = digitalSTROM.getLatest(getSessionToken(), MeteringTypeEnum.energy, ".meters(" + itemConf.dsmid.getValue().toLowerCase() + ")", MeteringUnitsEnum.Wh);
                        if (energyList != null) {
                            consumptionValue = 0;
                            for (CachedMeteringValue value : energyList) {
                                consumptionValue += value.getValue();
                            }
                        }
                        break;
                    default:
                        break;
                }
                org.openhab.core.types.State state = UnDefType.NULL;
                if (consumptionValue != -1) {
                    state = new DecimalType(consumptionValue);
                }
                if (state != null) {
                    eventPublisher.postUpdate(itemName, state);
                }
                lastUpdateMap.put(itemName, System.currentTimeMillis());
            }
        }
        for (DigitalSTROMBindingConfig itemConf : provider.getAllDeviceConsumptionItems()) {
            String itemName = itemConf.itemName;
            int refreshInterval = itemConf.timeinterval;
            Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
            if (lastUpdateTimeStamp == null) {
                lastUpdateTimeStamp = 0L;
            }
            long age = System.currentTimeMillis() - lastUpdateTimeStamp;
            boolean needsUpdate = age >= refreshInterval;
            if (needsUpdate) {
                logger.debug("item '{}' is about to be refreshed now", itemName);
                Device device = getDsidToDeviceMap().get(itemConf.dsid.getValue());
                if (device != null) {
                    if (itemConf.sensor == null) {
                        SensorIndexEnum sensorIndex = null;
                        try {
                            sensorIndex = SensorIndexEnum.valueOf(itemConf.consumption.name());
                        } catch (Exception e) {
                            sensorIndex = SensorIndexEnum.ACTIVE_POWER;
                        }
                        addLowPriorityJob(new DeviceConsumptionSensorJob(device, sensorIndex));
                        lastUpdateMap.put(itemName, System.currentTimeMillis());
                    } else {
                        SensorIndexEnum sensorIndex = null;
                        try {
                            sensorIndex = SensorIndexEnum.valueOf(itemConf.sensor.name());
                        } catch (Exception e) {
                        }
                        addHighPriorityJob(new DeviceSensorValueJob(device, sensorIndex));
                    }
                }
            }
        }
    }
}
Also used : Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) State(org.openhab.core.types.State) DeviceConsumptionSensorJob(org.openhab.binding.digitalstrom.internal.client.job.DeviceConsumptionSensorJob) ConfigurationException(org.osgi.service.cm.ConfigurationException) DigitalSTROMBindingProvider(org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig) DecimalType(org.openhab.core.library.types.DecimalType) CachedMeteringValue(org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue) SensorIndexEnum(org.openhab.binding.digitalstrom.internal.client.constants.SensorIndexEnum) DeviceSensorValueJob(org.openhab.binding.digitalstrom.internal.client.job.DeviceSensorValueJob)

Example 2 with DigitalSTROMBindingProvider

use of org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider in project openhab1-addons by openhab.

the class DigitalSTROMBinding method deactivate.

@Override
public void deactivate() {
    for (DigitalSTROMBindingProvider provider : providers) {
        provider.removeBindingChangeListener(this);
    }
    if (digitalSTROMEventListener != null) {
        digitalSTROMEventListener.shutdown();
        digitalSTROMEventListener = null;
    }
    if (sensorJobExecutor != null) {
        sensorJobExecutor.shutdown();
        sensorJobExecutor = null;
    }
    removeAllDeviceListener();
    deallocateResources();
    providers.clear();
    digitalSTROM.logout();
}
Also used : DigitalSTROMBindingProvider(org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider)

Example 3 with DigitalSTROMBindingProvider

use of org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider in project openhab1-addons by openhab.

the class DigitalSTROMBinding method initializeDevices.

private void initializeDevices() {
    for (DigitalSTROMBindingProvider provider : this.providers) {
        Collection<String> itemNames = provider.getItemNames();
        // initialize devices
        for (String itemName : itemNames) {
            DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
            if (confItem != null && confItem.dsid != null) {
                if (rawDsidToDeviceMap.size() == 0 && serverIsFound()) {
                    rawDsidToDeviceMap = getAllDigitalSTROMDevicesMap();
                }
                Device device = rawDsidToDeviceMap.get(confItem.dsid.getValue());
                if (device != null) {
                    addDevice(itemName, device);
                    updateItemState(confItem.item);
                    handleStructure(digitalSTROM.getApartmentStructure(getSessionToken()));
                }
            }
        }
    }
}
Also used : DigitalSTROMBindingProvider(org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)

Example 4 with DigitalSTROMBindingProvider

use of org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider in project openhab1-addons by openhab.

the class DigitalSTROMBinding method bindingChanged.

@Override
public void bindingChanged(BindingProvider provider, String itemName) {
    if (provider instanceof DigitalSTROMBindingProvider) {
        // remove device associated with the item
        Device device = deviceMap.get(itemName);
        if (device != null) {
            List<String> itemNamesForDsid = getItemNamesForDsid(device.getDSID().getValue());
            if (itemNamesForDsid.size() == 1) {
                device.removeDeviceListener(this);
                removeSensorJobs(device.getDSID());
            }
        }
        deviceMap.remove(itemName);
        // initialize the device
        DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
        if (confItem != null && confItem.dsid != null) {
            if (rawDsidToDeviceMap.size() == 0 && serverIsFound()) {
                rawDsidToDeviceMap = getAllDigitalSTROMDevicesMap();
            }
            device = rawDsidToDeviceMap.get(confItem.dsid.getValue());
            if (device != null) {
                addDevice(itemName, device);
                updateItemState(confItem.item);
                handleStructure(digitalSTROM.getApartmentStructure(getSessionToken()));
            }
        }
    }
}
Also used : DigitalSTROMBindingProvider(org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)

Aggregations

DigitalSTROMBindingProvider (org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider)4 Device (org.openhab.binding.digitalstrom.internal.client.entity.Device)3 DigitalSTROMBindingConfig (org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)3 SensorIndexEnum (org.openhab.binding.digitalstrom.internal.client.constants.SensorIndexEnum)1 CachedMeteringValue (org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue)1 DeviceConsumptionSensorJob (org.openhab.binding.digitalstrom.internal.client.job.DeviceConsumptionSensorJob)1 DeviceSensorValueJob (org.openhab.binding.digitalstrom.internal.client.job.DeviceSensorValueJob)1 DecimalType (org.openhab.core.library.types.DecimalType)1 State (org.openhab.core.types.State)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1