Bläddra i källkod

Added volume control support

Brendan Abolivier 8 år sedan
förälder
incheckning
9017343ec0
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 ändrade filer med 30 tillägg och 1 borttagningar
  1. 27
    1
      welcomehome
  2. 3
    0
      welcomehome.example.conf

+ 27
- 1
welcomehome Visa fil

@@ -23,6 +23,28 @@ check_url() {
23 23
     return $?
24 24
 }
25 25
 
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
+}
33
+
34
+set_volume() {
35
+    # Set the volume to the right value
36
+    volume=$1
37
+    check_volume $volume
38
+    log "Set volume to $volume"
39
+
40
+    data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
41
+    # Using sed cuz simple quotes
42
+    data=`echo $data | sed "s/VOLUME/${volume}/"`
43
+
44
+    # Send the load
45
+    curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
46
+}
47
+
26 48
 play_stream() {
27 49
     # Reset playback state
28 50
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
@@ -39,6 +61,9 @@ play_stream() {
39 61
     # Play dat sound
40 62
     curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
41 63
     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
42 67
 }
43 68
 
44 69
 stop_stream() {
@@ -59,6 +84,7 @@ config_file=$1
59 84
 log "Parsing the configuration file"
60 85
 
61 86
 # Get parameters from the given configuration file
87
+volume=$(parse_arg $config_file "volume")
62 88
 stream=$(parse_arg $config_file "stream")
63 89
 threshold=$(parse_arg $config_file "device_threshold")
64 90
 address=$(parse_arg $config_file "device_address")
@@ -80,7 +106,7 @@ while true; do
80 106
         # Lock file present = music already playing
81 107
         if [ ! -f $lock_file ]; then
82 108
             log "Device detected"
83
-            play_stream $stream
109
+            play_stream $stream $volume
84 110
             touch $lock_file
85 111
         fi
86 112
     else

+ 3
- 0
welcomehome.example.conf Visa fil

@@ -8,3 +8,6 @@ device_threshold = 3
8 8
 
9 9
 # URL of the stream to be played when the device is detected
10 10
 stream = http://fr5.ah.fm:443
11
+
12
+# Volume to play the stream at. Goes from 0 to 100
13
+volume = 80