Skip to content

Commit 9c1f3ef

Browse files
authored
[items] Refactor item-persistence.js exports & Refactor _toOpenhabString to helpers (#337)
Signed-off-by: Florian Hotze <[email protected]>
1 parent a53216a commit 9c1f3ef

12 files changed

+67
-55
lines changed

src/helpers.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,47 @@ const utils = require('./utils');
44
const javaZDT = Java.type('java.time.ZonedDateTime');
55
const javaDuration = Java.type('java.time.Duration');
66

7+
/**
8+
* @typedef { import("./items/items").Item } Item
9+
* @private
10+
*/
11+
12+
/**
13+
* Returns the name of a given Item or relays the passed in Item name.
14+
*
15+
* @param {Item|string} itemOrName Item or Item name
16+
* @returns {string} the Item name
17+
* @private
18+
*/
719
function _getItemName (itemOrName) {
8-
// Somehow instanceof check doesn't work here, so workaround the problem
9-
if (itemOrName.rawItem !== undefined) return itemOrName.name;
20+
if (_isItem(itemOrName)) return itemOrName.name;
1021
return itemOrName;
1122
}
1223

24+
/**
25+
* Helper function to convert JS types to a
26+
* {@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.
27+
*
28+
* Number and string are passed through.
29+
* Other objects should implement <code>toOpenHabString</code> (prioritized) or <code>toString</code> to return an openHAB compatible representation.
30+
*
31+
* @private
32+
* @param {*} value
33+
* @returns {*}
34+
*/
35+
function _toOpenhabType (value) {
36+
if (value === null) return 'NULL';
37+
if (value === undefined) return 'UNDEF';
38+
if (typeof value === 'number' || typeof value === 'string') {
39+
return value;
40+
} else if (typeof value.toOpenHabString === 'function') {
41+
return value.toOpenHabString();
42+
} else if (typeof value.toString === 'function') {
43+
return value.toString();
44+
}
45+
return value;
46+
}
47+
1348
/**
1449
* Checks whether the given object is an instance of {@link items.Item}.
1550
*
@@ -72,6 +107,7 @@ function _isDuration (o) {
72107

73108
module.exports = {
74109
_getItemName,
110+
_toOpenhabType,
75111
_isItem,
76112
_isQuantity,
77113
_isZonedDateTime,

src/items/item-persistence.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,4 @@ class ItemPersistence {
724724
}
725725
}
726726

727-
module.exports = {
728-
ItemPersistence,
729-
PersistedItem
730-
};
727+
module.exports = ItemPersistence;

src/items/items.js

+8-29
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
const osgi = require('../osgi');
88
const utils = require('../utils');
99
const log = require('../log')('items');
10-
const metadata = require('./metadata/metadata');
11-
const { ItemPersistence } = require('./item-persistence');
12-
const ItemSemantics = require('./item-semantics');
10+
const { _toOpenhabType } = require('../helpers');
1311
const { getQuantity, QuantityError } = require('../quantity');
1412

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

15+
const metadata = require('./metadata/metadata');
16+
const ItemPersistence = require('./item-persistence');
17+
const ItemSemantics = require('./item-semantics');
18+
1719
const itemBuilderFactory = osgi.getService('org.openhab.core.items.ItemBuilderFactory');
1820

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

54-
/**
55-
* Helper function to convert JS types to openHAB command/state types' string representation.
56-
*
57-
* Numbers and strings are passed.
58-
* Objects should implement `toOpenHabString` (prioritized) or `toString` to return an openHAB compatible representation.
59-
*
60-
* @private
61-
* @param {*} value
62-
* @returns {*}
63-
*/
64-
function _toOpenhabString (value) {
65-
if (value === null) return 'NULL';
66-
if (value === undefined) return 'UNDEF';
67-
if (typeof value === 'number' || typeof value === 'string') {
68-
return value;
69-
} else if (typeof value.toOpenHabString === 'function') {
70-
return value.toOpenHabString();
71-
} else if (typeof value.toString === 'function') {
72-
return value.toString();
73-
}
74-
return value;
75-
}
76-
7756
/**
7857
* Class representing an openHAB Item
7958
*
@@ -255,7 +234,7 @@ class Item {
255234
* @see postUpdate
256235
*/
257236
sendCommand (value) {
258-
events.sendCommand(this.rawItem, _toOpenhabString(value));
237+
events.sendCommand(this.rawItem, _toOpenhabType(value));
259238
}
260239

261240
/**
@@ -266,7 +245,7 @@ class Item {
266245
* @see sendCommand
267246
*/
268247
sendCommandIfDifferent (value) {
269-
value = _toOpenhabString(value);
248+
value = _toOpenhabType(value);
270249
if (value.toString() !== this.state) {
271250
this.sendCommand(value);
272251
return true;
@@ -330,7 +309,7 @@ class Item {
330309
* @see sendCommand
331310
*/
332311
postUpdate (value) {
333-
events.postUpdate(this.rawItem, _toOpenhabString(value));
312+
events.postUpdate(this.rawItem, _toOpenhabType(value));
334313
}
335314

336315
/**

types/items/item-persistence.d.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type Quantity = import('../quantity').Quantity;
1+
export = ItemPersistence;
22
/**
33
* Class representing the historic state of an openHAB Item.
44
* Wrapping the {@link https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions PersistenceExtensions}.
@@ -11,7 +11,7 @@ export type Quantity = import('../quantity').Quantity;
1111
* @memberOf items
1212
* @hideconstructor
1313
*/
14-
export class ItemPersistence {
14+
declare class ItemPersistence {
1515
constructor(rawItem: any);
1616
rawItem: any;
1717
/**
@@ -458,25 +458,28 @@ export class ItemPersistence {
458458
*/
459459
removeAllStatesBetween(begin: (time.ZonedDateTime | Date), end: (time.ZonedDateTime | Date), serviceId?: string, ...args: any[]): any;
460460
}
461+
declare namespace ItemPersistence {
462+
export { Quantity };
463+
}
464+
declare namespace time {
465+
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
466+
}
467+
import time = require("../time");
461468
/**
462469
* Class representing an instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/historicitem org.openhab.core.persistence.HistoricItem}.
463470
* Extends {@link items.PersistedState}.
464471
*
465472
* @memberof items
466473
* @hideconstructor
467474
*/
468-
export class PersistedItem extends PersistedState {
475+
declare class PersistedItem extends PersistedState {
469476
/**
470477
* Timestamp of persisted Item.
471478
* @type {time.ZonedDateTime}
472479
*/
473480
get timestamp(): JSJoda.ZonedDateTime;
474481
#private;
475482
}
476-
declare namespace time {
477-
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
478-
}
479-
import time = require("../time");
480483
/**
481484
* Class representing an instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/types/state org.openhab.core.types.State}.
482485
*
@@ -505,5 +508,5 @@ declare class PersistedState {
505508
get quantityState(): import("../quantity").Quantity;
506509
#private;
507510
}
508-
export {};
511+
type Quantity = import('../quantity').Quantity;
509512
//# sourceMappingURL=item-persistence.d.ts.map

types/items/item-persistence.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

types/items/items.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class Item {
314314
toString(): any;
315315
}
316316
import metadata = require("./metadata/metadata");
317-
import { ItemPersistence } from "./item-persistence";
317+
import ItemPersistence = require("./item-persistence");
318318
import ItemSemantics = require("./item-semantics");
319319
declare namespace time {
320320
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;

0 commit comments

Comments
 (0)