Search in sources :

Example 1 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceManagerImpl method initialize.

/**
 * Handles the "restoreOnStartup" strategy for the item.
 * If the item state is still undefined when entering this method, all persistence configurations are checked,
 * if they have the "restoreOnStartup" strategy configured for the item. If so, the item state will be set
 * to its last persisted value.
 *
 * @param item the item to restore the state for
 */
private void initialize(Item item) {
    // get the last persisted state from the persistence service if no state is yet set
    if (item.getState().equals(UnDefType.NULL) && item instanceof GenericItem) {
        for (Entry<String, PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
            final String serviceName = entry.getKey();
            final PersistenceServiceConfiguration config = entry.getValue();
            for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                if (hasStrategy(serviceName, itemConfig, SimpleStrategy.Globals.RESTORE)) {
                    if (appliesToItem(itemConfig, item)) {
                        PersistenceService service = persistenceServices.get(serviceName);
                        if (service instanceof QueryablePersistenceService) {
                            QueryablePersistenceService queryService = (QueryablePersistenceService) service;
                            FilterCriteria filter = new FilterCriteria().setItemName(item.getName()).setPageSize(1);
                            Iterable<HistoricItem> result = queryService.query(filter);
                            Iterator<HistoricItem> it = result.iterator();
                            if (it.hasNext()) {
                                HistoricItem historicItem = it.next();
                                GenericItem genericItem = (GenericItem) item;
                                genericItem.removeStateChangeListener(this);
                                genericItem.setState(historicItem.getState());
                                genericItem.addStateChangeListener(this);
                                logger.debug("Restored item state from '{}' for item '{}' -> '{}'", new Object[] { DateFormat.getDateTimeInstance().format(historicItem.getTimestamp()), item.getName(), historicItem.getState().toString() });
                                return;
                            }
                        } else if (service != null) {
                            logger.warn("Failed to restore item states as persistence service '{}' can not be queried.", serviceName);
                        }
                    }
                }
            }
        }
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) GenericItem(org.eclipse.smarthome.core.items.GenericItem) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 2 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensionsTest method testPreviousStateSkip.

@Test
public void testPreviousStateSkip() {
    item.setState(new DecimalType(2012));
    HistoricItem prevStateItem = PersistenceExtensions.previousState(item, true, "test");
    assertNotNull(prevStateItem);
    assertEquals("2011", prevStateItem.getState().toString());
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 3 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensionsTest method testMaximumSince.

@Test
public void testMaximumSince() {
    item.setState(new DecimalType(1));
    HistoricItem historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2012, 1, 1), "test");
    assertNotNull(historicItem);
    assertEquals("1", historicItem.getState().toString());
    historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2005, 1, 1), "test");
    assertEquals("2012", historicItem.getState().toString());
    assertEquals(new DateMidnight(2012, 1, 1).toDate(), historicItem.getTimestamp());
}
Also used : DateMidnight(org.joda.time.DateMidnight) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 4 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensions method changedSince.

/**
 * Checks if the state of a given <code>item</code> has changed since a certain point in time.
 * The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to check for state changes
 * @param timestamp the point in time to start the check
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return <code>true</code> if item state has changed, or <code>false</code> if it hasn't or if the given
 *         <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
 */
public static Boolean changedSince(Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem itemThen = historicState(item, timestamp);
    if (itemThen == null) {
        // If we've got results more recent that this, it must have changed
        return it.hasNext();
    }
    State state = itemThen.getState();
    while (it.hasNext()) {
        HistoricItem hItem = it.next();
        if (state != null && !hItem.getState().equals(state)) {
            return true;
        }
        state = hItem.getState();
    }
    return false;
}
Also used : State(org.eclipse.smarthome.core.types.State) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 5 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensions method minimumSince.

/**
 * Gets the historic item with the minimum value of the state of a given <code>item</code> since
 * a certain point in time. The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to get the minimum state value for
 * @param timestamp the point in time from which to search for the minimum state value
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the historic item with the minimum state value since the given point in time, or a {@link HistoricItem}
 *         constructed from the <code>item</code>'s state if <code>item</code>'s state is the minimum value or if
 *         the given <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
 */
public static HistoricItem minimumSince(final Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem minimumHistoricItem = null;
    DecimalType minimum = (DecimalType) item.getStateAs(DecimalType.class);
    while (it.hasNext()) {
        HistoricItem historicItem = it.next();
        State state = historicItem.getState();
        if (state instanceof DecimalType) {
            DecimalType value = (DecimalType) state;
            if (minimum == null || value.compareTo(minimum) < 0) {
                minimum = value;
                minimumHistoricItem = historicItem;
            }
        }
    }
    if (minimumHistoricItem == null && minimum != null) {
        // the minimal state is the current one, so construct a historic item on the fly
        final DecimalType state = minimum;
        return new HistoricItem() {

            @Override
            public Date getTimestamp() {
                return Calendar.getInstance().getTime();
            }

            @Override
            public State getState() {
                return state;
            }

            @Override
            public String getName() {
                return item.getName();
            }
        };
    } else {
        return minimumHistoricItem;
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Aggregations

HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)20 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)11 State (org.eclipse.smarthome.core.types.State)9 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)6 PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)5 QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)5 Test (org.junit.Test)5 ZonedDateTime (java.time.ZonedDateTime)3 Date (java.util.Date)3 DateMidnight (org.joda.time.DateMidnight)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)2 OpenClosedType (org.eclipse.smarthome.core.library.types.OpenClosedType)2 DateTime (org.joda.time.DateTime)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1