Search in sources :

Example 1 with TFConfig

use of org.openhab.binding.tinkerforge.internal.model.TFConfig in project openhab1-addons by openhab.

the class TinkerforgeBinding method addMDevice.

/**
     * Configures and enables newly found devices. For sub devices the master device is also enabled.
     * Configuration is only added if there is a configuration from openhab.cfg available and the
     * device is configurable which is the case for {@link MTFConfigConsumer}. Devices of type
     * {@link IODevice} are only enabled if they are configured in openhab.cfg, all other devices are
     * always enabled.
     *
     * @param device A device object as {@link MBaseDevice}.
     * @param uid The device uid as String.
     * @param subId The device subid as String or <code>null</code> if the device is not a sub device.
     */
@SuppressWarnings("unchecked")
private synchronized void addMDevice(MBaseDevice device, String uid, String subId) {
    String logId = subId == null ? uid : uid + " " + subId;
    OHTFDevice<?, ?> deviceConfig = ohConfig.getConfigByTFId(uid, subId);
    if (deviceConfig == null) {
        logger.debug("{} found no device configuration for uid \"{}\" subid \"{}\"", LoggerConstants.TFINITSUB, uid, subId);
    }
    if (device.getEnabledA().compareAndSet(false, true)) {
        if (subId != null) {
            MDevice<?> masterDevice = (MDevice<?>) device.eContainer();
            // recursion for adding the master device
            if (!masterDevice.getEnabledA().get()) {
                logger.debug("{} enabling masterDevice {}", LoggerConstants.TFINITSUB, masterDevice.getUid());
                addMDevice(masterDevice, uid, null);
            }
        }
        if (device instanceof MTFConfigConsumer<?> && deviceConfig != null) {
            logger.debug("{} found MTFConfigConsumer id {}", LoggerConstants.TFINIT, logId);
            if (device instanceof GenericDevice && checkDuplicateGenericDevice((GenericDevice) device, uid, subId)) {
                logger.error("{} ignoring duplicate device uid: {}, subId {}, genericId {}. Fix your openhab.cfg!", LoggerConstants.CONFIG, uid, subId);
                device.getEnabledA().compareAndSet(true, false);
            } else {
                TFConfig deviceTfConfig = EcoreUtil.copy(deviceConfig.getTfConfig());
                logger.debug("{} setting tfConfig for {}", LoggerConstants.TFINIT, logId);
                logger.debug("{} adding/enabling device {} with config: {}", LoggerConstants.TFINIT, logId, deviceTfConfig);
                ((MTFConfigConsumer<EObject>) device).setTfConfig(deviceTfConfig);
                device.enable();
            }
        } else if (device instanceof IODevice || device instanceof IO4Device) {
            logger.debug("{} ignoring unconfigured  IODevice: {}", LoggerConstants.TFINIT, logId);
            // set the device disabled, this is needed for not getting
            // states
            // through execute method
            device.getEnabledA().compareAndSet(true, false);
        } else {
            device.enable();
            logger.debug("{} adding/enabling device: {}", LoggerConstants.TFINIT, logId);
        }
    }
}
Also used : IO4Device(org.openhab.binding.tinkerforge.internal.model.IO4Device) GenericDevice(org.openhab.binding.tinkerforge.internal.model.GenericDevice) MTFConfigConsumer(org.openhab.binding.tinkerforge.internal.model.MTFConfigConsumer) MDevice(org.openhab.binding.tinkerforge.internal.model.MDevice) IODevice(org.openhab.binding.tinkerforge.internal.model.IODevice) TFConfig(org.openhab.binding.tinkerforge.internal.model.TFConfig)

Aggregations

GenericDevice (org.openhab.binding.tinkerforge.internal.model.GenericDevice)1 IO4Device (org.openhab.binding.tinkerforge.internal.model.IO4Device)1 IODevice (org.openhab.binding.tinkerforge.internal.model.IODevice)1 MDevice (org.openhab.binding.tinkerforge.internal.model.MDevice)1 MTFConfigConsumer (org.openhab.binding.tinkerforge.internal.model.MTFConfigConsumer)1 TFConfig (org.openhab.binding.tinkerforge.internal.model.TFConfig)1