use of org.openhab.core.library.types.IncreaseDecreaseType 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.IncreaseDecreaseType in project openhab1-addons by openhab.
the class FatekColorItem method command.
@Override
public void command(FatekPLC fatekPLC, Command command) throws CommandException {
try {
final HSBType val;
if (command instanceof OnOffType) {
val = valueForOnOff(fatekPLC, command);
} else if (command instanceof IncreaseDecreaseType) {
val = valueForIncreaseDecrease(fatekPLC, command);
} else if (command instanceof HSBType) {
val = (HSBType) command;
} else {
throw new UnsupportedCommandException(this, command);
}
if (val != null) {
int v1;
int v2;
int v3;
if (isColorRGB) {
Color c = val.toColor();
v1 = c.getRed();
v2 = c.getGreen();
v3 = c.getBlue();
} else {
v1 = val.getHue().intValue();
v2 = val.getSaturation().intValue();
v3 = val.getBrightness().intValue();
}
FatekWriteMixDataCmd cmd = new FatekWriteMixDataCmd(fatekPLC);
cmd.addReg(reg1, v1);
cmd.addReg(reg2, v2);
cmd.addReg(reg3, v3);
cmd.send();
}
} catch (FatekIOException | FatekException e) {
throw new CommandException(this, command, e);
}
}
use of org.openhab.core.library.types.IncreaseDecreaseType in project openhab1-addons by openhab.
the class MochadX10Binding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
MochadX10BindingConfig deviceConfig = getConfigForItemName(itemName);
if (deviceConfig == null) {
return;
}
String address = deviceConfig.getAddress();
String tm = deviceConfig.getTransmitMethod();
String commandStr = "none";
Command previousCommand = lastIssuedCommand.get(address);
int level = -1;
if (command instanceof OnOffType) {
commandStr = OnOffType.ON.equals(command) ? "on" : "off";
level = OnOffType.ON.equals(command) ? 100 : 0;
} else if (command instanceof UpDownType) {
commandStr = UpDownType.UP.equals(command) ? "bright" : "dim";
level = UpDownType.UP.equals(command) ? 100 : 0;
} else if (command instanceof StopMoveType) {
if (StopMoveType.STOP.equals(command)) {
commandStr = UpDownType.UP.equals(previousCommand) ? "dim" : "bright";
} else {
// Move not supported yet
commandStr = "none";
}
} else if (command instanceof PercentType) {
if (deviceConfig.getItemType() == DimmerItem.class) {
level = ((PercentType) command).intValue();
if (((PercentType) command).intValue() == 0) {
// If percent value equals 0 the x10 "off" command is used instead of the dim command
commandStr = "off";
} else {
long dim_value = 0;
if (deviceConfig.getDimMethod().equals("xdim")) {
// 100% maps to value (XDIM_LEVELS - 1) so we need to do scaling
dim_value = Math.round(((PercentType) command).doubleValue() * (MochadX10Command.XDIM_LEVELS - 1) / 100);
commandStr = "xdim " + dim_value;
} else {
// 100% maps to value (DIM_LEVELS - 1) so we need to do scaling
Integer currentValue = currentLevel.get(address);
if (currentValue == null) {
currentValue = 0;
}
logger.debug("Address " + address + " current level " + currentValue);
int newValue = ((PercentType) command).intValue();
int relativeValue;
if (newValue > currentValue) {
relativeValue = (int) Math.round((newValue - currentValue) * ((MochadX10Command.DIM_LEVELS - 1) * 1.0 / 100));
commandStr = "bright " + relativeValue;
} else if (currentValue > newValue) {
relativeValue = (int) Math.round((currentValue - newValue) * ((MochadX10Command.DIM_LEVELS - 1) * 1.0 / 100));
commandStr = "dim " + relativeValue;
} else {
// If there is no change in state, do nothing
commandStr = "none";
}
}
}
} else if (deviceConfig.getItemType() == RollershutterItem.class) {
level = ((PercentType) command).intValue();
Double invert_level = 100 - ((PercentType) command).doubleValue();
long newlevel = Math.round(invert_level * 25.0 / 100);
commandStr = "extended_code_1 0 1 " + newlevel;
}
} else if (command instanceof IncreaseDecreaseType) {
// Increase decrease not yet supported
commandStr = "none";
}
try {
if (!commandStr.equals("none")) {
out.writeBytes(tm + " " + address + " " + commandStr + "\n");
logger.debug(tm + " " + address + " " + commandStr);
out.flush();
previousX10Address.setAddress(address);
logger.debug("Previous X10 address set to " + previousX10Address.toString());
if (level != -1) {
currentLevel.put(address, level);
logger.debug("Address " + address + " level set to " + level);
}
}
} catch (IOException e) {
reconnectToMochadX10Server();
logger.error("IOException: " + e.getMessage() + " while trying to send a command to Mochad X10 host: " + hostIp + ":" + hostPort);
}
lastIssuedCommand.put(address, command);
}
use of org.openhab.core.library.types.IncreaseDecreaseType in project openhab1-addons by openhab.
the class RFXComLighting3Message method convertFromState.
@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
this.subType = ((SubType) subType);
seqNbr = seqNumber;
switch(valueSelector) {
case COMMAND:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
dimmingLevel = 0;
} else if (type instanceof DecimalType) {
command = Commands.fromByte(((DecimalType) type).intValue());
dimmingLevel = 0;
} else {
throw new RFXComException("Can't convert " + type + " to Command");
}
break;
case DIMMING_LEVEL:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
dimmingLevel = 0;
} else if (type instanceof PercentType) {
command = Commands.DIM;
dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
if (dimmingLevel == 0) {
command = Commands.OFF;
}
} else if (type instanceof IncreaseDecreaseType) {
command = Commands.DIM;
// Evert: I do not know how to get previous object state...
dimmingLevel = 5;
} else {
throw new RFXComException("Can't convert " + type + " to Command");
}
break;
default:
throw new RFXComException("Can't convert " + type + " to " + valueSelector);
}
}
use of org.openhab.core.library.types.IncreaseDecreaseType in project openhab1-addons by openhab.
the class TinkerforgeBinding method internalReceiveCommand.
/**
* {@inheritDoc}
*
* Searches the item with the given {@code itemName} in the {@link TinkerforgeBindingProvider}
* collection and gets the uid and subid of the device. The appropriate device is searched in the
* ecosystem and the command is executed on the device.
*
* {@code OnOffType} commands are executed on {@link MInSwitchActor} objects. {@code StringType}
* commands are executed on {@link MTextActor} objects.
*
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("received command {} for item {}", command, itemName);
for (TinkerforgeBindingProvider provider : providers) {
for (String itemNameP : provider.getItemNames()) {
if (itemNameP.equals(itemName)) {
String deviceUid = provider.getUid(itemName);
String deviceSubId = provider.getSubId(itemName);
String deviceName = provider.getName(itemName);
if (deviceName != null) {
String[] ids = getDeviceIdsForDeviceName(deviceName);
deviceUid = ids[0];
deviceSubId = ids[1];
}
logger.trace("{} found item for command: uid: {}, subid: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
MBaseDevice mDevice = tinkerforgeEcosystem.getDevice(deviceUid, deviceSubId);
if (mDevice != null && mDevice.getEnabledA().get()) {
if (command instanceof OnOffType) {
logger.trace("{} found onoff command", LoggerConstants.COMMAND);
OnOffType cmd = (OnOffType) command;
if (mDevice instanceof MSwitchActor) {
OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
((MSwitchActor) mDevice).turnSwitch(state);
} else if (mDevice instanceof DigitalActor) {
HighLowValue state = cmd == OnOffType.OFF ? HighLowValue.LOW : HighLowValue.HIGH;
((DigitalActor) mDevice).turnDigital(state);
} else if (mDevice instanceof ProgrammableSwitchActor) {
OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
((ProgrammableSwitchActor) mDevice).turnSwitch(state, provider.getDeviceOptions(itemName));
} else {
logger.error("{} received OnOff command for non-SwitchActor", LoggerConstants.COMMAND);
}
} else if (command instanceof StringType) {
logger.trace("{} found string command", LoggerConstants.COMMAND);
if (mDevice instanceof MTextActor) {
((MTextActor) mDevice).write(command.toString());
}
} else if (command instanceof DecimalType) {
logger.debug("{} found number command", LoggerConstants.COMMAND);
if (command instanceof HSBType) {
logger.debug("{} found HSBType command", LoggerConstants.COMMAND);
if (mDevice instanceof ProgrammableColorActor) {
logger.debug("{} found ProgrammableColorActor {}", itemName);
((ProgrammableColorActor) mDevice).setSelectedColor((HSBType) command, provider.getDeviceOptions(itemName));
} else if (mDevice instanceof SimpleColorActor) {
logger.debug("{} found SimpleColorActor {}", itemName);
((SimpleColorActor) mDevice).setSelectedColor((HSBType) command);
}
} else if (command instanceof PercentType) {
if (mDevice instanceof SetPointActor) {
((SetPointActor<?>) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
logger.debug("found SetpointActor");
} else if (mDevice instanceof PercentTypeActor) {
((PercentTypeActor) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
logger.debug("found PercentType actor");
} else {
logger.error("found no percenttype actor");
}
} else {
if (mDevice instanceof NumberActor) {
((NumberActor) mDevice).setNumber(((DecimalType) command).toBigDecimal());
} else if (mDevice instanceof SetPointActor) {
((SetPointActor<?>) mDevice).setValue(((DecimalType) command).toBigDecimal(), provider.getDeviceOptions(itemName));
} else {
logger.error("found no number actor");
}
}
} else if (command instanceof UpDownType) {
UpDownType cmd = (UpDownType) command;
logger.debug("{} UpDownType command {}", itemName, cmd);
if (mDevice instanceof MoveActor) {
((MoveActor) mDevice).move((UpDownType) command, provider.getDeviceOptions(itemName));
}
} else if (command instanceof StopMoveType) {
StopMoveType cmd = (StopMoveType) command;
if (mDevice instanceof MoveActor) {
if (cmd == StopMoveType.STOP) {
((MoveActor) mDevice).stop();
} else {
((MoveActor) mDevice).moveon(provider.getDeviceOptions(itemName));
}
}
logger.debug("{} StopMoveType command {}", itemName, cmd);
} else if (command instanceof IncreaseDecreaseType) {
IncreaseDecreaseType cmd = (IncreaseDecreaseType) command;
if (mDevice instanceof DimmableActor) {
((DimmableActor<?>) mDevice).dimm((IncreaseDecreaseType) command, provider.getDeviceOptions(itemName));
}
logger.debug("{} IncreaseDecreaseType command {}", itemName, cmd);
} else {
logger.error("{} got unknown command type: {}", LoggerConstants.COMMAND, command.toString());
}
} else {
logger.error("{} no tinkerforge device found for command for item uid: {} subId: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
}
}
}
}
}
Aggregations