Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an osgi.js library #158

Merged
merged 1 commit into from
Jun 19, 2019
Merged

Conversation

5iver
Copy link
Member

@5iver 5iver commented Jun 18, 2019

@lewie, would you like to review this one? I did my testing several months ago and I'm just getting around to committing now that the JS libraries are migrated. This should help a LOT in expanding the JS libraries.

Signed-off-by: Scott Rushworth openhab@5iver.com

Signed-off-by: Scott Rushworth <openhab@5iver.com>
@lewie
Copy link

lewie commented Jun 19, 2019

@openhab-5iver
Looks very interesting! :-)

you can possibly give me a simple example to test it meaningfully. Or a reference to an example in the Jython area?

@5iver
Copy link
Member Author

5iver commented Jun 19, 2019

Nearly all of the Jython component scripts utilize OSGi services. Here are some examples for getting, setting and removing metadata from an Item...

'use strict';
var log = Java.type("org.slf4j.LoggerFactory").getLogger("jsr223.javascript");
var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF");
load(OPENHAB_CONF + '/automation/lib/javascript/personal/osgi.js');

try {
    var MetadataRegistry = get_service("org.openhab.core.items.MetadataRegistry");
}
catch(err) {
    var MetadataRegistry =  get_service("org.eclipse.smarthome.core.items.MetadataRegistry");
}

try {
    var Metadata = Java.type("org.openhab.core.items.Metadata");
    var MetadataKey = Java.type("org.openhab.core.items.MetadataKey");
}
catch(err) {
    var Metadata = Java.type("org.eclipse.smarthome.core.items.Metadata");
    var MetadataKey = Java.type("org.eclipse.smarthome.core.items.MetadataKey");
}

//MetadataRegistry.update(new Metadata(new MetadataKey("Morning","DS_FamilyRoom_TV_LED_Color"), "LightLevels", {"Low_Lux_Trigger":90, "Level":10, "Hue":100, "Sat":100}))
//MetadataRegistry.remove(new MetadataKey("Morning","DS_FamilyRoom_TV_LED_Color"))

var test = MetadataRegistry.get(new MetadataKey("Morning","DS_FamilyRoom_TV_LED_Color"));
log.info("JS OSGI metadata test: " + test);
//2019-06-19 07:17:12.855 [INFO ] [jsr223.javascript] - JS OSGI metadata test: Metadata [key=Morning:DS_FamilyRoom_TV_LED_Color, value=LightLevels, configuration=[Hue=100, Low_Lux_Trigger=90, Level=10, Sat=100]]

Copy link

@lewie lewie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me!
It is a external lib therefore no context or scope is needed?! http://ryanmorr.com/understanding-scope-and-context-in-javascript/

@lewie
Copy link

lewie commented Jun 19, 2019

Thank You. Wow yes! It WILL help a LOT! :-)

@5iver 5iver merged commit 04d63c5 into openhab-scripters:master Jun 19, 2019
@5iver
Copy link
Member Author

5iver commented Jun 19, 2019

We're about to add a core.metadata.py module for Jython that will be pretty easy to implement for JS too, now that there's OSGi for JS.

@5iver 5iver deleted the add-osgi.js-library branch June 19, 2019 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants