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