openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 70 forks source link

Add an osgi.js library #158

Closed 5iver closed 5 years ago

5iver commented 5 years ago

@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

lewie commented 5 years ago

@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 commented 5 years ago

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]]
lewie commented 5 years ago

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

5iver commented 5 years ago

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.