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