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);
}
}
}
}
}
}
}
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());
}
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());
}
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;
}
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;
}
}
Aggregations