Browse Source

Move lprint and add more logging

Joachim M. Giæver 1 year ago
parent
commit
ccdd066747

+ 9 - 3
src/bin/cpy-2-zui

@@ -12,9 +12,15 @@ if [ $? -ne 0 ]; then
 fi
 
 if snapctl is-connected serial-port; then
-	echo "Serial-port is connected, which require manual configuration in zwave-js-ui"
+	lprint "Serial-port is connected, which require manual configuration in zwave-js-ui"
+elif [ -s "${SNAP_COMMON}/zui-store/.hook.install" ]; then
+	if [ "$(cat "${SNAP_COMMON}/.hook.install")" == "enable" ]; then
+		snapctl stop "${SNAP_NAME}" && snapctl stop --disable "${SNAP_NAME}"
+		lprint "Disabled ${SNAP_NAME} and zwave-js-ui should be enabled."
+	else
+		lprint "ZUI is set to be disabled, and wont disable ${SNAP_NAME}.".
+	fi
 else
-	snapctl stop "${SNAP_NAME}" && snapctl stop --disable "${SNAP_NAME}"
-	echo "Disabled ${SNAP_NAME}, you can now enable zwave-js-ui"
+	lprint "Wont disable Z2M"
 fi
 

+ 0 - 11
src/helper/env-wrapper

@@ -2,17 +2,6 @@
 
 source $SNAP/helper/functions
 
-if [ -z "${DAEMONIZED}" ]; then
-    DAEMONIZED=0
-fi
-
-function lprint {
-    if [ ${DAEMONIZED} -eq 0 ]; then
-        echo ${1}
-    else
-        logger "${SNAP_NAME}: ${1}"
-    fi
-}
 
 # WITH S0+n NETWORK KEY IS NOT USED ANYMORE
 #export NETWORK_KEY=$(snapctl get network.key)

+ 30 - 18
src/helper/functions

@@ -2,6 +2,18 @@
 
 export ZWAVE_JS_CONF="${SNAP}/lib/zwavejs2mqtt/node_modules/@zwave-js/config"
 
+if [ -z "${DAEMONIZED}" ]; then
+    DAEMONIZED=0
+fi
+
+function lprint {
+    if [ ${DAEMONIZED} -eq 0 ]; then
+        echo ${1}
+    else
+        logger "${SNAP_NAME}: ${1}"
+    fi
+}
+
 function ensure_zwavejs_config {
     # Not needed anymore as of ZWAVEJS_EXTERNAL_CONFIG
     # if [ "$(find ${ZWAVE_JS_CONF} -maxdepth 0 -empty -exec echo empty \;)" == "empty" ]; then
@@ -17,7 +29,7 @@ function get_network_key {
     if [ -f "${SNAP_DATA}/settings.json" ]; then
         SETTINGS_KEY=$(jq '.zwave.networkKey' -r "${SNAP_DATA}/settings.json")
         if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then
-            echo "${SETTINGS_KEY}"
+            lprint "${SETTINGS_KEY}"
             return 1
         fi
         return 0
@@ -42,12 +54,12 @@ function same_network_key {
 function cpy_2_zui {
 	if snapctl is-connected zui-store; then
 		if [ "$(ls "${SNAP_COMMON}/zui-store" | wc -l)" -ne 0 ]; then
-			echo "The zui-store (${SNAP_COMMON}/zui-store) is not empty, exiting..." >&2
+			lprint "The zui-store (${SNAP_COMMON}/zui-store) is not empty, exiting..." >&2
 			exit 1
 		elif [ -d "${SNAP_COMMON}/zui-store" ]; then
 			cp -rf "${SNAP_DATA}/"* "${SNAP_COMMON}/zui-store"
 			if [ $? -ne 0 ]; then
-				echo "Failed copying data from ${SNAP_COMMON}/zui-store" >&2
+				lprint "Failed copying data from ${SNAP_COMMON}/zui-store" >&2
 				return 1
 			fi
 			mkdir -p "${SNAP_COMMON}/zui-store/logs/zwave-js"
@@ -56,13 +68,13 @@ function cpy_2_zui {
 			rm -rf "${SNAP_COMMON}/zui-store/.ext-config"
 			touch "${SNAP_DATA}/.z2m-cpy"
 		else
-			echo "Missing directory ${SNAP_COMMON}/zui-store, cannot write to it. Exiting..." >&2
+			lprint "Missing directory ${SNAP_COMMON}/zui-store, cannot write to it. Exiting..." >&2
 			exit 1
 		fi
 	else
-		echo "Can not copy config: Missing connection «store-dir» (offered by <zwave-js-ui>: 'snap install zwave-js-ui')." >&2
-		echo "Please install and connect with:" 1>&2
-		echo "$ snap connect ${SNAP_NAME}:zui-store zwave-js-ui:store-dir" >&2
+		lprint "Can not copy config: Missing connection «store-dir» (offered by <zwave-js-ui>: 'snap install zwave-js-ui')." >&2
+		lprint "Please install and connect with:" 1>&2
+		lprint "$ snap connect ${SNAP_NAME}:zui-store zwave-js-ui:store-dir" >&2
 		return 1
 	fi
 	return 0
@@ -70,12 +82,12 @@ function cpy_2_zui {
 
 function plug_connected {
     if ! snapctl is-connected "${1}"; then
-        echo "Missing plug: «${1}»" >&2
-        echo "Connect with:"
+        lprint "Missing plug: «${1}»" >&2
+        lprint "Connect with:"
 		if [ "${1}" == "serial-port" ]; then
-			echo "$ sudo snap connect ${SNAP_NAME}:${1} <slot name>"
+			lprint "$ sudo snap connect ${SNAP_NAME}:${1} <slot name>"
 		else
-			echo "$ sudo snap connect ${SNAP_NAME}:${1}"
+			lprint "$ sudo snap connect ${SNAP_NAME}:${1}"
 		fi
         return 1
     fi
@@ -92,10 +104,10 @@ function plugs_connected {
     SERIAL_RES=$?
 
     if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then
-        echo $RAW_OUT
-        echo "----- OR -----"
-        echo $SERIAL_OUT
-        echo "See ${SNAP_NAME}.help for more info on the serial-port plug"
+        lprint $RAW_OUT
+        lprint "----- OR -----"
+        lprint $SERIAL_OUT
+        lprint "See ${SNAP_NAME}.help for more info on the serial-port plug"
         MISSING=1
     fi
 
@@ -128,13 +140,13 @@ function require_root {
 }
 
 function testnset_config {
-    logger "${SNAP_NAME}: Testing ${1}, or setting ${2}"
+    lprint "Testing ${1}, or setting ${2}"
     RES=$(snapctl get ${1})
     if [ $? -ne 0 ] || [ -z "${RES}" ]; then
-        logger "${SNAP_NAME}: Setting ${1}=${2}"
+        lprint "Setting ${1}=${2}"
         RES=$(snapctl set ${1}=${2})
         if [ $? -ne 0 ]; then
-            logger "${RES}"
+            lprint "${RES}"
             exit 1
         fi
         return 0

+ 5 - 4
src/hooks/configure

@@ -1,6 +1,7 @@
 #!/usr/bin/env bash
 
 set -e
+export DAEMONIZED=1
 source $SNAP/helper/functions
 
 SERVER_SSL=$(snapctl get server.ssl)
@@ -8,17 +9,17 @@ SERVER_PORT=$(snapctl get server.port)
 COOKIE_SECURE=$(snapctl get sessio.cookie-secure)
 
 if [ $(echo "${SERVER_PORT}" | grep -E "^\-?[0-9]+$") = "" ]; then
-    echo "!! server.port must be numeric, got ${SERVER_PORT}" >&2
+    lprint "!! server.port must be numeric, got ${SERVER_PORT}" >&2
     exit 1
 fi
 
 if [ ${SERVER_SSL} != true -a ${SERVER_SSL} != false ]; then
-    echo "!! server.ssl must be boolean value" >&2
+    lprint "!! server.ssl must be boolean value" >&2
     exit 1
 fi
 
 if [ ${COOKIE_SECURE} != true -a ${COOKIE_SECURE} != false ]; then
-    echo "!! session.cookie-secure must be boolean value" >&2
+    lprint "!! session.cookie-secure must be boolean value" >&2
     exit 1
 fi
 
@@ -37,6 +38,6 @@ test_default_config
 
 RUNNING=$(snapctl services | grep "${SNAP_NAME}" | grep inactive | wc -l)
 if [ ${RUNNING} -eq 0 ]; then
-    echo "Restarting ${SNAP_NAME}"
+    lprint "Restarting ${SNAP_NAME}"
     snapctl restart "${SNAP_NAME}"
 fi

+ 4 - 1
src/hooks/connect-plug-zui-store

@@ -1,5 +1,8 @@
 #!/usr/bin/env bash
 
+export DAEMONIZED=1
+source $SNAP/helper/functions
+
 $SNAP/bin/cpy-2-zui
 
-logger "Connected ZUI (from: z2m)"
+lprint "Connected ZUI (from: z2m)"

+ 1 - 0
src/hooks/install

@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
 
+export DAEMONIZED=1
 source $SNAP/helper/functions
 
 set -e 

+ 1 - 0
src/hooks/post-refresh

@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
 
+export DAEMONIZED=1
 source $SNAP/helper/functions
 
 # Not needed anymore as of ZWAVEJS_EXTERNAL_CONFIG?

+ 1 - 0
src/hooks/pre-refresh

@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
 
+export DAEMONIZED=1
 source $SNAP/helper/functions
 
 # Not needed anymore as of ZWAVEJS_EXTERNAL_CONFIG?

+ 12 - 2
src/hooks/prepare-plug-zui-store

@@ -1,4 +1,14 @@
 #!/usr/bin/env bash
 
-snapctl set :zui-store z2mconfig="$(snapctl get -d server mqtt session)"
-snapctl set :zui-store z2mserial="$(snapctl is-connected serial-port && echo 1 || echo 0)"
+export DAEMONIZED=1
+source $SNAP/helper/functions
+
+SNAP_CONFIG="$(snapctl get -d server mqtt session)"
+snapctl set :zui-store z2mconfig="${SNAP_CONFIG}"
+
+lprint "Export snap config over plug connection: ${SNAP_CONFIG}" 
+
+USING_SERIAL="$(snapctl is-connected serial-port && echo 1 || echo 0)"
+snapctl set :zui-store z2mserial="${USING_SERIAL}"
+
+lprint "Share serial-port plug be used ($([ "${USING_SERIAL}" -eq 1 ] && echo "True" || "False")) over plug connection."