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 previousStateTimestamp method #205

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 21 additions & 5 deletions items/item-history.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// const { ZonedDateTime } = require('@js-joda/core');
const time = require('../time');
const time = require('@js-joda/core');
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
const PersistenceExtensions = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');
const DateTime = Java.type('java.time.ZonedDateTime');

/**
* Class representing the historic state of an openHAB Item.
Expand Down Expand Up @@ -223,7 +221,7 @@ class ItemHistory {
* @returns {(string | null)} state
*/
latestState (serviceId) {
return this.historicState(DateTime.now(), ...arguments);
return this.historicState(time.ZonedDateTime.now(), ...arguments);
}

/**
Expand Down Expand Up @@ -300,6 +298,17 @@ class ItemHistory {
return this._stateOrNull(PersistenceExtensions.previousState(this.rawItem, ...arguments));
}

/**
* Returns the time when the previous state of a given Item was persisted.
*
* @param {boolean} [skipEqual] optional, if true, skips equal state values and searches the first state not equal the current state
* @param {string} [serviceId] Optional persistence service ID, if omitted, the default persistence service will be used.
* @returns {(time.ZonedDateTime | null)} {@link time.ZonedDateTime} or null
*/
previousStateTimestamp (skipEqual, serviceId) {
return this._timestampOrNull(PersistenceExtensions.previousState(this.rawItem, ...arguments));
}

/**
* Gets the sum of the states of a given Item between two certain points in time.
*
Expand Down Expand Up @@ -373,7 +382,14 @@ class ItemHistory {
* @private
*/
_stateOrNull (result) {
return result === null ? null : result.state.toString();
return result === null ? null : result.getState().toString();
}

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

/**
Expand Down
13 changes: 13 additions & 0 deletions types/items/item-history.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ declare class ItemHistory {
* @returns {(string | null)} state or null
*/
previousState(skipEqual?: boolean, serviceId?: string, ...args: any[]): (string | null);
/**
* Returns the time when the previous state of a given Item was persisted.
*
* @param {boolean} [skipEqual] optional, if true, skips equal state values and searches the first state not equal the current state
* @param {string} [serviceId] Optional persistence service ID, if omitted, the default persistence service will be used.
* @returns {(time.ZonedDateTime | null)} {@link time.ZonedDateTime} or null
*/
previousStateTimestamp(skipEqual?: boolean, serviceId?: string, ...args: any[]): (time.ZonedDateTime | null);
/**
* Gets the sum of the states of a given Item between two certain points in time.
*
Expand Down Expand Up @@ -268,6 +276,10 @@ declare class ItemHistory {
* @private
*/
private _stateOrNull;
/**
* @private
*/
private _timestampOrNull;
/**
* @private
*/
Expand All @@ -277,4 +289,5 @@ declare class ItemHistory {
*/
private _decimalOrNull;
}
import time = require("@js-joda/core");
//# sourceMappingURL=item-history.d.ts.map
2 changes: 1 addition & 1 deletion types/items/item-history.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.