use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class LcnGenericBindingProvider method processBindingConfiguration.
/**
* Item processing for the LCN bindings.
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
Matcher matcher = PATTERN_BINDING_GENERAL.matcher(bindingConfig);
if (!matcher.matches()) {
throw new BindingConfigParseException(bindingConfig + "' contains no valid binding!");
}
matcher.reset();
LcnBindingConfig bc = new LcnBindingConfig(item);
while (matcher.find()) {
String binding = matcher.group(1);
if (binding != null && !binding.trim().isEmpty()) {
String openHabCmd = null;
String connId, lcnTarget;
Matcher openHabMatcher = PATTERN_BINDING_WITH_OPENHAB.matcher(binding);
Matcher pureMatcher = PATTERN_BINDING_PURE.matcher(binding);
if (openHabMatcher.matches()) {
openHabCmd = openHabMatcher.group(1);
connId = openHabMatcher.group(2);
lcnTarget = openHabMatcher.group(3);
} else if (pureMatcher.matches()) {
connId = pureMatcher.group(1);
lcnTarget = pureMatcher.group(2);
} else {
throw new BindingConfigParseException("Invalid binding configuration for " + binding + "!");
}
String lcnShort = resolveMappings(lcnTarget, openHabCmd);
if (lcnShort == null || lcnShort.equals(openHabCmd)) {
lcnShort = lcnTarget;
}
Command cmd = openHabCmd == null ? TypeParser.parseCommand(new StringItem("").getAcceptedCommandTypes(), "") : openHabCmd.equals("%i") ? new StringType("%i") : TypeParser.parseCommand(item.getAcceptedCommandTypes(), openHabCmd);
bc.add(new LcnBindingConfig.Mapping(cmd, connId, lcnShort));
}
}
// Finished
this.addBindingConfig(item, bc);
for (LcnAddrMod addr : bc.getRelatedModules()) {
HashSet<String> l = this.itemNamesByModulCache.get(addr);
if (l == null) {
l = new HashSet<String>();
this.itemNamesByModulCache.put(addr, l);
}
l.add(item.getName());
}
}
use of org.openhab.core.types.Command 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.types.Command in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canSendGroup1DimmerUpdate.
@Test
public void canSendGroup1DimmerUpdate() throws Exception {
ModuleChannel item = group1.addChannel("test4", 4, new ArrayList<Class<? extends Command>>());
item.setState(new PercentType(25));
group1.publishStateToNikobus(item, binding);
Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
NikobusCommand cmd = command.getAllValues().get(0);
assertEquals("$1E156C94000000400000FF45DE7B", cmd.getCommand());
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canProcessGroup1StatusUpdate.
@Test
public void canProcessGroup1StatusUpdate() {
ModuleChannel item = group1.addChannel("test5", 5, new ArrayList<Class<? extends Command>>());
item.setState(OnOffType.OFF);
group1.processNikobusCommand(new NikobusCommand("$0512"), binding);
group1.processNikobusCommand(new NikobusCommand("$1C6C940000000000FF00557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test5", OnOffType.ON);
group1.processNikobusCommand(new NikobusCommand("$0512"), binding);
group1.processNikobusCommand(new NikobusCommand("$1C6C9400000000FF00FF557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test5", OnOffType.OFF);
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canProcessGroup2StatusUpdate.
@Test
public void canProcessGroup2StatusUpdate() {
ModuleChannel item = group2.addChannel("test11", 11, new ArrayList<Class<? extends Command>>());
item.setState(OnOffType.OFF);
group2.processNikobusCommand(new NikobusCommand("$0517"), binding);
group2.processNikobusCommand(new NikobusCommand("$1C6C940000000000FF00557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test11", OnOffType.ON);
group2.processNikobusCommand(new NikobusCommand("$0517"), binding);
group2.processNikobusCommand(new NikobusCommand("$1C6C9400000000FF00FF557CF8"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test11", OnOffType.OFF);
}
Aggregations