Наборчик для общения между несколькими mpd и openhab через mqtt.
[openhab-process.git] / mqtt-mpd / mqmpdj
1 #!/bin/bash
2
3 . /opt/mqtt-mpd/mqmpd.cfg
4
5 host=$1
6 passwd=$2
7
8 if [ "$passwd" = "-" ]
9 then
10
11   command="mpc -h $host"
12
13 else
14
15   command="mpc -h $host -P $passwd"
16
17 fi
18
19 while : 
20 do         
21   $command idle
22   if [ $? = 0 ]
23   then
24 #    current=`mpc -h $host -P $passwd current | sed 's/"/\"/g'`
25 #    volume=`mpc -h $host -P $passwd volume | cut -d: -f2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
26     
27     mapfile -t array < <( $command )
28     if [ ${#array[@]} = 3 ]
29     then
30       current=${array[0]}
31       str2=( ${array[1]})
32       str3=( ${array[2]})
33       status=`echo ${str2[0]} | sed 's/^\[//' | sed 's/\]$//'`
34       current_track_num=`echo ${str2[1]} | cut -d '/' -f 1 | sed 's/^#//'`
35       tracks_in_list=`echo ${str2[1]} | cut -d '/' -f 2`
36       current_pos=`echo ${str2[2]} | cut -d '/' -f 1`
37       track_length=`echo ${str2[2]} | cut -d '/' -f 2`
38       volume=`echo ${str3[1]} | sed 's/%//'`
39       repeat=${str3[3]}
40       random=${str3[5]}
41       single=${str3[7]}
42       consume=${str3[9]}
43     else
44       current='-'
45       status='stopped'
46       current_track_num='0'
47       tracks_in_list='0'
48       current_pos='-'
49       track_length='-'
50       str3=( ${array[0]})
51       volume=`echo ${str3[1]} | sed 's/%//'`
52       if [ "$volume" = "n/a" ]
53       then
54         volume="0"
55       fi
56       repeat=${str3[3]}
57       random=${str3[5]}
58       single=${str3[7]}
59       consume=${str3[9]}
60     fi  
61
62     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/nowplaying -m "$current" -u $mquser -P $mqpassword
63     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/volume -m "$volume" -u $mquser -P $mqpassword
64     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/status -m "$status" -u $mquser -P $mqpassword
65     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/currentnum -m "$current_track_num" -u $mquser -P $mqpassword
66     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/tracks -m "$tracks_in_list" -u $mquser -P $mqpassword
67     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/currentpos -m "$current_pos" -u $mquser -P $mqpassword
68     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/tracklen -m "$track_length" -u $mquser -P $mqpassword
69     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/repeat -m "$repeat" -u $mquser -P $mqpassword
70     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/random -m "$random" -u $mquser -P $mqpassword
71     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/single -m "$single" -u $mquser -P $mqpassword
72     mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/consume -m "$consume" -u $mquser -P $mqpassword
73
74   else
75     sleep 30
76   fi  
77 done