Hello everyone,
I am currently still using openhab 3.4.4 and would like to finally update to the latest version.
Unfortunately, this is currently not possible as I cannot migrate my rules.
I think I have a small comprehension problem and I hope you can help me to solve it.
But now to the main problem:
I migrated all my DSL rules to openhab-js (4.0.0) a long time ago. So that I could solve more complex problems, I used object-oriented Typescript to build the rules. This also worked well with corresponding build scripts.
Now I would like to switch to Openhab 4.1 with openhab-js 4.9.0. But that doesnât seem to be so easy. In the course of the migration, I also wanted to use the Quantity data type. This is where the problems startedâŠ
Iâd better post the code from a minimal test project:
The error after importing the test rule:
09:19:24.748 [DEBUG] [g.internal.OpenhabGraalJSScriptEngine] - Initializing GraalJS script engine...
09:19:24.758 [DEBUG] [g.internal.OpenhabGraalJSScriptEngine] - Injecting ThreadsafeTimers into the JS runtime...
09:19:24.760 [DEBUG] [g.internal.OpenhabGraalJSScriptEngine] - Evaluating cached global script...
09:19:24.764 [DEBUG] [g.internal.OpenhabGraalJSScriptEngine] - Successfully initialized GraalJS script engine.
09:19:24.778 [ERROR] [ab.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: Cannot load CommonJS module: 'openhab/types/quantity'
at com.oracle.truffle.polyglot.PolyglotMapAndFunction.apply(PolyglotMapAndFunction.java:46) ~[?:?]
...
The index.ts file:
import "openhab"
import { Quantity, getQuantity } from "openhab/types/quantity";
let myTestVariable: Quantity=getQuantity("0 kWh");
console.log(myTestVariable.toString());
The index.js file created by tsc:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("openhab");
const quantity_1 = require("openhab/types/quantity");
let myTestVariable = (0, quantity_1.getQuantity)("0 kWh");
console.log(myTestVariable.toString());
tsconfig.json:
{
"include": ["./src/**/*"],
"compilerOptions": {
"target": "es2021",
"module": "CommonJS",
"rootDir": "./src",
"moduleResolution": "node",
"baseUrl": "./",
"typeRoots": [],
"outDir": "./dist/",
"esModuleInterop": true,
"skipLibCheck": true
}
}
At least the package.json:
{
"name": "openhab-build-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rm -r ./dist/* ; ./node_modules/typescript/bin/tsc --build && cp -r ./node_modules ./dist/",
"clean": "./node_modules/typescript/bin/tsc --build --clean"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.4.5"
},
"dependencies": {
"openhab": "^4.9.0"
}
}
The imports from the types folder should of course not end up in index.js.
Therefore I have listed the types folder of the openhab package in the typeRoots property of tsconfig.json:
{
//...
"typeRoots": ["./node_modules/@types", "./node_modules/openhab/types"]
}
But then i get a build error:
error TS2688: Cannot find type definition file for 'items'.
The file is in the program because:
Entry point for implicit type library 'items'
Found 1 error.
I am grateful to anyone who can help me find a solution.