Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[items] ItemHistory: Add getAllStatesBetween & getAllStatesSince methods #309

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 105 additions & 68 deletions items/item-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,81 @@ class HistoricItem {
* @param {*} rawHistoricItem {@link https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/historicitem org.openhab.core.persistence.HistoricItem}
*/
constructor (rawHistoricItem) {
this.rawHistoricItem = rawHistoricItem;
/**
* Raw Java Item state
* @type {HostState}
*/
this.rawState = rawHistoricItem.getState();
/**
* Item state
* @type {string}
*/
this.state = this.rawState.toString();
const numeric = parseFloat(this.state.toString());
/**
* Numeric representation of Item state, or `null` if state is not numeric
* @type {number|null}
*/
this.numericState = isNaN(numeric) ? null : numeric;
let quantity = null;
}

/**
* String representation of the Item state.
* @type {string}
*/
get state () {
return this.rawState.toString();
}

/**
* Numeric representation of Item state, or `null` if state is not numeric
* @type {number|null}
*/
get numericState () {
const numericState = parseFloat(this.rawState.toString());
return isNaN(numericState) ? null : numericState;
}

/**
* Item state as {@link Quantity} or `null` if state is not Quantity-compatible or Quantity would be unit-less (without unit)
* @type {Quantity|null}
*/
get quantityState () {
try {
quantity = getQuantity(this.state);
const qty = getQuantity(this.rawState.toString());
return (qty !== null && qty.symbol !== null) ? qty : null;
} catch (e) {
if (!(e instanceof QuantityError)) {
if (e instanceof QuantityError) {
return null;
} else {
throw Error('Failed to create "quantityState": ' + e);
}
}
/**
* Item state as {@link Quantity} or `null` if state is not Quantity-compatible
* @type {Quantity|null}
*/
this.quantityState = quantity;
/**
* timestamp of persisted Item
* @type {time.ZonedDateTime}
*/
this.timestamp = utils.javaZDTToJsZDT(rawHistoricItem.getTimestamp());
}

/**
* Timestamp of persisted Item.
* @type {time.ZonedDateTime}
*/
get timestamp () {
return utils.javaZDTToJsZDT(this.rawHistoricItem.getTimestamp());
}
}

function _ZDTOrNull (result) {
return result === null ? null : time.ZonedDateTime.parse(result.toString());
}

function _decimalOrNull (result) {
return result === null ? null : result.toBigDecimal();
}

function _historicItemOrNull (result) {
if (result === null) return null;
return new HistoricItem(result);
}

function _javaIterableOfJavaHistoricItemsToJsArrayOfHistoricItems (result) {
if (result === null) return null;

const historicItems = [];
result.forEach((hi) => {
const historicItem = _historicItemOrNull(hi);
if (historicItem !== null) historicItems.push(historicItem);
});
return historicItems;
}

/**
* Class representing the historic state of an openHAB Item.
* If the Item receives its state from a binding that supports units of measurement, the returned state is in the according base unit, otherwise there is no unit conversion happening.
Expand All @@ -83,7 +121,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
averageBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.averageBetween(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.averageBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -99,7 +137,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
averageSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.averageSince(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.averageSince(this.rawItem, ...arguments));
}

/**
Expand Down Expand Up @@ -180,7 +218,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
deltaBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.deltaBetween(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.deltaBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -191,7 +229,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
deltaSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.deltaSince(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.deltaSince(this.rawItem, ...arguments));
}

/**
Expand All @@ -203,7 +241,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
deviationBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.deviationBetween(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.deviationBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -214,7 +252,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
deviationSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.deviationSince(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.deviationSince(this.rawItem, ...arguments));
}

/**
Expand All @@ -227,7 +265,7 @@ class ItemHistory {
*/
evolutionRate (timestamp, serviceId) {
console.warn('"evolutionRate" is deprecated and will be removed in the future. Use "evolutionRateSince" instead.');
return this._decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
}

/**
Expand All @@ -239,7 +277,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
evolutionRateBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
}

/**
Expand All @@ -250,7 +288,30 @@ class ItemHistory {
* @returns {(number | null)}
*/
evolutionRateSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.evolutionRate(this.rawItem, ...arguments));
}

/**
* Retrieves the {@link HistoricItems} for for a given Item between two certain points in time.
*
* @param {(time.ZonedDateTime | Date)} begin begin
* @param {(time.ZonedDateTime | Date)} end end
* @param {string} [serviceId] Optional persistence service ID, if omitted, the default persistence service will be used.
* @returns {HistoricItem[]}
*/
getAllStatesBetween (begin, end, serviceId) {
return _javaIterableOfJavaHistoricItemsToJsArrayOfHistoricItems(PersistenceExtensions.getAllStatesBetween(this.rawItem, ...arguments));
}

/**
* Retrieves the {@link HistoricItems} for for a given Item since a certain point in time.
*
* @param {(time.ZonedDateTime | Date)} timestamp
* @param {string} [serviceId] Optional persistence service ID, if omitted, the default persistence service will be used.
* @returns {HistoricItem[]}
*/
getAllStatesSince (timestamp, serviceId) {
return _javaIterableOfJavaHistoricItemsToJsArrayOfHistoricItems(PersistenceExtensions.getAllStatesSince(this.rawItem, ...arguments));
}

/**
Expand All @@ -261,7 +322,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item
*/
historicState (timestamp, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.historicState(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.historicState(this.rawItem, ...arguments));
}

/**
Expand All @@ -271,7 +332,7 @@ class ItemHistory {
* @returns {(time.ZonedDateTime | null)}
*/
lastUpdate (serviceId) {
return this._dateOrNull(PersistenceExtensions.lastUpdate(this.rawItem, ...arguments));
return _ZDTOrNull(PersistenceExtensions.lastUpdate(this.rawItem, ...arguments));
}

/**
Expand All @@ -294,7 +355,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item or null
*/
maximumBetween (begin, end, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.maximumBetween(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.maximumBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -305,7 +366,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item or null
*/
maximumSince (timestamp, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.maximumSince(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.maximumSince(this.rawItem, ...arguments));
}

/**
Expand All @@ -317,7 +378,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item or null
*/
minimumBetween (begin, end, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.minimumBetween(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.minimumBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -328,7 +389,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item or null
*/
minimumSince (timestamp, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.minimumSince(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.minimumSince(this.rawItem, ...arguments));
}

/**
Expand All @@ -348,7 +409,7 @@ class ItemHistory {
* @returns {(HistoricItem | null)} historic item or null
*/
previousState (skipEqual, serviceId) {
return this._historicItemOrNull(PersistenceExtensions.previousState(this.rawItem, ...arguments));
return _historicItemOrNull(PersistenceExtensions.previousState(this.rawItem, ...arguments));
}

/**
Expand All @@ -360,7 +421,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
sumBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.sumBetween(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.sumBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -371,7 +432,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
sumSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.sumSince(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.sumSince(this.rawItem, ...arguments));
}

/**
Expand Down Expand Up @@ -406,7 +467,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
varianceBetween (begin, end, serviceId) {
return this._decimalOrNull(PersistenceExtensions.varianceBetween(this.rawItem, ...arguments));
return _decimalOrNull(PersistenceExtensions.varianceBetween(this.rawItem, ...arguments));
}

/**
Expand All @@ -417,31 +478,7 @@ class ItemHistory {
* @returns {(number | null)}
*/
varianceSince (timestamp, serviceId) {
return this._decimalOrNull(PersistenceExtensions.varianceSince(this.rawItem, ...arguments));
}

/**
* @private
*/
_dateOrNull (result) {
return result === null ? null : time.ZonedDateTime.parse(result.toString());
}

/**
* @private
*/
_decimalOrNull (result) {
return result === null ? null : result.toBigDecimal();
}

/**
* @private
*/
_historicItemOrNull (result) {
if (result === null) {
return null;
}
return new HistoricItem(result);
return _decimalOrNull(PersistenceExtensions.varianceSince(this.rawItem, ...arguments));
}
}

Expand Down
Loading
Loading