-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[udf] Unlock JavaScript for user-defined functions
- Loading branch information
Showing
11 changed files
with
284 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* | ||
* Forward OwnTracks low-battery warnings to ntfy. | ||
* https://mqttwarn.readthedocs.io/en/latest/examples/owntracks-battery/readme.html | ||
* | ||
*/ | ||
|
||
// mqttwarn filter function, returning true if the message should be ignored. | ||
// In this case, ignore all battery level telemetry values above a certain threshold. | ||
function owntracks_batteryfilter(topic, message) { | ||
let ignore = true; | ||
let data; | ||
|
||
// Decode inbound message. | ||
try { | ||
data = JSON.parse(message); | ||
} catch { | ||
data = null; | ||
} | ||
|
||
// Evaluate filtering rule. | ||
if (data && "batt" in data && data.batt !== null) { | ||
ignore = Number.parseFloat(data.batt) > 20; | ||
} | ||
|
||
return ignore; | ||
} | ||
|
||
// Status message. | ||
console.log("Loaded JavaScript module."); | ||
|
||
// Export symbols. | ||
module.exports = { | ||
"owntracks_batteryfilter": owntracks_batteryfilter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
orphan: true | ||
--- | ||
|
||
(owntracks-ntfy-variants)= | ||
|
||
# OwnTracks-to-ntfy setup variants | ||
|
||
|
||
## About | ||
|
||
This section informs you about additional configuration and operation variants of the | ||
[](#owntracks-ntfy-recipe) recipe. For example, you may want to use Docker or Podman | ||
to run both mqttwarn and ntfy, or you may want to use another language than Python to | ||
implement your filtering function. | ||
|
||
|
||
## Docker and Podman | ||
|
||
### Running mqttwarn as container | ||
This command will run mqttwarn in a container, using the `docker` command to launch it. | ||
Alternatively, `podman` can be used. It expects an MQTT broker to be running on `localhost`, | ||
so it uses the `--network=host` option. The command will mount the configuration file and | ||
the user-defined functions file correctly, and will invoke mqttwarn with the corresponding | ||
`--config-file` option. | ||
```shell | ||
docker run --rm -it --network=host --volume=$PWD:/etc/mqttwarn \ | ||
ghcr.io/jpmens/mqttwarn-standard \ | ||
mqttwarn --config-file=mqttwarn-owntracks.ini | ||
``` | ||
|
||
### Running ntfy as container | ||
While this tutorial uses the ntfy service at ntfy.sh, it is possible to run your own | ||
instance. For example, use Docker or Podman. | ||
```shell | ||
docker run --name=ntfy --rm -it --publish=5555:80 \ | ||
binwiederhier/ntfy serve --base-url="http://localhost:5555" | ||
``` | ||
In this case, please adjust the ntfy configuration section `[config:ntfy]` to use | ||
a different URL, and make sure to restart mqttwarn afterwards. | ||
```ini | ||
[config:ntfy] | ||
targets = {'testdrive': 'http://localhost:5555/testdrive'} | ||
``` | ||
|
||
|
||
(owntracks-ntfy-variants-udf)= | ||
|
||
## Alternative languages for user-defined functions | ||
|
||
### JavaScript | ||
|
||
In order to try that on the OwnTracks-to-ntfy example, use the alternative | ||
`mqttwarn-owntracks.js` implementation by adjusting the `functions` setting within the | ||
`[defaults]` section of your configuration file, and restart mqttwarn. | ||
```ini | ||
[defaults] | ||
functions = mqttwarn-owntracks.js | ||
``` | ||
|
||
The JavaScript function `owntracks_batteryfilter()` implements the same rule as the | ||
previous one, which was written in Python. | ||
|
||
:::{literalinclude} mqttwarn-owntracks.js | ||
:language: javascript | ||
::: | ||
|
||
:::{attention} | ||
The feature to run JavaScript code is currently considered to be experimental. | ||
Please use it responsibly. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,9 @@ | |
"gspread>=2.1.1", | ||
"oauth2client>=4.1.2", | ||
], | ||
"javascript": [ | ||
"javascript==1!1.0.1; python_version>='3.7'", | ||
], | ||
"mysql": [ | ||
"mysql", | ||
], | ||
|
@@ -200,6 +203,8 @@ | |
"Operating System :: POSIX", | ||
"Operating System :: Unix", | ||
"Operating System :: MacOS", | ||
"Operating System :: Microsoft :: Windows", | ||
"Programming Language :: JavaScript", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3.6", | ||
"Programming Language :: Python :: 3.7", | ||
|
@@ -229,7 +234,7 @@ | |
author="Jan-Piet Mens, Ben Jones, Andreas Motl", | ||
author_email="[email protected]", | ||
url="https://github.com/mqtt-tools/mqttwarn", | ||
keywords="mqtt notification plugins data acquisition push transformation engine mosquitto", | ||
keywords="mqtt notification plugins data acquisition push transformation engine mosquitto python javascript", | ||
packages=find_packages(), | ||
include_package_data=True, | ||
package_data={ | ||
|
Oops, something went wrong.