From 29ceb55369a9338c213121fc6b85fd72fd0d9027 Mon Sep 17 00:00:00 2001 From: Jeff Backus Date: Sun, 4 May 2014 18:34:21 -0400 Subject: [PATCH] * Somewhere around Lua 5.1 math.mod() was renamed math.fmod(). Added a check for the original math.mod() before calling. * Added an comment and an alternate MPD session string to statusd_mpd for situations where the server closes the connection too quickly for the script to properly ascertain server status. --- contrib/statusd/statusd_mpd-socket.lua | 11 +++++++++-- contrib/statusd/statusd_mpd.lua | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/contrib/statusd/statusd_mpd-socket.lua b/contrib/statusd/statusd_mpd-socket.lua index c4b6c395..f7852bbc 100644 --- a/contrib/statusd/statusd_mpd-socket.lua +++ b/contrib/statusd/statusd_mpd-socket.lua @@ -124,8 +124,15 @@ local get_mpd_status = socket.protect(function() local _,_,attrib,val = string.find(data, "(.-): (.*)") if attrib == "time" then _,_,info.pos,info.len = string.find(val, "(%d+):(%d+)") - info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.mod(info.pos, 60)) - info.len = string.format("%d:%02d", math.floor(info.len / 60), math.mod(info.len, 60)) + + -- Around Lua 5.1, math.mod() was renamed math.fmod(). + if type(math.mod) == "function" then + info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.mod(info.pos, 60)) + info.len = string.format("%d:%02d", math.floor(info.len / 60), math.mod(info.len, 60)) + else + info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.fmod(info.pos, 60)) + info.len = string.format("%d:%02d", math.floor(info.len / 60), math.fmod(info.len, 60)) + end elseif attrib == "state" then info.state = val end diff --git a/contrib/statusd/statusd_mpd.lua b/contrib/statusd/statusd_mpd.lua index ed40b0ad..a3be5119 100644 --- a/contrib/statusd/statusd_mpd.lua +++ b/contrib/statusd/statusd_mpd.lua @@ -43,7 +43,14 @@ local function saferead(file) end local function get_mpd_status() + + -- The first version of cmd_string is the original, however, some versions + -- of MPD close the socket too quickly if close is included. If you see the + -- message: error querying mpd status + -- then try the second version. local cmd_string = "status\ncurrentsong\nclose\n" +-- local cmd_string = "status\ncurrentsong\n" + if settings.password ~= nil then cmd_string = "password " .. settings.password .. "\n" .. cmd_string end @@ -79,12 +86,19 @@ local function get_mpd_status() repeat data = saferead(mpd) if data == nil then break end - + local _,_,attrib,val = string.find(data, "(.-): (.*)") if attrib == "time" then _,_,info.pos,info.len = string.find(val, "(%d+):(%d+)") - info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.mod(info.pos, 60)) - info.len = string.format("%d:%02d", math.floor(info.len / 60), math.mod(info.len, 60)) + + -- Around Lua 5.1, math.mod() was renamed math.fmod(). + if type(math.mod) == "function" then + info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.mod(info.pos, 60)) + info.len = string.format("%d:%02d", math.floor(info.len / 60), math.mod(info.len, 60)) + else + info.pos = string.format("%d:%02d", math.floor(info.pos / 60), math.fmod(info.pos, 60)) + info.len = string.format("%d:%02d", math.floor(info.len / 60), math.fmod(info.len, 60)) + end elseif attrib == "state" then info.state = val end -- 2.11.4.GIT