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] Refactor item-persistence.js exports & Refactor _toOpenhabString to helpers #337

Merged
merged 2 commits into from
Jun 5, 2024
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
40 changes: 38 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,47 @@ const utils = require('./utils');
const javaZDT = Java.type('java.time.ZonedDateTime');
const javaDuration = Java.type('java.time.Duration');

/**
* @typedef { import("./items/items").Item } Item
* @private
*/

/**
* Returns the name of a given Item or relays the passed in Item name.
*
* @param {Item|string} itemOrName Item or Item name
* @returns {string} the Item name
* @private
*/
function _getItemName (itemOrName) {
// Somehow instanceof check doesn't work here, so workaround the problem
if (itemOrName.rawItem !== undefined) return itemOrName.name;
if (_isItem(itemOrName)) return itemOrName.name;
return itemOrName;
}

/**
* Helper function to convert JS types to a
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/types/type org.openhab.core.types.Type} or a valid string representation of a type.
*
* Number and string are passed through.
* Other objects should implement <code>toOpenHabString</code> (prioritized) or <code>toString</code> to return an openHAB compatible representation.
*
* @private
* @param {*} value
* @returns {*}
*/
function _toOpenhabType (value) {
if (value === null) return 'NULL';
if (value === undefined) return 'UNDEF';
if (typeof value === 'number' || typeof value === 'string') {
return value;
} else if (typeof value.toOpenHabString === 'function') {
return value.toOpenHabString();
} else if (typeof value.toString === 'function') {
return value.toString();
}
return value;
}

/**
* Checks whether the given object is an instance of {@link items.Item}.
*
Expand Down Expand Up @@ -72,6 +107,7 @@ function _isDuration (o) {

module.exports = {
_getItemName,
_toOpenhabType,
_isItem,
_isQuantity,
_isZonedDateTime,
Expand Down
5 changes: 1 addition & 4 deletions src/items/item-persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,4 @@ class ItemPersistence {
}
}

module.exports = {
ItemPersistence,
PersistedItem
};
module.exports = ItemPersistence;
37 changes: 8 additions & 29 deletions src/items/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
const osgi = require('../osgi');
const utils = require('../utils');
const log = require('../log')('items');
const metadata = require('./metadata/metadata');
const { ItemPersistence } = require('./item-persistence');
const ItemSemantics = require('./item-semantics');
const { _toOpenhabType } = require('../helpers');
const { getQuantity, QuantityError } = require('../quantity');

const { UnDefType, OnOffType, events, itemRegistry } = require('@runtime');

const metadata = require('./metadata/metadata');
const ItemPersistence = require('./item-persistence');
const ItemSemantics = require('./item-semantics');

const itemBuilderFactory = osgi.getService('org.openhab.core.items.ItemBuilderFactory');

// typedefs need to be global for TypeScript to fully work
Expand Down Expand Up @@ -51,29 +53,6 @@ const itemBuilderFactory = osgi.getService('org.openhab.core.items.ItemBuilderFa
*/
const DYNAMIC_ITEM_TAG = '_DYNAMIC_';

/**
* Helper function to convert JS types to openHAB command/state types' string representation.
*
* Numbers and strings are passed.
* Objects should implement `toOpenHabString` (prioritized) or `toString` to return an openHAB compatible representation.
*
* @private
* @param {*} value
* @returns {*}
*/
function _toOpenhabString (value) {
if (value === null) return 'NULL';
if (value === undefined) return 'UNDEF';
if (typeof value === 'number' || typeof value === 'string') {
return value;
} else if (typeof value.toOpenHabString === 'function') {
return value.toOpenHabString();
} else if (typeof value.toString === 'function') {
return value.toString();
}
return value;
}

/**
* Class representing an openHAB Item
*
Expand Down Expand Up @@ -255,7 +234,7 @@ class Item {
* @see postUpdate
*/
sendCommand (value) {
events.sendCommand(this.rawItem, _toOpenhabString(value));
events.sendCommand(this.rawItem, _toOpenhabType(value));
}

/**
Expand All @@ -266,7 +245,7 @@ class Item {
* @see sendCommand
*/
sendCommandIfDifferent (value) {
value = _toOpenhabString(value);
value = _toOpenhabType(value);
if (value.toString() !== this.state) {
this.sendCommand(value);
return true;
Expand Down Expand Up @@ -330,7 +309,7 @@ class Item {
* @see sendCommand
*/
postUpdate (value) {
events.postUpdate(this.rawItem, _toOpenhabString(value));
events.postUpdate(this.rawItem, _toOpenhabType(value));
}

/**
Expand Down
19 changes: 11 additions & 8 deletions types/items/item-persistence.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Quantity = import('../quantity').Quantity;
export = ItemPersistence;
/**
* Class representing the historic state of an openHAB Item.
* Wrapping the {@link https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions PersistenceExtensions}.
Expand All @@ -11,7 +11,7 @@ export type Quantity = import('../quantity').Quantity;
* @memberOf items
* @hideconstructor
*/
export class ItemPersistence {
declare class ItemPersistence {
constructor(rawItem: any);
rawItem: any;
/**
Expand Down Expand Up @@ -458,25 +458,28 @@ export class ItemPersistence {
*/
removeAllStatesBetween(begin: (time.ZonedDateTime | Date), end: (time.ZonedDateTime | Date), serviceId?: string, ...args: any[]): any;
}
declare namespace ItemPersistence {
export { Quantity };
}
declare namespace time {
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
}
import time = require("../time");
/**
* Class representing an instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/historicitem org.openhab.core.persistence.HistoricItem}.
* Extends {@link items.PersistedState}.
*
* @memberof items
* @hideconstructor
*/
export class PersistedItem extends PersistedState {
declare class PersistedItem extends PersistedState {
/**
* Timestamp of persisted Item.
* @type {time.ZonedDateTime}
*/
get timestamp(): JSJoda.ZonedDateTime;
#private;
}
declare namespace time {
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
}
import time = require("../time");
/**
* Class representing an instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/types/state org.openhab.core.types.State}.
*
Expand Down Expand Up @@ -505,5 +508,5 @@ declare class PersistedState {
get quantityState(): import("../quantity").Quantity;
#private;
}
export {};
type Quantity = import('../quantity').Quantity;
//# sourceMappingURL=item-persistence.d.ts.map
2 changes: 1 addition & 1 deletion types/items/item-persistence.d.ts.map

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

2 changes: 1 addition & 1 deletion types/items/items.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class Item {
toString(): any;
}
import metadata = require("./metadata/metadata");
import { ItemPersistence } from "./item-persistence";
import ItemPersistence = require("./item-persistence");
import ItemSemantics = require("./item-semantics");
declare namespace time {
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
Expand Down
Loading
Loading