| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
local have_socket, socket = pcall(function() return require("socket") end) |
| 47 |
local string = string |
| 48 |
local tonumber = tonumber |
| 49 |
local setmetatable = setmetatable |
| 50 |
local os = os |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
local MPD = {} |
| 55 |
|
| 56 |
local MPD_mt = { __index = MPD } |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
local function new(settings) |
| 67 |
local client = {} |
| 68 |
if settings == nil then settings = {} end |
| 69 |
|
| 70 |
client.hostname = settings.hostname or "localhost" |
| 71 |
client.port = settings.port or 6600 |
| 72 |
client.desc = settings.desc or client.hostname |
| 73 |
client.password = settings.password |
| 74 |
client.timeout = settings.timeout or 1 |
| 75 |
client.retry = settings.retry or 60 |
| 76 |
|
| 77 |
setmetatable(client, MPD_mt) |
| 78 |
|
| 79 |
return client |
| 80 |
end |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
function MPD:send(action) |
| 100 |
local command = string.format("%s\n", action) |
| 101 |
local values = {} |
| 102 |
|
| 103 |
if not have_socket then |
| 104 |
return { errormsg = "could not require(\"socket\")" } |
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
if not self.connected then |
| 109 |
local now = os.time(); |
| 110 |
if not self.last_try or (now - self.last_try) > self.retry then |
| 111 |
self.socket = socket.tcp() |
| 112 |
self.socket:settimeout(self.timeout, 't') |
| 113 |
self.last_try = os.time() |
| 114 |
self.connected = self.socket:connect(self.hostname, self.port) |
| 115 |
if not self.connected then |
| 116 |
return { errormsg = "could not connect" } |
| 117 |
end |
| 118 |
|
| 119 |
|
| 120 |
local line = self.socket:receive("*l") |
| 121 |
if not line:match("^OK MPD") then |
| 122 |
self.connected = false |
| 123 |
return { errormsg = string.format("invalid hello message: %s", line) } |
| 124 |
end |
| 125 |
|
| 126 |
|
| 127 |
if self.password then |
| 128 |
local rsp = self:send(string.format("password %s", self.password)) |
| 129 |
if rsp.errormsg then |
| 130 |
return rsp |
| 131 |
end |
| 132 |
end |
| 133 |
else |
| 134 |
local retry_sec = self.retry - (now - self.last_try) |
| 135 |
return { errormsg = string.format("retrying connection in %d sec", retry_sec) } |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
self.socket:send(command) |
| 140 |
|
| 141 |
local line = "" |
| 142 |
while not line:match("^OK$") do |
| 143 |
line = self.socket:receive("*l") |
| 144 |
if not line then |
| 145 |
self.connected = false |
| 146 |
return self:send(action) |
| 147 |
end |
| 148 |
|
| 149 |
if line:match("^ACK") then |
| 150 |
return { errormsg = line } |
| 151 |
end |
| 152 |
|
| 153 |
local _, _, key, value = string.find(line, "([^:]+):%s(.+)") |
| 154 |
if key then |
| 155 |
values[string.lower(key)] = value |
| 156 |
end |
| 157 |
end |
| 158 |
|
| 159 |
return values |
| 160 |
end |
| 161 |
|
| 162 |
function MPD:next() |
| 163 |
return self:send("next") |
| 164 |
end |
| 165 |
|
| 166 |
function MPD:previous() |
| 167 |
return self:send("previous") |
| 168 |
end |
| 169 |
|
| 170 |
function MPD:stop() |
| 171 |
return self:send("stop") |
| 172 |
end |
| 173 |
|
| 174 |
|
| 175 |
function MPD:volume_up(delta) |
| 176 |
local stats = self:send("status") |
| 177 |
local new_volume = tonumber(stats.volume) + delta |
| 178 |
|
| 179 |
return self:send(string.format("setvol %d", new_volume)) |
| 180 |
end |
| 181 |
|
| 182 |
function MPD:volume_down(delta) |
| 183 |
return self:volume_up(-delta) |
| 184 |
end |
| 185 |
|
| 186 |
function MPD:toggle_random() |
| 187 |
local stats = self:send("status") |
| 188 |
if tonumber(stats.random) == 0 then |
| 189 |
return self:send("random 1") |
| 190 |
else |
| 191 |
return self:send("random 0") |
| 192 |
end |
| 193 |
end |
| 194 |
|
| 195 |
function MPD:toggle_repeat() |
| 196 |
local stats = self:send("status") |
| 197 |
if tonumber(stats["repeat"]) == 0 then |
| 198 |
return self:send("repeat 1") |
| 199 |
else |
| 200 |
return self:send("repeat 0") |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
function MPD:toggle_play() |
| 205 |
if self:send("status").state == "stop" then |
| 206 |
return self:send("play") |
| 207 |
else |
| 208 |
return self:send("pause") |
| 209 |
end |
| 210 |
end |
| 211 |
|
| 212 |
return { |
| 213 |
new = new, |
| 214 |
} |
| 215 |
|
| 216 |
|