mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-29 12:34:21 +02:00
[jsscripting] Sync docs from openhab-js repo (#21074)
Signed-off-by: Florian Hotze <dev@florianhotze.com>
This commit is contained in:
@@ -1611,12 +1611,61 @@ Follow these steps to create your own library (it's called a CommonJS module):
|
|||||||
1. After you've installed it with `npm`, you can continue development of the library inside `node_modules`.
|
1. After you've installed it with `npm`, you can continue development of the library inside `node_modules`.
|
||||||
1. As you might have already noticed, the JavaScript Scripting add-on is reloading a script as soon as one of its dependencies changes.
|
1. As you might have already noticed, the JavaScript Scripting add-on is reloading a script as soon as one of its dependencies changes.
|
||||||
When developing a library inside `node_modules`, this can cause regular reloads.
|
When developing a library inside `node_modules`, this can cause regular reloads.
|
||||||
To avoid this situation, you can disable dependency tracking in the JavaScript Scripting add-on settings (you need to tick "show advanced" for the setting to come up).
|
To avoid this situation, you can disable dependency tracking in the JavaScript Scripting add-on settings (you need to tick _Show advanced_ for the setting to come up).
|
||||||
|
|
||||||
It is also possible to upload your library to [npm](https://npmjs.com) to share it with other users.
|
It is also possible to upload your library to [npm](https://npmjs.com) to share it with other users.
|
||||||
|
|
||||||
If you want to get some advanced information, you can read [this blog post](https://bugfender.com/blog/how-to-create-an-npm-package/) or just google it.
|
If you want to get some advanced information, you can read [this blog post](https://bugfender.com/blog/how-to-create-an-npm-package/) or just google it.
|
||||||
|
|
||||||
|
### Debugging
|
||||||
|
|
||||||
|
The JavaScript Scripting add-on provides built-in debugger support for `.js` scripts from `automation/js` in the user configuration directory.
|
||||||
|
This allows attaching any debugger compatible with the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/), e.g., [Visual Studio Code](https://code.visualstudio.com/docs/nodejs/nodejs-debugging) or [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/javascript).
|
||||||
|
|
||||||
|
Debugger support has to be enabled in the JavaScript Scripting add-on settings (you need to tick _Show advanced_ for the setting to come up).
|
||||||
|
The debugger will listen on port 9229 by default, but this can be changed in the settings.
|
||||||
|
Please note: A restart of openHAB is required to apply the changes.
|
||||||
|
|
||||||
|
::: warning
|
||||||
|
Enabling debugger support will allow any system on your local network to attach to the debugger and execute arbitrary code in your openHAB instance.
|
||||||
|
Only enable debugger support if you need it and you trust all systems on your local network,
|
||||||
|
or consider using a firewall to restrict access to the debugger port to only trusted systems, or use SSH tunneling to access the debugger port from a remote system.
|
||||||
|
:::
|
||||||
|
|
||||||
|
#### VS Code
|
||||||
|
|
||||||
|
To debug a script in VS Code, you need to create a launch configuration in `.vscode/launch.json` in your workspace folder:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"request": "attach",
|
||||||
|
"name": "Attach to openHAB JavaScript Scripting",
|
||||||
|
// openHAB server address
|
||||||
|
"address": "127.0.0.1",
|
||||||
|
"port": 9229,
|
||||||
|
// path to scripts on the VS Code host (your machine)
|
||||||
|
"localRoot": "${workspaceFolder}/automation/js", // assumes your workspace folder is the openHAB user configuration directory
|
||||||
|
// path to scripts on the openHAB server
|
||||||
|
"remoteRoot": "/etc/openhab/automation/js", // Linux default, on Docker use /openhab/conf/automation/js
|
||||||
|
"restart": true,
|
||||||
|
"continueOnAttach": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Make sure to adjust the `address`, `localRoot` and `remoteRoot` values to your environment.
|
||||||
|
|
||||||
|
::: tip Note
|
||||||
|
Docker users need to expose the debugger port to the host machine by adding `-p 9229:9229` to the `docker run` command.
|
||||||
|
:::
|
||||||
|
|
||||||
|
Finally, add breakpoints in your script and attach the debugger in the _Run and Debug_ view in VS Code.
|
||||||
|
|
||||||
### @runtime
|
### @runtime
|
||||||
|
|
||||||
In most cases, the [Standard Library](#standard-library) provides pure-JS APIs to interact with the openHAB runtime.
|
In most cases, the [Standard Library](#standard-library) provides pure-JS APIs to interact with the openHAB runtime.
|
||||||
|
|||||||
Reference in New Issue
Block a user