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 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| Enhancement | `rules` | Display `execute` code of `JSRule` in MainUI | [#199](https://github.com/openhab/openhab-js/pull/199) | No |
| Enhancement | `time` | Support ISO8601 string parsing in `toZDT` | [#202](https://github.com/openhab/openhab-js/pull/202) | No |
| Enhancement | `time` | Add `isBetweenDates` & `isBetweenDateTimes` polyfills to `ZonedDateTime` | [#203](https://github.com/openhab/openhab-js/pull/203) | No |
| Enhancement | `items` | ItemHistory: ItemHistory: Add `previousStateTimestamp` method | [#205](https://github.com/openhab/openhab-js/pull/205) | No |

Also see the [Release Milestone](https://github.com/openhab/openhab-js/milestone/10).

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ Calling `Item.history` returns a `ItemHistory` object with the following functio
- .minimumSince(timestamp, serviceId) ⇒ `string | null`
- .persist(serviceId)
- .previousState(skipEqual, serviceId) ⇒ `string | null`
- .previousStateTimestamp(skipEqual, serviceId) ⇒ `time.ZonedDateTime | null`
- .sumBetween(begin, end, serviceId) ⇒ `number | null`
- .sumSince(timestamp, serviceId) ⇒ `number | null`
- .updatedBetween(begin, end, serviceId) ⇒ `boolean`
Expand Down
25 changes: 21 additions & 4 deletions items/item-history.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// const { ZonedDateTime } = require('@js-joda/core');
/** @typedef {import('@js-joda/core')} time */
const time = require('../time');
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 +222,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 +299,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 +383,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
5 changes: 3 additions & 2 deletions docs_config.json → jsdoc.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"excludePattern": "(node_modules/|docs|dist)"
},
"plugins": [
"plugins/markdown"
"plugins/markdown",
"node_modules/jsdoc-tsimport-plugin/index.js"
],
"templates": {
"cleverLinks": false,
Expand Down Expand Up @@ -47,4 +48,4 @@
"nameInOutputPath": false,
"versionInOutputPath": false
}
}
}
13 changes: 13 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test:mocha": "mocha test/**/*.test.js",
"test:jest": "jest",
"test": "npm run lint && npm run test:mocha && npm run test:jest",
"docs": "rm -Rf ./docs/* && jsdoc --configure docs_config.json && mv ./docs/$npm_package_name/$npm_package_version/* ./docs/ && rm -Rf ./docs/$npm_package_name/$npm_package_version",
"docs": "rm -Rf ./docs/* && jsdoc --configure jsdoc.conf.json && mv ./docs/$npm_package_name/$npm_package_version/* ./docs/ && rm -Rf ./docs/$npm_package_name/$npm_package_version",
"deploy": "npm test && npm run docs && npm run webpack",
"lint": "npx standardx | snazzy",
"lint:fix": "npx standardx --fix",
Expand All @@ -33,6 +33,7 @@
"docdash": "^1.2.0",
"jest": "^28.1.3",
"jsdoc": "^4.0.0",
"jsdoc-tsimport-plugin": "^1.0.5",
"mocha": "^6.2.2",
"proxyquire": "^2.1.3",
"rewiremock": "^3.13.9",
Expand Down
16 changes: 16 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,8 @@ declare class ItemHistory {
*/
private _decimalOrNull;
}
declare namespace ItemHistory {
export { time };
}
type time = typeof JSJoda;
//# 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.