Let your computer welcome you with music

welcomehome 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. log "Initialisation complete"
  95. }
  96. main() {
  97. #Main loop
  98. log "Looking for device"
  99. while true; do
  100. # Polling the device's IP address
  101. count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
  102. if [ $count -gt 0 ]; then
  103. # Lock file present = music already playing
  104. if [ ! -f $lock_file ]; then
  105. log "Device detected"
  106. start_playing
  107. fi
  108. else
  109. if [ -f $lock_file ]; then
  110. log "Device lost"
  111. stop_playing
  112. fi
  113. fi
  114. done
  115. }
  116. ##### PROGRAM #####
  117. if [ $# -eq 0 ] || [ $# -gt 2 ]; then
  118. echo "Wrong arguments number"
  119. display_err_arg
  120. exit 1
  121. fi
  122. # Setting lock file path
  123. lock_file="/etc/welcomehome/home.lck"
  124. # Check if we know the requested feature
  125. case "$1" in
  126. "--start") init $2
  127. main
  128. ;;
  129. "--stop") stop_playing
  130. ;;
  131. *) echo "Unknown argument $1"
  132. display_err_arg
  133. ;;
  134. esac