Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Updated OHJ from oh-config
Browse files Browse the repository at this point in the history
  • Loading branch information
jpg0 committed Dec 3, 2020
1 parent 4fd6225 commit 791e3c8
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 66 deletions.
4 changes: 2 additions & 2 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const utils = require('./utils');
const { actions } = require('@runtime/Defaults');
const log = require('./log')('actions');

const Things = utils.typeBySuffix('model.script.actions.Things');
const Things = utils.typeBySuffix('core.model.script.actions.Things');

const oh1_actions = osgi.findServices("org.openhab.core.scriptengine.action.ActionService", null) || [];
const oh2_actions = osgi.findServices("org.eclipse.smarthome.model.script.engine.action.ActionService", null) || [];
Expand All @@ -37,7 +37,7 @@ oh1_actions.concat(oh2_actions).forEach(function (item) {

let Exec = utils.typeWithFallback('org.openhab.core.model.script.actions.Exec', 'org.eclipse.smarthome.model.script.actions.Exec');
let HTTP = utils.typeWithFallback('org.openhab.core.model.script.actions.HTTP', 'org.eclipse.smarthome.model.script.actions.HTTP');
let LogAction = utils.typeWithFallback('org.openhab.core.model.script.actions.LogAction', 'org.eclipse.smarthome.model.script.actions.LogAction');
let LogAction = utils.typeWithFallback('org.openhab.core.model.script.actions.Log', 'org.eclipse.smarthome.model.script.actions.LogAction');
let Ping = utils.typeWithFallback('org.openhab.core.model.script.actions.Ping', 'org.eclipse.smarthome.model.script.actions.Ping');

[Exec, HTTP, LogAction, Ping].forEach(function (item) {
Expand Down
12 changes: 6 additions & 6 deletions fluent/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions itemhistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @namespace itemshistory
*/

const PersistenceExtensions = Java.type("org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions");
const DateTime = Java.type('org.joda.time.DateTime');
const PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
const DateTime = Java.type('java.time.ZonedDateTime');

let historicState = function (item, timestamp) {
//todo: check item param
Expand All @@ -28,4 +28,4 @@ module.exports = {
historicState,
latestState,
previousState
}
}
4 changes: 2 additions & 2 deletions items/managed.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ const replaceItem = function (/* same args as addItem */) {
removeItem(itemName);
}
} catch (e) {
if (("" + e).startsWith("org.eclipse.smarthome.core.items.ItemNotFoundException")) {
if (("" + e).startsWith("org.openhab.core.items.ItemNotFoundException")) {
// item not present
} else {
throw e;
Expand Down Expand Up @@ -468,4 +468,4 @@ module.exports = {
throw Error("unsupported function call: " + name);
}
})
}
}
32 changes: 19 additions & 13 deletions log.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Log namespace.
* This namespace provides loggers to log messages to the Openhab Log.
*
*
* @example <caption>Basic logging</caption>
* let log = require('ohj').log('my_logger');
* log.info("Hello World!")
*
*
* @namespace log
*/

Expand All @@ -15,20 +15,20 @@
*/
const LOGGER_PREFIX = "script.js";

const RuntimeExceptionEx = require('@runtime/osgi').classutil.extend(Java.type('java.lang.RuntimeException'));
//const RuntimeExceptionEx = require('@runtime/osgi').classutil.extend(Java.type('java.lang.RuntimeException'));

const MessageFormatter = Java.type("org.slf4j.helpers.MessageFormatter");

/**
* Logger class. A named logger providing the ability to log formatted messages.
*
*
* @memberof log
* @hideconstructor
*/
class Logger {
/**
* Creates a new logger. Don't use directly, use {@link log} on module.
*
*
* @param {String} _name the name of the logger. Will be prefixed by {@link LOGGER_PREFIX}
* @param {*} _listener a callback to receive logging calls. Can be used to send calls elsewhere, such as escalate errors.
*/
Expand All @@ -40,7 +40,7 @@ class Logger {

/**
* Method to determine caller. Don't use directly.
*
*
* @private
* @param {Object} msg the message to get caller details for
* @param {Number} ignoreStackDepth the number of stack frames which to ignore in calculating caller
Expand Down Expand Up @@ -68,15 +68,20 @@ class Logger {
fileName: "<unknown>",
caller: "<root script>"
}, match.groups)
}
} else {
Object.assign(msg, {
fileName: "<unknown>",
caller: "<root script>"
})
} //throw Error(`Failed to parse stack line: ${stackLine}`);
}

return msg;
}

/**
* Method to format a log message. Don't use directly.
*
*
* @private
* @param {Object} msg the message to get caller details for
* @param {String} levelString the level being logged at
Expand All @@ -101,7 +106,7 @@ class Logger {
case "none": return msgData.message;
case "level": return `[${levelString}] ${msgData.message}`
case "short": return `${msgData.message}\t\t[${this.name}, ${msgData.caller.fileName}:${msgData.caller.lineNumber}]`
case "log": return `${msgData.message}\t\t[${this.name}${msgData.caller.fileName ? ` at source ${msgData.caller.fileName}, line ${msgData.caller.lineNumber}` : ''}]`
case "log": return `${msgData.message}\t\t[${this.name} at source ${msgData.caller.fileName}, line ${msgData.caller.lineNumber}]`
default: throw Error(`Unknown prefix type ${prefix}`)
}
}
Expand Down Expand Up @@ -135,11 +140,11 @@ class Logger {
/**
* Logs a message at the supplied level. The message may include placeholders {} which
* will be substituted into the message string only if the message is actually logged.
*
*
* @example
* log.atLevel('INFO', 'The widget was created as {}', widget);
*
*
*
*
* @param {String} level The level at which to log, such as 'INFO', or 'DEBUG'
* @param {String|Error} msg the message to log, possibly with object placeholders
* @param {Object[]} [objects] the objects to substitute into the log message
Expand All @@ -148,8 +153,9 @@ class Logger {
let titleCase = level[0].toUpperCase() + level.slice(1)
try {
if (this._logger[`is${titleCase}Enabled`]()) {

this.maybeLogWithThrowable(level, msg, objects) ||
this.writeLogLine(level, this._formatLogMessage(msg, level, 6), objects);
this.writeLogLine(level, this._formatLogMessage(msg, level, 6), objects);
}
} catch (err) {
this._logger.error(this._formatLogMessage(err, "error", 0));
Expand Down
10 changes: 6 additions & 4 deletions osgi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @namespace osgi
*/

const log = require('./log')('osgi');
const bundleContext = require('@runtime/osgi').bundleContext;
const lifecycle = require('@runtime/osgi').lifecycle;
Expand Down Expand Up @@ -54,13 +54,15 @@ let lookupService = function (classOrName) {
* @memberOf osgi
*/
let getService = function (...classOrNames) {

log.warn(`bc=${bundleContext}`);
let rv = null;

for(let classOrName of classOrNames) {
try {
rv = lookupService(classOrName)
} catch(e) {}
} catch(e) {
log.warn(`Failed to get service ${classOrName}: {}`, e);
}

if(typeof rv !== 'undefined' && rv !== null) {
return rv;
Expand Down Expand Up @@ -127,4 +129,4 @@ module.exports = {
registerService,
registerPermanentService,
unregisterService
}
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ohj",
"version": "0.1.1",
"version": "0.1.0",
"description": "JavaScript Library for Openhan",
"private": false,
"license": "EPL-2.0",
Expand Down
4 changes: 2 additions & 2 deletions provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getAllFunctionNames(obj) {
class AbstractProvider {
constructor(type) {
this.typeName = type.class.getName();
this.javaType = require('@runtime/osgi').classutil.extend(type);
this.javaType = Java.extend(type);//require('@runtime/osgi').classutil.extend(type);
}

register() {
Expand Down Expand Up @@ -112,4 +112,4 @@ module.exports = {
newCallbackItemProvider: c => new ItemProvider(c),
newCallbackThingProvider: () => new CallbackProvider(ThingProviderClass),
newCallbackStateDescriptionFragmentProvider: () => new StateDescriptionFragmentProvider
}
}
26 changes: 12 additions & 14 deletions rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Rules namespace.
* This namespace allows creation of Openhab rules.
*
*
* @namespace rules
*/

Expand All @@ -21,7 +21,7 @@ let factory = require('@runtime/rules').factory;

/**
* Generates an item name given it's configuration.
*
*
* @memberOf rules
* @private
* @param {Object} ruleConfig The rule config
Expand All @@ -33,7 +33,7 @@ const itemNameForRule = function (ruleConfig) {

/**
* Links an item to a rule. When the item is switched on or off, so will the rule be.
*
*
* @memberOf rules
* @private
* @param {HostRule} rule The rule to link to the item.
Expand Down Expand Up @@ -61,7 +61,7 @@ const linkItemToRule = function (rule, item) {

/**
* Gets the groups that an rule-toggling-item should be a member of. Will create the group item if necessary.
*
*
* @memberOf rules
* @private
* @param {Object} ruleConfig The rule config describing the rule
Expand All @@ -81,23 +81,23 @@ const getGroupsForItem = function (ruleConfig) {

/**
* Creates a rule. The rule will be created and immediately available.
*
*
* @example
* import { rules, triggers } = require('ohj');
*
*
* rules.JSRule({
* name: "my_new_rule",
* description": "this rule swizzles the swallows",
* triggers: triggers.GenericCronTrigger("0 30 16 * * ? *"),
* execute: triggerConfig => { //do stuff }
* });
*
*
* @memberOf rules
* @param {Object} ruleConfig The rule config describing the rule
* @param {String} ruleConfig.name the name of the rule
* @param {String} ruleConfig.description a description of the rule
* @param {*} ruleConfig.execute callback that will be called when the rule fires
* @param {HostTrigger|HostTrigger[]} ruleConfig.triggers triggers which will define when to fire the rule
* @param {HostTrigger[]} ruleConfig.triggers triggers which will define when to fire the rule
* @returns {HostRule} the created rule
*/
let JSRule = function (ruleConfig) {
Expand All @@ -116,15 +116,12 @@ let JSRule = function (ruleConfig) {
}
};

let rule = new SimpleRule({
var rule = new SimpleRule({
execute: doExecute,
getUID: () => ruid
});

let triggers = ruleConfig.triggers ? ruleConfig.triggers : ruleConfig.getEventTrigger();
if (!Array.isArray(triggers)) {
triggers = [triggers];
}
var triggers = ruleConfig.triggers ? ruleConfig.triggers : ruleConfig.getEventTrigger();

rule.setTemplateUID(ruTemplateid);

Expand Down Expand Up @@ -185,7 +182,7 @@ let registerRule = function(rule) {
/**
* Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state.
* The rule will be created and immediately available.
*
*
* @memberOf rules
* @param {Object} ruleConfig The rule config describing the rule
* @param {String} ruleConfig.name the name of the rule
Expand Down Expand Up @@ -309,3 +306,4 @@ module.exports = {
JSRule,
SwitchableJSRule
}

3 changes: 1 addition & 2 deletions test/condition-conf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ describe('Conditionals', function () {
messages,
mock:function(name){
return {
error: a => messages.push(`error: ${a}`),
debug: a => messages.push(`debug: ${a}`)
error: a => messages.push(a)
}
}
};
Expand Down
Loading

0 comments on commit 791e3c8

Please sign in to comment.