functions 5.2 KB

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