Browse Source

Init commit

Joachim M. Giæver 4 years ago
parent
commit
01ce8ae1b6
7 changed files with 246 additions and 0 deletions
  1. 101 0
      snap/snapcraft.yaml
  2. 23 0
      src/bin/daemonize
  3. 9 0
      src/bin/de-daemonize
  4. 89 0
      src/bin/env-wrapper
  5. 3 0
      src/bin/restart
  6. 9 0
      src/hooks/configure
  7. 12 0
      src/hooks/install

+ 101 - 0
snap/snapcraft.yaml

@@ -0,0 +1,101 @@
+name: zwavejs2mqtt
+base: core20
+version: 'v1.0.5'
+summary: Fully configurable Zwave to MQTT Gateway and Control Panel. 
+description: |
+  - Configurable Zwave to Mqtt Gateway
+  - Home Assistant integration (beta)
+  - Zwave Control Panel:
+    - Nodes management: check all nodes discovered in the z-wave network, send/receive nodes values updates directly from the UI and send action to the nodes and controller for diagnostics and network heal Custom Node naming and Location: Starting from v1.3.0 nodes name and location are stored in a JSON file named nodes.json. This because not all nodes have native support for naming and location features (#45). This change is back compatible with older versions of this package: on startup it will get all nodes names and location from the zwcfg_homeHEX.xml file (if present) and create the new nodes.json file based on that. This file can be imported/exported from the UI control panel with the import/export buttons placed on the top of nodes table, on the right of controller actions select.
+    - Firmware updates: You are able to send firmware updates to your devices using the UI, just select the controller action Begin Firmware Update
+    - Groups associations: create associations between nodes (also supports multi-instance associations, need to use last version of zwave-js)
+    - Custom scenes management
+  - Log debug in UI
+  - Mesh graph showing devices neighbors
+grade: stable # must be 'stable' to release into candidate/stable channels
+confinement: strict # use 'strict' once you have the right plugs and slots
+
+apps:
+  zwavejs2mqtt:
+    daemon: simple
+    restart-condition: always
+    environment:
+      STORE_DIR: $SNAP_DATA
+      DAEMONIZED: 1
+    command: usr/lib/zwavejs2mqtt/bin/www
+    command-chain:
+      - bin/env-wrapper
+    plugs:
+      - network
+      - network-bind
+      - raw-usb
+  exec:
+    environment:
+      STORE_DIR: $SNAP_DATA
+    command: usr/lib/zwavejs2mqtt/bin/www
+    command-chain:
+      - bin/env-wrapper
+    plugs:
+      - network
+      - network-bind
+      - raw-usb
+  help:
+    command: bin/env-wrapper --help
+  enable:
+    command: bin/daemonize
+  disable:
+    command: bin/de-daemonize
+  restart:
+    command: bin/restart
+
+layout:
+  /usr/src/app/store:
+    bind: $SNAP_DATA/zwavejs2mqtt/store
+  #/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config/devices/index.json:
+  /usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config/devices/index.json:
+    bind-file: $SNAP/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config/devices/index.json
+
+parts:
+  zwavejs2mqtt:
+    after: [nodejs]
+    plugin: npm
+    source: https://github.com/zwave-js/zwavejs2mqtt.git
+    source-tag: $SNAPCRAFT_PROJECT_VERSION
+    npm-node-version: '12.20.0'
+    build-packages:
+      - gcc
+      - build-essential
+    stage-packages:
+      - git
+    override-build: |
+      set -ex
+      npm config set unsafe-perm true
+      npm install detect-libc
+      npm run build
+      chmod +x $SNAPCRAFT_PART_BUILD/bin/www
+      cp -rf $SNAPCRAFT_PART_BUILD $SNAPCRAFT_PART_INSTALL/usr/lib/zwavejs2mqtt
+    stage:
+      - -usr/lib/zwavejs2mqtt/kubernetes
+      - -usr/lib/zwavejs2mqtt/kustomization.yaml
+      - -usr/lib/zwavejs2mqtt/docker
+      - -usr/lib/zwavejs2mqtt/.dockerignore
+      - -usr/lib/zwavejs2mqtt/.git*
+      - -usr/lib/zwavejs2mqtt/.markdown*
+  nodejs:
+    plugin: dump
+    source:
+      - on amd64: https://nodejs.org/dist/v12.20.0/node-v12.20.0-linux-x64.tar.gz
+      - on arm64: https://nodejs.org/dist/v12.20.0/node-v12.20.0-linux-arm64.tar.gz
+      - on armhf: https://nodejs.org/dist/v12.20.0/node-v12.20.0-linux-armv7l.tar.gz
+    organize:
+      '*.md' : nodejs/
+      LICENSE : nodejs/
+    prime:
+      - -include
+      - -share
+
+  local:
+    plugin: dump
+    source: src/
+    organize:
+      hooks: snap/hooks

+ 23 - 0
src/bin/daemonize

@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+if ! snapctl is-connected raw-usb; then
+    echo "Failed to enable ${SNAP_NAME} service!"
+    echo ""
+    echo "Consult the help command:"
+    echo "${SNAP_NAME}.exec --help"
+    echo ""
+    echo "Ensure ${SNAP_NAME} is booting successfully before daemonize it"
+    echo "by running the app manually (remove the --help flag)."
+    exit 1
+fi
+
+snapctl start --enable "${SNAP_NAME}.${SNAP_NAME}"
+
+if [ $? -eq 0 ]; then
+    echo "Service enabled! Follow logs with"
+    echo "$ snap logs ${SNAP_NAME}"
+    exit 0
+fi
+
+echo "Failed enabling service"
+exit 1

+ 9 - 0
src/bin/de-daemonize

@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+snapctl stop --disable "${SNAP_NAME}.${SNAP_NAME}"
+
+if [ $? -eq 0 ]; then
+    echo "Service ${SNAP_NAME} disabled."
+    exit 0
+fi
+echo "Failed disabling ${SNAP_NAME} service"
+exit 1

+ 89 - 0
src/bin/env-wrapper

@@ -0,0 +1,89 @@
+#!/usr/bin/env bash
+
+set -e
+
+if [ -z ${DAEMONIZED} ]; then
+    DAEMONIZED=0
+fi
+
+function lprint {
+    if [ ${DAEMONIZED} -eq 0 ]; then
+        echo ${1}
+    else
+        logger "${SNAP_NAME}: ${1}"
+    fi
+}
+
+if ! snapctl is-connected raw-usb; then
+    lprint "Please connect raw-usb interface!"
+    lprint "Run: snap connect ${SNAP_NAME}:raw-usb"
+    exit 1
+fi
+
+export USB_PATH=$(snapctl get usb-path)
+export SERVER_SSL=$(snapctl get server.ssl)
+export SERVER_HOST=$(snapctl get server.host)
+export SERVER_PORT=$(snapctl get server.port)
+
+export SERVER_URL=$(snapctl get server.url)
+export SERVER_WS_URL=$(snapctl get server.url)
+
+OPT_HELP=false
+
+if [ $(snapctl services "${SNAP_NAME}" | grep inactive | wc -l) -eq 0 -a ${OPT_HELP} = false ]; then
+    echo ""
+    for ARG in "${ARGV[@]}"; do
+        shift
+        if [ "${ARG}" = "--exec" ]; then
+            lprint "Service already running, showing help text."
+            lprint "Use: 'snap stop ${SNAP_NAME}' to stop the service and'"
+            lprint "to execute the ${SNAP_NAME} manually"
+            OPT_HELP=true
+            continue
+        fi
+        set -- "$@" "${ARG}"
+    done
+fi
+
+
+if [ ${OPT_HELP} = false ]; then
+    if [ ! -c "${USB_PATH}" ]; then 
+        lprint "usb-path (${USB_PATH}) does not exist, or is not a Character Device."
+        lprint "See: snap get ${SNAP_NAME} -d usb-path"
+        exit 1
+    fi
+else
+    echo "Zwavejs2mqtt ($ ($ 1 --version))"
+    echo ""
+    echo "IMPORTANT! The DAEMON/SERVICE is disabled by default after installation." 
+    echo "You have to manually 'daemonize' OpenZWave Daemon with executing the command"
+    echo ""
+    echo "$ ${SNAP_NAME}.enable"
+    echo ""
+    echo "But first set your configuration as descrived below"
+    echo ""
+    echo "Base configuration values:"
+    echo "- usb-path:               $(snapctl get "usb-path")"
+    echo "Server configuration values: $(snapctl get server -d)"
+    echo ""
+    echo "- server.ssl: Use secure communication"
+    echo "- server.host: IP address to bind to, e.g 127.0.0.1"
+    echo "- server.port: Port to reach the web interface"
+    echo "- server.url: Hostname for http/https"
+    echo "- server.ws-url: Hostname for websocket"
+    echo ""
+    echo "Set options with:         $ snap set ${SNAP_NAME} param=key"
+    echo "For example:              $ snap set ${SNAP_NAME} server.host=0.0.0.0"
+    echo ""
+    echo "Also see 'snap info ${SNAP_NAME}' for information about secure devices."
+    echo ""
+    echo "Follow the log with"
+    echo "$ snap logs ${SNAP_NAME} -f"
+    echo ""
+    echo "Other commands"
+    echo "Disable the daemon:       $ ${SNAP_NAME}.disable"
+    echo "Restart the daemon:       $ ${SNAP_NAME}.restart"
+    exit 0
+fi
+
+exec "$@"

+ 3 - 0
src/bin/restart

@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+set -e
+snapctl restart "${SNAP_NAME}.${SNAP_NAME}"

+ 9 - 0
src/hooks/configure

@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+set -e
+# silence is golden.
+
+RUNNING=$(snapctl services | grep "${SNAP_NAME}" | grep inactive | wc -l)
+if [ ${RUNNING} -eq 0 ]; then
+    snapctl restart "${SNAP_NAME}"
+fi

+ 12 - 0
src/hooks/install

@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+set -e
+
+snapctl set usb-path="/dev/ttyACM0"
+snapctl set server.ssl=false
+snapctl set server.host="localhost"
+snapctl set server.port="8091"
+snapctl set server.url=false
+snapctl set server.ws-url=false
+
+snapctl stop --disable "${SNAP_NAME}.${SNAP_NAME}"