use of org.openhab.core.library.types.UpDownType in project openhab1-addons by openhab.
the class UrtsiBinding method sendToUrtsi.
/**
* The method determines the appropriate
* {@link org.openhab.core.binding.BindingProvider} and uses it to get the
* corresponding URTSI device and channel. Bases on the given type a command
* is send to the device.
*
* @param itemName
* name of the item
* @param type
* Type of the command or status update
* @return Returns true, if the command has been executed successfully.
* Returns false otherwise.
* @throws BindingConfigParseException
*/
private boolean sendToUrtsi(String itemName, Type type) {
UrtsiBindingProvider provider = null;
if (!providers.isEmpty()) {
provider = providers.iterator().next();
}
if (provider == null) {
logger.error("doesn't find matching binding provider [itemName={}, type={}]", itemName, type);
return false;
}
String urtsiDeviceId = provider.getDeviceId(itemName);
UrtsiDevice urtsiDevice = idToDeviceMap.get(urtsiDeviceId);
if (urtsiDevice == null) {
logger.error("No serial port has been configured for urtsi device id '{}'", urtsiDeviceId);
return false;
}
int channel = provider.getChannel(itemName);
int address = provider.getAddress(itemName);
logger.debug("Send to URTSI for item: {}; Type: {}", itemName, type);
String actionKey = null;
if (type instanceof UpDownType) {
switch((UpDownType) type) {
case UP:
actionKey = COMMAND_UP;
break;
case DOWN:
actionKey = COMMAND_DOWN;
break;
}
} else if (type instanceof StopMoveType) {
switch((StopMoveType) type) {
case STOP:
actionKey = COMMAND_STOP;
break;
default:
break;
}
}
logger.debug("Action key: {}", actionKey);
if (actionKey != null) {
String channelString = String.format("%02d", channel);
String addressString = String.format("%02d", address);
String command = addressString + channelString + actionKey;
boolean executedSuccessfully = urtsiDevice.writeString(command);
if (!executedSuccessfully) {
logger.warn("Command has not been processed [itemName={}, command={}]", itemName, command);
}
return executedSuccessfully;
}
return false;
}
use of org.openhab.core.library.types.UpDownType in project openhab1-addons by openhab.
the class MServoImpl method moveon.
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @generated NOT
*/
@Override
public void moveon(DeviceOptions opts) {
if (direction != null && direction != DirectionValue.UNDEF) {
UpDownType directiontmp = this.direction == DirectionValue.LEFT ? UpDownType.UP : UpDownType.DOWN;
move(directiontmp, opts);
} else {
logger.warn("cannot moveon because direction is undefined");
}
}
use of org.openhab.core.library.types.UpDownType in project openhab1-addons by openhab.
the class InsteonHubBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// get configuration for this item
InsteonHubBindingConfig config = InsteonHubBindingConfigUtil.getConfigForItem(providers, itemName);
if (config == null) {
logger.error(BINDING_NAME + " received command for unknown item '" + itemName + "'");
return;
}
// parse info from config
BindingType type = config.getBindingType();
String hubId = config.getDeviceInfo().getHubId();
String deviceId = config.getDeviceInfo().getDeviceId();
// lookup proxy from this configuration
InsteonHubProxy proxy = proxies.get(hubId);
if (proxy == null) {
logger.error(BINDING_NAME + " received command for unknown hub id '" + hubId + "'");
return;
}
if (logger.isDebugEnabled()) {
logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
}
try {
// process according to type
if (type == BindingType.SWITCH) {
// set value on or off
if (command instanceof OnOffType) {
proxy.setDevicePower(deviceId, command == OnOffType.ON);
}
} else if (type == BindingType.DIMMER) {
// INSTEON Dimmer supports Dimmer and RollerShutter types
if (command instanceof OnOffType) {
// ON or OFF => Set level to 255 or 0
int level = command == OnOffType.ON ? 255 : 0;
proxy.setDeviceLevel(deviceId, level);
} else if (command instanceof IncreaseDecreaseType) {
// Increase/Decrease => Incremental Brighten/Dim
InsteonHubAdjustmentType adjustmentType;
if (command == IncreaseDecreaseType.INCREASE) {
adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
} else {
adjustmentType = InsteonHubAdjustmentType.DIM;
}
if (setDimTimeout(itemName)) {
proxy.startDeviceAdjustment(deviceId, adjustmentType);
}
} else if (command instanceof UpDownType) {
// Up/Down => Start Brighten/Dim
InsteonHubAdjustmentType adjustmentType;
if (command == UpDownType.UP) {
adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
} else {
adjustmentType = InsteonHubAdjustmentType.DIM;
}
proxy.startDeviceAdjustment(deviceId, adjustmentType);
} else if (command instanceof StopMoveType) {
// Stop => Stop Brighten/Dim
if (command == StopMoveType.STOP) {
proxy.stopDeviceAdjustment(deviceId);
}
} else {
// set level from 0 to 100 percent value
byte percentByte = Byte.parseByte(command.toString());
float percent = percentByte * .01f;
int level = (int) (255 * percent);
proxy.setDeviceLevel(deviceId, level);
}
}
} catch (Throwable t) {
logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
}
}
use of org.openhab.core.library.types.UpDownType in project openhab1-addons by openhab.
the class KNXCoreTypeMapper method toDPTValue.
/*
* (non-Javadoc)
*
* @see org.openhab.binding.knx.config.KNXTypeMapper#toDPTValue(org.openhab.core.types.Type, java.lang.String)
*/
@Override
public String toDPTValue(Type type, String dptID) {
DPT dpt;
int mainNumber = getMainNumber(dptID);
if (mainNumber == -1) {
logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
return null;
}
try {
DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
dpt = translator.getType();
} catch (KNXException e) {
e.printStackTrace();
return null;
}
// check for HSBType first, because it extends PercentType as well
if (type instanceof HSBType) {
Color color = ((HSBType) type).toColor();
return "r:" + Integer.toString(color.getRed()) + " g:" + Integer.toString(color.getGreen()) + " b:" + Integer.toString(color.getBlue());
} else if (type instanceof OnOffType) {
return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (type instanceof UpDownType) {
return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (type instanceof IncreaseDecreaseType) {
DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5" : valueDPT.getUpperValue() + " 5";
} else if (type instanceof OpenClosedType) {
return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (type instanceof StopMoveType) {
return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (type instanceof PercentType) {
return type.toString();
} else if (type instanceof DecimalType) {
switch(mainNumber) {
case 2:
DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
switch(((DecimalType) type).intValue()) {
case 0:
return "0 " + valueDPT.getLowerValue();
case 1:
return "0 " + valueDPT.getUpperValue();
case 2:
return "1 " + valueDPT.getLowerValue();
default:
return "1 " + valueDPT.getUpperValue();
}
case 18:
int intVal = ((DecimalType) type).intValue();
if (intVal > 63) {
return "learn " + (intVal - 0x80);
} else {
return "activate " + intVal;
}
default:
return type.toString();
}
} else if (type instanceof StringType) {
return type.toString();
} else if (type instanceof DateTimeType) {
return formatDateTime((DateTimeType) type, dptID);
}
logger.debug("toDPTValue: Couldn't get value for {} dpt id {} (no mapping).", type, dptID);
return null;
}
use of org.openhab.core.library.types.UpDownType in project openhab1-addons by openhab.
the class SappBinding method executeSappCommand.
/**
* executes the real command on pnmas device
*/
private void executeSappCommand(String itemName, Command command) {
SappBindingProvider provider = findFirstMatchingBindingProvider(itemName);
if (provider == null) {
logger.error("cannot find a provider, skipping command");
}
try {
Item item = itemRegistry.getItem(itemName);
logger.debug("found item {}", item);
if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigSwitchItem);
if (sappBindingConfigSwitchItem.isPollerSuspender()) {
if (pollingEnabled) {
// turning off polling
pollingEnabled = false;
// force updates of polling switches because polling is
updatePollingSwitchesState(provider);
// off
} else {
// turning on polling
provider.getSappUpdatePendingRequests().replaceAllPendingUpdateRequests(ALL_UPDATE_REQUEST_KEY);
pollingEnabled = true;
}
} else {
SappAddressOnOffControl controlAddress = sappBindingConfigSwitchItem.getControl();
if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigSwitchItem);
return;
}
try {
if (command instanceof OnOffType) {
switch(controlAddress.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), command.equals(OnOffType.ON) ? controlAddress.getOnValue() : controlAddress.getOffValue(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
}
} else if (item instanceof NumberItem) {
SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigNumberItem);
SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigNumberItem);
return;
}
try {
if (command instanceof DecimalType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((DecimalType) command).toBigDecimal()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else if (item instanceof RollershutterItem) {
SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigRollershutterItem);
SappAddressRollershutterControl controlAddress = null;
if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.UP) {
controlAddress = sappBindingConfigRollershutterItem.getUpControl();
} else if (command instanceof UpDownType && ((UpDownType) command) == UpDownType.DOWN) {
controlAddress = sappBindingConfigRollershutterItem.getDownControl();
} else if (command instanceof StopMoveType && ((StopMoveType) command) == StopMoveType.STOP) {
controlAddress = sappBindingConfigRollershutterItem.getStopControl();
}
if (controlAddress != null) {
if (!provider.getPnmasMap().containsKey(controlAddress.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", controlAddress.getPnmasId(), sappBindingConfigRollershutterItem);
return;
}
try {
switch(controlAddress.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, controlAddress.getPnmasId(), controlAddress.getAddress(), controlAddress.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(controlAddress.getSubAddress(), controlAddress.getActivateValue(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(controlAddress.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), controlAddress.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), controlAddress.getAddressType());
break;
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} else if (item instanceof DimmerItem) {
SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
logger.debug("found binding {}", sappBindingConfigDimmerItem);
SappAddressDimmer address = sappBindingConfigDimmerItem.getStatus();
if (!provider.getPnmasMap().containsKey(address.getPnmasId())) {
logger.error("bad pnmas id ({}) in binding ({}) ... skipping", address.getPnmasId(), sappBindingConfigDimmerItem);
return;
}
try {
if (command instanceof OnOffType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((OnOffType) command) == OnOffType.ON ? address.getOriginalMaxScale() : address.getOriginalMinScale(), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else if (command instanceof IncreaseDecreaseType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), ((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE ? Math.min(previousValue + address.getIncrement(), address.getOriginalMaxScale()) : Math.max(previousValue - address.getIncrement(), address.getOriginalMinScale()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else if (command instanceof PercentType) {
switch(address.getAddressType()) {
case VIRTUAL:
{
// mask bits on previous value
int previousValue = getVirtualValue(provider, address.getPnmasId(), address.getAddress(), address.getSubAddress(), false);
int newValue = SappBindingConfigUtils.maskWithSubAddressAndSet(address.getSubAddress(), address.backScaledValue(((PercentType) command).toBigDecimal()), previousValue);
// update pnmas
SappPnmas pnmas = provider.getPnmasMap().get(address.getPnmasId());
SappCentralExecuter sappCentralExecuter = SappCentralExecuter.getInstance();
sappCentralExecuter.executeSapp7DCommand(pnmas.getIp(), pnmas.getPort(), address.getAddress(), newValue);
break;
}
default:
logger.error("cannot run {} on type {}", command.getClass().getSimpleName(), address.getAddressType());
break;
}
} else {
logger.error("command {} not applicable", command.getClass().getSimpleName());
}
} catch (SappException e) {
logger.error("could not run sappcommand", e);
}
} else {
logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
}
} catch (ItemNotFoundException e) {
logger.error("Item {} not found", itemName);
}
}
Aggregations