use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class RuleTriggerManager method internalGetChangeRules.
private void internalGetChangeRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes, State newState, State oldState, List<Rule> result) {
final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
for (Rule rule : getAllRules(CHANGE, mapName)) {
for (EventTrigger t : rule.getEventtrigger()) {
String triggerOldStateString = null;
String triggerNewStateString = null;
if ((!isGroup) && (t instanceof ChangedEventTrigger)) {
final ChangedEventTrigger ct = (ChangedEventTrigger) t;
if (ct.getItem().equals(name)) {
triggerOldStateString = ct.getOldState();
triggerNewStateString = ct.getNewState();
} else {
continue;
}
} else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
if (gmct.getGroup().equals(name)) {
triggerOldStateString = gmct.getOldState();
triggerNewStateString = gmct.getNewState();
} else {
continue;
}
} else {
continue;
}
if (triggerOldStateString != null) {
final State triggerOldState = TypeParser.parseState(acceptedDataTypes, triggerOldStateString);
if (!oldState.equals(triggerOldState)) {
continue;
}
}
if (triggerNewStateString != null) {
final State triggerNewState = TypeParser.parseState(acceptedDataTypes, triggerNewStateString);
if (!newState.equals(triggerNewState)) {
continue;
}
}
result.add(rule);
}
}
}
use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class TestPersistenceService method query.
@SuppressWarnings("deprecation")
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
int startValue = 1950;
int endValue = 2012;
if (filter.getBeginDateZoned() != null) {
startValue = filter.getBeginDateZoned().getYear();
}
if (filter.getEndDateZoned() != null) {
endValue = filter.getEndDateZoned().getYear();
}
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() {
@Override
public Date getTimestamp() {
return new Date(year - 1900, 0, 1);
}
@Override
public State getState() {
return new DecimalType(year);
}
@Override
public String getName() {
return "Test";
}
});
}
if (filter.getOrdering() == Ordering.DESCENDING) {
Collections.reverse(results);
}
return results;
}
use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class PersistenceExtensions method sumSince.
/**
* Gets the sum 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 for which we will sum its persisted state values since <code>timestamp</code>
* @param timestamp the point in time from which to start the summation
* @param serviceId the name of the {@link PersistenceService} to use
* @return the sum of the state values since the given point in time, or {@link DecimalType.ZERO} if no historic
* states could be found for the <code>item</code> or if <code>serviceId</code> does no refer to a
* {@link QueryablePersistenceService}
*/
public static DecimalType sumSince(Item item, AbstractInstant timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
BigDecimal sum = BigDecimal.ZERO;
while (it.hasNext()) {
State state = it.next().getState();
if (state instanceof DecimalType) {
sum = sum.add(((DecimalType) state).toBigDecimal());
}
}
return new DecimalType(sum);
}
use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class PersistenceExtensions method maximumSince.
/**
* Gets the historic item with the maximum 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 maximum state value for
* @param timestamp the point in time to start the check
* @param serviceId the name of the {@link PersistenceService} to use
* @return a {@link HistoricItem} with the maximum 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
* maximum value or if the given <code>serviceId</code> does not refer to an available
* {@link QueryablePersistenceService}
*/
public static HistoricItem maximumSince(final Item item, AbstractInstant timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
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() {
@Override
public Date getTimestamp() {
return Calendar.getInstance().getTime();
}
@Override
public State getState() {
return state;
}
@Override
public String getName() {
return item.getName();
}
};
} else {
return maximumHistoricItem;
}
}
use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class PersistenceExtensions method averageSince.
/**
* Gets the average 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 average state value for
* @param timestamp the point in time from which to search for the average state value
* @param serviceId the name of the {@link PersistenceService} to use
* @return the average state values since <code>timestamp</code>, or the state of the given <code>item</code> if no
* previous states could be found or if the persistence service given by <code>serviceId</code> does not
* refer to an available {@link QueryablePersistenceService}
*/
public static DecimalType averageSince(Item item, AbstractInstant timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
BigDecimal total = BigDecimal.ZERO;
BigDecimal avgValue, timeSpan;
DecimalType lastState = null, thisState = null;
BigDecimal lastTimestamp = null, thisTimestamp = null;
BigDecimal firstTimestamp = null;
while (it.hasNext()) {
HistoricItem thisItem = it.next();
State state = thisItem.getState();
if (state instanceof DecimalType) {
thisState = (DecimalType) state;
thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().getTime());
if (firstTimestamp == null) {
firstTimestamp = thisTimestamp;
} else {
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
timeSpan = thisTimestamp.subtract(lastTimestamp);
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
}
lastTimestamp = thisTimestamp;
lastState = thisState;
}
}
if (lastState != null) {
thisState = (DecimalType) item.getStateAs(DecimalType.class);
thisTimestamp = BigDecimal.valueOf((new DateTime()).getMillis());
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
timeSpan = thisTimestamp.subtract(lastTimestamp);
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
}
if (thisTimestamp != null) {
timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64);
BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64);
return new DecimalType(average);
} else {
return null;
}
}
Aggregations