functions 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env bash
  2. export ZWAVE_JS_CONF="${SNAP}/lib/zwavejs2mqtt/node_modules/@zwave-js/config"
  3. if [ -z "${DAEMONIZED}" ]; then
  4. DAEMONIZED=0
  5. fi
  6. function lprint {
  7. if [ ${DAEMONIZED} -eq 0 ]; then
  8. echo ${1}
  9. else
  10. logger "${SNAP_NAME}: ${1}"
  11. fi
  12. }
  13. function ensure_zwavejs_config {
  14. # Not needed anymore as of ZWAVEJS_EXTERNAL_CONFIG
  15. # if [ "$(find ${ZWAVE_JS_CONF} -maxdepth 0 -empty -exec echo empty \;)" == "empty" ]; then
  16. # logger "Config directory is empty, copying data from ${SNAP}/snap/zwave-js/config"
  17. # rsync -raz "${SNAP}/snap/zwave-js/config" "${ZWAVE_JS_CONF}/.."
  18. # else
  19. # logger "Config directory ${ZWAVE_JS_CONF} exists"
  20. # fi
  21. return 0
  22. }
  23. function get_network_key {
  24. if [ -f "${SNAP_DATA}/settings.json" ]; then
  25. SETTINGS_KEY=$(jq '.zwave.networkKey' -r "${SNAP_DATA}/settings.json")
  26. if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then
  27. lprint "${SETTINGS_KEY}"
  28. return 1
  29. fi
  30. return 0
  31. fi
  32. return 0
  33. }
  34. function same_network_key {
  35. # SKIP NOW: NETWORK KEY SHOULD NOT BE USED
  36. # FAVOR SETTING
  37. # if [ -f "${SNAP_DATA}/settings.json" ]; then
  38. # SETTINGS_KEY=$(jq '.zwave.networkKey' -r ${SNAP_DATA}/settings.json)
  39. # if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then
  40. # echo "${SETTINGS_KEY}"
  41. # return 1
  42. # fi
  43. # return 0
  44. # fi
  45. return 0
  46. }
  47. function cpy_2_zui {
  48. if snapctl is-connected zui-store; then
  49. if [ "$(ls "${SNAP_COMMON}/zui-store" | wc -l)" -ne 0 ]; then
  50. lprint "The zui-store (${SNAP_COMMON}/zui-store) is not empty, exiting..." >&2
  51. exit 1
  52. elif [ -d "${SNAP_COMMON}/zui-store" ]; then
  53. cp -rf "${SNAP_DATA}/"* "${SNAP_COMMON}/zui-store"
  54. if [ $? -ne 0 ]; then
  55. lprint "Failed copying data from ${SNAP_COMMON}/zui-store" >&2
  56. return 1
  57. fi
  58. mkdir -p "${SNAP_COMMON}/zui-store/logs/zwave-js"
  59. mv "${SNAP_COMMON}/zui-store/zwave"*.{log,json} "${SNAP_COMMON}/zui-store/logs/zwave-js"
  60. mv "${SNAP_COMMON}/zui-store/logs" "${SNAP_COMMON}/zui-store/.old-z2m-logs"
  61. rm -rf "${SNAP_COMMON}/zui-store/.ext-config"
  62. touch "${SNAP_DATA}/.z2m-cpy"
  63. else
  64. lprint "Missing directory ${SNAP_COMMON}/zui-store, cannot write to it. Exiting..." >&2
  65. exit 1
  66. fi
  67. else
  68. lprint "Can not copy config: Missing connection «store-dir» (offered by <zwave-js-ui>: 'snap install zwave-js-ui')." >&2
  69. lprint "Please install and connect with:" 1>&2
  70. lprint "$ snap connect ${SNAP_NAME}:zui-store zwave-js-ui:store-dir" >&2
  71. return 1
  72. fi
  73. return 0
  74. }
  75. function plug_connected {
  76. if ! snapctl is-connected "${1}"; then
  77. lprint "Missing plug: «${1}»" >&2
  78. lprint "Connect with:"
  79. if [ "${1}" == "serial-port" ]; then
  80. lprint "$ sudo snap connect ${SNAP_NAME}:${1} <slot name>"
  81. else
  82. lprint "$ sudo snap connect ${SNAP_NAME}:${1}"
  83. fi
  84. return 1
  85. fi
  86. return 0
  87. }
  88. function plugs_connected {
  89. MISSING=0
  90. RAW_OUT=$(plug_connected "raw-usb")
  91. RAW_RES=$?
  92. SERIAL_OUT=$(plug_connected "serial-port")
  93. SERIAL_RES=$?
  94. if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then
  95. lprint $RAW_OUT
  96. lprint "----- OR -----"
  97. lprint $SERIAL_OUT
  98. lprint "See ${SNAP_NAME}.help for more info on the serial-port plug"
  99. MISSING=1
  100. fi
  101. plug_connected "hardware-observe"
  102. if [ $? -ne 0 ]; then
  103. MISSING=1
  104. fi
  105. if [ $MISSING -ne 0 ]; then
  106. return 1
  107. fi
  108. return 0
  109. }
  110. function is_root {
  111. if [ ${EUID:-$(id -u)} -eq 0 ]; then
  112. return 0
  113. fi
  114. return 1
  115. }
  116. function require_root {
  117. is_root
  118. if [ $? -eq 1 ]; then
  119. echo "Running as root is required." >&2
  120. echo "Re-run with sudo."
  121. exit 1
  122. fi
  123. }
  124. function testnset_config {
  125. lprint "Testing ${1}, or setting ${2}"
  126. RES=$(snapctl get ${1})
  127. if [ $? -ne 0 ] || [ -z "${RES}" ]; then
  128. lprint "Setting ${1}=${2}"
  129. RES=$(snapctl set ${1}=${2})
  130. if [ $? -ne 0 ]; then
  131. lprint "${RES}"
  132. exit 1
  133. fi
  134. return 0
  135. fi
  136. return 0
  137. }
  138. function test_priority_dir {
  139. Z2M_SETTINGS="${SNAP_DATA}/settings.json"
  140. if ! [ -w "${Z2M_SETTINGS}" ]; then
  141. return 1
  142. fi
  143. PRIORITY_DIR=$(echo "$(sed -E "s#$(dirname ${SNAP_DATA})/(current|[0-9]+)#${SNAP_DATA}#g" <<< $(cat "${Z2M_SETTINGS}" | jq '.zwave.deviceConfigPriorityDir'))" | tr -d '"')
  144. if [[ -d $(dirname "${PRIORITY_DIR}") && -w "${PRIORITY_DIR}" ]]; then
  145. jq --arg deviceConfigPriorityDir ${PRIORITY_DIR} '.zwave.deviceConfigPriorityDir = $deviceConfigPriorityDir' "${Z2M_SETTINGS}" > "${Z2M_SETTINGS}.tmp"
  146. if [ $? -eq 0 ]; then
  147. mv "${Z2M_SETTINGS}.tmp" "${Z2M_SETTINGS}"
  148. fi
  149. elif ! [[ "${PRIORITY_DIR}" =~ ^"$(dirname $SNAP_DATA)"* ]]; then
  150. jq --arg deviceConfigPriorityDir "${SNAP_DATA}/devicePriorityConfig" '.zwave.deviceConfigPriorityDir = $deviceConfigPriorityDir' "${Z2M_SETTINGS}" > "${Z2M_SETTINGS}.tmp"
  151. if [ $? -eq 0 ]; then
  152. mv "${Z2M_SETTINGS}.tmp" "${Z2M_SETTINGS}"
  153. fi
  154. fi
  155. return 0
  156. }
  157. function test_default_config {
  158. testnset_config "server.host" "localhost"
  159. testnset_config "server.port" 8091
  160. testnset_config "server.ssl" false
  161. testnset_config "server.force-disable-ssl" false
  162. testnset_config "session.cookie-secure" $(snap get server.ssl)
  163. testnset_config "session.secret" $(uuid)
  164. testnset_config "mqtt.name" ""
  165. }