Somehow it’s very confusing to me that i cannot simply set a type without Java.Type("").
Can someone please help me how this can be done?
I’ve already took a look to Blocky but I guess creating a variable of type is not possible.
Can someone tell me if the script below can be written in an easier notation?
var home_location = Java.type("org.openhab.core.library.types.PointType");
var home_lat = Java.type("org.openhab.core.library.types.DecimalType");
var home_long = Java.type("org.openhab.core.library.types.DecimalType");
home_lat = new home_lat(49.0007495);
home_long = new home_long(12.0779579);
home_location = new home_location(home_lat, home_long);
phone_location = itemRegistry.getItem("FJSIphonePingableNetworkDevice_Location").getStateAs(PointType.class);
var distance = phone_location.distanceFrom(home_location).intValue();
openHAB is written in the language Java.
That’s what Java.type does. It’s like those import statements you’ve seen in Rules DSL code, only it’s importing and translating.
Assuming you are using Nashorn and not the GraalVM add-on, a bunch of stuff is in fact “imported” for you including all the Item and State types.
So you don’t need to do the Java.type yourself.
home_lat = new DecimalType(49.0007495);
home_long = new DecimalType(12.0779579);
home_location = new PointType(home_lat, home_long);
But if you did, by chance, have to import something to use it, such as ZonedDateTime, you only do it once.
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var now = ZonedDateTime.now();
var yesterday = ZonedDateTime.now().minusDays(1);
var Duration = Java.type("java.time.Duration");
var between = new Duration(yesterday, now);