Merge branch 'master' of rvb.name:openhab-process
[openhab-process.git] / mqtt-mpd / mqmpd
1 #!/bin/bash
2
3 id=`hostname`-$$
4
5 if [ "$1" = "stop" ] 
6 then
7
8   for i in `pgrep -f "/bin/bash /opt/mqtt-mpd/mqmpdj"` 
9   do  
10     killtree $i 9
11   done
12
13   exit 0
14
15 fi
16
17 . /opt/mqtt-mpd/mqmpd.cfg
18
19 # запускаем процессы, мониторящие состояние
20
21 i=0
22 while [ "x${hosts[i]}" != "x" ]
23 do
24
25   /opt/mqtt-mpd/mqmpdj ${hosts[i]} ${passwd[i]} &
26
27   i=$(( $i + 1 ))
28
29 done
30
31 # сами остаемся слушать команды
32
33 mosquitto_sub -i mqtt-mpd-$id -v -h $mqhost -p $mqport -t "$mqcmd/#" -u $mquser -P $mqpassword | while read line
34 do
35
36   template="s!$mqcmd/!!"
37   addr=`echo $line | cut -f 1 -d " " | sed $template`
38   arg=`echo $line | cut -f 2 -d " "`
39   echo $addr $arg 
40
41   host=`echo $addr | cut -f 1 -d "/"`
42   cmd=`echo $addr | cut -f 2 -d "/"`
43
44   i=0
45   while [ "x${hosts[i]}" != "x" ]
46   do
47     if [ "${hosts[i]}" = "$host" ]
48     then
49       mpdpass=${passwd[i]}
50       if [ "$mpdpass" = "-" ] 
51       then
52         mpccmd="mpc -h $host"
53       else
54         mpccmd="mpc -h $host -P $mpdpass"
55       fi
56       break
57     fi
58     i=$(( $i + 1 ))
59   done
60
61   arg=`printf "%q" $arg`
62   if [[ "$arg" =~ [A-Za-z0-9]+ ]]
63   then
64
65     echo "$cmd -> $arg"
66
67     case $cmd in
68
69     volume)
70         if [ "$arg" = "INCREASE" ]
71         then
72           $mpccmd volume +5
73         fi
74         if [ "$arg" = "DECREASE" ]
75         then
76           $mpccmd volume -5
77         fi
78         if [[ "$arg" =~ [0-9]+ ]]
79         then
80           reply=( `$mpccmd volume`)
81           currvolume=`echo ${reply[1]} | sed 's/%//'`
82           if [ "$currvolume" ]
83           then
84             if [ "$arg" -gt "$currvolume" ] 
85             then
86               delta=$(( $arg - $currvolume ))
87               $mpccmd volume +$delta
88             fi
89             if [ "$arg" -lt "$currvolume" ] 
90             then
91               delta=$(( $currvolume - $arg ))
92               $mpccmd volume -$delta
93             fi
94             if [ "$arg" -eq "$currvolume" ] 
95             then
96               $mpccmd repeat
97               $mpccmd repeat
98             fi
99           fi
100         fi
101         ;;  
102     status)
103         case $arg in
104                 playing)
105                         $mpccmd play
106                         ;;
107                 stopped)
108                         $mpccmd stop
109                         ;;
110                 paused)
111                         $mpccmd pause
112                         ;;
113                 *)
114                 ;;
115         esac
116         ;;
117     currentnum)
118         $mpccmd play $arg
119         ;;
120     repeat|random|single|consume)
121         $mpccmd $cmd $arg
122         ;;
123     *)
124         echo "Неизвестная команда $cmd"
125         ;;
126
127     esac
128     
129   else
130   
131     echo "Недопустимый аргумент $arg"
132     
133   fi  
134
135 done