Search in sources :

Example 1 with DigitalSTROMBindingConfig

use of org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig 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 DigitalSTROMBindingConfig

use of org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig in project openhab1-addons by openhab.

the class DigitalSTROMGenericBindingProvider method parseBindingConfigString.

protected DigitalSTROMBindingConfig parseBindingConfigString(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    DigitalSTROMBindingConfig configItem = new DigitalSTROMBindingConfig();
    configItem.init(item, bindingConfig);
    if (!configItem.isValid()) {
        throw new BindingConfigParseException("itemType mismatch ... wrong item:" + item.getName() + " for digitalstrom hardware");
    }
    return configItem;
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)

Example 3 with DigitalSTROMBindingConfig

use of org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig 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 DigitalSTROMBindingConfig

use of org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig in project openhab1-addons by openhab.

the class DigitalSTROMBinding method deviceCall.

private void deviceCall(String itemName, Command cm) {
    Device device = deviceMap.get(itemName);
    if (device != null) {
        if (cm instanceof org.openhab.core.library.types.OnOffType) {
            if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.ON)) {
                boolean transmitted = digitalSTROM.turnDeviceOn(getSessionToken(), device.getDSID(), null);
                if (transmitted) {
                    device.setOutputValue(device.getMaxOutPutValue());
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MAXIMUM.getSceneNumber());
                }
            } else if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.OFF)) {
                boolean transmitted = digitalSTROM.turnDeviceOff(getSessionToken(), device.getDSID(), null);
                if (transmitted) {
                    device.setOutputValue(0);
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MINIMUM.getSceneNumber());
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.IncreaseDecreaseType) {
            if (!device.isDimmable()) {
                logger.warn("device is not in dimm mode: " + itemName + " outputMode: " + device.getOutputMode().getMode());
                return;
            }
            if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.INCREASE)) {
                boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.INCREMENT, false);
                if (transmitted) {
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.INCREMENT.getSceneNumber());
                    if (device.getOutputValue() == 0) {
                        initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
                    } else {
                        device.increase();
                    }
                } else {
                    logger.error("transmitting increase command FAILED " + itemName);
                }
            } else if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.DECREASE)) {
                boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.DECREMENT, false);
                if (transmitted) {
                    addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.DECREMENT.getSceneNumber());
                    device.decrease();
                } else {
                    logger.error("transmitting decrease command FAILED " + itemName);
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.PercentType) {
            int percent = -1;
            try {
                percent = (int) Float.parseFloat(cm.toString());
            } catch (java.lang.NumberFormatException e) {
                logger.error("NumberFormatException on a PercentType with command: " + cm.toString());
            }
            if (percent != -1) {
                if (percent > -1 && percent < 101) {
                    if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                        DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                        if (confItem != null) {
                            if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                                int old = device.getSlatPosition();
                                device.setSlatPosition(fromPercentToValue(percent, device.getMaxSlatPosition()));
                                boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, fromPercentToValue(percent, device.getMaxSlatPosition()));
                                if (!transmitted) {
                                    device.setSlatPosition(old);
                                    logger.error("could NOT successfully set new value for slats ..." + cm.toString());
                                }
                            } else {
                                int old = device.getOutputValue();
                                device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
                                boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
                                if (!transmitted) {
                                    device.setOutputValue(old);
                                    logger.error("could NOT successfully set new value ..." + cm.toString());
                                }
                            }
                        }
                    } else {
                        int old = device.getOutputValue();
                        device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
                        boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
                        if (!transmitted) {
                            device.setOutputValue(old);
                            logger.error("could NOT successfully set new value ..." + cm.toString());
                        }
                    }
                }
            }
        } else if (cm instanceof org.openhab.core.library.types.StopMoveType) {
            if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        logger.warn("stop and move command NOT possible for slats, use PercentType command or up and down please");
                    } else {
                        handleStopMoveForRollershutter(device, cm);
                    }
                }
            } else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
                handleStopMoveForRollershutter(device, cm);
            }
        } else if (cm instanceof org.openhab.core.library.types.UpDownType) {
            if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
                // 255 is max open, 0 is closed
                DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.UP)) {
                            int slatPosition = device.getSlatPosition();
                            int newPosition = slatPosition + DeviceConstants.MOVE_STEP_SLAT;
                            if (newPosition > device.getMaxSlatPosition()) {
                                newPosition = device.getMaxSlatPosition();
                            }
                            boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
                            if (transmitted) {
                                device.setSlatPosition(newPosition);
                            }
                        } else if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.DOWN)) {
                            int slatPosition = device.getSlatPosition();
                            int newPosition = slatPosition - DeviceConstants.MOVE_STEP_SLAT;
                            if (newPosition < device.getMinSlatPosition()) {
                                newPosition = device.getMinSlatPosition();
                            }
                            boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
                            if (transmitted) {
                                device.setSlatPosition(newPosition);
                            }
                        }
                    } else {
                        handleUpDownForRollershutter(device, cm);
                    }
                }
            } else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
                handleUpDownForRollershutter(device, cm);
            } else {
                logger.warn("Wrong item configuration ... this hardware is not a rollershutter: " + itemName);
            }
        }
    } else {
        if (cm instanceof DecimalType) {
            DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
            if (confItem != null && confItem.context != null) {
                if (confItem.context.equals(ContextConfig.apartment)) {
                    digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(((DecimalType) cm).intValue()), false);
                } else if (confItem.context.equals(ContextConfig.zone)) {
                    digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(((DecimalType) cm).intValue()), false);
                }
            }
        } else if (cm instanceof StringType) {
            DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
            if (confItem != null && confItem.context != null) {
                int scene = -1;
                try {
                    scene = Integer.parseInt(cm.toString());
                } catch (java.lang.NumberFormatException e) {
                    logger.error("NumberFormatException by parsing " + cm.toString() + " for " + confItem.itemName);
                }
                if (scene != -1) {
                    if (confItem.context.equals(ContextConfig.apartment)) {
                        digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(scene), false);
                    } else if (confItem.context.equals(ContextConfig.zone)) {
                        digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(scene), false);
                    }
                }
            }
        } else {
            logger.warn("couldn't find digitalstrom device for " + itemName);
        }
    }
}
Also used : IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) StringType(org.openhab.core.library.types.StringType) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) OnOffType(org.openhab.core.library.types.OnOffType) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) StopMoveType(org.openhab.core.library.types.StopMoveType)

Example 5 with DigitalSTROMBindingConfig

use of org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig 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

DigitalSTROMBindingConfig (org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)6 Device (org.openhab.binding.digitalstrom.internal.client.entity.Device)5 DigitalSTROMBindingProvider (org.openhab.binding.digitalstrom.DigitalSTROMBindingProvider)3 DecimalType (org.openhab.core.library.types.DecimalType)3 PercentType (org.openhab.core.library.types.PercentType)2 State (org.openhab.core.types.State)2 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 DimmerItem (org.openhab.core.library.items.DimmerItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 RollershutterItem (org.openhab.core.library.items.RollershutterItem)1 StringItem (org.openhab.core.library.items.StringItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 StopMoveType (org.openhab.core.library.types.StopMoveType)1 StringType (org.openhab.core.library.types.StringType)1 UpDownType (org.openhab.core.library.types.UpDownType)1