functions 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 plug_connected {
  48. if ! snapctl is-connected "${1}"; then
  49. lprint "Missing plug: «${1}»" >&2
  50. lprint "Connect with:"
  51. if [ "${1}" == "serial-port" ]; then
  52. lprint "$ sudo snap connect ${SNAP_NAME}:${1} <slot name>"
  53. else
  54. lprint "$ sudo snap connect ${SNAP_NAME}:${1}"
  55. fi
  56. return 1
  57. fi
  58. return 0
  59. }
  60. function plugs_connected {
  61. MISSING=0
  62. RAW_OUT=$(plug_connected "raw-usb")
  63. RAW_RES=$?
  64. SERIAL_OUT=$(plug_connected "serial-port")
  65. SERIAL_RES=$?
  66. if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then
  67. lprint $RAW_OUT
  68. lprint "----- OR -----"
  69. lprint $SERIAL_OUT
  70. lprint "See ${SNAP_NAME}.help for more info on the serial-port plug"
  71. MISSING=1
  72. fi
  73. plug_connected "hardware-observe"
  74. if [ $? -ne 0 ]; then
  75. MISSING=1
  76. fi
  77. if [ $MISSING -ne 0 ]; then
  78. return 1
  79. fi
  80. return 0
  81. }
  82. function is_root {
  83. if [ ${EUID:-$(id -u)} -eq 0 ]; then
  84. return 0
  85. fi
  86. return 1
  87. }
  88. function require_root {
  89. is_root
  90. if [ $? -eq 1 ]; then
  91. echo "Running as root is required." >&2
  92. echo "Re-run with sudo."
  93. exit 1
  94. fi
  95. }
  96. function testnset_config {
  97. lprint "Testing ${1}, or setting ${2}"
  98. RES=$(snapctl get ${1})
  99. if [ $? -ne 0 ] || [ -z "${RES}" ]; then
  100. lprint "Setting ${1}=${2}"
  101. RES=$(snapctl set ${1}=${2})
  102. if [ $? -ne 0 ]; then
  103. lprint "${RES}"
  104. exit 1
  105. fi
  106. return 0
  107. fi
  108. return 0
  109. }
  110. function test_priority_dir {
  111. Z2M_SETTINGS="${SNAP_DATA}/settings.json"
  112. if ! [ -w "${Z2M_SETTINGS}" ]; then
  113. return 1
  114. fi
  115. PRIORITY_DIR=$(echo "$(sed -E "s#$(dirname ${SNAP_DATA})/(current|[0-9]+)#${SNAP_DATA}#g" <<< $(cat "${Z2M_SETTINGS}" | jq '.zwave.deviceConfigPriorityDir'))" | tr -d '"')
  116. if [[ -d $(dirname "${PRIORITY_DIR}") && -w "${PRIORITY_DIR}" ]]; then
  117. jq --arg deviceConfigPriorityDir ${PRIORITY_DIR} '.zwave.deviceConfigPriorityDir = $deviceConfigPriorityDir' "${Z2M_SETTINGS}" > "${Z2M_SETTINGS}.tmp"
  118. if [ $? -eq 0 ]; then
  119. mv "${Z2M_SETTINGS}.tmp" "${Z2M_SETTINGS}"
  120. fi
  121. elif ! [[ "${PRIORITY_DIR}" =~ ^"$(dirname $SNAP_DATA)"* ]]; then
  122. jq --arg deviceConfigPriorityDir "${SNAP_DATA}/devicePriorityConfig" '.zwave.deviceConfigPriorityDir = $deviceConfigPriorityDir' "${Z2M_SETTINGS}" > "${Z2M_SETTINGS}.tmp"
  123. if [ $? -eq 0 ]; then
  124. mv "${Z2M_SETTINGS}.tmp" "${Z2M_SETTINGS}"
  125. fi
  126. fi
  127. return 0
  128. }
  129. function test_default_config {
  130. testnset_config "server.host" "localhost"
  131. testnset_config "server.port" 8091
  132. testnset_config "server.ssl" false
  133. testnset_config "server.force-disable-ssl" false
  134. testnset_config "session.cookie-secure" $(snap get server.ssl)
  135. testnset_config "session.secret" $(uuid)
  136. testnset_config "mqtt.name" ""
  137. }