mqtt-tools / mqttwarn

A highly configurable MQTT message router, where the routing targets are notification plugins, primarily written in Python.
https://mqttwarn.readthedocs.io/
Eclipse Public License 2.0
950 stars 183 forks source link

[udf] Unlock JavaScript for user-defined functions #667

Open amotl opened 1 year ago

amotl commented 1 year ago

Hi there,

after bouncing on and off on the idea to leverage the javascript package for data transformation purposes, in order to get rid of our PutsReq instance, which is a resource hog, and ceased to exist anyway, and while being unsure whether or how to also bring it to Kotori or LorryStream exactly, I am finally making a start on behalf of mqttwarn.

On the PutsReq instance I was referring to, we are running small snippets of JavaScript code like putsreq.hiveeyes.ttn_v3.js, in order to converge telemetry data packages received from TTN into a format digested by Kotori, effectively just unwrapping a few levels of JSON nestedness.

Following this idea, and annoyed about PutsReq itself, we've also explored the problem space on behalf of other projects like flow-heater already. At about the same time, Cloudflare Workers have been released, allowing to route and modify HTTP requests in different ways, using JavaScript or TypeScript. /cc @elbart

This patch essentially brings the same idea to mqttwarn, where all the routing and transforming machinery is already in place, and just needed a laughably small patch to achieve the same things with JavaScript without much further ado, thanks to the excellent JSPyBridge by @extremeheat. 💯

A highly configurable MQTT message router, where the routing targets are notification plugins, primarily written in Python.

By providing an alternative way of writing user-defined functions, the patch challenges the current slogan "primarily written in Python". I hope you will still like it, and I will be happy to hear back about your opinion about it.

Rest assured I am not a strong advocate of the JavaScript language, nor its paradigms and its NPM ecosystem ^1. But I recognize the value of the V8 runtime underneath Node.js, that it's an excellent and robust piece of software, and that people are doing many amazing things of their own kind with it.

The preview of the corresponding documentation can be inspected here:

With kind regards, Andreas.

NB: Next stop: Lua? Edit: See GH-669. NB: Next stop: Java? See https://github.com/jpype-project/jpype and https://github.com/kivy/pyjnius. NB: Next stop: Golang? See https://github.com/grumpyhome/grumpy.

codecov[bot] commented 1 year ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (772f9f5) 49.75% compared to head (8cd99f7) 49.96%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #667 +/- ## ========================================== + Coverage 49.75% 49.96% +0.21% ========================================== Files 81 81 Lines 4034 4051 +17 ========================================== + Hits 2007 2024 +17 Misses 2027 2027 ``` | [Flag](https://app.codecov.io/gh/mqtt-tools/mqttwarn/pull/667/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/mqtt-tools/mqttwarn/pull/667/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools) | `49.96% <100.00%> (+0.21%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.