Наборчик для общения между несколькими mpd и openhab через mqtt.
[openhab-process.git] / mqtt-mpd / mqmpdj
diff --git a/mqtt-mpd/mqmpdj b/mqtt-mpd/mqmpdj
new file mode 100755 (executable)
index 0000000..70603f2
--- /dev/null
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+. /opt/mqtt-mpd/mqmpd.cfg
+
+host=$1
+passwd=$2
+
+if [ "$passwd" = "-" ]
+then
+
+  command="mpc -h $host"
+
+else
+
+  command="mpc -h $host -P $passwd"
+
+fi
+
+while : 
+do         
+  $command idle
+  if [ $? = 0 ]
+  then
+#    current=`mpc -h $host -P $passwd current | sed 's/"/\"/g'`
+#    volume=`mpc -h $host -P $passwd volume | cut -d: -f2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
+    
+    mapfile -t array < <( $command )
+    if [ ${#array[@]} = 3 ]
+    then
+      current=${array[0]}
+      str2=( ${array[1]})
+      str3=( ${array[2]})
+      status=`echo ${str2[0]} | sed 's/^\[//' | sed 's/\]$//'`
+      current_track_num=`echo ${str2[1]} | cut -d '/' -f 1 | sed 's/^#//'`
+      tracks_in_list=`echo ${str2[1]} | cut -d '/' -f 2`
+      current_pos=`echo ${str2[2]} | cut -d '/' -f 1`
+      track_length=`echo ${str2[2]} | cut -d '/' -f 2`
+      volume=`echo ${str3[1]} | sed 's/%//'`
+      repeat=${str3[3]}
+      random=${str3[5]}
+      single=${str3[7]}
+      consume=${str3[9]}
+    else
+      current='-'
+      status='stopped'
+      current_track_num='0'
+      tracks_in_list='0'
+      current_pos='-'
+      track_length='-'
+      str3=( ${array[0]})
+      volume=`echo ${str3[1]} | sed 's/%//'`
+      if [ "$volume" = "n/a" ]
+      then
+        volume="0"
+      fi
+      repeat=${str3[3]}
+      random=${str3[5]}
+      single=${str3[7]}
+      consume=${str3[9]}
+    fi  
+
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/nowplaying -m "$current" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/volume -m "$volume" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/status -m "$status" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/currentnum -m "$current_track_num" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/tracks -m "$tracks_in_list" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/currentpos -m "$current_pos" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/tracklen -m "$track_length" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/repeat -m "$repeat" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/random -m "$random" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/single -m "$single" -u $mquser -P $mqpassword
+    mosquitto_pub -h $mqhost -p $mqport -t $mqtopic/$host/consume -m "$consume" -u $mquser -P $mqpassword
+
+  else
+    sleep 30
+  fi  
+done