use of org.teleal.cling.model.types.UDN in project openhab1-addons by openhab.
the class SonosBinding method updated.
@Override
@SuppressWarnings("rawtypes")
public void updated(Dictionary config) throws ConfigurationException {
if (config != null) {
Enumeration keys = config.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
// don't want to process here ...
if ("service.pid".equals(key)) {
continue;
}
if ("pollingPeriod".equals(key)) {
pollingPeriod = Integer.parseInt((String) config.get(key));
logger.debug("Setting polling period to {} ms", pollingPeriod);
continue;
}
Matcher matcher = EXTRACT_SONOS_CONFIG_PATTERN.matcher(key);
if (!matcher.matches()) {
logger.debug("given sonos-config-key '" + key + "' does not follow the expected pattern '<sonosId>.<udn>'");
continue;
}
matcher.reset();
matcher.find();
String sonosID = matcher.group(1);
SonosZonePlayer sonosConfig = sonosZonePlayerCache.getById(sonosID);
if (sonosConfig == null) {
sonosConfig = new SonosZonePlayer(sonosID, self);
sonosZonePlayerCache.add(sonosConfig);
}
String configKey = matcher.group(2);
String value = (String) config.get(key);
if ("udn".equals(configKey)) {
sonosConfig.setUdn(new UDN(value));
logger.debug("Add predefined Sonos device with UDN {}", sonosConfig.getUdn());
} else {
throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' is unknown");
}
}
}
setProperlyConfigured(true);
}
use of org.teleal.cling.model.types.UDN 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