use of org.openhab.binding.tinkerforge.internal.types.PercentValue in project openhab1-addons by openhab.
the class TinkerforgeBinding method postUpdate.
private void postUpdate(String uid, String subId, TinkerforgeValue sensorValue) {
// TODO undef handling
logger.trace("postUpdate called for uid {} subid {}", uid, subId);
Map<String, TinkerforgeBindingProvider> providerMap = getBindingProviders(uid, subId);
if (providerMap.size() == 0) {
logger.debug("{} found no item for uid {}, subid {}", LoggerConstants.TFMODELUPDATE, uid, subId);
}
for (Entry<String, TinkerforgeBindingProvider> entry : providerMap.entrySet()) {
String itemName = entry.getKey();
TinkerforgeBindingProvider provider = entry.getValue();
Class<? extends Item> itemType = provider.getItemType(itemName);
State value = UnDefType.UNDEF;
if (sensorValue instanceof DecimalValue) {
if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
value = DecimalType.valueOf(String.valueOf(sensorValue));
logger.trace("found item to update for DecimalValue {}", itemName);
} else if (itemType.isAssignableFrom(ContactItem.class)) {
value = sensorValue.equals(DecimalValue.ZERO) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
value = sensorValue.equals(DecimalValue.ZERO) ? OnOffType.OFF : OnOffType.ON;
} else {
logger.trace("no update for DecimalValue for item {}", itemName);
continue;
}
} else if (sensorValue instanceof HighLowValue) {
if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
value = sensorValue == HighLowValue.HIGH ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
} else if (itemType.isAssignableFrom(ContactItem.class)) {
value = sensorValue == HighLowValue.HIGH ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
value = sensorValue == HighLowValue.HIGH ? OnOffType.ON : OnOffType.OFF;
} else {
continue;
}
} else if (sensorValue instanceof OnOffValue) {
if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
value = sensorValue == OnOffValue.ON ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
} else if (itemType.isAssignableFrom(ContactItem.class)) {
value = sensorValue == OnOffValue.ON ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
value = sensorValue == OnOffValue.ON ? OnOffType.ON : OnOffType.OFF;
} else {
continue;
}
} else if (sensorValue instanceof PercentValue) {
if (itemType.isAssignableFrom(SwitchItem.class)) {
value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == 1 ? OnOffType.ON : OnOffType.OFF;
logger.debug("switch found {}", itemName);
} else if (itemType.isAssignableFrom(RollershutterItem.class) || itemType.isAssignableFrom(DimmerItem.class)) {
value = new PercentType(((PercentValue) sensorValue).toBigDecimal());
logger.debug("Rollershutter or dimmer found {} {}", itemName);
} else if (itemType.isAssignableFrom(ContactItem.class)) {
value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == -1 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
logger.debug("contact found {}", itemName);
} else {
continue;
}
} else if (sensorValue instanceof DirectionValue) {
if (itemType.isAssignableFrom(RollershutterItem.class)) {
value = sensorValue == DirectionValue.RIGHT ? UpDownType.UP : UpDownType.DOWN;
logger.trace("found item to update for UpDownValue {}", itemName);
} else {
continue;
}
} else if (sensorValue instanceof HSBValue) {
if (itemType.isAssignableFrom(ColorItem.class)) {
logger.trace("found item to update for HSBValue {}", itemName);
value = ((HSBValue) sensorValue).getHsbValue();
}
} else if (sensorValue == UnDefValue.UNDEF || sensorValue == null) {
value = UnDefType.UNDEF;
}
eventPublisher.postUpdate(itemName, value);
logger.debug("{} postupdate: found sensorValue: {} for item {}", LoggerConstants.TFMODELUPDATE, sensorValue, itemName);
}
}
use of org.openhab.binding.tinkerforge.internal.types.PercentValue in project openhab1-addons by openhab.
the class MServoImpl method setPercentValue.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
public void setPercentValue(PercentValue newPercentValue) {
PercentValue oldPercentValue = percentValue;
percentValue = newPercentValue;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.MSERVO__PERCENT_VALUE, oldPercentValue, percentValue));
}
use of org.openhab.binding.tinkerforge.internal.types.PercentValue in project openhab1-addons by openhab.
the class MBrickletAnalogOutV2Impl method setPercentValue.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
@Override
public void setPercentValue(PercentValue newPercentValue) {
PercentValue oldPercentValue = percentValue;
percentValue = newPercentValue;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.MBRICKLET_ANALOG_OUT_V2__PERCENT_VALUE, oldPercentValue, percentValue));
}
use of org.openhab.binding.tinkerforge.internal.types.PercentValue in project openhab1-addons by openhab.
the class RemoteSwitchBImpl method dimm.
/**
* <!-- begin-user-doc -->
* wait for device ready
* if dimm value is 0% call switch off
* if dimm value is 100% call switch on
* <!-- end-user-doc -->
*
* @generated NOT
*/
private void dimm(Short newDimmValue) {
if (newDimmValue == null) {
logger.error("newDimmValue must not be null");
return;
}
logger.trace("new dimm value {}", newDimmValue);
if (newDimmValue > getMaxValue().shortValue()) {
if (absDimmValue != null && absDimmValue < getMaxValue().shortValue()) {
newDimmValue = getMaxValue().shortValue();
} else {
logger.trace("max dim value already reached");
return;
}
} else if (newDimmValue < getMinValue().shortValue()) {
if (absDimmValue != null && absDimmValue > getMinValue().shortValue()) {
newDimmValue = getMinValue().shortValue();
} else {
logger.trace("min dim value already reached");
return;
}
}
logger.debug("newDimmValue {}", newDimmValue);
int maxRetries = 20;
int trial = 0;
try {
while (tinkerforgeDevice.getSwitchingState() == BrickletRemoteSwitch.SWITCHING_STATE_BUSY && trial < maxRetries) {
trial++;
logger.trace("waiting for ready state {}", trial);
Thread.sleep(50);
}
if (trial == maxRetries) {
logger.error("remote switch doesn't go to ready state in spite of {} retries.", trial);
return;
}
PercentValue dimmState;
if (newDimmValue == getMinValue().shortValue()) {
// switch off
dimmState = new PercentValue(BigDecimal.ZERO);
logger.debug("switching socket B with address {}, unit {} to {}", getAddress(), getUnit(), BrickletRemoteSwitch.SWITCH_TO_OFF);
tinkerforgeDevice.switchSocketB(getAddress(), getUnit(), BrickletRemoteSwitch.SWITCH_TO_OFF);
} else if (newDimmValue == getMaxValue().shortValue()) {
// switch on
dimmState = new PercentValue(new BigDecimal(100));
logger.debug("switching socket B with address {}, unit {} to {}", getAddress(), getUnit(), BrickletRemoteSwitch.SWITCH_TO_ON);
tinkerforgeDevice.switchSocketB(getAddress(), getUnit(), BrickletRemoteSwitch.SWITCH_TO_ON);
} else {
logger.debug("*** newValue {}", (new BigDecimal(newDimmValue).divide(getMaxValue(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))));
dimmState = new PercentValue(new BigDecimal(newDimmValue).divide(getMaxValue(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
getMbrick().getTinkerforgeDevice().dimSocketB(getAddress(), getUnit(), newDimmValue);
}
logger.debug("new dimmState is {}", dimmState);
// do not notify listeners
absDimmValue = newDimmValue;
setSensorValue(dimmState);
} catch (TimeoutException e) {
TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_TIMEOUT_EXCEPTION, e);
} catch (NotConnectedException e) {
TinkerforgeErrorHandler.handleError(this, TinkerforgeErrorHandler.TF_NOT_CONNECTION_EXCEPTION, e);
} catch (InterruptedException e) {
logger.warn("retry was interrupted");
}
}
Aggregations