use of org.openhab.binding.plugwise.internal.PlugwiseGenericBindingProvider.PlugwiseBindingConfigElement in project openhab1-addons by openhab.
the class PlugwiseBinding method scheduleJobs.
private void scheduleJobs(Scheduler scheduler) {
for (PlugwiseBindingProvider provider : providers) {
for (PlugwiseBindingConfigElement element : provider.getIntervalList()) {
PlugwiseCommandType type = element.getCommandType();
if (type.getJobClass() == null) {
continue;
}
if (stick.getDevice(element.getId()) == null) {
logger.debug("The Plugwise device with id {} is not yet defined", element.getId());
// check if the config string really contains a MAC address
Pattern MAC_PATTERN = Pattern.compile("(\\w{16})");
Matcher matcher = MAC_PATTERN.matcher(element.getId());
if (matcher.matches()) {
List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
if (!cps.isEmpty()) {
CirclePlus cp = cps.get(0);
if (!cp.getMAC().equals(element.getId())) {
// a circleplus has been added/detected and it is not what is in the binding config
PlugwiseDevice device = new Circle(element.getId(), stick, element.getId());
stick.addDevice(device);
logger.debug("Plugwise added Circle with MAC address: {}", element.getId());
}
} else {
logger.warn("Plugwise can not guess the device that should be added. Consider defining it in the openHAB configuration file");
}
} else {
logger.warn("Plugwise can not add a valid device without a proper MAC address. {} can not be used", element.getId());
}
}
if (stick.getDevice(element.getId()) != null) {
String jobName = element.getId() + "-" + type.getJobClass().toString();
if (!isExistingJob(scheduler, jobName)) {
// set up the Quartz jobs
JobDataMap map = new JobDataMap();
map.put(STICK_JOB_DATA_KEY, stick);
map.put(MAC_JOB_DATA_KEY, stick.getDevice(element.getId()).MAC);
JobDetail job = newJob(type.getJobClass()).withIdentity(jobName, "Plugwise-" + provider.toString()).usingJobData(map).build();
Trigger trigger = newTrigger().withIdentity(element.getId() + "-" + type.getJobClass().toString(), "Plugwise-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(element.getInterval())).build();
try {
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("An exception occurred while scheduling a Plugwise Quartz Job", e);
}
}
} else {
logger.error("Error scheduling a Quartz Job for a non-defined Plugwise device (" + element.getId() + ")");
}
}
}
List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
if (!cps.isEmpty()) {
CirclePlus cp = cps.get(0);
String jobName = cp.MAC + "-SetCirclePlusClock";
if (!isExistingJob(scheduler, jobName)) {
JobDataMap map = new JobDataMap();
map.put(CirclePlus.CIRCLE_PLUS_JOB_DATA_KEY, cp);
JobDetail job = newJob(SetClockJob.class).withIdentity(jobName, "Plugwise").usingJobData(map).build();
CronTrigger trigger = newTrigger().withIdentity(jobName, "Plugwise").startNow().withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
try {
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("Error scheduling Circle+ setClock Quartz Job", e);
}
}
}
}
Aggregations