From 949eb8e9a6466560a38b8f000d36903b2e3011cc Mon Sep 17 00:00:00 2001 From: Christian Weimann Date: Tue, 15 Aug 2023 08:20:44 +0200 Subject: [PATCH] Improve logging --- utils/watch.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/utils/watch.js b/utils/watch.js index 04140fc..e7137cf 100644 --- a/utils/watch.js +++ b/utils/watch.js @@ -8,12 +8,12 @@ class Watch { #watchObjects = new Object(); constructor(itemName) { - console.log('Create new Watch instance for ' + 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'); + throw(`Item ${itemName} not existing`); } this.#watchItemName = itemName; @@ -27,7 +27,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.#watchItemName} because no valid config was provided`); return; } @@ -35,7 +35,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.#watchItemName} with state ${params['targetState']} and operator ${operator} with UUID ${watchUUID}`); this.#watchObjects[watchUUID] = { targetState: params['targetState'], operator: operator, @@ -56,7 +56,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.#watchItemName} with watchUUID ${watchUUID}`); // End repeatAlertTimer if existing if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) { @@ -95,7 +95,7 @@ class Watch { } #applyHysteresis(currentState, targetState, hysteresis) { - console.log('Applying hysteresis with: ' + currentState + ', ' + targetState + ', ' + hysteresis); + console.log(`Applying hysteresis with: ${currentState}, ${targetState}, ${hysteresis}`); let delta = Math.abs(targetState - currentState); console.log(delta); @@ -111,7 +111,7 @@ class Watch { // Do comparison if (lib.compare(currentState, this.#watchObjects[watchUUID].targetState, this.#watchObjects[watchUUID].operator)) { // Comparison successful - console.log('State ' + currentState + ' is ' + this.#watchObjects[watchUUID].operator + ' ' + this.#watchObjects[watchUUID].targetState + ' triggered by ' + watchUUID); + console.log(`State ${currentState} is ${this.#watchObjects[watchUUID].operator} ${this.#watchObjects[watchUUID].targetState} triggered by ${watchUUID}`); if (this.#watchObjects[watchUUID].alert == true) { // Comparison successful and alert is already active this.#rescheduleAlert(watchUUID); } else { // Comparison successful and alert is not active @@ -131,7 +131,7 @@ 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.#watchItemName}`); let ruleID = rules.JSRule({ name: 'Watch rule for ' + this.#watchItemName, triggers: [triggers.ItemStateChangeTrigger(this.#watchItemName)], @@ -140,12 +140,12 @@ class Watch { } #endAlert(watchUUID) { - console.log('End alert for watchObject ' + watchUUID + ' triggered'); + console.log(`End alert for watchObject ${watchUUID} triggered`); this.#watchObjects[watchUUID].alert = false; // Run end alert function if existing if (this.#watchObjects[watchUUID].endAlertFunc != '') { - console.log('Run end alert function for watchObject ' + watchUUID); + console.log(`Run end alert function for watchObject ${watchUUID}`); this.#watchObjects[watchUUID].endAlertFunc(); } @@ -165,11 +165,11 @@ class Watch { #processStateChange(event) { // Skip if function is triggered without openHAB event if (event === undefined || event.eventType === undefined) { - console.warn('ProcessStateChange triggered without openHAB event'); + console.warn(`ProcessStateChange for ${this.#watchItemName} triggered without openHAB event`); return; } - console.log('Processing state ' + this.#item.state + ' for ' + this.#watchItemName); + console.log(`Processing state ${this.#item.state} for ${this.#watchItemName}`); // Iterate through watchObjetcs // todo: rework to only fetch UUID for (let [watchUUID, watchObject] of Object.entries(this.#watchObjects)) { @@ -178,11 +178,11 @@ class Watch { } #rescheduleAlert(watchUUID) { - console.log('Subsequent alert for ' + watchUUID); + console.log(`Subsequent alert for ${watchUUID}`); } #startAlert(watchUUID) { - console.log('Initial alert for watchObject ' + watchUUID + ' triggered'); + console.log(`Initial alert for watchObject ${watchUUID} triggered`); // Set alertFunc as inital alert function if no initialAlertFunc is set let initialAlertFunc = this.#watchObjects[watchUUID].alertFunc; @@ -195,10 +195,10 @@ class Watch { // Execute initial alert function or create timer to run initial alert rule if required if (this.#watchObjects[watchUUID].alertDelay == '') { - console.log('Run initial alert function for watchObject ' + watchUUID); + console.log(`Run initial alert function for watchObject ${watchUUID}`); initialAlertFunc(); } else { - console.log('Run initial alert function for watchObject ' + watchUUID + ' with delay setting ' + this.#watchObjects[watchUUID].alertDelay); + console.log(`Run initial alert function for watchObject ${watchUUID} with delay setting ${this.#watchObjects[watchUUID].alertDelay}`); this.#watchObjects[watchUUID].startAlertTimer = actions.ScriptExecution.createTimer('startAlert ' + watchUUID, time.toZDT(this.#watchObjects[watchUUID].alertDelay), () => {