Browse Source

Reorganised code, added procedures support and graceful shutdown

Brendan Abolivier 8 years ago
parent
commit
8422cf9fb7
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 changed files with 97 additions and 49 deletions
  1. 95
    47
      welcomehome
  2. 2
    2
      welcomehome.service

+ 95
- 47
welcomehome View File

1
 #!/bin/bash
1
 #!/bin/bash
2
 
2
 
3
-##### FUNCTIONS #####
3
+##### SNIPPETS #####
4
 
4
 
5
 log() {
5
 log() {
6
     # Beautiful logs
6
     # Beautiful logs
8
     echo "[$date]" $1
8
     echo "[$date]" $1
9
 }
9
 }
10
 
10
 
11
+display_err_arg() {
12
+    echo "Usage: welcomehome --start $(tput smul)configuration file$(tput rmul)"
13
+    echo "OR"
14
+    echo "Usage: welcomehome --stop"
15
+}
16
+
11
 parse_arg() {
17
 parse_arg() {
12
     # Echoes the value of a given parameter in a given configuration file
18
     # Echoes the value of a given parameter in a given configuration file
13
     value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
19
     value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
23
     return $?
29
     return $?
24
 }
30
 }
25
 
31
 
26
-check_volume() {
27
-    # Check if the volume value is valid
28
-    if [ $1 -gt 100 ] || [ $1 -lt 0 ]; then
29
-        echo "Invalid volume value"
30
-        exit 1
31
-    fi
32
-}
32
+##### STREAM CONTROL #####
33
 
33
 
34
 set_volume() {
34
 set_volume() {
35
     # Set the volume to the right value
35
     # Set the volume to the right value
36
     volume=$1
36
     volume=$1
37
-    check_volume $volume
38
     log "Set volume to $volume"
37
     log "Set volume to $volume"
39
 
38
 
40
     data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
39
     data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
61
     # Play dat sound
60
     # Play dat sound
62
     curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
61
     curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
63
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
62
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
64
-
65
-    # The right music at the righ volume. Nothing's more perfect.
66
-    set_volume $2
67
 }
63
 }
68
 
64
 
69
 stop_stream() {
65
 stop_stream() {
72
     log "Stream stopped"
68
     log "Stream stopped"
73
 }
69
 }
74
 
70
 
75
-##### PROGRAM #####
71
+##### CONTROLLERS #####
76
 
72
 
77
-if [[ $# -eq 0 || ! -f $1 ]]; then
78
-    echo "I need a config file as first argument"
79
-    exit 1
80
-fi
73
+start_playing() {
74
+    touch $lock_file
75
+    play_stream $stream
76
+}
81
 
77
 
82
-config_file=$1
78
+stop_playing() {
79
+    if [ -f $lock_file ]; then
80
+        rm $lock_file
81
+    fi
82
+    stop_stream
83
+}
83
 
84
 
84
-log "Parsing the configuration file"
85
+##### MAIN FUNCTIONS #####
85
 
86
 
86
-# Get parameters from the given configuration file
87
-volume=$(parse_arg $config_file "volume")
88
-stream=$(parse_arg $config_file "stream")
89
-threshold=$(parse_arg $config_file "device_threshold")
90
-address=$(parse_arg $config_file "device_address")
87
+init() {
88
+    # Initialise the variables
89
+    config_file=$1
91
 
90
 
92
-if [ ! $(check_url $stream) ]; then
93
-    echo "Incorrect stream address"
94
-    exit 1
95
-fi
91
+    # Check if the config file exists
92
+    if [ ! -f $config_file ]; then
93
+        echo "$config_file is not a file"
94
+        exit 1
95
+    fi
96
+
97
+    log "Parsing the configuration file"
96
 
98
 
97
-log "Initialisation complete"
99
+    # Get parameters from the given configuration file
100
+    volume=$(parse_arg $config_file "volume")
101
+    stream=$(parse_arg $config_file "stream")
102
+    threshold=$(parse_arg $config_file "device_threshold")
103
+    address=$(parse_arg $config_file "device_address")
98
 
104
 
99
-while true; do
100
-    # Lock file
101
-    lock_file="/etc/welcomehome/home.lck"
105
+    # Check stream URL validity
106
+    if [ ! $(check_url $stream) ]; then
107
+        echo "Incorrect stream address"
108
+        exit 1
109
+    fi
102
 
110
 
103
-    count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
111
+    # Check volume value validity
112
+    if [ $volume -gt 100 ] || [ $volume -lt 0 ]; then
113
+        echo "Invalid volume value"
114
+        exit 1
115
+    fi
104
 
116
 
105
-    if [ $count -gt 0 ]; then
106
-        # Lock file present = music already playing
107
-        if [ ! -f $lock_file ]; then
108
-            log "Device detected"
109
-            play_stream $stream $volume
110
-            touch $lock_file
111
-        fi
112
-    else
113
-        if [ -f $lock_file ]; then
114
-            log "Device lost"
115
-            stop_stream
116
-            rm $lock_file
117
+    # Set volume to the right value
118
+    set_volume $volume
119
+
120
+    log "Initialisation complete"
121
+}
122
+
123
+main() {
124
+    #Main loop
125
+    log "Looking for device"
126
+    while true; do
127
+        # Polling the device's IP address
128
+        count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
129
+
130
+        if [ $count -gt 0 ]; then
131
+            # Lock file present = music already playing
132
+            if [ ! -f $lock_file ]; then
133
+                log "Device detected"
134
+                start_playing
135
+            fi
136
+        else
137
+            if [ -f $lock_file ]; then
138
+                log "Device lost"
139
+                stop_playing
140
+            fi
117
         fi
141
         fi
118
-    fi
119
-done
142
+    done
143
+}
144
+
145
+##### PROGRAM #####
146
+
147
+if [ $# -eq 0 ] || [ $# -gt 2 ]; then
148
+    echo "Wrong arguments number"
149
+    display_err_arg
150
+    exit 1
151
+fi
152
+
153
+# Setting lock file path
154
+lock_file="/etc/welcomehome/home.lck"
155
+
156
+# Check if we know the requested feature
157
+case "$1" in
158
+    "--start") init $2
159
+               main
160
+               ;;
161
+    "--stop") stop_playing
162
+              ;;
163
+    *) echo "Unknown argument $1"
164
+       display_err_arg
165
+       ;;
166
+esac
167
+

+ 2
- 2
welcomehome.service View File

4
 Requires=mopidy.service
4
 Requires=mopidy.service
5
 [Service]
5
 [Service]
6
 Type=simple
6
 Type=simple
7
-ExecStart=/usr/local/bin/welcomehome /etc/welcomehome/welcomehome.conf
8
-ExecStopPost=-/bin/rm /etc/welcomehome/home.lck
7
+ExecStart=/usr/local/bin/welcomehome --start /etc/welcomehome/welcomehome.conf
8
+ExecStopPost=/usr/local/bin/welcomehome --stop
9
 Restart=on-abort
9
 Restart=on-abort
10
 [Install]
10
 [Install]
11
 WantedBy=multi-user.target
11
 WantedBy=multi-user.target