use of org.openhab.binding.hdanywhere.HDanywhereBindingProvider in project openhab1-addons by openhab.
the class HDanywhereBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
HDanywhereBindingProvider provider = findFirstMatchingBindingProvider(itemName);
if (provider == null) {
logger.trace("doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
List<String> hosts = provider.getHosts(itemName);
int sourcePort = Integer.valueOf(command.toString());
for (String aHost : hosts) {
Integer numberOfPorts = matrixCache.get(aHost);
if (numberOfPorts == null) {
// we default to the smallest matrix currently sold by HDanywhere
numberOfPorts = 4;
}
if (sourcePort > numberOfPorts) {
// nice try - we can switch to a port that does not physically exist
logger.warn("{} goes beyond the physical number of {} ports available on the matrix {}", new Object[] { sourcePort, numberOfPorts, aHost });
} else {
List<Integer> ports = provider.getPorts(aHost, itemName);
String httpMethod = "GET";
String url = "http://" + aHost + "/switch.cgi?command=3&data0=";
for (Integer aPort : ports) {
url = url + aPort.toString() + "&data1=";
url = url + command.toString() + "&checksum=";
int checksum = 3 + aPort + sourcePort;
url = url + String.valueOf(checksum);
if (isNotBlank(httpMethod) && isNotBlank(url)) {
String response = HttpUtil.executeUrl(httpMethod, url, null, null, null, timeout);
Pattern p = Pattern.compile("The output " + aPort + " select input (.*).");
Matcher m = p.matcher(response);
while (m.find()) {
List<Class<? extends State>> stateTypeList = new ArrayList<Class<? extends State>>();
stateTypeList.add(DecimalType.class);
State state = TypeParser.parseState(stateTypeList, m.group(1));
if (!portMappingCache.containsKey(aHost + ":" + aPort)) {
portMappingCache.put(aHost + ":" + aPort, Integer.valueOf(m.group(1)));
eventPublisher.postUpdate(itemName, state);
} else {
int cachedValue = portMappingCache.get(aHost + ":" + aPort);
if (cachedValue != Integer.valueOf(m.group(1))) {
portMappingCache.put(aHost + ":" + aPort, Integer.valueOf(m.group(1)));
eventPublisher.postUpdate(itemName, state);
}
}
}
}
}
}
}
}
use of org.openhab.binding.hdanywhere.HDanywhereBindingProvider in project openhab1-addons by openhab.
the class HDanywhereBinding method execute.
@Override
protected void execute() {
if (isProperlyConfigured()) {
Scheduler sched = null;
try {
sched = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException e) {
logger.error("An exception occurred while getting a reference to the Quartz Scheduler");
}
for (HDanywhereBindingProvider provider : providers) {
HashMap<String, Integer> compiledList = provider.getIntervalList();
if (compiledList != null) {
Iterator<String> pbcIterator = compiledList.keySet().iterator();
while (pbcIterator.hasNext()) {
String aHost = pbcIterator.next();
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(aHost)) {
jobExists = true;
break;
}
}
}
} catch (SchedulerException e1) {
logger.error("An exception occurred while querying the Quartz Scheduler ({})", e1.getMessage());
}
if (!jobExists) {
// set up the Quartz jobs
JobDataMap map = new JobDataMap();
map.put("host", aHost);
map.put("binding", this);
JobDetail job = newJob(HDanywhereBinding.PollJob.class).withIdentity(aHost, "HDanywhere-" + provider.toString()).usingJobData(map).build();
Trigger trigger = newTrigger().withIdentity(aHost, "HDanywhere-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(compiledList.get(aHost))).build();
try {
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("An exception occurred while scheduling a Quartz Job");
}
}
}
}
}
}
}
Aggregations