diff --git a/utils/watch.js b/utils/watch.js index 82fcd34..10af8ed 100644 --- a/utils/watch.js +++ b/utils/watch.js @@ -1,5 +1,5 @@ -console.loggerName = 'js.watch'; -console.log('Load watch module'); +const log = Java.type('org.slf4j.LoggerFactory').getLogger('js.watch'); +log.info('Load watch module'); class Watch { @@ -9,7 +9,7 @@ class Watch { #timers = new timer.Timer(); constructor() { - console.log('Initialization of watch helper class'); + log.info('Initialization of watch helper class'); } add(params) { @@ -25,7 +25,7 @@ class Watch { if (!this.#watchItems.hasOwnProperty(params['item'])) { this.#createWatchRule(params['item']); } else { - console.debug(`Watch rule for item ${params['item']} already existing`) + log.debug(`Watch rule for item ${params['item']} already existing`) } @@ -33,7 +33,7 @@ class Watch { let operator = (params['operator'] !== undefined) ? params['operator'] : '=='; // Create watch object and return UUID - console.log(`Add watch object for item ${params['item']} with state ${params['targetState']} and operator ${operator} with UUID ${watchUUID}`); + log.info(`Add watch object for item ${params['item']} with state ${params['targetState']} and operator ${operator} with UUID ${watchUUID}`); this.#watchObjects[watchUUID] = { item: params['item'], targetState: params['targetState'], @@ -58,7 +58,7 @@ class Watch { // Get itemName let watchItemName = this.#watchObjects[watchUUID].item; - console.log(`Delete watch object for item ${watchItemName} with watchUUID ${watchUUID}`); + log.info(`Delete watch object for item ${watchItemName} with watchUUID ${watchUUID}`); // End repeatAlertTimer if existing this.#timers.cancel('repeatAlarm ' + watchUUID); @@ -70,7 +70,7 @@ class Watch { // Delete watch rule if no more watchObjects for respective item are present if (Object.keys(this.#watchObjects).length == 0) { - console.debug(`Remove openHAB watch rule for item ${watchItemName}`); + log.debug(`Remove openHAB watch rule for item ${watchItemName}`); rules.removeRule(this.#watchItems[watchItemName]); } } @@ -84,15 +84,15 @@ class Watch { validateWatchConfig(params) { if (params['item'] === undefined) { - console.error('No item set'); + log.error('No item set'); return false; } if (params['targetState'] === undefined) { - console.error('No targetState set'); + log.error('No targetState set'); return false; } if (params['alertFunc'] === undefined) { - console.error('No alertFunc set'); + log.error('No alertFunc set'); return false; } @@ -101,9 +101,9 @@ class Watch { } #applyHysteresis(currentState, targetState, hysteresis) { - console.log(`Applying hysteresis with: ${currentState}, ${targetState}, ${hysteresis}`); + log.info(`Applying hysteresis with: ${currentState}, ${targetState}, ${hysteresis}`); let delta = Math.abs(targetState - currentState); - console.log(delta); + log.info(delta); if (delta < hysteresis) { return false; @@ -116,14 +116,14 @@ class Watch { // Get itemName let watchItemName = this.#watchObjects[watchUUID].item; - console.debug(`Check if item ${watchItemName} is in alert state for watchObject ${watchUUID}`) + log.debug(`Check if item ${watchItemName} is in alert state for watchObject ${watchUUID}`) // Convert currentState for comparison let currentState = lib.convertValue(items[watchItemName].state); // 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}`); + log.info(`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 @@ -133,7 +133,7 @@ class Watch { } else if (this.#watchObjects[watchUUID].alert == true) { // Comparison failed but alert is active // Skip if currentState fails hysteresis test if (this.#watchObjects[watchUUID].hysteresis != '' && !this.#applyHysteresis(currentState, this.#watchObjects[watchUUID].targetState, this.#watchObjects[watchUUID].hysteresis)) { - console.log('CurrentState is still in boundaries provided by hysteresis value'); + log.info('CurrentState is still in boundaries provided by hysteresis value'); return; } // End alert @@ -143,7 +143,7 @@ class Watch { #createWatchRule(item) { // Create openHAB rule - console.log(`Create openHAB watch rule for item ${item}`); + log.info(`Create openHAB watch rule for item ${item}`); let watchRuleID = String(utils.randomUUID()); rules.JSRule({ id: watchRuleID, @@ -155,12 +155,12 @@ class Watch { } #endAlert(watchUUID) { - console.log(`End alert for watchObject ${watchUUID} triggered`); + log.info(`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}`); + log.info(`Run end alert function for watchObject ${watchUUID}`); this.#watchObjects[watchUUID].endAlertFunc(); } @@ -178,7 +178,7 @@ class Watch { return; } - console.log(`Processing state ${event.newState} for ${event.itemName}`); + log.info(`Processing state ${event.newState} for ${event.itemName}`); for (const [id, watchObject] of Object.entries(this.#watchObjects)) { if (watchObject.item == event.itemName) { @@ -188,11 +188,11 @@ class Watch { } #rescheduleAlert(watchUUID) { - console.log(`Subsequent alert for ${watchUUID}`); + log.info(`Subsequent alert for ${watchUUID}`); } #startAlert(watchUUID) { - console.log(`Initial alert for watchObject ${watchUUID} triggered`); + log.info(`Initial alert for watchObject ${watchUUID} triggered`); // Set alertFunc as inital alert function if no initialAlertFunc is set let initialAlertFunc = this.#watchObjects[watchUUID].alertFunc; @@ -209,10 +209,10 @@ class Watch { // Execute initial alert function or create timer to run initial alert rule if required if (alertDelay == '') { - console.log(`Run initial alert function for watchObject ${watchUUID}`); + log.info(`Run initial alert function for watchObject ${watchUUID}`); initialAlertFunc(); } else { - console.log(`Shedule initial alert function for watchObject ${watchUUID} with delay setting ${alertDelay}`); + log.info(`Shedule initial alert function for watchObject ${watchUUID} with delay setting ${alertDelay}`); let alertDelayZDT = time.toZDT(alertDelay); alertDelayAbsolute = (alertDelayZDT.getMillisFromNow() / 1000); @@ -220,7 +220,7 @@ class Watch { this.#timers.create('startAlert ' + watchUUID, alertDelayZDT, () => { - console.log('Run initial alert function for watchObject ' + watchUUID); + log.info('Run initial alert function for watchObject ' + watchUUID); initialAlertFunc(); } ); @@ -228,11 +228,11 @@ class Watch { // Create timer to run repeat alert rule if required if (alertRepeat != '') { - console.log(`Shedule repeat alert function for watchObject ${watchUUID} with repeat setting ${alertRepeat}`); + log.info(`Shedule repeat alert function for watchObject ${watchUUID} with repeat setting ${alertRepeat}`); this.#timers.create('repeatAlarm ' + watchUUID, time.toZDT(alertRepeat).plusSeconds(alertDelayAbsolute), () => { - console.log('Run repeat alert function for watchObject ' + watchUUID); + log.info('Run repeat alert function for watchObject ' + watchUUID); this.#watchObjects[watchUUID].alertFunc(); this.#watchObjects[watchUUID].repeatAlertTimer.reschedule(time.toZDT(alertRepeat)); }