1
0

Rework internal watchItem naming

This commit is contained in:
2023-08-19 07:50:04 +02:00
parent 7b8229a047
commit 0d5f0985e0

View File

@@ -3,20 +3,25 @@ console.log('Load watch module');
class Watch {
#item
#watchItemName
#watchItem
#watchObjects = new Object();
constructor(itemName) {
console.log(`Create new Watch instance for ${itemName}`);
// Check if item to watch is existing
this.#item = items.getItem(itemName, true);
if (this.#item == null) {
throw(`Item ${itemName} not existing`);
}
this.#watchItemName = itemName;
constructor(watchItem) {
// Fetch item if provided itemName is a string
if (typeof watchItem === 'string' || watchItem instanceof String) {
this.#watchItem = items[watchItem];
} else {
this.#watchItem = watchItem;
}
// Throw error if item is not existing
if (this.#watchItem === null) {
throw(`Item ${watchItem} not existing`);
}
console.log(`Create new Watch instance for ${this.#watchItem.name}`);
// Create watch rule
this.#createWatchRule();
}
@@ -27,7 +32,7 @@ class Watch {
// Validate config for watchObject
if (!this.validateWatchConfig(params)) {
console.warn(`Failed to add watch object for ${this.#watchItemName} because no valid config was provided`);
console.warn(`Failed to add watch object for ${this.#watchItem.name} because no valid config was provided`);
return;
}
@@ -35,7 +40,7 @@ class Watch {
let operator = (params['operator'] !== undefined) ? params['operator'] : '==';
// Create watch object and return UUID
console.log(`Add watch object for item ${this.#watchItemName} with state ${params['targetState']} and operator ${operator} with UUID ${watchUUID}`);
console.log(`Add watch object for item ${this.#watchItem.name} with state ${params['targetState']} and operator ${operator} with UUID ${watchUUID}`);
this.#watchObjects[watchUUID] = {
targetState: params['targetState'],
operator: operator,
@@ -56,7 +61,7 @@ class Watch {
}
delete(watchUUID) {
console.log(`Delete watch object for item ${this.#watchItemName} with watchUUID ${watchUUID}`);
console.log(`Delete watch object for item ${this.#watchItem.name} with watchUUID ${watchUUID}`);
// End repeatAlertTimer if existing
if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) {
@@ -74,7 +79,7 @@ class Watch {
// Delete watchRule if no more watchObjects are present
if (Object.keys(this.#watchObjects).length == 0) {
console.debug(`Remove openHAB watch rule for item ${this.#watchItemName}`);
console.debug(`Remove openHAB watch rule for item ${this.#watchItem.name}`);
rules.removeRule(this.watchRuleID);
}
}
@@ -116,7 +121,7 @@ class Watch {
console.debug(`Check if item is in alert state for watchObject ${watchUUID}`)
// Convert currentState for comparison
let currentState = lib.convertValue(this.#item.state);
let currentState = lib.convertValue(this.watchItem.state);
// Do comparison
if (lib.compare(currentState, this.#watchObjects[watchUUID].targetState, this.#watchObjects[watchUUID].operator)) { // Comparison successful
@@ -140,12 +145,12 @@ class Watch {
#createWatchRule() {
// Create openHAB rule
console.log(`Create openHAB watch rule for item ${this.#watchItemName}`);
console.log(`Create openHAB watch rule for item ${this.#watchItem.name}`);
this.watchRuleID = String(utils.randomUUID());
rules.JSRule({
id: this.watchRuleID,
name: 'Watch rule for ' + this.#watchItemName,
triggers: [triggers.ItemStateUpdateTrigger(this.#watchItemName)],
name: 'Watch rule for ' + this.#watchItem.name,
triggers: [triggers.ItemStateUpdateTrigger(this.#watchItem.name)],
execute: (event) => { this.#processItemEvent(event) },
});
}
@@ -176,11 +181,11 @@ class Watch {
#processItemEvent(event) {
// Skip if function is triggered without openHAB event
if (event === undefined || event.eventType === undefined) {
console.warn(`ProcessItemEvent for ${this.#watchItemName} triggered without openHAB event`);
console.warn(`ProcessItemEvent for ${this.#watchItem.name} triggered without openHAB event`);
return;
}
console.log(`Processing state ${this.#item.state} for ${this.#watchItemName}`);
console.log(`Processing state ${this.watchItem.state} for ${this.#watchItem.name}`);
// Iterate through watchObjetcs // todo: rework to only fetch UUID
for (let [watchUUID, watchObject] of Object.entries(this.#watchObjects)) {