Example usage for org.joda.time.base AbstractInstant toDate

List of usage examples for org.joda.time.base AbstractInstant toDate

Introduction

In this page you can find the example usage for org.joda.time.base AbstractInstant toDate.

Prototype

public Date toDate() 

Source Link

Document

Get the date time as a java.util.Date.

Usage

From source file:org.eclipse.smarthome.core.persistence.extensions.PersistenceExtensions.java

License:Open Source License

/**
 * Retrieves the state of a given <code>item</code> to a certain point in time through  a {@link PersistenceService} identified
 * by the <code>serviceName</code>. 
 * //from  w w  w.j a v a2s  .co  m
 * @param item the item to retrieve the state for
 * @param the point in time for which the state should be retrieved 
 * @param serviceName the name of the {@link PersistenceService} to use
 * @return the item state at the given point in time
 */
static public HistoricItem historicState(Item item, AbstractInstant timestamp, String serviceName) {
    PersistenceService service = services.get(serviceName);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setEndDate(timestamp.toDate());
        filter.setItemName(item.getName());
        filter.setPageSize(1);
        filter.setOrdering(Ordering.DESCENDING);
        Iterable<HistoricItem> result = qService.query(filter);
        if (result.iterator().hasNext()) {
            return result.iterator().next();
        } else {
            return null;
        }
    } else {
        logger.warn("There is no queryable persistence service registered with the name '{}'", serviceName);
        return null;
    }
}

From source file:org.eclipse.smarthome.core.persistence.extensions.PersistenceExtensions.java

License:Open Source License

static private Iterable<HistoricItem> getAllStatesSince(Item item, AbstractInstant timestamp,
        String serviceName) {/* ww w  .  ja v  a  2  s  .  c  o m*/
    PersistenceService service = services.get(serviceName);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setBeginDate(timestamp.toDate());
        filter.setItemName(item.getName());
        filter.setOrdering(Ordering.ASCENDING);
        return qService.query(filter);
    } else {
        logger.warn("There is no queryable persistence service registered with the name '{}'", serviceName);
        return Collections.emptySet();
    }
}

From source file:org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.java

License:Open Source License

/**
 * Retrieves the state of a given <code>item</code> to a certain point in time through a {@link PersistenceService}
 * identified//from  www  . j a va  2  s  .  c o  m
 * by the <code>serviceName</code>.
 * 
 * @param item the item to retrieve the state for
 * @param the point in time for which the state should be retrieved
 * @param serviceName the name of the {@link PersistenceService} to use
 * @return the item state at the given point in time
 */
static public HistoricItem historicState(Item item, AbstractInstant timestamp, String serviceName) {
    PersistenceService service = services.get(serviceName);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setEndDate(timestamp.toDate());
        filter.setItemName(item.getName());
        filter.setPageSize(1);
        filter.setOrdering(Ordering.DESCENDING);
        Iterable<HistoricItem> result = qService.query(filter);
        if (result.iterator().hasNext()) {
            return result.iterator().next();
        } else {
            return null;
        }
    } else {
        LoggerFactory.getLogger(PersistenceExtensions.class)
                .warn("There is no queryable persistence service registered with the name '{}'", serviceName);
        return null;
    }
}

From source file:org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.java

License:Open Source License

static private Iterable<HistoricItem> getAllStatesSince(Item item, AbstractInstant timestamp,
        String serviceName) {/*from   w  w  w  . j  av  a2  s. c o  m*/
    PersistenceService service = services.get(serviceName);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setBeginDate(timestamp.toDate());
        filter.setItemName(item.getName());
        filter.setOrdering(Ordering.ASCENDING);
        return qService.query(filter);
    } else {
        LoggerFactory.getLogger(PersistenceExtensions.class)
                .warn("There is no queryable persistence service registered with the name '{}'", serviceName);
        return Collections.emptySet();
    }
}

From source file:org.eclipse.smarthome.model.script.actions.ScriptExecution.java

License:Open Source License

/**
 * Schedules a block of code for later execution.
 * //from   www. j  av a 2s  . co  m
 * @param instant the point in time when the code should be executed
 * @param closure the code block to execute
 * 
 * @return a handle to the created timer, so that it can be canceled or rescheduled
 * @throws ScriptExecutionException if an error occurs during the execution
 */
public static Timer createTimer(AbstractInstant instant, Procedure0 closure) {
    Logger logger = LoggerFactory.getLogger(ScriptExecution.class);
    JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString());
    Trigger trigger = newTrigger().startAt(instant.toDate()).build();
    Timer timer = new TimerImpl(jobKey, trigger.getKey(), instant);
    try {
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("procedure", closure);
        dataMap.put("timer", timer);
        JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build();
        TimerImpl.scheduler.scheduleJob(job, trigger);
        logger.debug("Scheduled code for execution at {}", instant.toString());
        return timer;
    } catch (SchedulerException e) {
        logger.error("Failed to schedule code for execution.", e);
        return null;
    }
}

From source file:org.eclipse.smarthome.model.script.internal.actions.TimerImpl.java

License:Open Source License

@Override
public boolean reschedule(AbstractInstant newTime) {
    try {//from  ww  w .j  av  a2 s  .  co m
        Trigger trigger = newTrigger().startAt(newTime.toDate()).build();
        scheduler.rescheduleJob(triggerKey, trigger);
        this.triggerKey = trigger.getKey();
        this.cancelled = false;
        this.terminated = false;
        return true;
    } catch (SchedulerException e) {
        logger.warn("An error occured while rescheduling the job '{}': {}",
                new String[] { jobKey.toString(), e.getMessage() });
        return false;
    }
}

From source file:org.openhab.core.jsr223.internal.actions.Timer.java

License:Open Source License

public boolean reschedule(AbstractInstant newTime) {
    try {/* w  w w . j  a va 2  s  .  c  om*/
        Trigger trigger = newTrigger().startAt(newTime.toDate()).build();
        Date nextTriggerTime = scheduler.rescheduleJob(triggerKey, trigger);
        if (nextTriggerTime != null) {
            this.triggerKey = trigger.getKey();
            this.cancelled = false;
            this.terminated = false;
            return true;
        }
    } catch (SchedulerException e) {
        logger.warn("An error occured while rescheduling the job '{}': {}",
                new String[] { jobKey.toString(), e.getMessage() });
    }
    return false;
}

From source file:org.openhab.core.jsr223.internal.actions.TimerImpl.java

License:Open Source License

public boolean reschedule(AbstractInstant newTime) {
    try {//from  www  . j a v a 2 s.  co  m
        Trigger trigger = newTrigger().startAt(newTime.toDate()).build();
        scheduler.rescheduleJob(triggerKey, trigger);
        this.triggerKey = trigger.getKey();
        this.cancelled = false;
        this.terminated = false;
        return true;
    } catch (SchedulerException e) {
        logger.warn("An error occured while rescheduling the job '{}': {}",
                new String[] { jobKey.toString(), e.getMessage() });
        return false;
    }
}

From source file:org.openhab.core.jsr223.internal.shared.Openhab.java

License:Open Source License

private static Timer makeTimer(AbstractInstant instant, String closure, JobDataMap dataMap) {
    JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString());
    Trigger trigger = newTrigger().startAt(instant.toDate()).build();
    Timer timer = new Timer(jobKey, trigger.getKey(), instant);
    dataMap.put("timer", timer);
    try {/* w  w  w . ja v  a  2 s .  com*/
        JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build();
        Timer.scheduler.scheduleJob(job, trigger);
        logger.debug("Scheduled code for execution at {}", instant.toString());
        return timer;
    } catch (SchedulerException e) {
        logger.error("Failed to schedule code for execution.", e);
        return null;
    }
}

From source file:org.openhab.model.script.actions.ScriptExecution.java

License:Open Source License

/**
 * helper function to create the timer/*from  w  w w .  j a va2  s  .c  o  m*/
 * @param instant the point in time when the code should be executed
 * @param closure string for job id
 * @param dataMap job data map, preconfigured with arguments
 * @return
 */

private static Timer makeTimer(AbstractInstant instant, String closure, JobDataMap dataMap) {
    JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString());
    Trigger trigger = newTrigger().startAt(instant.toDate()).build();
    Timer timer = new TimerImpl(jobKey, trigger.getKey(), instant);
    dataMap.put("timer", timer);
    try {
        JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build();
        TimerImpl.scheduler.scheduleJob(job, trigger);
        logger.debug("Scheduled code for execution at {}", instant.toString());
        return timer;
    } catch (SchedulerException e) {
        logger.error("Failed to schedule code for execution.", e);
        return null;
    }
}