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