zwave-js / zwave-js-ui

Full featured Z-Wave Control Panel UI and MQTT gateway. Built using Nodejs, and Vue/Vuetify
https://zwave-js.github.io/zwave-js-ui
MIT License
972 stars 203 forks source link

OpenWrt (and other minimal embedded environment) support #3473

Closed dwmw2 closed 10 months ago

dwmw2 commented 11 months ago

We package Domoticz for the OpenWrt distribution, and have historically used OpenZWave with it. Domoticz is now switching to ZWave-JS-UI so it would be great to package ZWave-JS-UI to work out of the box on OpenWrt too: https://github.com/nxhack/openwrt-node-packages/pull/1826

An always-on embedded router/AP is a perfect place to run home automation, but the environment can be a little constrained in CPU, memory and "disk" storage. For the last few years mine has been running on a MIPS24k with 128MiB of RAM.

We have a proof of concept basically working (not on that box, but on a quad Arm7 with 256MiB of RAM). I'd appreciate some help cleaning it up though, please. There are a few things to look at. Please let me know if I should file separate issues for each.

1. The package is huge — about 100MiB.

Are we doing it wrong? Are there any parts which can be left out? Here are some of the largest files....

644 ./node_modules/vue/node_modules/@vue/compiler-sfc/dist/compiler-sfc.js
644 ./node_modules/vue/packages/compiler-sfc/dist/compiler-sfc.js
676 ./node_modules/vis-network/standalone/esm/vis-network.min.js
676 ./node_modules/vis-network/standalone/umd/vis-network.min.js
704 ./node_modules/vis-network/esnext/esm/vis-network.js
740 ./node_modules/mqtt/dist/mqtt.js
748 ./node_modules/vis-network/esnext/umd/vis-network.js
1136    ./dist/assets/index-822e6be1.js
1192    ./node_modules/vis-network/peer/esm/vis-network.js
1224    ./node_modules/vis-network/peer/umd/vis-network.js
1364    ./node_modules/vis-network/dist/vis-network.esm.js
1368    ./node_modules/vuetify/dist/json/web-types.json
1400    ./node_modules/vis-network/dist/vis-network.js
1552    ./node_modules/vis-network/standalone/esm/vis-network.js
1592    ./node_modules/vis-network/standalone/umd/vis-network.js
1628    ./node_modules/vuetify/dist/vuetify.js
2748    ./node_modules/@mdi/js/mdi.js
2928    ./node_modules/@mdi/js/commonjs/mdi.js

Some of those seem like amalgamated or preprocessed versions of the original sources; in some cases we end up with what looks like three copies of the same code. Do we need to ship them all? In the case of MQTT there's a separate package for that anyway, which has the same three files that the zwave-js-ui package is shipping too.

root@OpenWrt:/usr/lib/node/mqtt/dist# ls -l
-rw-r--r--    1 root     root        319479 Nov 27 07:02 mqtt.esm.js
-rw-r--r--    1 root     root        741706 Nov 27 07:02 mqtt.js
-rw-r--r--    1 root     root        319504 Nov 27 07:02 mqtt.min.js

Some help understanding how this stuff works would be much appreciated.

Also, is it possible to compile down to bytecode or even native code, and ship only that to the target system? That might help with...

2. Memory usage

Its virtual memory size is 180MiB. Is there anything which can be done to reduce that? Is the full UI loaded into the interpreter even when only the ZWave←→MQTT bridge is active? Is it possible to run a version with the UI completely removed? It would actually be OK even if the user has to stop the zwave-js-ui process, and restart it with UI enabled.

Of course it would be nice to have a separate minimal server, and run the UI on demand (and as much as possible on the browser side), but that's probably a lot more work. Anything we can do to reduce memory usage in a relatively non-intrusive fashion would be useful though.

3. File system usage in /etc/zwave-js-ui.

In OpenWrt we typically use a writable overlay flash file system on top of a read-only root, and have the following categories of storage:

• The initial root filesystem (including the 100MiB package, as discussed above). • Files written to flash in the overlay, which are preserved across upgrade. • Files written to flash in the overlay, which are lost across upgrade. • Files in RAM, lost on every reboot.

We try to reduce the amount written to the flash file system — both in terms of size, and frequency of writes. We also try to minimize the amount that's preserved across system upgrade, as it's packaged into a smallish tarball which is then extracted into the new system after upgrade. Looking through the contents of /etc/zwave-js-ui, here's my idea of what might go where; please let me know if it's sane...

robertsLando commented 10 months ago

I think this can be closed? @dwmw2

dwmw2 commented 10 months ago

Yes, I think we can close it; thanks. If we want to revisit ways of reducing memory usage in future that can be done separately. I'll work with @nxhack on OpenWrt packaging.

dwmw2 commented 10 months ago

Not sure if this is expected, but on a completely fresh setup if I just run 'npm run bundle' it says

Asset "./dist" does not exist. Skipping...

I ran npm run build and now I have a dist/ directory.

robertsLando commented 10 months ago

Not sure if this is expected, but on a completely fresh setup if I just run 'npm run bundle' it says

Yeah that's expected, you must compile frontend before running npm run bundle command.