Избегаем "залипания" последнего сообщения в MQTT.
[openhab-process.git] / mqtt-mpd / mqmpd
index 9867146a7e72c128dc032e256c13e4320e8d55a8..595c08d9ca9b5f7dfd2b78a4377394a227651f81 100755 (executable)
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+id=`hostname`-$$
+
 if [ "$1" = "stop" ] 
 then
 
@@ -28,7 +30,7 @@ done
 
 # сами остаемся слушать команды
 
-mosquitto_sub -v -h $mqhost -p $mqport -t "$mqcmd/#" -u $mquser -P $mqpassword | while read line
+mosquitto_sub -i mqtt-mpd-$id -v -h $mqhost -p $mqport -t "$mqcmd/#" -u $mquser -P $mqpassword | while read line
 do
 
   template="s!$mqcmd/!!"
@@ -60,24 +62,42 @@ do
   if [[ "$arg" =~ [A-Za-z0-9]+ ]]
   then
 
+    echo "$cmd -> $arg"
+
     case $cmd in
 
     volume)
-        reply=( `$mpccmd volume`)
-        currvolume=`echo ${reply[1]} | sed 's/%//'`
-        if [ "$currvolume" ]
+        if [ "$arg" = "INCREASE" ]
         then
-          if [ "$arg" -gt "$currvolume" ] 
-          then
-            delta=$(( $arg - $currvolume ))
-           $mpccmd volume +$delta
-         fi
-          if [ "$arg" -lt "$currvolume" ] 
+          $mpccmd volume +5
+        fi
+        if [ "$arg" = "DECREASE" ]
+        then
+          $mpccmd volume -5
+        fi
+        if [[ "$arg" =~ [0-9]+ ]]
+        then
+          reply=( `$mpccmd volume`)
+          currvolume=`echo ${reply[1]} | sed 's/%//'`
+          if [ "$currvolume" ]
           then
-            delta=$(( $currvolume - $arg ))
-           $mpccmd volume -$delta
-         fi
-       fi
+            if [ "$arg" -gt "$currvolume" ] 
+            then
+              delta=$(( $arg - $currvolume ))
+             $mpccmd volume +$delta
+           fi
+            if [ "$arg" -lt "$currvolume" ] 
+            then
+              delta=$(( $currvolume - $arg ))
+             $mpccmd volume -$delta
+            fi
+            if [ "$arg" -eq "$currvolume" ] 
+            then
+             $mpccmd repeat
+             $mpccmd repeat
+            fi
+          fi
+        fi
        ;;  
     status)
        case $arg in
@@ -94,6 +114,9 @@ do
                ;;
        esac
        ;;
+    currentnum)
+       $mpccmd play $arg
+       ;;
     repeat|random|single|consume)
        $mpccmd $cmd $arg
        ;;