use of org.openhab.binding.tinkerforge.internal.model.MDevice in project openhab1-addons by openhab.
the class TinkerforgeBinding method initializeTFDevices.
/**
* Adds or removes a device to / from the Ecosystem. Notifications from {@link MBrickd} are used
* for adding devices (not sub devices) and removing of devices and their corresponding sub
* devices.
*
* Notifications from {@link MSubDeviceHolder} for adding sub devices.
*
* @param notification The {@link Notification} for add and remove events to the {@link Ecosystem}
* .
*/
private void initializeTFDevices(Notification notification) {
logger.trace("{} notifier {}", LoggerConstants.TFINIT, notification.getNotifier());
if (notification.getNotifier() instanceof MBrickd) {
logger.debug("{} notifier is Brickd", LoggerConstants.TFINIT);
int featureID = notification.getFeatureID(MBrickd.class);
if (featureID == ModelPackage.MBRICKD__MDEVICES) {
if (notification.getEventType() == Notification.ADD) {
MDevice<?> mDevice = (MDevice<?>) notification.getNewValue();
addMDevice(mDevice, mDevice.getUid(), null);
} else if (notification.getEventType() == Notification.ADD_MANY) {
logger.debug("{} Notifier: add many called: ", LoggerConstants.TFINIT);
} else if (notification.getEventType() == Notification.REMOVE) {
logger.debug("{} Notifier: remove called: ", LoggerConstants.TFINIT);
if (notification.getOldValue() instanceof MBaseDevice) {
logger.debug("{} Notifier: remove called for MBaseDevice", LoggerConstants.TFINIT);
MBaseDevice mDevice = (MBaseDevice) notification.getOldValue();
String uid = mDevice.getUid();
String subId = null;
if (searchConfiguredItemName(uid, subId) != null) {
logger.debug("{} Notifier: removing device: uid {} subid {}", LoggerConstants.TFINIT, uid, subId);
postUpdate(uid, subId, UnDefValue.UNDEF);
}
} else {
logger.debug("{} unknown notification from mdevices {}", LoggerConstants.TFINIT, notification);
}
}
} else {
logger.debug("{} Notifier: unknown feature {}", LoggerConstants.TFINIT, notification.getFeature());
}
} else if (notification.getNotifier() instanceof MSubDeviceHolder<?>) {
int featureID = notification.getFeatureID(MSubDeviceHolder.class);
if (featureID == ModelPackage.MSUB_DEVICE_HOLDER__MSUBDEVICES) {
logger.debug("{} MSubdevices Notifier called", LoggerConstants.TFINITSUB);
if (notification.getEventType() == Notification.ADD) {
MSubDevice<?> mSubDevice = (MSubDevice<?>) notification.getNewValue();
addMDevice(mSubDevice, mSubDevice.getUid(), mSubDevice.getSubId());
}
if (notification.getEventType() == Notification.REMOVE) {
logger.debug("{} remove notification from subdeviceholder", LoggerConstants.TFINIT);
logger.debug("{} Notifier: remove called for MSubDevice", LoggerConstants.TFINIT);
MSubDevice<?> mDevice = (MSubDevice<?>) notification.getOldValue();
String uid = mDevice.getUid();
String subId = mDevice.getSubId();
if (searchConfiguredItemName(uid, subId) != null) {
logger.debug("{} Notifier: removing device: uid {} subid {}", LoggerConstants.TFINIT, uid, subId);
postUpdate(uid, subId, UnDefValue.UNDEF);
}
}
}
} else {
logger.debug("{} unhandled notifier {}", LoggerConstants.TFINIT, notification.getNotifier());
}
}
use of org.openhab.binding.tinkerforge.internal.model.MDevice 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);
}
}
}
Aggregations