1
0

Set js.lib logger

This commit is contained in:
2024-01-04 10:06:46 +01:00
parent a62107f02c
commit bdbe664125

View File

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