Package org.openhab.core.persistence

Examples of org.openhab.core.persistence.HistoricItem


    if(endValue<=startValue || startValue<1950) return Collections.emptyList();
   
    ArrayList<HistoricItem> results = new ArrayList<HistoricItem>(endValue - startValue);
    for(int i = startValue; i <= endValue; i++) {
      final int year = i;
      results.add(new HistoricItem() {
        public Date getTimestamp() {
          return new Date(year-1900, 0, 1);
        }
       
        @Override
View Full Code Here


    ext.removePersistenceService(testPersistenceService)
  }
 
  @Test
  public void testHistoricState() {
    HistoricItem historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2012, 1, 1), "test");
    assertEquals("2012", historicItem.getState().toString());

    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 12, 31), "test");
    assertEquals("2011", historicItem.getState().toString());

    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 1, 1), "test");
    assertEquals("2011", historicItem.getState().toString());

    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2000, 1, 1), "test");
    assertEquals("2000", historicItem.getState().toString());
  }
View Full Code Here

  }

  @Test
  public void testMinimumSince() {
    item.setState(new DecimalType(5000));
    HistoricItem historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(1940, 1, 1), "test");
    assertNotNull(historicItem);
    assertEquals("5000", historicItem.getState().toString());
   
    historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(2005, 1, 1), "test");
    assertEquals("2005", historicItem.getState().toString());
    assertEquals(new DateMidnight(2005, 1, 1).toDate(), historicItem.getTimestamp());
  }
View Full Code Here

  }

  @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());
  }
View Full Code Here

          if(filter.getOrdering()==Ordering.DESCENDING && filter.getPageSize()==1 && filter.getPageNumber()==0) {
            if(filter.getEndDate()==null) {
              // we are asked only for the most recent value!
              double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
              if(!Double.isNaN(lastValue)) {
                HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, itemName), new Date(db.getLastArchiveUpdateTime() * 1000));
                return Collections.singletonList(rrd4jItem);
              } else {
                return Collections.emptyList();
              }
            } else {
View Full Code Here

                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);
              }
View Full Code Here

   * @return true, if item state had changed
   */
  static public Boolean changedSince(Item item, AbstractInstant timestamp, String serviceName) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem itemThen = historicState(item, timestamp);
    if(itemThen == null) {
      // Can't get the state at the start time
      // 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;
  }
View Full Code Here

   * @return a historic item with the maximum state value since the given point in time
   */
  static public HistoricItem maximumSince(final Item item, AbstractInstant timestamp, String serviceName) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem maximumHistoricItem = null;
    DecimalType maximum = (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(maximum==null || value.compareTo(maximum)>0) {
          maximum = value;
          maximumHistoricItem = historicItem;
        }
      }
    }
    if(maximumHistoricItem==null && maximum!=null) {
      // the maximum state is the current one, so construct a historic item on the fly
      final DecimalType state = maximum;
      return new HistoricItem() {
       
        public Date getTimestamp() {
          return Calendar.getInstance().getTime();
        }
       
View Full Code Here

   * @return the historic item with the minimum state value since the given point in time
   */
  static public HistoricItem minimumSince(final Item item, AbstractInstant timestamp, String serviceName) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceName);
    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() {
       
        public Date getTimestamp() {
          return Calendar.getInstance().getTime();
        }
       
View Full Code Here

   * @param the point in time to start the check
   * @param serviceName the name of the {@link PersistenceService} to use
   * @return the difference between now and then, null if not calculable
   */
  static public DecimalType deltaSince(Item item, AbstractInstant timestamp, String serviceName) {
    HistoricItem itemThen = historicState(item, timestamp);
    DecimalType valueThen = (DecimalType) itemThen.getState();
    DecimalType valueNow = (DecimalType) item.getStateAs(DecimalType.class);
    DecimalType result = null;
    if (( valueThen != null) && ( valueNow != null)) {
      result = new DecimalType(valueNow.doubleValue() - valueThen.doubleValue());
    };
View Full Code Here

TOP

Related Classes of org.openhab.core.persistence.HistoricItem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.