Hello,
I want to save a snapshot to a static image. I used a DSL rule from the forum and converted it to Javascript. As a DSL rule it works perfectly.
Unfortunately it doesn’t work in JavaScript.
I’ve created several log entries so I can isolate the error, but I can’t fix it.
Can someone please give me a tip?
Here is the JavaScript:
var URL = Java.type("java.net.URL");
var RawType = Java.type("org.openhab.core.library.types.RawType");
rules.JSRule({
name: "ab_JS Test Camera",
description: "Test JS Camera",
triggers: [
triggers.ItemStateUpdateTrigger('swTestSwitch', '', 'ON')
],
execute: (event) => {
const snapURL = items['SnapShotUri_Arenti_IN1_1'].state.toString();
console.log("snapURL: " + snapURL);
const url = new URL(snapURL);
const connection = url.openConnection();
const contentType = connection.getContentType();
console.log("copyImageLocal: " + "after contentType [ " + contentType.toString() + " ]");
const inputStream = connection.getInputStream();
console.log("copyImageLocal: " + "after getInputStream");
const bytes = inputStream.readAllBytes();
console.log("copyImageLocal: " + "after bytes");
const image = new RawType(bytes, contentType);
console.log("copyImageLocal: " + "after RawType");
items['pictureLetterbox'].postUpdate(image);
}
})
Here are the log entries:
2023-10-06 15:25:25.033 [INFO ] [hab.automation.script.file.camera.js] - snapURL: http://192.168.2.167:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&version=8&method=GetSnapshot&_sid=lKqX-mkSLdq7UBHQl0Pyz9pAsZgFD2ycSem92mXwIfIDD5qJFSsF_zn48-2Z2ZYzkJXSKQjqbc_WZ4EEUYJwg0&camStm=1&cameraId=3
2023-10-06 15:25:25.276 [INFO ] [hab.automation.script.file.camera.js] - copyImageLocal: after contentType [ image/jpeg ]
2023-10-06 15:25:25.278 [WARN ] [e.automation.internal.RuleEngineImpl] - Failed to execute action: 1(java.lang.IllegalStateException: java.lang.IllegalAccessException: access to public member failed: sun.net.www.protocol.http.HttpURLConnection.getInputStream[Ljava.lang.Object;@541d4b99/invokeVirtual, from public Lookup)
org.graalvm.polyglot.PolyglotException: java.lang.IllegalStateException: java.lang.IllegalAccessException: access to public member failed: sun.net.www.protocol.http.HttpURLConnection.getInputStream[Ljava.lang.Object;@541d4b99/invokeVirtual, from public Lookup
at com.oracle.truffle.host.HostMethodDesc$SingleMethod$MethodMHImpl.makeMethodHandle(HostMethodDesc.java:394) ~[?:?]
Thanks in advance.