use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canProcessGroup1StatusUpdateDimmer.
@Test
public void canProcessGroup1StatusUpdateDimmer() {
List<Class<? extends Command>> acceptedCommands = new ArrayList<Class<? extends Command>>();
acceptedCommands.add(PercentType.class);
ModuleChannel item = group3.addChannel("test2", 1, acceptedCommands);
item.setState(OnOffType.OFF);
group3.processNikobusCommand(new NikobusCommand("$0512"), binding);
group3.processNikobusCommand(new NikobusCommand("$1C5FCB03400000000000E36D38"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test2", PercentType.valueOf("26"));
group3.processNikobusCommand(new NikobusCommand("$0512"), binding);
group3.processNikobusCommand(new NikobusCommand("$1C5FCB037F000000000009E2C0"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test2", PercentType.valueOf("50"));
group3.processNikobusCommand(new NikobusCommand("$0512"), binding);
group3.processNikobusCommand(new NikobusCommand("$1C5FCB03D90000000000652B76"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test2", PercentType.valueOf("86"));
group3.processNikobusCommand(new NikobusCommand("$0512"), binding);
group3.processNikobusCommand(new NikobusCommand("$1C5FCB03A70000000000A0143B"), binding);
Mockito.verify(binding, Mockito.times(1)).postUpdate("test2", PercentType.valueOf("66"));
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class PlugwiseBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
PlugwiseBindingProvider provider = findFirstMatchingBindingProvider(itemName);
if (command != null) {
String commandAsString = command.toString();
List<Command> commands = new ArrayList<Command>();
// check if the command is valid for this item by checking if a pw ID exists
String checkID = provider.getPlugwiseID(itemName, command);
if (checkID != null) {
commands.add(command);
} else {
// ooops - command is not defined, but maybe we have something of the same Type (e.g Decimal, String
// types)
// commands = provider.getCommandsByType(itemName, command.getClass());
commands = provider.getAllCommands(itemName);
}
for (Command someCommand : commands) {
String plugwiseID = provider.getPlugwiseID(itemName, someCommand);
PlugwiseCommandType plugwiseCommandType = provider.getPlugwiseCommandType(itemName, someCommand);
if (plugwiseID != null) {
if (plugwiseCommandType != null) {
@SuppressWarnings("unused") boolean result = executeCommand(plugwiseID, plugwiseCommandType, commandAsString);
// Each command is responsible to make sure that a result value for the action is polled from
// the device
// which then will be used to do a postUpdate
// if new commands would be added later on that do not have this possibility, then a kind of
// auto-update has to be performed here below
} else {
logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
}
} else {
logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
}
}
}
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class PlugwiseGenericBindingProvider method getCommandsByType.
@Override
public List<Command> getCommandsByType(String itemName, Class<? extends Command> commandClass) {
List<Command> commands = new ArrayList<Command>();
PlugwiseBindingConfig config = (PlugwiseBindingConfig) bindingConfigs.get(itemName);
for (Command command : config.keySet()) {
PlugwiseBindingConfigElement element = config.get(command);
if (element.getCommandType().getTypeClass().equals(commandClass)) {
commands.add(command);
}
}
return commands;
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class PlugwiseGenericBindingProvider method parseBindingConfig.
/**
* Parses the configuration string and update the provided config
*
* @param config
* @param item
* @param bindingConfig
* @throws BindingConfigParseException
*/
private void parseBindingConfig(PlugwiseBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
String commandAsString = null;
String plugwiseID = null;
String plugwiseCommand = null;
int interval = 60;
if (bindingConfig == null) {
logger.warn("bindingConfig for item '{}' is null", item.getName());
return;
}
Matcher actionWithJobMatcher = ACTION_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
Matcher statusWithJobMatcher = STATUS_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
Matcher actionWithoutJobMatcher = ACTION_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
Matcher statusWithoutJobMatcher = STATUS_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
if (!actionWithJobMatcher.matches() && !statusWithJobMatcher.matches() && !actionWithoutJobMatcher.matches() && !statusWithoutJobMatcher.matches()) {
throw new //
BindingConfigParseException(//
"Plugwise binding configuration must consist of either:\n" + "* 2 parts: [config=" + statusWithoutJobMatcher + //
"]\n" + "* 3 parts: [config=" + statusWithJobMatcher + //
"]\n" + " [config=" + actionWithoutJobMatcher + //
"]\n" + "* 4 parts: [config=" + actionWithJobMatcher + "]");
}
if (actionWithJobMatcher.matches()) {
commandAsString = actionWithJobMatcher.group(1);
plugwiseID = actionWithJobMatcher.group(2);
plugwiseCommand = actionWithJobMatcher.group(3);
interval = Integer.valueOf(actionWithJobMatcher.group(4));
} else if (statusWithJobMatcher.matches()) {
commandAsString = null;
plugwiseID = statusWithJobMatcher.group(1);
plugwiseCommand = statusWithJobMatcher.group(2);
interval = Integer.valueOf(statusWithJobMatcher.group(3));
} else if (actionWithoutJobMatcher.matches()) {
commandAsString = actionWithoutJobMatcher.group(1);
plugwiseID = actionWithoutJobMatcher.group(2);
plugwiseCommand = actionWithoutJobMatcher.group(3);
interval = -1;
} else if (statusWithoutJobMatcher.matches()) {
commandAsString = null;
plugwiseID = statusWithoutJobMatcher.group(1);
plugwiseCommand = statusWithoutJobMatcher.group(2);
interval = -1;
}
PlugwiseCommandType type = PlugwiseCommandType.getCommandType(plugwiseCommand);
if (PlugwiseCommandType.validateBinding(type, item)) {
PlugwiseBindingConfigElement newElement = new PlugwiseBindingConfigElement(plugwiseID, type, interval);
Command command = null;
if (commandAsString == null) {
// for those configuration strings that are not really linked to a openHAB command we
// create a dummy Command to be able to store the configuration information
// I have choosen to do that with NumberItems
NumberItem dummy = new NumberItem(Integer.toString(counter));
command = createCommandFromString(dummy, Integer.toString(counter));
counter++;
config.put(command, newElement);
} else {
command = createCommandFromString(item, commandAsString);
config.put(command, newElement);
}
} else {
String validItemType = PlugwiseCommandType.getValidItemTypes(plugwiseCommand);
if (StringUtils.isEmpty(validItemType)) {
throw new BindingConfigParseException("'" + bindingConfig + "' is no valid binding type");
} else {
throw new BindingConfigParseException("'" + bindingConfig + "' is not bound to a valid item type. Valid item type(s): " + validItemType);
}
}
}
use of org.openhab.core.types.Command in project openhab1-addons by openhab.
the class SonosBinding method execute.
@Override
protected void execute() {
if (isProperlyConfigured()) {
if (!bindingStarted) {
// This will create necessary network resources for UPnP right away
upnpService = new UpnpServiceImpl(new SonosUpnpServiceConfiguration(), listener);
try {
Iterator<SonosZonePlayer> it = sonosZonePlayerCache.iterator();
while (it.hasNext()) {
SonosZonePlayer aPlayer = it.next();
if (aPlayer.getDevice() == null) {
logger.info("Querying the network for a predefined Sonos device with UDN {}", aPlayer.getUdn());
upnpService.getControlPoint().search(new UDNHeader(aPlayer.getUdn()));
}
}
logger.info("Querying the network for any other Sonos device");
final UDAServiceType udaType = new UDAServiceType("AVTransport");
upnpService.getControlPoint().search(new UDAServiceTypeHeader(udaType));
} catch (Exception e) {
logger.warn("An exception occurred while searching the network for Sonos devices: ", e.getMessage());
}
bindingStarted = true;
}
Scheduler sched = null;
try {
sched = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException e) {
logger.error("An exception occurred while getting a reference to the Quartz Scheduler");
}
// Cycle through the Items and setup sonos zone players if required
for (SonosBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
for (String sonosID : provider.getSonosID(itemName)) {
if (!sonosZonePlayerCache.contains(sonosID)) {
// the device is not yet discovered on the network or not defined in the .cfg
// Verify that the sonosID has the format of a valid UDN
Pattern SONOS_UDN_PATTERN = Pattern.compile("RINCON_(\\w{17})");
Matcher matcher = SONOS_UDN_PATTERN.matcher(sonosID);
if (matcher.matches()) {
// Add device to the cached Configs
SonosZonePlayer thePlayer = new SonosZonePlayer(sonosID, self);
thePlayer.setUdn(new UDN(sonosID));
sonosZonePlayerCache.add(thePlayer);
// Query the network for this device
logger.info("Querying the network for a predefined Sonos device with UDN '{}'", thePlayer.getUdn());
upnpService.getControlPoint().search(new UDNHeader(thePlayer.getUdn()));
}
}
}
}
}
// Cycle through the item binding configuration that define polling criteria
for (SonosCommandType sonosCommandType : SonosCommandType.getPolling()) {
for (SonosBindingProvider provider : providers) {
for (String itemName : provider.getItemNames(sonosCommandType.getSonosCommand())) {
for (Command aCommand : provider.getCommands(itemName, sonosCommandType.getSonosCommand())) {
// We are dealing with a valid device
SonosZonePlayer thePlayer = sonosZonePlayerCache.getById(provider.getSonosID(itemName, aCommand));
if (thePlayer != null) {
RemoteDevice theDevice = thePlayer.getDevice();
// Not all Sonos devices have the same capabilities
if (theDevice != null) {
if (theDevice.findService(new UDAServiceId(sonosCommandType.getService())) != null) {
boolean jobExists = false;
// enumerate each job group
try {
for (String group : sched.getJobGroupNames()) {
// enumerate each job in group
for (JobKey jobKey : sched.getJobKeys(jobGroupEquals(group))) {
if (jobKey.getName().equals(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString())) {
jobExists = true;
break;
}
}
}
} catch (SchedulerException e1) {
logger.error("An exception occurred while quering the Quartz Scheduler ({})", e1.getMessage());
}
if (!jobExists) {
// set up the Quartz jobs
JobDataMap map = new JobDataMap();
map.put("Player", thePlayer);
JobDetail job = newJob(sonosCommandType.getJobClass()).withIdentity(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString(), "Sonos-" + provider.toString()).usingJobData(map).build();
Trigger trigger = newTrigger().withIdentity(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString(), "Sonos-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInMilliseconds(pollingPeriod)).build();
try {
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("An exception occurred while scheduling a Quartz Job ({})", e.getMessage());
}
}
}
}
}
}
}
}
}
}
}
Aggregations