Search in sources :

Example 1 with Device

use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.

the class DigitalSTROMBinding method removeAllDeviceListener.

private void removeAllDeviceListener() {
    Map<String, Device> clonedMap = getDsidToDeviceMap();
    Collection<Device> collection = clonedMap.values();
    for (Device device : collection) {
        device.removeDeviceListener(this);
    }
}
Also used : Device(org.openhab.binding.digitalstrom.internal.client.entity.Device)

Example 2 with Device

use of org.openhab.binding.digitalstrom.internal.client.entity.Device 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 3 with Device

use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.

the class DigitalSTROMBinding method handleApartmentScene.

private void handleApartmentScene(short sceneId, short groupId) {
    if (groupId == 0) {
        Map<String, Device> clonedDeviceMap = getDsidToDeviceMap();
        Set<String> dsidSet = clonedDeviceMap.keySet();
        for (String dsid : dsidSet) {
            Device device = clonedDeviceMap.get(dsid);
            if (device != null) {
                if (!device.containsSceneConfig(sceneId)) {
                    getSceneSpec(device, sceneId);
                }
                if (!device.doIgnoreScene(sceneId)) {
                    short output = device.getSceneOutputValue(sceneId);
                    if (output != -1) {
                        device.setOutputValue(output);
                    } else {
                        initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
                        initSceneOutputValue(device, sceneId);
                    }
                }
            }
        }
    } else if (groupId != -1) {
        Map<String, Device> clonedDeviceMap = getDsidToDeviceMap();
        Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(0);
        List<String> dsidList = map.get(groupId);
        if (dsidList != null) {
            for (String dsid : dsidList) {
                Device device = clonedDeviceMap.get(dsid);
                if (device != null) {
                    if (!device.containsSceneConfig(sceneId)) {
                        getSceneSpec(device, sceneId);
                    }
                    if (!device.doIgnoreScene(sceneId)) {
                        short output = device.getSceneOutputValue(sceneId);
                        if (output != -1) {
                            device.setOutputValue(output);
                        } else {
                            initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
                            initSceneOutputValue(device, sceneId);
                        }
                    }
                }
            }
        }
    }
}
Also used : Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Device

use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.

the class DigitalSTROMBinding method handleStructure.

// Here we build up a new hashmap in order to replace it with the old one.
// This hashmap is used to find the affected items after an event from
// digitalSTROM.
private void handleStructure(Apartment apartment) {
    if (apartment != null) {
        Map<Integer, Map<Short, List<String>>> newZoneGroupMap = Collections.synchronizedMap(new HashMap<Integer, Map<Short, List<String>>>());
        Map<String, Device> clonedDsidMap = getDsidToDeviceMap();
        for (Zone zone : apartment.getZoneMap().values()) {
            Map<Short, List<String>> groupMap = new HashMap<Short, List<String>>();
            for (DetailedGroupInfo g : zone.getGroups()) {
                List<String> devicesInGroup = new LinkedList<String>();
                for (String dsid : g.getDeviceList()) {
                    if (clonedDsidMap.containsKey(dsid)) {
                        devicesInGroup.add(dsid);
                    }
                }
                groupMap.put(g.getGroupID(), devicesInGroup);
            }
            newZoneGroupMap.put(zone.getZoneId(), groupMap);
        }
        synchronized (digitalSTROMZoneGroupMap) {
            digitalSTROMZoneGroupMap = newZoneGroupMap;
        }
    }
}
Also used : HashMap(java.util.HashMap) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) Zone(org.openhab.binding.digitalstrom.internal.client.entity.Zone) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) DetailedGroupInfo(org.openhab.binding.digitalstrom.internal.client.entity.DetailedGroupInfo)

Example 5 with Device

use of org.openhab.binding.digitalstrom.internal.client.entity.Device 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)

Aggregations

Device (org.openhab.binding.digitalstrom.internal.client.entity.Device)10 DigitalSTROMBindingConfig (org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig)5 LinkedList (java.util.LinkedList)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 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 Set (java.util.Set)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 SensorIndexEnum (org.openhab.binding.digitalstrom.internal.client.constants.SensorIndexEnum)1 CachedMeteringValue (org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue)1 DetailedGroupInfo (org.openhab.binding.digitalstrom.internal.client.entity.DetailedGroupInfo)1 Zone (org.openhab.binding.digitalstrom.internal.client.entity.Zone)1 JSONDeviceImpl (org.openhab.binding.digitalstrom.internal.client.entity.impl.JSONDeviceImpl)1 DeviceConsumptionSensorJob (org.openhab.binding.digitalstrom.internal.client.job.DeviceConsumptionSensorJob)1 DeviceSensorValueJob (org.openhab.binding.digitalstrom.internal.client.job.DeviceSensorValueJob)1