Let your computer welcome you with music

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. ##### FUNCTIONS #####
  3. log() {
  4. # Beautiful logs
  5. date=`date +"%d/%m/%Y - %H:%M:%S"`
  6. echo "[$date]" $1
  7. }
  8. parse_arg() {
  9. # Echoes the value of a given parameter in a given configuration file
  10. value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
  11. value=$(echo $value | sed "s,^\s+,,")
  12. echo $value
  13. }
  14. check_url() {
  15. # Check if URL is starting with "http(s)://"
  16. # Returns the same code as grep, which is 1 if incorrect,
  17. # 0 if correct
  18. echo $1 | grep -E "^https?\://"
  19. return $?
  20. }
  21. check_volume() {
  22. # Check if the volume value is valid
  23. if [ $1 -gt 100 ] || [ $1 -lt 0 ]; then
  24. echo "Invalid volume value"
  25. exit 1
  26. fi
  27. }
  28. set_volume() {
  29. # Set the volume to the right value
  30. volume=$1
  31. check_volume $volume
  32. log "Set volume to $volume"
  33. data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
  34. # Using sed cuz simple quotes
  35. data=`echo $data | sed "s/VOLUME/${volume}/"`
  36. # Send the load
  37. curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
  38. }
  39. play_stream() {
  40. # Reset playback state
  41. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
  42. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' http://localhost:6680/mopidy/rpc > /dev/null
  43. # Add track infos
  44. stream=$1
  45. log "Playing stream $stream"
  46. data='{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": [ [ { "__model__": "Track", "uri": "STREAM" } ] ] }'
  47. # Using sed cuz simple quotes
  48. data=`echo $data | sed "s,STREAM,${stream},"`
  49. # Play dat sound
  50. curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
  51. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
  52. # The right music at the righ volume. Nothing's more perfect.
  53. set_volume $2
  54. }
  55. stop_stream() {
  56. # Stop dat sound
  57. curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
  58. log "Stream stopped"
  59. }
  60. ##### PROGRAM #####
  61. if [[ $# -eq 0 || ! -f $1 ]]; then
  62. echo "I need a config file as first argument"
  63. exit 1
  64. fi
  65. config_file=$1
  66. log "Parsing the configuration file"
  67. # Get parameters from the given configuration file
  68. volume=$(parse_arg $config_file "volume")
  69. stream=$(parse_arg $config_file "stream")
  70. threshold=$(parse_arg $config_file "device_threshold")
  71. address=$(parse_arg $config_file "device_address")
  72. if [ ! $(check_url $stream) ]; then
  73. echo "Incorrect stream address"
  74. exit 1
  75. fi
  76. log "Initialisation complete"
  77. while true; do
  78. # Lock file
  79. lock_file="/etc/welcomehome/home.lck"
  80. count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
  81. if [ $count -gt 0 ]; then
  82. # Lock file present = music already playing
  83. if [ ! -f $lock_file ]; then
  84. log "Device detected"
  85. play_stream $stream $volume
  86. touch $lock_file
  87. fi
  88. else
  89. if [ -f $lock_file ]; then
  90. log "Device lost"
  91. stop_stream
  92. rm $lock_file
  93. fi
  94. fi
  95. done