functions 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env bash
  2. export ZWAVE_JS_CONF="${SNAP}/lib/node_modules/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 same_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 plug_connected {
  25. if ! snapctl is-connected "${1}"; then
  26. echo "Missing plug: «${1}»" >&2
  27. echo "Connect with:"
  28. echo "$ sudo snap connect ${SNAP_NAME}:${1}"
  29. return 1
  30. fi
  31. return 0
  32. }
  33. function plugs_connected {
  34. MISSING=0
  35. RAW_OUT=$(plug_connected "raw-usb")
  36. RAW_RES=$?
  37. SERIAL_OUT=$(plug_connected "serial-port")
  38. SERIAL_RES=$?
  39. if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then
  40. echo $RAW_OUT
  41. echo "----- OR -----"
  42. echo $SERIAL_OUT
  43. MISSING=1
  44. elif [ $RAW_RES -ne 0 ]; then
  45. echo "OPTIONAL plug:"
  46. echo $RAW_OUT
  47. elif [ $SERIAL_RES -ne 0 ]; then
  48. echo "OPTIONAL plug"
  49. echo $SERIAL_OUT
  50. fi
  51. plug_connected "hardware-observe"
  52. if [ $? -ne 0 ]; then
  53. MISSING=1
  54. fi
  55. if [ $MISSING -ne 0 ]; then
  56. return 1
  57. fi
  58. return 0
  59. }
  60. function is_root {
  61. if [ ${EUID:-$(id -u)} -eq 0 ]; then
  62. return 0
  63. fi
  64. return 1
  65. }
  66. function require_root {
  67. is_root
  68. if [ $? -eq 1 ]; then
  69. echo "Running as root is required." >&2
  70. echo "Re-run with sudo."
  71. exit 1
  72. fi
  73. }
  74. function testnset_config {
  75. logger "${SNAP_NAME}: Testing ${1}, or setting ${2}"
  76. RES=$(snapctl get ${1})
  77. if [ $? -ne 0 ] || [ -z "${RES}" ]; then
  78. logger "${SNAP_NAME}: Setting ${1}=${2}"
  79. RES=$(snapctl set ${1}=${2})
  80. if [ $? -ne 0 ]; then
  81. logger "${RES}"
  82. exit 1
  83. fi
  84. return 0
  85. else
  86. logger "${SNAP_NAME}: ${1} has ${RES}, leaving as is"
  87. fi
  88. return 0
  89. }
  90. function test_default_config {
  91. testnset_config "server.ssl" false
  92. testnset_config "session.cookie-secure" $(snap get server.ssl)
  93. testnset_config "session.secret" $(uuid)
  94. testnset_config "server.host" "localhost"
  95. testnset_config "server.port" 8091
  96. }