123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #!/bin/bash
-
- ##### SNIPPETS #####
-
- log() {
- # Beautiful logs
- date=`date +"%d/%m/%Y - %H:%M:%S"`
- echo "[$date]" $1
- }
-
- display_err_arg() {
- echo "Usage: welcomehome --start $(tput smul)configuration file$(tput rmul)"
- echo "OR"
- echo "Usage: welcomehome --stop"
- }
-
- parse_arg() {
- # Echoes the value of a given parameter in a given configuration file
- value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
- value=$(echo $value | sed "s,^\s+,,")
- echo $value
- }
-
- check_url() {
- # Check if URL is starting with "http(s)://"
- # Returns the same code as grep, which is 1 if incorrect,
- # 0 if correct
- echo $1 | grep -E "^https?\://"
- return $?
- }
-
- ##### STREAM CONTROL #####
-
- set_volume() {
- # Set the volume to the right value
- volume=$1
- log "Set volume to $volume"
-
- data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
- # Using sed cuz simple quotes
- data=`echo $data | sed "s/VOLUME/${volume}/"`
-
- # Send the load
- curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
- }
-
- play_stream() {
- # Reset playback state
- curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
- curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' http://localhost:6680/mopidy/rpc > /dev/null
-
- # Add track infos
- stream=$1
- log "Playing stream $stream"
-
- data='{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": [ [ { "__model__": "Track", "uri": "STREAM" } ] ] }'
- # Using sed cuz simple quotes
- data=`echo $data | sed "s,STREAM,${stream},"`
-
- # Play dat sound
- curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
- curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
- }
-
- stop_stream() {
- # Stop dat sound
- curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
- log "Stream stopped"
- }
-
- ##### CONTROLLERS #####
-
- start_playing() {
- touch $lock_file
- play_stream $stream
- }
-
- stop_playing() {
- if [ -f $lock_file ]; then
- rm $lock_file
- fi
- stop_stream
- }
-
- ##### MAIN FUNCTIONS #####
-
- init() {
- # Initialise the variables
- config_file=$1
-
- # Check if the config file exists
- if [ ! -f $config_file ]; then
- echo "$config_file is not a file"
- exit 1
- fi
-
- log "Parsing the configuration file"
-
- # Get parameters from the given configuration file
- volume=$(parse_arg $config_file "volume")
- stream=$(parse_arg $config_file "stream")
- threshold=$(parse_arg $config_file "device_threshold")
- address=$(parse_arg $config_file "device_address")
-
- # Check stream URL validity
- if [ ! $(check_url $stream) ]; then
- echo "Incorrect stream address"
- exit 1
- fi
-
- # Check volume value validity
- if [ $volume -gt 100 ] || [ $volume -lt 0 ]; then
- echo "Invalid volume value"
- exit 1
- fi
-
- # Set volume to the right value
- set_volume $volume
-
- # Removing lock file in case of ungraceful shutdown
- if [ ! -f $lock_file ]; then
- rm $lock_file
- fi
-
- log "Initialisation complete"
- }
-
- main() {
- #Main loop
- log "Looking for device"
- while true; do
- # Polling the device's IP address
- count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
-
- if [ $count -gt 0 ]; then
- # Lock file present = music already playing
- if [ ! -f $lock_file ]; then
- log "Device detected"
- start_playing
- fi
- else
- if [ -f $lock_file ]; then
- log "Device lost"
- stop_playing
- fi
- fi
- done
- }
-
- ##### PROGRAM #####
-
- if [ $# -eq 0 ] || [ $# -gt 2 ]; then
- echo "Wrong arguments number"
- display_err_arg
- exit 1
- fi
-
- # Setting lock file path
- lock_file="/etc/welcomehome/home.lck"
-
- # Check if we know the requested feature
- case "$1" in
- "--start") init $2
- main
- ;;
- "--stop") stop_playing
- ;;
- *) echo "Unknown argument $1"
- display_err_arg
- ;;
- esac
|