Косметические доработки интерфейса.
[mpd-lua.git] / mpd.lua
diff --git a/mpd.lua b/mpd.lua
index 31275b69277cf1ae3210969c6537ddfcec155622..8935c9a605af940fa108e19c62424f8bee5ce26e 100755 (executable)
--- a/mpd.lua
+++ b/mpd.lua
@@ -1,6 +1,7 @@
 #!/usr/bin/lua
 
-require "uci"
+local hasuci,uci = pcall(require,"uci")
+
 require("socket")
 json=require("json")
 
@@ -160,16 +161,44 @@ function mpd_send(mpd,action,raw)
     return values
 end
 
-x = uci.cursor()
+if hasuci then
+
+  x = uci.cursor()
+
+  settings = {}
+  settings['host'] = x.get("mpd","server","host") or "localhost"
+  settings['port'] = x.get("mpd","server","port") or 6600
+  settings['timeout'] = x.get("mpd","server","timeout") or 1
 
-settings = {}
-settings['host'] = x.get("mpd","server","host") or "localhost"
-settings['port'] = x.get("mpd","server","port") or 6600
-settings['timeout'] = x.get("mpd","server","timeout") or 1
+  volstep = x.get("mpd","control","volume_step") or 3
 
-volstep = x.get("mpd","control","volume_step") or 3
+  password = x.get("mpd","server","password")
 
-password = x.get("mpd","server","password")
+else
+
+  config = arg[1]
+  if not config then
+    config="/etc/mpd-lua.json"
+  end
+
+  settings={}
+  local open = io.open
+  local file = open(config, "r")
+  if file then
+    local content = file:read "*a"
+    file:close()
+    settings=json.decode(content)
+  end
+    
+  settings['host'] = settings['host'] or "localhost"
+  settings['port'] = settings["port"] or 6600
+  settings['timeout'] = settings["timeout"] or 1
+
+  volstep = settings["volstep"] or 3
+
+end
+  
+  
 if password then
   settings["password"] = password
 end
@@ -182,10 +211,15 @@ if not command or command=="" then
   command="idle"
 end
 
-if command=="play" or command=="pause" or command=="stop" or command=="previous" or command=="next" then
+if command=="play" or command=="pause" or command=="stop" then
 
   res=mpd_send(m,command)
 
+elseif command=="previous" or command=="next" then
+
+  res=mpd_send(m,"play")
+  res=mpd_send(m,command)
+
 elseif command=="idle" then
 
   m.timeout=30
@@ -203,6 +237,79 @@ elseif command=="volu" then
   volume=tonumber(status["volume"])
   res=mpd_send(m,"setvol "..(volume+volstep))
 
+elseif string.starts(command,"fastfwd") then
+
+  cmd=split(command,"|")
+  skip=tonumber(cmd[2])
+  if not skip then
+    skip=15
+  end
+
+  status=mpd_send(m,"status")
+  rec_time=status["time"]
+  song=status["song"]
+  
+  if song then
+
+    if rec_time then
+      rec_time=split(rec_time,":")
+      cur_time=tonumber(rec_time[1])
+
+      track_time=tonumber(rec_time[2])
+      cur_time=cur_time+skip
+      if cur_time>track_time then
+        cur_time=track_time
+      end
+
+      mpd_send(m,"seek "..song.." "..cur_time)
+
+    else
+
+      mpd_send(m,"play")
+
+    end  
+  
+  end
+
+  res={}
+
+elseif string.starts(command,"rewind") then
+
+  cmd=split(command,"|")
+  skip=tonumber(cmd[2])
+  if not skip then
+    skip=15
+  end
+
+  status=mpd_send(m,"status")
+  rec_time=status["time"]
+  song=status["song"]
+  
+  if song then
+
+    if rec_time then
+      rec_time=split(rec_time,":")
+      cur_time=tonumber(rec_time[1])
+
+      track_time=tonumber(rec_time[2])
+      cur_time=cur_time-skip
+      if cur_time<0 then
+        cur_time=0
+      end
+
+      mpd_send(m,"seek "..song.." "..cur_time)
+
+    else
+
+      mpd_send(m,"play")
+      mpd_send(m,"previous")
+
+    end  
+  
+  end
+
+  res={}
+
 elseif command=="status" then
 
   res=mpd_send(m,"status")
@@ -212,8 +319,8 @@ elseif command=="status" then
 
   if song then 
     res['current_playing']=pl[song]['name']
-  else
-    res['current_playing']="No songs selected"
+  else  
+    res['current_playing']="---"
   end
 
 elseif command=="playlist" then