Let your computer welcome you with music

welcomehome 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. ##### SNIPPETS #####
  3. log() {
  4. # Beautiful logs
  5. date=`date +"%d/%m/%Y - %H:%M:%S"`
  6. echo "[$date]" $1
  7. }
  8. display_err_arg() {
  9. echo "Usage: welcomehome --start $(tput smul)configuration file$(tput rmul)"
  10. echo "OR"
  11. echo "Usage: welcomehome --stop"
  12. }
  13. parse_arg() {
  14. # Echoes the value of a given parameter in a given configuration file
  15. value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
  16. value=$(echo $value | sed "s,^\s+,,")
  17. echo $value
  18. }
  19. check_url() {
  20. # Check if URL is starting with "http(s)://"
  21. # Returns the same code as grep, which is 1 if incorrect,
  22. # 0 if correct
  23. echo $1 | grep -E "^https?\://"
  24. return $?
  25. }
  26. ##### STREAM CONTROL #####
  27. set_volume() {
  28. # Set the volume to the right value
  29. volume=$1
  30. log "Set volume to $volume"
  31. data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
  32. # Using sed cuz simple quotes
  33. data=`echo $data | sed "s/VOLUME/${volume}/"`
  34. # Send the load
  35. curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
  36. }
  37. play_stream() {
  38. # Reset playback state
  39. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
  40. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' http://localhost:6680/mopidy/rpc > /dev/null
  41. # Add track infos
  42. stream=$1
  43. log "Playing stream $stream"
  44. data='{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": [ [ { "__model__": "Track", "uri": "STREAM" } ] ] }'
  45. # Using sed cuz simple quotes
  46. data=`echo $data | sed "s,STREAM,${stream},"`
  47. # Play dat sound
  48. curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
  49. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
  50. }
  51. stop_stream() {
  52. # Stop dat sound
  53. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
  54. log "Stream stopped"
  55. }
  56. ##### CONTROLLERS #####
  57. start_playing() {
  58. touch $lock_file
  59. play_stream $stream
  60. }
  61. stop_playing() {
  62. if [ -f $lock_file ]; then
  63. rm $lock_file
  64. fi
  65. stop_stream
  66. }
  67. ##### MAIN FUNCTIONS #####
  68. init() {
  69. # Initialise the variables
  70. config_file=$1
  71. # Check if the config file exists
  72. if [ ! -f $config_file ]; then
  73. echo "$config_file is not a file"
  74. exit 1
  75. fi
  76. log "Parsing the configuration file"
  77. # Get parameters from the given configuration file
  78. volume=$(parse_arg $config_file "volume")
  79. stream=$(parse_arg $config_file "stream")
  80. threshold=$(parse_arg $config_file "device_threshold")
  81. address=$(parse_arg $config_file "device_address")
  82. # Check stream URL validity
  83. if [ ! $(check_url $stream) ]; then
  84. echo "Incorrect stream address"
  85. exit 1
  86. fi
  87. # Check volume value validity
  88. if [ $volume -gt 100 ] || [ $volume -lt 0 ]; then
  89. echo "Invalid volume value"
  90. exit 1
  91. fi
  92. # Set volume to the right value
  93. set_volume $volume
  94. # Removing lock file in case of ungraceful shutdown
  95. if [ ! -f $lock_file ]; then
  96. rm $lock_file
  97. fi
  98. log "Initialisation complete"
  99. }
  100. main() {
  101. #Main loop
  102. log "Looking for device"
  103. while true; do
  104. # Polling the device's IP address
  105. count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
  106. if [ $count -gt 0 ]; then
  107. # Lock file present = music already playing
  108. if [ ! -f $lock_file ]; then
  109. log "Device detected"
  110. start_playing
  111. fi
  112. else
  113. if [ -f $lock_file ]; then
  114. log "Device lost"
  115. stop_playing
  116. fi
  117. fi
  118. done
  119. }
  120. ##### PROGRAM #####
  121. if [ $# -eq 0 ] || [ $# -gt 2 ]; then
  122. echo "Wrong arguments number"
  123. display_err_arg
  124. exit 1
  125. fi
  126. # Setting lock file path
  127. lock_file="/etc/welcomehome/home.lck"
  128. # Check if we know the requested feature
  129. case "$1" in
  130. "--start") init $2
  131. main
  132. ;;
  133. "--stop") stop_playing
  134. ;;
  135. *) echo "Unknown argument $1"
  136. display_err_arg
  137. ;;
  138. esac