'use strict';
// Append the script file name OR rule UID depending on which is available
- const defaultLoggerName = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : "");
+ const defaultIdentifier = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : "");
const System = Java.type('java.lang.System');
const ZonedDateTime = Java.type('java.time.ZonedDateTime');
const formatRegExp = /%[sdj%]/g;
- function createLogger(name = defaultLoggerName) {
+ function createLogger(name = defaultIdentifier) {
return Java.type("org.slf4j.LoggerFactory").getLogger(name);
}
- //user configurable
+ // User configurable
let log = createLogger();
function stringify(value) {
const counters = {};
const timers = {};
+ // Polyfills for common NodeJS functions
+
const console = {
'assert': function (expression, message) {
if (!expression) {
}
},
- //Allow user customizable logging names
+ // Allow user customizable logging names
set loggerName(name) {
log = createLogger(name);
this._loggerName = name;
function setTimeout(cb, delay) {
const args = Array.prototype.slice.call(arguments, 2);
return ThreadsafeTimers.createTimerWithArgument(
+ defaultIdentifier + '.setTimeout',
ZonedDateTime.now().plusNanos(delay * 1000000),
args,
function (args) {
const args = Array.prototype.slice.call(arguments, 2);
const delayNanos = delay * 1000000
let timer = ThreadsafeTimers.createTimerWithArgument(
+ defaultIdentifier + '.setInterval',
ZonedDateTime.now().plusNanos(delayNanos),
args,
function (args) {
clearTimeout(timer);
}
- //Polyfil common functions onto the global object
+ // Polyfill common NodeJS functions onto the global object
globalThis.console = console;
globalThis.setTimeout = setTimeout;
globalThis.clearTimeout = clearTimeout;
globalThis.setInterval = setInterval;
globalThis.clearInterval = clearInterval;
- //Support legacy NodeJS libraries
+ // Support legacy NodeJS libraries
globalThis.global = globalThis;
globalThis.process = { env: { NODE_ENV: '' } };
})(this);